Thuộc tính lineWidth
Thuộc tính lineWidth đặt độ dày
của đường thẳng khi vẽ trên canvas.
Cú pháp
context.lineWidth = số nguyên;
Ví dụ
Hãy vẽ một đường thẳng và đặt độ dày của nó
là 5 pixel:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.lineWidth = 5; // độ dày 5px
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.stroke();
:
Ví dụ
Độ dày cũng có thể thay đổi cho các đường viền,
ví dụ như được vẽ bằng rect:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
ctx.rect(50, 50, 100, 100);
ctx.lineWidth = 5;
ctx.stroke();
: