Metodo beginPath

Il metodo beginPath deve essere chiamato all'inizio del disegno di una linea utilizzando combinazioni dei metodi moveTo e lineTo.

Sintassi

javascriptcontesto.beginPath();

Esempio

Disegniamo una linea:

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();

Результат:

Esempio

Disegniamo un quadratino:

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();

Результат:

Vedi anche

  • metodo moveTo,
    che sposta il pennino per disegnare
  • metodo lineTo,
    che disegna linee