offline version v3


255 of 264 menu

lineCap property

The lineCap property sets the type of a line end. Can take the following values: butt - flat end (default), round - rounded end, square - square end.

Syntax

context.lineCap = 'butt', 'round' or 'square';

Example

Let's draw some lines and set the first to butt, the second to round, and the third to square. Pay attention to the red line - it marks the set beginning of the line (and round and square climb out of it):

<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 = 10; ctx.lineCap = 'butt'; ctx.moveTo(75, 50); ctx.lineTo(75, 150); ctx.stroke(); ctx.beginPath(); ctx.lineWidth = 10; ctx.lineCap = 'round'; ctx.moveTo(105, 50); ctx.lineTo(105, 150); ctx.stroke(); ctx.beginPath(); ctx.lineWidth = 10; ctx.lineCap = 'square'; ctx.moveTo(135, 50); ctx.lineTo(135, 150); ctx.stroke(); ctx.beginPath(); ctx.lineWidth = 1; ctx.lineCap = 'butt'; ctx.strokeStyle = 'red'; ctx.moveTo(60, 50); ctx.lineTo(150, 50); ctx.stroke();

:

See also

  • the lineJoin property
    that sets lines join style
  • the miterLimit property
    that sets the miter limit ratio
  • the lineCap property
    that sets the type of a line end
  • the lineWidth property
    that sets the line width
enru