คุณสมบัติ lineWidth

คุณสมบัติ lineWidth กำหนดความหนา ของเส้นเมื่อวาดบน canvas

ไวยากรณ์

javascriptcontext.lineWidth = จำนวนเต็ม;

ตัวอย่าง

มาวาดเส้นและตั้งค่าความหนา เป็น 5 พิกเซล:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptlet 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:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptlet canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');

ctx.rect(50, 50, 100, 100);
ctx.lineWidth = 5;
ctx.stroke();

Результат:

ดูเพิ่มเติม

  • คุณสมบัติ lineCap,
    ซึ่งกำหนดรูปลักษณ์ของปลายเส้น
  • คุณสมบัติ lineJoin,
    ซึ่งกำหนดสไตล์ของจุดต่อเส้น