คุณสมบัติ lineWidth
คุณสมบัติ lineWidth กำหนดความหนา
ของเส้นเมื่อวาดบน canvas
ไวยากรณ์
context.lineWidth = จำนวนเต็ม;
ตัวอย่าง
มาวาดเส้นและตั้งค่าความหนา
เป็น 5 พิกเซล:
<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; // ความหนา 5px
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.stroke();
:
ตัวอย่าง
ความหนายังสามารถเปลี่ยนได้สำหรับเส้นโครงร่าง,
เช่นที่วาดด้วย 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();
: