Files
uconsole_asw_game/data/commander/js/plot/heatmap-worker.js
T
Claude 9f6264940d Web client: paint waterfalls in an OffscreenCanvas worker (1.3.3)
Heatmap rasters (LOFAR/BTR/DEMON waterfalls) are packed into typed-array
jobs and painted by a module worker into an OffscreenCanvas, which hands
back an ImageBitmap. The main thread keeps drawing the previous bitmap at
its anchor until the new one arrives, keeps one job in flight per plot and
falls back to the shared synchronous painter when Worker/OffscreenCanvas
is missing or the worker fails. Headless Chrome stays on the synchronous
path unless opted in, because workers stall its virtual time.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AS36Eg72iLviiSVbCUVNAi
2026-09-28 06:49:07 +00:00

30 lines
1.2 KiB
JavaScript

// Module worker: paints waterfall rasters into an OffscreenCanvas and hands
// the finished bitmap back, so the page's main thread (and the live-sonar
// audio scheduler on it) never runs the per-cell loop.
import { paintHeatmap } from "./heatmap-paint.js";
const MAX_SURFACES = 16;
const surfaces = new Map();
function surfaceFor(id, width, height) {
let surface = surfaces.get(id);
if (surface && surface.canvas.width === width && surface.canvas.height === height) return surface;
const canvas = new OffscreenCanvas(width, height);
const context = canvas.getContext("2d");
const image = context.createImageData(width, height);
surface = {canvas, context, image, pixels: new Uint32Array(image.data.buffer)};
surfaces.delete(id);
surfaces.set(id, surface);
if (surfaces.size > MAX_SURFACES) surfaces.delete(surfaces.keys().next().value);
return surface;
}
self.onmessage = ({data}) => {
const {id, seq, job} = data;
const surface = surfaceFor(id, job.width, job.rasterHeight);
paintHeatmap(surface.pixels, job);
surface.context.putImageData(surface.image, 0, 0);
const bitmap = surface.canvas.transferToImageBitmap();
self.postMessage({id, seq, bitmap, anchor: job.anchor}, [bitmap]);
};