<canvas-slot worker="./renderer.js"> hands its canvas to a worker. On each paint the main thread captures
the slotted elements with captureElementImage() and transfers the ElementImage snapshots. The worker animates them
with its own requestAnimationFrame.
Live clock
Button
Plain text
Drawn off the main thread.
worker fps: –
Show the code DOM drawn by a worker
Put ordinary elements in the canvas element and name a worker script. Each child has a slot name. They stay DOM, so the button takes clicks.
Load the element on the page. That is all the page does. The worker can post messages back (here, its frame rate).
What the element does for you: hand the canvas to the worker, then send snapshots.transferControlToOffscreen() once, then on every paint event captureElementImage(el) for each element that changed, and transfer the snapshots with postMessage.
The worker draws the snapshots. It owns the canvas and runs its own requestAnimationFrame. ctx.drawElementImage(img, …) works with a transferred snapshot the same way it works with an element.
While the main thread is blocked the orbit keeps moving (the worker owns the canvas), but the clock freezes: new snapshots only come from the main thread's rendering update. Try clicking the button while it orbits; drawing in the worker updates the element's geometry back on the main thread.
Reprojecting parts of the DOM
One element, drawn once, then re-used: a magnifier that follows the pointer or your finger (source-rect crops of the same snapshot), and plain <canvas> elements elsewhere on the page that copy regions of the source canvas.
A document, drawn once
Move your pointer over this, or press and drag on a touch screen. The lens redraws a crop of the samedrawElementImage() snapshot at 2.5×, clipped to a circle, so it stays sharp. Text stays selectable underneath.
Region
Q3
Q4
North
1,204
1,388
South
982
1,045
Show the code magnifier and mirrors
One element in the canvas.
Draw it once, then draw a crop of it again, bigger. The nine-argument drawElementImage(el, sx, sy, sw, sh, dx, dy, dw, dh) takes a source rectangle in canvas pixels. preserveElementGeometry keeps clicks going to the first, full-size draw.
Copy regions into other canvases. A snapshot only draws into its own canvas, so the mirrors copy pixels with plain drawImage(sourceCanvas, …).
heading regiontable region (live)
What works with OffscreenCanvas (live)
Result
Experiment
Detail
Findings
Worker rendering works with the transferred canvas: captureElementImage() → postMessage(…, [img]) → drawElementImage(img) on the OffscreenCanvas, and a snapshot can be redrawn every frame until it's replaced or closed.
Snapshots are tied to their canvas. A new OffscreenCanvas(), or any canvas other than the source (or its transferred OffscreenCanvas), throws "The source was captured from a different canvas". To show DOM in a second canvas you copy pixels (drawImage(sourceCanvas)), which loses interactivity.
The worker can't make new snapshots. Only the main thread's rendering update produces them, so a blocked main thread means motion continues but content freezes.
Hit testing follows the worker. Worker-side draws update geometry asynchronously and fire elementgeometryupdate on the canvas element.
The source canvas must render.display: none and visibility: hidden canvases never record a snapshot ("No cached paint record"), and a canvas positioned far off-screen is culled (transparent). A canvas that is visible with opacity: 0 does work, so that is the way to hide a source canvas.