HTML canvas lineTo() method

Definition and usage

lineTo() method to add a new point and then create a line from that point to the last specified point on the canvas (this method does not create a line).

Tip:Please use stroke() The method draws the exact path on the canvas.

Instance

Example 1

Start a path, move to position 0,0. Create a line to the position 300,150:

Your browser does not support the HTML5 canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(300,150);
ctx.stroke();

Try It Yourself

Tip:More examples are provided at the bottom of the page.

Syntax

context.lineTo(x,y);

Parameter value

Parameter Description
x The x coordinate of the target location of the path.
y The y coordinate of the target location of the path.

More examples

Example 2

Draw a path, forming the letter L:

Your browser does not support the HTML5 canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20,20);
ctx.lineTo(20,100);
ctx.lineTo(70,100);
ctx.stroke();

Try It Yourself

Browser Support

The numbers in the table indicate the first browser version that fully supports this property.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.