The createImageBitmap method of the WindowOrWorkerGlobalScope mixin accepts a variety of different image sources, and returns a Promise which resolves to an ImageBitmap. Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh.
Syntax
createImageBitmap(image[, options]).then(function(response) { ... });
createImageBitmap(image, sx, sy, sw, sh[, options]).then(function(response) { ... });
Parameters
image- An image source, which can be an
<img>, SVG<image>,<video>, OffscreenCanvas, or<canvas>element, aBlob,ImageData, or anotherImageBitmapobject. sx- The x coordinate of the reference point of the rectangle from which the ImageBitmap will be extracted.
sy- The y coordinate of the reference point of the rectangle from which the ImageBitmap will be extracted.
sw- The width of the rectangle from which the ImageBitmap will be extracted. This value can be negative.
sh- The height of the rectangle from which the ImageBitmap will be extracted. This value can be negative.
- options Optional
- An object that sets options for . The available options are:
imageOrientation: Indicates whether the image is presented as is or flipped vertically. Eithernone(default) orflipY.premultiplyAlpha: Indicates that the bitmaps color channels are premultiplied by the alpha channel. One ofnone,premultiply, ordefault(default).colorSpaceConversion: Indicates whether the image is decoded using color space conversion. Eithernoneordefault(default). The valuedefaultindicates that implementation-specific behavior is used.resizeWidth: A long integer that indicates the new width.resizeHeight: A long integer that indicates the new height.resizeQuality: Specifies an image scaling algorithm. One ofpixelated,low(default),medium, orhigh.
Return value
A Promise which resolves to an ImageBitmap object containing bitmap data from the given rectangle.
Example
var canvas = document.getElementById('myCanvas'),
ctx = canvas.getContext('2d'),
image = new Image();
image.onload = function() {
Promise.all([
createImageBitmap(this, 0, 0, 32, 32),
createImageBitmap(this, 32, 0, 32, 32)
]).then(function(sprites) {
ctx.drawImage(sprites[0], 0, 0);
ctx.drawImage(sprites[1], 32, 32);
});
}
image.src = '/old?u=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FWindowOrWorkerGlobalScope%2Fsprites.png&y=1999';
Specifications
| Specification | Status | Comment |
|---|---|---|
| WHATWG HTML Living Standard The definition of 'WindowOrWorkerGlobalScope.createImageBitmap()' in that specification. |
Living Standard | Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec. |
| WHATWG HTML Living Standard The definition of 'createImageBitmap' in that specification. |
Living Standard |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 50.0 | ? | (Yes) | No support | |
options parameter |
52.0 | ? | ? | 39 | No support |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | No support | 50.0 |
42.0 (42) |
? | (Yes) | ? | 50.0 |
options parameter |
No support | 52.0 | ? | ? | 39 | ? | 52.0 |
[1] createImageBitmap() now defined on WindowOrWorkerGlobalScope mixin.
See also
Document Tags and Contributors
Tags:
Contributors to this page:
Brettz9,
MatthiasSaihttam,
nmve,
chrisdavidmills,
jpmedley,
yisibl,
DrRataplan,
zeird,
fscholz,
Kaku,
adria
Last updated by:
Brettz9,