Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. OffscreenCanvas
  4. convertToBlob()

OffscreenCanvas: convertToBlob() 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.

* Some parts of this feature may have varying levels of support.

Note: This feature is available inWeb Workers.

TheOffscreenCanvas.convertToBlob() method creates aBlob object representing the image contained in the canvas.

The desired file format and image quality may be specified.If the file format is not specified, or if the given format is not supported, then the data will be exported asimage/png.Browsers are required to supportimage/png; many will support additional formats includingimage/jpeg andimage/webp.

The created image will have a resolution of 96dpi for file formats that support encoding resolution metadata.

Syntax

js
convertToBlob()convertToBlob(options)

Parameters

optionsOptional

An object with the following properties:

type

A string indicating the image format.The default type isimage/png; this image format will be also used if the specified type is not supported.

quality

ANumber between0 and1 indicating the image quality to be used when creating images using file formats that support lossy compression (such asimage/jpeg orimage/webp).A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.

Return value

APromise returning aBlob object representing the image contained in the canvas.

Exceptions

The promise may be rejected with the following exceptions:

InvalidStateErrorDOMException

TheOffscreenCanvas is not detached; in other words it still associated with the DOM and not the current worker.

SecurityErrorDOMException

The canvas context mode is 2d and the bitmap is not origin-clean; at least some of its contents have or may have been loaded from a site other than the one from which the document itself was loaded.

IndexSizeErrorDOMException

The canvas bitmap has no pixels (either the horizontal or vertical dimension is zero).

EncodingErrorDOMException

The blob could not be created due to an encoding error.

Examples

js
const offscreen = new OffscreenCanvas(256, 256);const gl = offscreen.getContext("webgl");// Perform some drawing using the gl contextoffscreen.convertToBlob().then((blob) => console.log(blob));// Blob { size: 334, type: "image/png" }

Specifications

Specification
HTML
# dom-offscreencanvas-converttoblob-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp