HTML canvas closePath() method
Definition and Usage
closePath()
Method to create 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() Method to fill an image (default is black). Please use fillStyle Properties to fill another color/gradient.
Instances
Example 1
Draw a path in the form of the letter L, and then draw lines to return to the starting point:
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();
Tip:More examples are provided at the bottom of the page.
Syntax
context.closePath();
More examples
Example 2
Set green as the fill color:
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();
Selaintuki
Taulukossa olevat numerot mainitsevat ensimmäisen version, joka tukee tätä ominaisuutta täysin.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
Huomautus:Internet Explorer 8 ja aikaisemmat versiot eivät tue <canvas> -elementtiä.