Latest web development tutorials

HTML canvas closePath () method

HTML canvas Reference Manual HTML canvas Reference Manual

Examples

Draw a path, in the form of the letter L, and then draw a line to return to the starting point:

YourbrowserdoesnotsupporttheHTML5canvastag.

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.closePath();
ctx.stroke();

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

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


Definition and Usage

closePath () method to create a path from the current point to the starting point.

Tip: Use the stroke () method to draw the exact path on the canvas.

Tip: Use the fill () method to fill the image (default is black). Use fillStyle property to populate another color / gradient.

JavaScript syntax: context .closePath ();


Examples

More examples

Examples

As the fill color to red:

Yourbrowserdoesnotsupportthecanvastag.

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.closePath();
ctx.stroke();
ctx.fillStyle="red";
ctx.fill();

try it"


HTML canvas Reference Manual HTML canvas Reference Manual