HTML canvas closePath() method

Definition and Usage

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

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

Tip:Please use fill() Use a method to fill an image (the default is black). Please use fillStyle Use properties to fill another color/gradient.

Instances

Example 1

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

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

Try It Yourself

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

Syntax

context.closePath();

More examples

Example 2

Set green as the fill color:

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

Try It Yourself

Browser Support

The numbers in the table indicate the first browser version to fully support 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.