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
|
---
title: CanvasRenderingContext2D.strokeStyle
slug: Web/API/CanvasRenderingContext2D/strokeStyle
tags:
- Canvas
- CanvasRenderingContext2D
translation_of: Web/API/CanvasRenderingContext2D/strokeStyle
---
<div>{{APIRef}}</div>
<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.strokeStyle</code></strong> 是 Canvas 2D API 描述画笔(绘制图形)颜色或者样式的属性。默认值是 <code>#000</code> (black)。</p>
<p>参见 <a href="/en-US/docs/Web/API/Canvas_API/Tutorial">Canvas Tutorial</a> 中的 <a href="/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors">Applying styles and color</a> 章节。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><var><em>ctx</em>.strokeStyle = color;
</var><var><em>ctx</em>.strokeStyle = gradient;
</var><var><em>ctx</em>.strokeStyle = pattern;</var>
</pre>
<h3 id="选项">选项</h3>
<dl>
<dt><code>color</code></dt>
<dd>{{domxref("DOMString")}} 字符串,可以转换成 CSS {{cssxref("<color>")}} 值。</dd>
<dt><code>gradient</code></dt>
<dd>{{domxref("CanvasGradient")}} 对象(线性渐变或放射性渐变)。</dd>
<dt><code>pattern</code></dt>
<dd>{{domxref("CanvasPattern")}} 对象(可重复的图片)。</dd>
</dl>
<h2 id="示例">示例</h2>
<h3 id="Using_the_strokeStyle_property" name="Using_the_strokeStyle_property">使用 <code>strokeStyle</code> 属性设置不同的颜色</h3>
<p>这是一段简单的代码片段,使用 <code>strokeStyle</code> 属性设置不同的颜色。</p>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><canvas id="canvas"></canvas>
</pre>
<h4 id="JavaScript">JavaScript</h4>
<pre class="brush: js">var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.strokeStyle = "blue";
ctx.strokeRect(10, 10, 100, 100);
</pre>
<p>修改下面的代码并在线查看 canvas 的变化:</p>
<div class="hidden">
<h6 id="Playable_code" name="Playable_code">Playable code</h6>
<pre class="brush: html"><canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas>
<div class="playable-buttons">
<input id="edit" type="button" value="Edit" />
<input id="reset" type="button" value="Reset" />
</div>
<textarea id="code" class="playable-code">
ctx.strokeStyle = "blue";
ctx.strokeRect(10, 10, 100, 100);</textarea>
</pre>
<pre class="brush: js">var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;
function drawCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
eval(textarea.value);
}
reset.addEventListener("click", function() {
textarea.value = code;
drawCanvas();
});
edit.addEventListener("click", function() {
textarea.focus();
})
textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);
</pre>
</div>
<p>{{ EmbedLiveSample('Playable_code', 700, 360) }}</p>
<h3 id="A_strokeStyle_example" name="A_strokeStyle_example"><code>strokeStyle</code> 例子</h3>
<p>这个例子使用 <code>strokeStyle 属性改变图形轮廓线的颜色。</code>我们使用 {{domxref("CanvasRenderingContext2D.arc", "arc()")}} 绘制圆形来代替正方形。</p>
<pre class="brush: js">var ctx = document.getElementById('canvas').getContext('2d');
for (var i=0;i<6;i++){
for (var j=0;j<6;j++){
ctx.strokeStyle = 'rgb(0,' + Math.floor(255-42.5*i) + ',' +
Math.floor(255-42.5*j) + ')';
ctx.beginPath();
ctx.arc(12.5+j*25,12.5+i*25,10,0,Math.PI*2,true);
ctx.stroke();
}
}
</pre>
<div class="hidden">
<pre class="brush: html"><canvas id="canvas" width="150" height="150"></canvas></pre>
</div>
<p>结果如下显示:</p>
<p>{{EmbedLiveSample("A_strokeStyle_example", "180", "180", "https://mdn.mozillademos.org/files/253/Canvas_strokestyle.png")}}</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-strokestyle", "CanvasRenderingContext2D.strokeStyle")}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div class="hidden">
<p>此页面上的兼容性表是根据结构化数据生成的。 如果您想提供数据,请查看 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并向我们发送请求请求。</p>
</div>
<p>{{Compat("api.CanvasRenderingContext2D.strokeStyle")}}</p>
<div id="compat-mobile"></div>
<h3 id="WebKitBlink-specific_注解">WebKit/Blink-specific 注解</h3>
<ul>
<li>在基于 WebKit- 和 Blink- 的浏览器中,除了此属性外,还实现了一个不标准的并且不赞成使用的方法 <code>ctx.setStrokeColor()</code> 。
<pre class="brush: js">setStrokeColor(color, optional alpha);
setStrokeColor(grayLevel, optional alpha);
setStrokeColor(r, g, b, a);
setStrokeColor(c, m, y, k, a);
</pre>
</li>
</ul>
<h2 id="参见">参见</h2>
<ul>
<li>接口定义, {{domxref("CanvasRenderingContext2D")}}</li>
<li>{{domxref("CanvasGradient")}}</li>
<li>{{domxref("CanvasPattern")}}</li>
</ul>
|