Metoda beginPath

Metoda beginPath trebuie apelată la începutul desenării unei linii folosind combinații de metode moveTo și lineTo.

Sintaxă

javascriptcontext.beginPath();

Exemplu

Să desenăm o linie:

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

Результат:

Exemplu

Să desenăm un pătrat:

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

Результат:

Vezi și

  • metoda moveTo,
    care mută stiloul pentru desenare
  • metoda lineTo,
    care desenează linii