HTML canvas lineTo()Method

Canvas 对象参考手册Canvas Object

Example

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

YourbrowserdoesnotsupporttheHTML5canvastag.

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 »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the lineTo() method.

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


Definition and Usage

The lineTo() method adds a new point, and creates a line from that point to the last specified point on the canvas (the method does not draw the line).

Tip:Please usestroke()the method to draw the exact path on the canvas.

JavaScript Syntax: context.lineTo(x,y);

Parameter Values

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

Draw a path shaped like the letter L:

YourbrowserdoesnotsupporttheHTMLcanvastag.

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 »


Canvas 对象参考手册Canvas Object

Other Extensions