HTML-Canvas closePath()Methode
Beispiel
Zeichnen Sie einen Pfad in Form des Buchstabens L und zeichnen Sie dann eine Linie, um zum Startpunkt zurückzukehren:
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();
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20,20);
ctx.lineTo(20,100);
ctx.lineTo(70,100);
ctx.closePath();
ctx.stroke();
Selbst ausprobieren »
Browser-Unterstützung
![]()
![]()
![]()
![]()
![]()
Internet Explorer 9, Firefox, Opera, Chrome und Safari unterstützen die closePath()-Methode.
Hinweis:Internet Explorer 8 und frühere Versionen unterstützen das <canvas>-Element nicht.
Definition und Verwendung
Die Methode closePath() erstellt einen Pfad vom aktuellen Punkt zum Ausgangspunkt.
Tipp:Verwenden Sie diestroke()Methode, um den genauen Pfad auf dem Canvas zu zeichnen.
Tipp:Verwenden Sie diefill()-Methode, um das Bild zu füllen (Standard ist Schwarz). Verwenden Sie diefillStyle-Eigenschaft, um mit einer anderen Farbe/einem Farbverlauf zu füllen.
| JavaScript-Syntax: | context.closePath(); |
|---|

Weitere Beispiele
Beispiel
Verwenden Sie Rot als Füllfarbe:
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();
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();
Selbst ausprobieren »
HTML-Canvas-Referenz Andere Erweiterungen