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.

O método CanvasRenderingContext2D.beginPath() da API Canvas 2D inicia um novo caminho (path), esvaziando a lista de sub-caminhos (sub-paths). Use esse método quando você quiser criar um novo path.

Sintaxe

void ctx.beginPath();

Exemplos

Usando o método beginPath

Isto é só um simples trecho de código que usa o método fillRect.

HTML

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

JavaScript

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

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

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

Edite o código abaixo e veja as alterações instantâneas no canvas:

Especificações

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

Compatibilidade com navegadores

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

Veja também