This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

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 ⁨июль 2015 г.⁩.

Метод CanvasRenderingContext2D.beginPath() Canvas 2D API запускает новый путь, опуская список подпутей. Вызовите этот метод, когда хотите создать новый путь.

Синтаксис

void ctx.beginPath();

Примеры

Использование beginPath метода

Это простой фрагмент кода, использующий beginPath метод.

HTML

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

JavaScript

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

// Первый путь
ctx.beginPath();
ctx.strokeStyle = "blue";
ctx.moveTo(20, 20);
ctx.lineTo(200, 20);
ctx.stroke();

// Второй путь
ctx.beginPath();
ctx.strokeStyle = "green";
ctx.moveTo(20, 20);
ctx.lineTo(120, 120);
ctx.stroke();

Отредактируйте код ниже чтобы увидеть изменения в canvas:

Спецификации

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

Совместимость с браузерами

Смотрите также