lineTo method

The lineTo method draws a line from point to point. The line is drawn from the current drawing pen position to the specified coordinates. Coordinates are specified by method parameters.

Syntax

context.lineTo(x, y);

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

:

See also

  • the moveTo method
    that moves the pen to draw
  • the closePath method
    that closes the lines into a shape
  • the lineWidth property
    that sets the line width
  • the lineJoin property
    that sets lines join style
enru