Path2D
The Path2D
interface of the Canvas 2D API is used to declare a path that can then be used on a CanvasRenderingContext2D
object. The path methods of the CanvasRenderingContext2D
interface are also present on this interface, which gives you the convenience of being able to retain and replay your path whenever desired.
Constructors
Path2D()
-
Path2D
constructor. Creates a newPath2D
object.
Methods
Path2D.addPath()
-
Adds a path to the current path.
Path2D.closePath()
-
Causes the point of the pen to move back to the start of the current sub-path. It tries to draw a straight line from the current point to the start. If the shape has already been closed or has only one point, this function does nothing.
Path2D.moveTo()
-
Moves the starting point of a new sub-path to the (
x, y
) coordinates. Path2D.lineTo()
-
Connects the last point in the subpath to the (
x, y
) coordinates with a straight line. Path2D.bezierCurveTo()
-
Adds a cubic Bézier curve to the path. It requires three points. The first two points are control points and the third one is the end point. The starting point is the last point in the current path, which can be changed using
moveTo()
before creating the Bézier curve. Path2D.quadraticCurveTo()
-
Adds a quadratic Bézier curve to the current path.
Path2D.arc()
-
Adds an arc to the path which is centered at (
x, y
) position with radiusr
starting atstartAngle
and ending atendAngle
going in the given direction bycounterclockwise
(defaulting to clockwise). Path2D.arcTo()
-
Adds a circular arc to the path with the given control points and radius, connected to the previous point by a straight line.
Path2D.ellipse()
-
Adds an elliptical arc to the path which is centered at (
x, y
) position with the radiiradiusX
andradiusY
starting atstartAngle
and ending atendAngle
going in the given direction bycounterclockwise
(defaulting to clockwise). Path2D.rect()
-
Creates a path for a rectangle at position (
x, y
) with a size that is determined bywidth
andheight
.
Specifications
Specification |
---|
HTML Standard (HTML) # path2d-objects |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Path2D |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
Path2D |
36 |
14
Before Edge 79, the constructor for
Path2D objects does not support invocation with a string consisting of SVG path data. |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
addPath |
68 |
79 |
34 |
No |
55 |
9 |
68 |
68 |
34 |
48 |
9 |
10.0 |
arc |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
arcTo |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
bezierCurveTo |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
closePath |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
ellipse |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
lineTo |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
moveTo |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
quadraticCurveTo |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
rect |
36 |
14 |
31 |
No |
23 |
8 |
37 |
36 |
31 |
24 |
8 |
3.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Path2D