大网数据广告活动
Web 标准与前端开发

OffscreenCanvas: convertToBlob() method

· 52CE 我爱测

The **`OffscreenCanvas.convertToBlob()`** method creates a Blob object representing the image contained in the canvas.

Canvas API

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.

Syntax

convertToBlob()
convertToBlob(options)

Parameters

  • options
    • : An object with the following properties:
      • type
        • : A string indicating the image format. The default type is image/png; this image format will be also used if the specified type is not supported.
      • quality
        • : A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/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

A Promise returning a Blob object representing the image contained in the canvas.

Exceptions

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.

Examples

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

// Perform some drawing using the gl context

offscreen.convertToBlob().then((blob) => console.log(blob));
// Blob { size: 334, type: "image/png" }

Specifications

Browser compatibility

See also

  • The interface defining this method, OffscreenCanvas.

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。