const canvas = document.getElementById("canvas");
const offscreen = canvas.transferControlToOffscreen();
const worker = new Worker("worker.js");
worker.postMessage({ canvas: offscreen }, [offscreen]);OffscreenCanvasRenderingContext2D / notebook
OffscreenCanvasRenderingContext2D
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
onmessage = (event) => {
const canvas = event.data.canvas;
const offCtx = canvas.getContext("2d");
// draw to the offscreen canvas context
offCtx.fillStyle = "red";
offCtx.fillRect(0, 0, 100, 100);
};