Changing a line width in canvas in JavaScript

The line width is set using the lineWidth property. Just give it a width, like this: lineWidth = 5 - you get a line width of 5px. Let's look at an example:

<canvas width="200" height="200" style="background: #f4f4f4;"></canvas> let canvas = document.querySelector('canvas'); let ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.lineWidth = 5; // width of 5px ctx.moveTo(50, 50); ctx.lineTo(150, 50); ctx.stroke();

:

A width can also be changed for outlines, such as those drawn with 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();

:

enru