The ImageDecoder
interface of the WebCodecs API provides a way to unpack and decode encoded image data.
Constructor
ImageDecoder()
Experimental
-
Creates a new ImageDecoder
object.
Instance properties
ImageDecoder.complete
Read only Experimental
-
Returns a boolean value indicating whether encoded data is completely buffered.
ImageDecoder.completed
Read only Experimental
-
Returns a Promise
that resolves once complete
is true.
ImageDecoder.tracks
Read only Experimental
-
Returns an ImageTrackList
object listing the available tracks and providing a method for selecting a track to decode.
ImageDecoder.type
Read only Experimental
-
Returns a string reflecting the MIME type configured during construction.
Static methods
ImageDecoder.isTypeSupported()
Experimental
-
Indicates if the provided MIME type is supported for unpacking and decoding.
Instance methods
ImageDecoder.close()
Experimental
-
Ends all pending work and releases system resources.
ImageDecoder.decode()
Experimental
-
Enqueues a control message to decode the frame of an image.
ImageDecoder.reset()
Experimental
-
Aborts all pending decode()
operations.
Examples
Given a <canvas>
element:
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;
if (imageDecoder.complete) {
if (track.frameCount === 1) return;
if (imageIndex + 1 >= track.frameCount) imageIndex = 0;
}
imageDecoder
.decode({ frameIndex: ++imageIndex })
.then((nextResult) =>
setTimeout(() => {
renderImage(nextResult);
}, result.image.duration / 1000.0),
)
.catch((e) => {
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
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
ImageDecoder |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
ImageDecoder |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
close |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
complete |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
completed |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
decode |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
isTypeSupported_static |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
reset |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
tracks |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |
type |
94 |
94 |
No |
No |
80 |
No |
94 |
94 |
No |
66 |
No |
17.0 |