Změna tloušťky čar v canvasu v JavaScriptu
Tloušťka čáry se nastavuje pomocí vlastnosti
lineWidth. Jednoduše ji přiřaďte tloušťku,
třeba takto: lineWidth = 5 - získáte
tloušťku čáry 5px. Podívejme se na příklad:
<canvas width="200" height="200" style="background: #f4f4f4;"></canvas>
let canvas = document.querySelector('canvas');
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.lineWidth = 5; // tloušťka 5px
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.stroke();
:
Tloušťku lze také měnit i u obrysů,
třeba nakreslených pomocí rect:
<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();
: