aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/canvasrenderingcontext2d/linewidth/index.html
blob: 12216cccb7069688ba17f8bab4ed29ecac7f9785 (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
---
title: CanvasRenderingContext2D.lineWidth
slug: Web/API/CanvasRenderingContext2D/lineWidth
tags:
  - API
  - Canvas
translation_of: Web/API/CanvasRenderingContext2D/lineWidth
---
<div>{{APIRef}}</div>

<p>The <code><strong>CanvasRenderingContext2D</strong></code><strong><code>.lineWidth</code></strong> 是 Canvas 2D API 设置线段厚度的属性(即线段的宽度)。</p>

<div class="blockIndicator note">
<p><strong>Note:</strong>线可以通过{{domxref("CanvasRenderingContext2D.stroke()", "stroke()")}}{{domxref("CanvasRenderingContext2D.strokeRect()", "strokeRect()")}}, 和{{domxref("CanvasRenderingContext2D.strokeText()", "strokeText()")}} 方法绘制。</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var><em>ctx</em>.lineWidth = value;</var></pre>

<h3 id="选项">选项</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>描述线段宽度的数字。 0、 负数、 {{jsxref("Infinity")}}{{jsxref("NaN")}} 会被忽略。</dd>
</dl>

<h2 id="示例">示例</h2>

<h3 id="改变线宽">改变线宽</h3>

<p>此示例使用15个单位的线宽绘制直线和矩形。</p>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
</pre>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js">const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

ctx.lineWidth = 15;

ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(130, 130);
ctx.rect(40, 40, 70, 70);
ctx.stroke();
</pre>

<h4 id="结果">结果</h4>

<p>{{ EmbedLiveSample('改变线宽', 700, 180) }}</p>

<h3 id="更多示例">更多示例</h3>

<p>有关此属性的更多示例和说明,请参阅在“画布教程”中的<a href="/zh-CN/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors">使用样式和颜色</a></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-linewidth", "CanvasRenderingContext2D.lineWidth")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.CanvasRenderingContext2D.lineWidth")}}</p>

<h3 id="WebKitBlink-specific_注解">WebKit/Blink-specific 注解</h3>

<ul>
 <li>在基于 WebKit- 和 Blink- 的浏览器中,除了此属性外,实现了一个不标准的并且不赞成使用的方法 <code>ctx.setLineWidth()</code></li>
</ul>

<h3 id="Gecko-specific_注解">Gecko-specific 注解</h3>

<ul>
 <li>从 Gecko 2.0 {{geckoRelease("2.0")}}版本开始, 设置<code>lineWidth</code> 为负数不再抛出异常,所有非正数值都会被忽略。</li>
</ul>

<h2 id="参见">参见</h2>

<ul>
 <li>接口定义, {{domxref("CanvasRenderingContext2D")}}</li>
 <li>{{domxref("CanvasRenderingContext2D.lineCap")}}</li>
 <li>{{domxref("CanvasRenderingContext2D.lineJoin")}}</li>
 <li><a href="/zh-CN/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors">使用样式和颜色</a></li>
</ul>