aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/createimagebitmap/index.html
blob: 0e4bd707dd687e5c5fab68cb61333ad74330a8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
title: self.createImageBitmap()
slug: Web/API/createImageBitmap
translation_of: Web/API/WindowOrWorkerGlobalScope/createImageBitmap
original_slug: Web/API/WindowOrWorkerGlobalScope/createImageBitmap
---
<div>{{APIRef("Canvas API")}}</div>

<p><code><strong>createImageBitmap</strong></code> 方法存在 windows 和 workers 中. 它接受各种不同的图像来源, 并返回一个{{domxref("Promise")}}, resolve为{{domxref("ImageBitmap")}}. 可选地参数,图像被剪裁成自(sx,sy)且宽度为sw,高度为sh的像素的矩形。</p>

<h2 id="Syntax">Syntax</h2>

<pre>createImageBitmap(<em>image</em>[, options]).then(function(response) { ... });
createImageBitmap(<em>image, sx, sy, sw, sh</em>[, options]).then(function(response) { ... });
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>image</code></dt>
 <dd>一个图像源, 可以是一个 {{HTMLElement("img")}}, SVG {{SVGElement("image")}}, {{HTMLElement("video")}}, {{HTMLElement("canvas")}}, {{domxref("HTMLImageElement")}}, {{domxref("SVGImageElement")}}, {{domxref("HTMLVideoElement")}}, {{domxref("HTMLCanvasElement")}}{{domxref("Blob")}}, {{domxref("ImageData")}}, {{domxref("ImageBitmap")}}, 或 {{domxref("OffscreenCanvas")}} 对象.</dd>
 <dt><code>sx</code></dt>
 <dd>裁剪点x坐标.</dd>
 <dt><code>sy</code></dt>
 <dd>裁剪点y坐标.</dd>
 <dt><code>sw</code></dt>
 <dd>裁剪宽度,值可为负数.</dd>
 <dt><code>sh</code></dt>
 <dd>裁剪高度,值可为负数.</dd>
 <dt>options {{optional_inline}}</dt>
 <dd>为其设置选项的对象。可用的选项是:
 <ul>
  <li><code>imageOrientation</code>: 指示图像是按原样呈现还是垂直翻转.  <code>none</code> (默认不翻转) 或 <code>flipY</code>.</li>
  <li><code>premultiplyAlpha</code>: 指示位图颜色通道由alpha通道预乘. 选择其一:<code>none</code>, <code>premultiply</code>, 或 <code>default</code> (默认).</li>
  <li><code>colorSpaceConversion</code>: 指示图像是否使用色彩空间转换进行解码. <code>none</code> 或 <code>default</code> (默认). The value <code>default</code> indicates that implementation-specific behavior is used.</li>
  <li><code>resizeWidth</code>: 指示新宽度的长整数。</li>
  <li><code>resizeHeight</code>: 指示新高度的长整数。</li>
  <li><code>resizeQuality</code>: 指定图像缩放算法. 选择其一<code>pixelated</code>, <code>low</code> (默认), <code>medium</code>, 或 <code>high</code>.</li>
 </ul>
 </dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>返回一个解决ImageBitmap的{{domxref("Promise")}} ,当Promise成功时resolves接收一个包含所得到的矩形的位图数据{{domxref("ImageBitmap")}}</p>

<h2 id="Example">Example</h2>

<pre class="brush: js  language-js">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';
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "webappapis.html#dom-createimagebitmap", "createImageBitmap")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

{{Compat("api.createImageBitmap")}}

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("CanvasRenderingContext2D.drawImage()")}}</li>
 <li>{{domxref("ImageData")}}</li>
</ul>

<p>
 <audio class="hidden"> </audio>
</p>