Capítulo 833 de 859
OffscreenCanvas lets a web worker own and render to a canvas, moving heavy three.js rendering (and asset loading/parsing) off the main thread so the page stays responsive — at the cost of the worker having no DOM access at all.
canvas.transferControlToOffscreen(): hands control of a <canvas> element to an OffscreenCanvas object that can be transferred into a worker.new Worker(path, { type: "module" }) + postMessage: starts the worker and sends it the offscreen canvas; the second argument to postMessage lists objects (like the offscreen canvas) to transfer rather than clone — transferred objects become unusable back on the main thread.canvas.clientWidth/clientHeight, receive mouse/keyboard events, or touch any DOM API — all of that must be explicitly forwarded from the main thread via messages.{ type, ...data } convention on both ends lets the main thread and worker route messages to the right handler function.canvas.transferControlToOffscreen before using this path, and fall back to normal main-thread rendering (sharing the same core three.js code) when unsupported.postMessage, since the worker can't listen for them itself.function makeSendPropertiesHandler(properties) {
return function sendProperties(event, sendFn) {
const data = { type: event.type };
for (const name of properties) data[name] = event[name];
sendFn(data);
};
}
const mouseEventHandler = makeSendPropertiesHandler([
"ctrlKey", "metaKey", "shiftKey", "button", "clientX", "clientY",
]);
Event objects can't be structured-cloned.OffscreenCanvas moves rendering (and ideally loading/parsing) off the main thread, which can reduce jank during heavy scenes or page load.postMessage's second argument neuters it on the sending side — the main-thread offscreen reference becomes unusable after transfer.OffscreenCanvas isn't supported.