HTML canvas lineTo() Method
Example
Begin a path, move to position 0, 0. Create a line to position 300, 150:
JavaScript:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(300, 150);
ctx.stroke();
Try it Yourself »
Description
The lineTo() method adds a new point and creates a line from that point to the last specified point in the canvas (this method does not draw the line).
Tip: Use the stroke() method to actually draw the path on the canvas.
JavaScript syntax: | context.lineTo(x, y); |
---|
Parameter Values
Parameter | Description | Play it |
---|---|---|
x | The x-coordinate of where to create the line to | Play it » |
y | The y-coordinate of where to create the line to | Play it » |
More Examples
Example
Draw a path, shaped as the letter L:
JavaScript:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.stroke();
Try it Yourself »
Browser Support
The <canvas>
element is an HTML5 standard (2014).
lineTo()
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
❮ Canvas API