Skip to content

Commit df440f2

Browse files
MUSKANNISHADgithub-actions[bot]Josh-Cena
authored
Add example demonstrating creating and copying Path2D paths (#42532)
* Add Path2D example for creating and copying paths * Update files/en-us/web/api/path2d/index.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update files/en-us/web/api/path2d/index.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update files/en-us/web/api/path2d/index.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Redo --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Joshua Chen <[email protected]>
1 parent d359e01 commit df440f2

File tree

1 file changed

+8
-15
lines changed
  • files/en-us/web/api/path2d/path2d

1 file changed

+8
-15
lines changed

files/en-us/web/api/path2d/path2d/index.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ browser-compat: api.Path2D.Path2D
88

99
{{APIRef("Canvas API")}}{{AvailableInWorkers}}
1010

11-
The **`Path2D()`** constructor returns a newly instantiated
12-
`Path2D` object, optionally with another path as an argument (creates a
13-
copy), or optionally with a string consisting of [SVG path](/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths) data.
11+
The **`Path2D()`** constructor returns a newly instantiated `Path2D` object, optionally with another path as an argument (creates a copy), or optionally with a string consisting of [SVG path](/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths) data.
1412

1513
## Syntax
1614

@@ -23,17 +21,15 @@ new Path2D(d)
2321
### Parameters
2422

2523
- `path` {{optional_inline}}
26-
- : When invoked with another `Path2D` object, a copy of the
27-
`path` argument is created.
24+
- : When invoked with another `Path2D` object, a copy of the `path` argument is created.
2825
- `d` {{optional_inline}}
29-
- : When invoked with a string consisting of [SVG path](/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths) data, a new path is created
30-
from that description.
26+
- : When invoked with a string consisting of [SVG path](/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths) data, a new path is created from that description.
3127

3228
## Examples
3329

3430
### Creating and copying paths
3531

36-
This example creates and copies a `Path2D` path.
32+
This example creates and copies a `Path2D` path. First, `path1` is a rectangular path. Then, we copy `path1` into `path2` and add a circle to it. Finally, we stroke `path2`, which contains both the rectangle and the circle. Note that `path1` remains unchanged, although we never draw it to the canvas. Its only purpose is to show how you can build up a complex path by building on existing paths.
3733

3834
```html hidden
3935
<canvas id="canvas"></canvas>
@@ -43,10 +39,10 @@ This example creates and copies a `Path2D` path.
4339
const canvas = document.getElementById("canvas");
4440
const ctx = canvas.getContext("2d");
4541

46-
let path1 = new Path2D();
42+
const path1 = new Path2D();
4743
path1.rect(10, 10, 100, 100);
4844

49-
let path2 = new Path2D(path1);
45+
const path2 = new Path2D(path1);
5046
path2.moveTo(220, 60);
5147
path2.arc(170, 60, 50, 0, 2 * Math.PI);
5248

@@ -57,10 +53,7 @@ ctx.stroke(path2);
5753

5854
### Using SVG paths
5955

60-
This example creates a `Path2D` path using [SVG path data](/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths). The path will move to
61-
point (`M10 10`) and then move horizontally 80 points to the right
62-
(`h 80`), then 80 points down (`v 80`), then 80 points to the left
63-
(`h -80`), and then back to the start (`Z`).
56+
This example creates a `Path2D` path using [SVG path data](/en-US/docs/Web/SVG/Tutorials/SVG_from_scratch/Paths). The path will move to point (`M10 10`) and then move horizontally 80 points to the right (`h 80`), then 80 points down (`v 80`), then 80 points to the left (`h -80`), and then back to the start (`Z`).
6457

6558
```html hidden
6659
<canvas id="canvas"></canvas>
@@ -70,7 +63,7 @@ point (`M10 10`) and then move horizontally 80 points to the right
7063
const canvas = document.getElementById("canvas");
7164
const ctx = canvas.getContext("2d");
7265

73-
let p = new Path2D("M10 10 h 80 v 80 h -80 Z");
66+
const p = new Path2D("M10 10 h 80 v 80 h -80 Z");
7467
ctx.fill(p);
7568
```
7669

0 commit comments

Comments
 (0)