250 of 264 menu

stroke method

The stroke method is called to complete the line drawing with lineTo, and to outline shapes drawn, for example, through the rect method.

Syntax

context.stroke()

Example

Let's draw a line:

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

:

Example

Let's draw a square:

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

:

See also

  • the fill method
    that fills a shape with a given color
  • the strokeStyle property
    that sets a line color
byenru