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 an exact path on the canvas.
Instance
Example 1
Start a path, move to position 0,0. Create a line to position 300,150:
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(300,150); ctx.stroke();
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 position of the path. |
y | The y coordinate of the target position of the path. |
More examples
Example 2
Draw a path, the shape is the letter L:
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();
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.