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

ImageDecoder

· 52CE 我爱测

The **`ImageDecoder`** interface of the WebCodecs API provides a way to unpack and decode encoded image data.

WebCodecs APIwindow_and_dedicated

The ImageDecoder interface of the WebCodecs API provides a way to unpack and decode encoded image data.

Constructor

  • ImageDecoder.ImageDecoder
    • : Creates a new ImageDecoder object.

Instance properties

  • ImageDecoder.complete
    • : Returns a boolean value indicating whether encoded data is completely buffered.
  • ImageDecoder.completed
    • : Returns a Promise that resolves once complete is true.
  • ImageDecoder.tracks
    • : Returns an ImageTrackList object listing the available tracks and providing a method for selecting a track to decode.
  • ImageDecoder.type
    • : Returns a string reflecting the MIME type configured during construction.

Static methods

  • ImageDecoder.isTypeSupported_static
    • : Indicates if the provided MIME type is supported for unpacking and decoding.

Instance methods

  • ImageDecoder.close()
    • : Ends all pending work and releases system resources.
  • ImageDecoder.decode()
    • : Enqueues a control message to decode the frame of an image.
  • ImageDecoder.reset()
    • : Aborts all pending decode() operations.

Examples

Given a canvas element:

<canvas></canvas>

the following code decodes and renders an animated image to that canvas:

let imageDecoder = null;
let imageIndex = 0;

function renderImage(result) {
  const canvas = document.querySelector("canvas");
  const canvasContext = canvas.getContext("2d");

  canvasContext.drawImage(result.image, 0, 0);

  const track = imageDecoder.tracks.selectedTrack;

  // We check complete here since `frameCount` won't be stable until all
  // data has been received. This may cause us to receive a RangeError
  // during the decode() call below which needs to be handled.
  if (imageDecoder.complete) {
    if (track.frameCount === 1) return;

    if (imageIndex + 1 >= track.frameCount) imageIndex = 0;
  }

  // Decode the next frame ahead of display so it's ready in time.
  imageDecoder
    .decode({ frameIndex: ++imageIndex })
    .then((nextResult) =>
      setTimeout(() => {
        renderImage(nextResult);
      }, result.image.duration / 1000.0),
    )
    .catch((e) => {
      // We can end up requesting an imageIndex past the end since
      // we're using a ReadableStream from fetch(), when this happens
      // just wrap around.
      if (e instanceof RangeError) {
        imageIndex = 0;
        imageDecoder.decode({ frameIndex: imageIndex }).then(renderImage);
      } else {
        throw e;
      }
    });
}

function decodeImage(imageByteStream) {
  imageDecoder = new ImageDecoder({ data: imageByteStream, type: "image/gif" });
  imageDecoder.decode({ frameIndex: imageIndex }).then(renderImage);
}

fetch("fancy.gif").then((response) => decodeImage(response.body));

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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