The OffscreenCanvas.convertToBlob()
method creates a Blob
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 as image/png
. Browsers are required to support image/png
; many will support additional formats including image/jpeg
and image/webp
.
The created image will have a resolution of 96dpi for file formats that support encoding resolution metadata.
convertToBlob()
convertToBlob(options)
A Promise
returning a Blob
object representing the image contained in the canvas.
The promise may be rejected with the following exceptions:
-
InvalidStateError
DOMException
-
The OffscreenCanvas
is not detached; in other words it still associated with the DOM and not the current worker.
-
SecurityError
DOMException
-
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.
-
IndexSizeError
DOMException
-
The canvas bitmap has no pixels (either the horizontal or vertical dimension is zero).
-
EncodingError
DOMException
-
The blob could not be created due to an encoding error.
const offscreen = new OffscreenCanvas(256, 256);
const gl = offscreen.getContext("webgl");
offscreen.convertToBlob().then((blob) => console.log(blob));