此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
CanvasRenderingContext2D:lineWidth 属性
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Canvas 2D API 的 CanvasRenderingContext2D.lineWidth 属性用于设置线宽。
备注:线可以通过 stroke()、strokeRect() 和 strokeText() 方法绘制。
值
示例
>改变线宽
此示例使用 15 个单位的线宽绘制直线和矩形。
HTML
html
<canvas id="canvas"></canvas>
JavaScript
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();
结果
[フレーム]
更多示例
规范
| Specification |
|---|
| HTML> # dom-context-2d-linewidth-dev> |
浏览器兼容性
Enable JavaScript to view this browser compatibility table.