--- title: self.createImageBitmap() slug: Web/API/createImageBitmap translation_of: Web/API/WindowOrWorkerGlobalScope/createImageBitmap original_slug: Web/API/WindowOrWorkerGlobalScope/createImageBitmap ---
createImageBitmap
方法存在 windows 和 workers 中. 它接受各种不同的图像来源, 并返回一个{{domxref("Promise")}}, resolve为{{domxref("ImageBitmap")}}. 可选地参数,图像被剪裁成自(sx,sy)且宽度为sw,高度为sh的像素的矩形。
createImageBitmap(image[, options]).then(function(response) { ... }); createImageBitmap(image, sx, sy, sw, sh[, options]).then(function(response) { ... });
image
sx
sy
sw
sh
imageOrientation
: 指示图像是按原样呈现还是垂直翻转. none
(默认不翻转) 或 flipY
.premultiplyAlpha
: 指示位图颜色通道由alpha通道预乘. 选择其一:none
, premultiply
, 或 default
(默认).colorSpaceConversion
: 指示图像是否使用色彩空间转换进行解码. none
或 default
(默认). The value default
indicates that implementation-specific behavior is used.resizeWidth
: 指示新宽度的长整数。resizeHeight
: 指示新高度的长整数。resizeQuality
: 指定图像缩放算法. 选择其一pixelated
, low
(默认), medium
, 或 high
.返回一个解决ImageBitmap的{{domxref("Promise")}} ,当Promise成功时resolves接收一个包含所得到的矩形的位图数据{{domxref("ImageBitmap")}}。
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 = 'sprites.png';
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "webappapis.html#dom-createimagebitmap", "createImageBitmap")}} | {{Spec2('HTML WHATWG')}} |
{{ CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | {{CompatChrome(50)}} |
{{CompatGeckoDesktop(42)}} |
{{CompatNo}} | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatNo}} |
options parameter |
{{CompatChrome(52)}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatNo}} | 39 | {{CompatNo}} |
resizeWidth , resizeHeight , and resizeQuality |
{{CompatChrome(54)}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} |
SVGImageElement as source image |
{{CompatChrome(59)}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} |
Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatChrome(50)}} | {{CompatChrome(50)}} |
{{CompatGeckomobile(42)}} |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} |
options parameter |
{{CompatChrome(52)}} | {{CompatChrome(52)}} | {{CompatUnknown}} | {{CompatUnknown}} | 39 | {{CompatUnknown}} |
resizeWidth , resizeHeight , and resizeQuality |
{{CompatChrome(54)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | |
SVGImageElement as source image |
{{CompatChrome(59)}} | {{CompatChrome(59)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
[1] createImageBitmap()
now defined on {{domxref("WindowOrWorkerGlobalScope")}} mixin.