CanvasRenderingContext2D.beginPath()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La méthode CanvasRenderingContext2D.beginPath() de l'API Canvas 2D permet de commencer un nouveau chemin en vidant la liste des sous-chemins. Appelez cette méthode quand vous voulez créer un nouveau chemin.

Syntaxe

js
void ctx.beginPath();

Exemples

Utiliser la méthode beginPath

Ceci est un simple snippet de code qui utilise la méthode beginPath.

HTML

html
<canvas id="canvas"></canvas>

JavaScript

js
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// Premier chemin
ctx.beginPath();
ctx.strokeStyle = "blue";
ctx.moveTo(20, 20);
ctx.lineTo(200, 20);
ctx.stroke();

// Second chemin
ctx.beginPath();
ctx.strokeStyle = "green";
ctx.moveTo(20, 20);
ctx.lineTo(120, 120);
ctx.stroke();

Éditez le code ci-dessous pour voir vos changements directemment apportés au canvas:

Code jouable

Spécifications

Specification
HTML
# dom-context-2d-beginpath-dev

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
beginPath

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi