createImageBitmap()
method creates a bitmap from a given source, optionally cropped to contain only a portion of that source. The method exists on the global scope in both windows and workers. It accepts a variety of different image sources, and returns a
Promise
which resolves to an
ImageBitmap
.
const imageBitmapPromise = createImageBitmap(image[, options]); const imageBitmapPromise = createImageBitmap(image, sx, sy, sw, sh[, options]);
image
<img>
, SVG
<image>
,
<video>
,
<canvas>
,
HTMLImageElement
,
SVGImageElement
,
HTMLVideoElement
,
HTMLCanvasElement
,
Blob
,
ImageData
,
ImageBitmap
,或
OffscreenCanvas
对象。
sx
ImageBitmap
will be extracted.
sy
ImageBitmap
will be extracted.
sw
ImageBitmap
will be extracted. This value can be negative.
sh
ImageBitmap
will be extracted. This value can be negative.
选项
可选
imageOrientation
: Specifies whether the image should be presented as is or flipped vertically. Either
none
(默认) 或
flipY
.
premultiplyAlpha
: Specifies whether the bitmap's color channels should be premultiplied by the alpha channel. One of
none
,
premultiply
,或
default
(default).
colorSpaceConversion
: Specifies whether the image should be decoded using color space conversion. Either
none
or
default
(default). The value
default
indicates that implementation-specific behavior is used.
resizeWidth
: A long integer that indicates the output width.
resizeHeight
: A long integer that indicates the output height.
resizeQuality
: Specifies the algorithm to be used for resizing the input to match the output dimensions. One of
pixelated
,
low
(默认),
medium
,或
high
.
A
Promise
which resolves to an
ImageBitmap
object containing bitmap data from the given rectangle.
This example loads a sprite sheet, extracts individual sprites, and then renders each sprite to the canvas. A sprite sheet is an image containing multiple smaller images, each of which you want to be able to render separately.
var canvas = document.getElementById('myCanvas'),
ctx = canvas.getContext('2d'),
image = new Image();
// Wait for the sprite sheet to load
image.onload = function() {
Promise.all([
// Cut out two sprites from the sprite sheet
createImageBitmap(image, 0, 0, 32, 32),
createImageBitmap(image, 32, 0, 32, 32)
]).then(function(sprites) {
// Draw each sprite onto the canvas
ctx.drawImage(sprites[0], 0, 0);
ctx.drawImage(sprites[1], 32, 32);
});
}
// Load the sprite sheet from an image file
image.src = 'sprites.png';
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'createImageBitmap' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createImageBitmap
|
Chrome 50 | Edge 79 |
Firefox
52
|
IE No | Opera Yes | Safari No | WebView Android 50 | Chrome Android 50 | Firefox Android Yes | Opera Android Yes | Safari iOS ? | Samsung Internet Android 5.0 |
选项
参数
|
Chrome 52 | Edge 79 | Firefox No | IE No | Opera 39 | Safari No | WebView Android 52 | Chrome Android 52 | Firefox Android No | Opera Android 41 | Safari iOS ? | Samsung Internet Android 6.0 |
resizeWidth
,
resizeHeight
,和
resizeQuality
|
Chrome 54 | Edge 79 | Firefox No | IE No | Opera Yes | Safari No | WebView Android 54 | Chrome Android 54 | Firefox Android No | Opera Android Yes | Safari iOS ? | Samsung Internet Android 6.0 |
SVGImageElement
as source image
|
Chrome 59 | Edge 79 | Firefox 65 | IE No | Opera Yes | Safari No | WebView Android 59 | Chrome Android 59 | Firefox Android 65 | Opera Android Yes | Safari iOS ? | Samsung Internet Android 7.0 |
完整支持
不支持
兼容性未知
见实现注意事项。
WindowOrWorkerGlobalScope
atob()
btoa()
clearInterval()
clearTimeout()
createImageBitmap()
fetch()
queueMicrotask()
setInterval()
setTimeout()