HTMLCanvasElement: transferControlToOffscreen() method - Web APIs | MDN
Syntax
js
transferControlToOffscreen()
Parameters
None.
Return value
An OffscreenCanvas object.
Exceptions
InvalidStateErrorDOMException-
Throws if:
- the canvas has been set a context mode by calling
HTMLCanvasElement.getContext() - the canvas has already transferred its control to offscreen.
- the canvas has been set a context mode by calling
Examples
The following example shows how to transfer control to an OffscreenCanvas object on the main thread.
js
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.
js
const offscreen = document.querySelector("canvas").transferControlToOffscreen();
const worker = new Worker("my-worker-url.js");
worker.postMessage({ canvas: offscreen }, [offscreen]);
Specifications
| Specification |
|---|
| HTML # dom-canvas-transfercontroltooffscreen-dev |
Browser compatibility
See also
- The interface defining this method,
HTMLCanvasElement OffscreenCanvas