Méthode beginPath

La méthode beginPath doit être appelée au début du dessin d'une ligne en utilisant des combinaisons des méthodes moveTo et lineTo.

Syntaxe

javascriptcontexte.beginPath();

Exemple

Dessinons une ligne :

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptlet canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');

ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.stroke();

Результат:

Exemple

Dessinons un carré :

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptlet canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');

ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.lineTo(150, 150);
ctx.lineTo(50, 150);
ctx.closePath();
ctx.stroke();

Результат:

Voir aussi

  • la méthode moveTo,
    qui déplace le stylo pour dessiner
  • la méthode lineTo,
    qui dessine des lignes