返回到:Canvas API:CanvasRenderingContext2D
The CanvasRenderingContext2D.lineWidth
是 Canvas 2D API 设置线段厚度的属性(即线段的宽度)。
备注: 线可以通过
stroke()
,strokeRect()
, 和strokeText()
方法绘制。
语法
ctx.lineWidth = value;
选项
value
描述线段宽度的数字。0、负数、 Infinity
和 NaN
会被忽略。
示例
改变线宽
此示例使用 15 个单位的线宽绘制直线和矩形。
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();
看下demo:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CanvasRenderingContext2D.lineWidth - 改变线宽 | Web176教程www.web176.com</title> <meta name="robots" content="noindex, nofollow"> <style> body { padding: 0; margin: 0; } svg:not(:root) { display: block; } .playable-code { background-color: #f4f7f8; border: none; border-left: 6px solid #558abb; border-width: medium medium medium 6px; color: #4d4e53; height: 100px; width: 90%; padding: 10px 10px 0; } .playable-canvas { border: 1px solid #4d4e53; border-radius: 2px; } .playable-buttons { text-align: right; width: 90%; padding: 5px 10px 5px 26px; } </style> </head> <body> <canvas id="canvas"></canvas> <script> 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(); </script> </body> </html>
返回到:Canvas API:CanvasRenderingContext2D
作者:terry,如若转载,请注明出处:https://www.web176.com/canvas_api/7927.html