aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/canvasrenderingcontext2d/clearrect/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/canvasrenderingcontext2d/clearrect/index.html')
-rw-r--r--files/zh-cn/web/api/canvasrenderingcontext2d/clearrect/index.html110
1 files changed, 110 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/canvasrenderingcontext2d/clearrect/index.html b/files/zh-cn/web/api/canvasrenderingcontext2d/clearrect/index.html
new file mode 100644
index 0000000000..0a08faaaf2
--- /dev/null
+++ b/files/zh-cn/web/api/canvasrenderingcontext2d/clearrect/index.html
@@ -0,0 +1,110 @@
+---
+title: CanvasRenderingContext2D.clearRect()
+slug: Web/API/CanvasRenderingContext2D/clearRect
+translation_of: Web/API/CanvasRenderingContext2D/clearRect
+---
+<div>{{APIRef}}</div>
+
+<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.clearRect()</code></strong>是Canvas 2D API的方法,这个方法通过把像素设置为透明以达到擦除一个矩形区域的目的。</p>
+
+<div class="blockIndicator note">
+<p><strong>注意:</strong> 如果没有依照 <a href="/zh-CN/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes#%E7%BB%98%E5%88%B6%E8%B7%AF%E5%BE%84">绘制路径</a> 的步骤,使用 <code>clearRect()</code> 会导致意想之外的结果。请确保在调用 <code>clearRect()</code>之后绘制新内容前调用{{domxref("CanvasRenderingContext2D.beginPath", "beginPath()")}} 。</p>
+</div>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox notranslate">void <em>ctx</em>.clearRect(<em>x</em>, <em>y</em>, <em>width</em>, <em>height</em>);
+</pre>
+
+<p> <code>clearRect()</code> 方法在一个矩形区域内设置所有像素都是透明的(<code>rgba(0,0,0,0)</code>)。这个矩形范围的左上角在 <code>(x, y)</code>,宽度和高度分别由 <code>width</code> 和<code>height</code>确定。</p>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><code>x</code></dt>
+ <dd>矩形起点的 x 轴坐标。</dd>
+ <dt><code>y</code></dt>
+ <dd>矩形起点的 y 轴坐标。</dd>
+ <dt><code>width</code></dt>
+ <dd>矩形的宽度。</dd>
+ <dt><code>height</code></dt>
+ <dd>矩形的高度。</dd>
+</dl>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="清除整个画布">清除整个画布</h3>
+
+<p>这段代码清除整个画布。这段代码通常在动画的每一帧开始被执行。清除的范围涵覆盖了整个 {{HtmlElement("canvas")}} 元素。</p>
+
+<pre class="brush: js; highlight:[3] notranslate">const canvas = document.getElementById('canvas');
+const ctx = canvas.getContext('2d');
+ctx.clearRect(0, 0, canvas.width, canvas.height);</pre>
+
+<h3 id="清除一部分画布">清除一部分画布</h3>
+
+<p>这仅是一段简单地使用 <code>clearRect</code> 方法的代码片段。</p>
+
+<h4 id="HTML">HTML</h4>
+
+<pre class="brush: html notranslate">&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
+</pre>
+
+<h4 id="JavaScript">JavaScript</h4>
+
+<p>下面代码中被清除的区域是一个矩形,它的左上点坐标在(10, 10),宽度和高度分别是120和100像素。</p>
+
+<pre class="brush: js; highlight:[11] notranslate">var canvas = document.getElementById("canvas");
+var ctx = canvas.getContext("2d");
+
+// 绘制黄色背景
+ctx.beginPath();
+ctx.fillStyle = '#ff6';
+ctx.fillRect(0, 0, canvas.width, canvas.height);
+
+// 绘制蓝色三角形
+ctx.beginPath();
+ctx.fillStyle = 'blue';
+ctx.moveTo(20, 20);
+ctx.lineTo(180, 20);
+ctx.lineTo(130, 130);
+ctx.closePath();
+ctx.fill();
+
+// 清除一部分画布
+ctx.clearRect(10, 10, 120, 100);</pre>
+
+<h4 id="结果">结果</h4>
+
+<p>{{EmbedLiveSample('Erasing_part_of_a_canvas', 700, 180)}}</p>
+
+<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-clearrect", "CanvasRenderingContext2D.clearRect")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.CanvasRenderingContext2D.clearRect")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>接口定义, {{domxref("CanvasRenderingContext2D")}}</li>
+ <li>{{domxref("CanvasRenderingContext2D.fillRect()")}}</li>
+ <li>{{domxref("CanvasRenderingContext2D.strokeRect()")}}</li>
+</ul>