diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/canvasrenderingcontext2d/getimagedata | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/canvasrenderingcontext2d/getimagedata')
-rw-r--r-- | files/zh-cn/web/api/canvasrenderingcontext2d/getimagedata/index.html | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/canvasrenderingcontext2d/getimagedata/index.html b/files/zh-cn/web/api/canvasrenderingcontext2d/getimagedata/index.html new file mode 100644 index 0000000000..1f4d826121 --- /dev/null +++ b/files/zh-cn/web/api/canvasrenderingcontext2d/getimagedata/index.html @@ -0,0 +1,96 @@ +--- +title: CanvasRenderingContext2D.getImageData() +slug: Web/API/CanvasRenderingContext2D/getImageData +translation_of: Web/API/CanvasRenderingContext2D/getImageData +--- +<div>{{APIRef}}</div> + +<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.getImageData()</code></strong> 返回一个{{domxref("ImageData")}}对象,用来描述canvas区域隐含的像素数据,这个区域通过矩形表示,起始点为<em>(sx, sy)、</em>宽为<em>sw、</em>高为<em>sh。</em></p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">ImageData <var><em>ctx</em>.getImageData(sx, sy, sw, sh);</var> +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <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> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个{{domxref("ImageData")}} 对象,包含canvas给定的矩形图像数据。</p> + +<h3 id="错误抛出">错误抛出</h3> + +<dl> + <dt><code>IndexSizeError</code></dt> + <dd>如果高度或者宽度变量为0,则抛出错误。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="使用_getImageData_方法">使用 <code>getImageData</code> 方法</h3> + +<p>这是一段使用 <code>getImageData</code> 方法的简单的代码片段。 获取更多信息,请看 <a href="/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas">canvas像素控制 </a>和 {{domxref("ImageData")}} 对象。</p> + +<h4 id="HTML">HTML</h4> + +<pre class="brush: html notranslate"><canvas id="canvas"></canvas> +</pre> + +<h4 id="JavaScript">JavaScript</h4> + +<pre class="brush: js; highlight:[6] notranslate">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); +ctx.rect(10, 10, 100, 100); +ctx.fill(); + +console.log(ctx.getImageData(50, 50, 100, 100)); +// ImageData { width: 100, height: 100, data: Uint8ClampedArray[40000] } +</pre> + +<h2 id="规范描述">规范描述</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', "scripting.html#dom-context-2d-getimagedata", "CanvasRenderingContext2D.getImageData")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.CanvasRenderingContext2D.getImageData")}}</p> + +<h3 id="兼容性注解">兼容性注解</h3> + +<ul> + <li>从 {{Gecko("5.0")}}开始, <code>getImageData()</code> 可以正确地接受超出canvas边界的矩形;canvas范围外的像素返回值是透明的,并且如果矩形小于一个像素,会返回至少一像素值的图像数据。</li> +</ul> + +<h2 id="参见">参见</h2> + +<ul> + <li>接口定义, {{domxref("CanvasRenderingContext2D")}}.</li> + <li>{{domxref("ImageData")}}</li> + <li><a href="/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas">Pixel manipulation with canvas</a></li> +</ul> |