HTMLCanvasElement: transferControlToOffscreen() method

The HTMLCanvasElement.transferControlToOffscreen() method transfers control to an OffscreenCanvas object, either on the main thread or on a worker.

Syntax

js
transferControlToOffscreen()

Parameters

None.

Return value

An OffscreenCanvas object.

Exceptions

InvalidStateError DOMException

Throws if:

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

Browser compatibility

See also