aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/createimagebitmap/index.html
blob: c71c5925516655d2537067cb21a14095e2bd7f10 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
---
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>

<p>{{ CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Edge</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(50)}}</td>
   <td>
    <p>{{CompatGeckoDesktop(42)}}<br>
     {{CompatGeckoDesktop(52)}}<sup>[1]</sup></p>
   </td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>options</code> parameter</td>
   <td>{{CompatChrome(52)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>39</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>resizeWidth</code>, <code>resizeHeight</code>, and <code>resizeQuality</code></td>
   <td>{{CompatChrome(54)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>SVGImageElement</code> as source image</td>
   <td>{{CompatChrome(59)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android Webview</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(50)}}</td>
   <td>{{CompatChrome(50)}}</td>
   <td>
    <p>{{CompatGeckomobile(42)}}<br>
     {{CompatGeckoMobile(52)}}<sup>[1]</sup></p>
   </td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>options</code> parameter</td>
   <td>{{CompatChrome(52)}}</td>
   <td>{{CompatChrome(52)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>39</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>resizeWidth</code>, <code>resizeHeight</code>, and <code>resizeQuality</code></td>
   <td>{{CompatChrome(54)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td> </td>
  </tr>
  <tr>
   <td><code>SVGImageElement</code> as source image</td>
   <td>{{CompatChrome(59)}}</td>
   <td>{{CompatChrome(59)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] <code>createImageBitmap()</code> now defined on {{domxref("WindowOrWorkerGlobalScope")}} mixin.</p>

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

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

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