This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
Note: This feature is available in Web Workers.
The Path2D.addPath() method of the Canvas 2D API adds one Path2D object to another Path2D object.
addPath(path) addPath(path, transform)
pathA Path2D path to add.
transform OptionalA DOMMatrix to be used as the transformation matrix for the path that is added. (Technically an object that possesses the same properties as a DOMMatrix object.)
None (undefined).
This example adds one path to another.
<canvas id="canvas"></canvas>
First, we create two separate Path2D objects, each of which contains a rectangle made using the rect() method. We then create a matrix using DOMMatrix(). We then add the second path to the first using addPath(), also applying the matrix to move the second path to the right. Finally, we draw the first path (which now contains both rectangles) using fill().
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// Create first path and add a rectangle
let p1 = new Path2D();
p1.rect(0, 0, 100, 150);
// Create second path and add a rectangle
let p2 = new Path2D();
p2.rect(0, 0, 100, 75);
// Create transformation matrix that moves 200 points to the right
let m = new DOMMatrix();
m.a = 1;
m.b = 0;
m.c = 0;
m.d = 1;
m.e = 200;
m.f = 0;
// Add second path to the first path
p1.addPath(p2, m);
// Draw the first path
ctx.fill(p1);
| Specification |
|---|
| HTML> # dom-path2d-addpath-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
addPath |
68 | 79 | 34 | 55 | 9 | 68 | 34 | 48 | 9 | 10.0 | 68 | 9 |
Path2D
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Path2D/addPath