OffscreenCanvas: transferToImageBitmap() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Note: This feature is available in Web Workers.

The transferToImageBitmap() method of the OffscreenCanvas interface creates an ImageBitmap object from the most recently rendered image of the OffscreenCanvas. The image in the OffscreenCanvas is replaced with a new blank image for subsequent rendering.

If you only need to copy the current OffscreenCanvas content into another canvas, use that canvas context's CanvasRenderingContext2D.drawImage() method with the OffscreenCanvas as input.

Syntax

js
transferToImageBitmap()

Parameters

None.

Return value

A newly-allocated ImageBitmap.

Exceptions

InvalidStateError DOMException

Throws if:

  • the canvas has transferred to another context scope, such as a worker
  • the canvas context mode has not been set by calling OffscreenCanvas.getContext().

Description

This ImageBitmap references a potentially large graphics resource, and to ensure your web application remains robust, it is important to avoid allocating too many of these resources at any point in time. For this reason it is important to ensure that the ImageBitmap is either consumed or closed.

As described in the OffscreenCanvas examples, passing this ImageBitmap to ImageBitmapRenderingContext.transferFromImageBitmap() consumes the ImageBitmap object; it no longer references the underlying graphics resource, and can not be passed to any other web APIs.

If your goal is to pass the ImageBitmap to other web APIs which do not consume it - for example, CanvasRenderingContext2D.drawImage() - then you should close it when you're done with it by calling ImageBitmap.close(). Don't simply drop the JavaScript reference to the ImageBitmap; doing so will keep its graphics resource alive until the next time the garbage collector runs.

If you call transferToImageBitmap() and don't intend to pass it to ImageBitmapRenderingContext.transferFromImageBitmap(), consider whether you need to call transferToImageBitmap() at all. Many web APIs which accept ImageBitmap also accept OffscreenCanvas as an argument, including CanvasRenderingContext2D.drawImage().

Examples

js
const offscreen = new OffscreenCanvas(256, 256);
const gl = offscreen.getContext("webgl");

// Perform some drawing using the gl context

offscreen.transferToImageBitmap();
// ImageBitmap { width: 256, height: 256 }

// Either:
// Pass this `ImageBitmap` to `ImageBitmapRenderingContext.transferFromImageBitmap`
// or:
// Use the `ImageBitmap` with other web APIs, and call `ImageBitmap.close()`!

Specifications

Specification
HTML
# dom-offscreencanvas-transfertoimagebitmap-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
transferToImageBitmap
Chrome – Full support
Chrome 69 (Release date: 2018-09-04)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 105 (Release date: 2022-09-20)
footnote Full support
Opera – Full support
Opera 56 (Release date: 2018-09-25)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 69 (Release date: 2018-09-04)
footnote Full support
Firefox for Android – Full support
Firefox for Android 105 (Release date: 2022-09-20)
footnote Full support
Opera Android – Full support
Opera Android 48 (Release date: 2018-11-08)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 10 (Release date: 2019-08-22)
footnote Full support
WebView Android – Full support
WebView Android 69 (Release date: 2018-09-04)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16.4 (Release date: 2023-03-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also