web api instance method
HTMLCanvasElement: transferControlToOffscreen() method
The HTMLCanvasElement.transferControlToOffscreen() method transfers control to an OffscreenCanvas object, either on the main thread or on a worker.
Syntax
transferControlToOffscreen()
Parameters
None.
Return value
An OffscreenCanvas object.
Exceptions
InvalidStateErrorDOMException- : Throws if:
- the canvas has been set a context mode by calling
getContext() - the canvas has already transferred its control to offscreen.
- the canvas has been set a context mode by calling
- : Throws if:
Examples
The following example shows how to transfer control to an OffscreenCanvas object on the main thread.
const htmlCanvas = document.createElement("canvas");
const offscreen = htmlCanvas.transferControlToOffscreen();
const gl = offscreen.getContext("webgl");
// Some drawing using the gl context…
The following example shows how to transfer control to an OffscreenCanvas object on a worker.
const offscreen = document.querySelector("canvas").transferControlToOffscreen();
const worker = new Worker("my-worker-url.js");
worker.postMessage({ canvas: offscreen }, [offscreen]);
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.
See also
- The interface defining this method,
HTMLCanvasElement OffscreenCanvas