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
|
---
title: CanvasRenderingContext2D.strokeRect()
slug: Web/API/CanvasRenderingContext2D/strokeRect
tags:
- API
- Canvas
translation_of: Web/API/CanvasRenderingContext2D/strokeRect
---
<div>{{APIRef}}</div>
<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.strokeRect()</code></strong> 是 Canvas 2D API 在 canvas 中,使用当前的绘画样式,描绘一个起点在 <em>(x, y)</em> 、宽度为<em> w</em> 、高度为 <em>h</em> 的矩形的方法。</p>
<p>此方法直接绘制到画布而不修改当前路径,因此任何后续{{domxref("CanvasRenderingContext2D.fill()", "fill()")}} 或{{domxref("CanvasRenderingContext2D.stroke()", "stroke()")}}调用对它没有影响。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">void <var><em>ctx</em>.strokeRect(x, y, width, height);</var>
</pre>
<p><code>strokeRect()</code>方法绘制一个描边矩形,其起点为<code>(x, y)</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>这是一段使用 <code>strokeRect</code> 方法的简单的代码片段。</p>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><canvas id="canvas"></canvas>
</pre>
<h4 id="JavaScript">JavaScript</h4>
<p>矩形的左上角是(20,10)。它的宽度为160,高度为100。</p>
<pre class="brush: js">const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'green';
ctx.strokeRect(20, 10, 160, 100);</pre>
<h4 id="结果">结果</h4>
<p>{{ EmbedLiveSample('一个简单的填充矩形', 700, 180) }}</p>
<h3 id="应用多种上下文设置">应用多种上下文设置</h3>
<p>此示例绘制一个带有阴影和粗斜面轮廓的矩形。</p>
<h4 id="HTML_2">HTML</h4>
<pre class="brush: html"><code><canvas id="canvas"></canvas></code></pre>
<h4 id="JavaScript_2">JavaScript</h4>
<pre class="brush: js">const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.shadowColor = '#d53';
ctx.shadowBlur = 20;
ctx.lineJoin = 'bevel';
ctx.lineWidth = 15;
ctx.strokeStyle = '#38f';
ctx.strokeRect(30, 30, 160, 90);</pre>
<h4 id="结果_2">结果</h4>
<p>{{ EmbedLiveSample('应用多种上下文设置', 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-strokerect", "CanvasRenderingContext2D.strokeRect")}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.CanvasRenderingContext2D.strokeRect")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>接口定义,{{domxref("CanvasRenderingContext2D")}}</li>
<li>{{domxref("CanvasRenderingContext2D.strokeStyle")}}</li>
<li>{{domxref("CanvasRenderingContext2D.clearRect()")}}</li>
<li>{{domxref("CanvasRenderingContext2D.fillRect()")}}</li>
</ul>
|