CanvasRenderingContext2D .lineWidth property of the Canvas 2D API sets the thickness of lines.

注意: Lines can be drawn with the stroke() , strokeRect() ,和 strokeText() 方法。

句法

ctx.lineWidth = value;
					

选项

value
A number specifying the line width, in coordinate space units. Zero, negative, Infinity ,和 NaN values are ignored. This value is 1.0 在默认情况下。

范例

Changing line width

This example draws a line and a rectangle, using a line width of 15 units.

HTML

<canvas id="canvas"></canvas>
					

JavaScript

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();
					

结果

More examples

For more examples and explanation about this property, see Applying styles and color Canvas Tutorial .

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.lineWidth' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
lineWidth Chrome Yes Edge 12 Firefox 1.5 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

  • In WebKit- and Blink-based Browsers, a non-standard and deprecated method ctx.setLineWidth() is implemented in addition to this property.

Gecko-specific notes

  • Starting Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1), setting lineWidth to a negative value no longer throws an exception; instead, it properly ignores non-positive values.

另请参阅

元数据

  • 最后修改: