HTMLCanvasElement.transferControlToOffscreen()

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.

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…

// Push frames back to the original HTMLCanvasElement
gl.commit();

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('myworkerurl.js');
worker.postMessage({ canvas: offscreen }, [offscreen]);

Specifications

Specification
HTML Standard
# dom-canvas-transfercontroltooffscreen-dev

Browser compatibility

BCD tables only load in the browser

See also