lineWidth property

The lineWidth property sets the line width when drawing on the canvas.

Syntax

javascriptcontext.lineWidth = integer;

Example

Let's draw a line and set its width to 5 pixels:

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 width
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.stroke();

Результат:

Example

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

Результат:

See also

  • the lineCap property
    that sets the type of a line end
  • the lineJoin property
    that sets lines join style