path2d-polyfill 2.0.0-beta.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,107 @@
1
+ ## [2.0.1](https://github.com/nilzona/path2d-polyfill/compare/v2.0.0...v2.0.1) (2023-01-22)
2
+
3
+ ### Bug Fixes
4
+
5
+ - add browser and main fields in package.json ([82e8522](https://github.com/nilzona/path2d-polyfill/commit/82e8522b17c750cc06dbade1e7022ffea312fd5d))
6
+
7
+ ## [2.0.0](https://github.com/nilzona/path2d-polyfill/compare/v1.2.3...v2.0.0) (2023-01-22)
8
+
9
+ ### ⚠ BREAKING CHANGES
10
+
11
+ - target build now has the name `dist/path2d-polyfill.min.js`
12
+
13
+ - chore: null Path2D in example index.html
14
+
15
+ - Update tsconfig.json
16
+
17
+ Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
18
+
19
+ - chore: build for node and browsers
20
+
21
+ - docs: update doc example
22
+
23
+ Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
24
+
25
+ ### Features
26
+
27
+ - node support with roundRect polyfill ([#43](https://github.com/nilzona/path2d-polyfill/issues/43)) ([2fffe15](https://github.com/nilzona/path2d-polyfill/commit/2fffe15a76e7adac50beacc0f9587d5b3cc71485))
28
+
29
+ ## [1.2.3](https://github.com/nilzona/path2d-polyfill/compare/v1.2.2...v1.2.3) (2022-10-11)
30
+
31
+ ## [1.2.2](https://github.com/nilzona/path2d-polyfill/compare/v1.2.1...v1.2.2) (2022-07-23)
32
+
33
+ undefined
34
+
35
+ ### [1.2.1](https://github.com/nilzona/path2d-polyfill/compare/v1.2.0...v1.2.1) (2022-03-06)## [1.2.0](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2021-10-30)
36
+
37
+ ### Bug Fixes
38
+
39
+ - fixup lint and build ([94be3cd](https://github.com/nilzona/path2d-polyfill/commit/94be3cd38a58f83efc403a58380ff3c23b4f18bf))
40
+ - sync version in package.json with tags ([e12e852](https://github.com/nilzona/path2d-polyfill/commit/e12e852ffb13034fa13efd5112c59b6b13a46013))
41
+
42
+ ### [1.1.2](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2021-05-04)
43
+
44
+ ### [1.1.1](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2020-11-16)
45
+
46
+ ### Features
47
+
48
+ - support for isPointInPath ([605f5d1](https://github.com/nilzona/path2d-polyfill/commit/605f5d188812e472575ccbaba351b9bbc58d3677))
49
+
50
+ ### Bug Fixes
51
+
52
+ - documentation readme ([5a0abaa](https://github.com/nilzona/path2d-polyfill/commit/5a0abaab6976f10c57ac83e6f6481ed425ac940e))
53
+
54
+ ### [1.0.2](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2020-08-27)
55
+
56
+ ### Bug Fixes
57
+
58
+ - remove unused css rule ([54e5174](https://github.com/nilzona/path2d-polyfill/commit/54e5174c64f6e84ef891d8830c828efc45c73b93))
59
+ - update badge in README.md ([483afe2](https://github.com/nilzona/path2d-polyfill/commit/483afe2b3440083bbb0c3dae74510959055d0f86))
60
+
61
+ ## [1.0.0](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2020-05-27)
62
+
63
+ ### [0.4.2](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2019-08-28)
64
+
65
+ ### [0.4.1](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2019-04-15)
66
+
67
+ ## [0.4.0](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2019-04-12)
68
+
69
+ ### Features
70
+
71
+ - support ellipse commands ([#7](https://github.com/nilzona/path2d-polyfill/issues/7)) ([02af6d5](https://github.com/nilzona/path2d-polyfill/commit/02af6d55365b8ef70b8eecad88b913e71ae191b2))
72
+
73
+ ### Bug Fixes
74
+
75
+ - move current position when a path is closed ([#9](https://github.com/nilzona/path2d-polyfill/issues/9)) ([ff6d2f7](https://github.com/nilzona/path2d-polyfill/commit/ff6d2f770f9895be2ac2e947697b8f76e488cb37))
76
+
77
+ ### [0.3.1](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-09-26)
78
+
79
+ ### Bug Fixes
80
+
81
+ - better checks if polyfill is needed ([bd65468](https://github.com/nilzona/path2d-polyfill/commit/bd654681662530d19a0958db22c5990424fb77f2))
82
+
83
+ ## [0.3.0](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-09-25)
84
+
85
+ ### [0.2.2](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-09-04)
86
+
87
+ ### [0.2.1](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-06-05)
88
+
89
+ ## [0.2.0](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-06-04)
90
+
91
+ ### [0.1.3](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-05-22)
92
+
93
+ ### Bug Fixes
94
+
95
+ - Avoid reference to global window ([9d3e742](https://github.com/nilzona/path2d-polyfill/commit/9d3e74241eb0652afe22d34523f10f3a3837e179))
96
+ - Remove wrong removeChild call ([123db85](https://github.com/nilzona/path2d-polyfill/commit/123db8571ff8b1b6c2fa917b513c74f8215acc37))
97
+ - Update README.md ([bcb01e1](https://github.com/nilzona/path2d-polyfill/commit/bcb01e153add2dccf95adaced13646e871086a3a))
98
+
99
+ ### [0.1.1](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-05-18)
100
+
101
+ ## [0.1.0](https://github.com/nilzona/path2d-polyfill/compare/v1.1.8...v1.2.0) (2018-05-18)
102
+
103
+ ### Features
104
+
105
+ - Added examples ([261165c](https://github.com/nilzona/path2d-polyfill/commit/261165cfb0ea3a9910624f719a34a3596f1791d0))
106
+ - First version of Path2D polyfill ([a660f19](https://github.com/nilzona/path2d-polyfill/commit/a660f194f416785a40d8d7f5ad451bb54cec15c3))
107
+ - Implement addPath ([72d4228](https://github.com/nilzona/path2d-polyfill/commit/72d4228bd6c4dda64fa957ab80e49bd7c7677d8f))
package/README.md CHANGED
@@ -9,7 +9,7 @@ Polyfills `Path2D` api and `roundRect` for CanvasRenderingContext2D
9
9
  Add this script tag to your page to enable the feature.
10
10
 
11
11
  ```html
12
- <script lang="javascript" src="https://unpkg.com/path2d-polyfill"></script>
12
+ <script lang="javascript" src="https://cdn.jsdelivr.net/npm/path2d-polyfill/dist/path2d-polyfill.min.js"></script>
13
13
  ```
14
14
 
15
15
  or install from npm
@@ -61,9 +61,9 @@ To get Path2D features with the [node-canvas library](https://github.com/Automat
61
61
  const { createCanvas, CanvasRenderingContext2D } = require("canvas");
62
62
  const { polyfillPath2D } = require("path2d-polyfill/path2d");
63
63
 
64
- let polyfilled = { CanvasRenderingContext2D, Path2D: null };
65
- polyfillPath2D(polyfilled);
66
- const Path2D = polyfilled.Path2D;
64
+ global.CanvasRenderingContext2D = CanvasRenderingContext2D;
65
+ polyfillPath2D(global);
66
+ // Path2D has now been added to global object
67
67
 
68
68
  const canvas = createCanvas(200, 200);
69
69
  const ctx = canvas.getContext("2d");
File without changes
@@ -0,0 +1,591 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+
16
+ function __spreadArray(to, from, pack) {
17
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
18
+ if (ar || !(i in from)) {
19
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
20
+ ar[i] = from[i];
21
+ }
22
+ }
23
+ return to.concat(ar || Array.prototype.slice.call(from));
24
+ }
25
+
26
+ var ARG_LENGTH = {
27
+ a: 7,
28
+ c: 6,
29
+ h: 1,
30
+ l: 2,
31
+ m: 2,
32
+ q: 4,
33
+ s: 4,
34
+ t: 2,
35
+ v: 1,
36
+ z: 0
37
+ };
38
+ var SEGMENT_PATTERN = /([astvzqmhlc])([^astvzqmhlc]*)/gi;
39
+ var NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;
40
+ function parseValues(args) {
41
+ var numbers = args.match(NUMBER);
42
+ return numbers ? numbers.map(Number) : [];
43
+ }
44
+ /**
45
+ * parse an svg path data string. Generates an Array
46
+ * of commands where each command is an Array of the
47
+ * form `[command, arg1, arg2, ...]`
48
+ *
49
+ * https://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation
50
+ * @ignore
51
+ *
52
+ * @param {string} path
53
+ * @returns {array}
54
+ */
55
+ function parsePath(path) {
56
+ var data = [];
57
+ var p = String(path).trim();
58
+ // A path data segment (if there is one) must begin with a "moveto" command
59
+ if (p[0] !== "M" && p[0] !== "m") {
60
+ return data;
61
+ }
62
+ p.replace(SEGMENT_PATTERN, function (_, command, args) {
63
+ var theArgs = parseValues(args);
64
+ var type = command.toLowerCase();
65
+ var theCommand = command;
66
+ // overloaded moveTo
67
+ if (type === "m" && theArgs.length > 2) {
68
+ data.push(__spreadArray([theCommand], theArgs.splice(0, 2), true));
69
+ type = "l";
70
+ theCommand = theCommand === "m" ? "l" : "L";
71
+ }
72
+ // Ignore invalid commands
73
+ if (theArgs.length < ARG_LENGTH[type]) {
74
+ return "";
75
+ }
76
+ data.push(__spreadArray([theCommand], theArgs.splice(0, ARG_LENGTH[type]), true));
77
+ // The command letter can be eliminated on subsequent commands if the
78
+ // same command is used multiple times in a row (e.g., you can drop the
79
+ // second "L" in "M 100 200 L 200 100 L -100 -200" and use
80
+ // "M 100 200 L 200 100 -100 -200" instead).
81
+ while (theArgs.length >= ARG_LENGTH[type] && theArgs.length && ARG_LENGTH[type]) {
82
+ data.push(__spreadArray([theCommand], theArgs.splice(0, ARG_LENGTH[type]), true));
83
+ }
84
+ return "";
85
+ });
86
+ return data;
87
+ }
88
+
89
+ function rotatePoint(point, angle) {
90
+ var nx = point.x * Math.cos(angle) - point.y * Math.sin(angle);
91
+ var ny = point.y * Math.cos(angle) + point.x * Math.sin(angle);
92
+ point.x = nx;
93
+ point.y = ny;
94
+ }
95
+ function translatePoint(point, dx, dy) {
96
+ point.x += dx;
97
+ point.y += dy;
98
+ }
99
+ function scalePoint(point, s) {
100
+ point.x *= s;
101
+ point.y *= s;
102
+ }
103
+ /**
104
+ * Implements a browser's Path2D api
105
+ */
106
+ var Path2D = /** @class */ (function () {
107
+ function Path2D(path) {
108
+ var _a;
109
+ this.commands = [];
110
+ if (path && path instanceof Path2D) {
111
+ (_a = this.commands).push.apply(_a, path.commands);
112
+ }
113
+ else if (path) {
114
+ this.commands = parsePath(path);
115
+ }
116
+ }
117
+ Path2D.prototype.addPath = function (path) {
118
+ var _a;
119
+ if (path && path instanceof Path2D) {
120
+ (_a = this.commands).push.apply(_a, path.commands);
121
+ }
122
+ };
123
+ Path2D.prototype.moveTo = function (x, y) {
124
+ this.commands.push(["M", x, y]);
125
+ };
126
+ Path2D.prototype.lineTo = function (x, y) {
127
+ this.commands.push(["L", x, y]);
128
+ };
129
+ Path2D.prototype.arc = function (x, y, r, start, end, ccw) {
130
+ this.commands.push(["AC", x, y, r, start, end, !!ccw]);
131
+ };
132
+ Path2D.prototype.arcTo = function (x1, y1, x2, y2, r) {
133
+ this.commands.push(["AT", x1, y1, x2, y2, r]);
134
+ };
135
+ Path2D.prototype.ellipse = function (x, y, rx, ry, angle, start, end, ccw) {
136
+ this.commands.push(["E", x, y, rx, ry, angle, start, end, !!ccw]);
137
+ };
138
+ Path2D.prototype.closePath = function () {
139
+ this.commands.push(["Z"]);
140
+ };
141
+ Path2D.prototype.bezierCurveTo = function (cp1x, cp1y, cp2x, cp2y, x, y) {
142
+ this.commands.push(["C", cp1x, cp1y, cp2x, cp2y, x, y]);
143
+ };
144
+ Path2D.prototype.quadraticCurveTo = function (cpx, cpy, x, y) {
145
+ this.commands.push(["Q", cpx, cpy, x, y]);
146
+ };
147
+ Path2D.prototype.rect = function (x, y, width, height) {
148
+ this.commands.push(["R", x, y, width, height]);
149
+ };
150
+ Path2D.prototype.roundRect = function (x, y, width, height, radii) {
151
+ if (typeof radii === "undefined") {
152
+ this.commands.push(["RR", x, y, width, height, 0]);
153
+ }
154
+ else {
155
+ this.commands.push(["RR", x, y, width, height, radii]);
156
+ }
157
+ };
158
+ return Path2D;
159
+ }());
160
+ function buildPath(ctx, commands) {
161
+ var x = 0;
162
+ var y = 0;
163
+ var endAngle;
164
+ var startAngle;
165
+ var largeArcFlag;
166
+ var sweepFlag;
167
+ var endPoint;
168
+ var midPoint;
169
+ var angle;
170
+ var lambda;
171
+ var t1;
172
+ var t2;
173
+ var x1;
174
+ var y1;
175
+ var r;
176
+ var rx;
177
+ var ry;
178
+ var w;
179
+ var h;
180
+ var pathType;
181
+ var centerPoint;
182
+ var ccw;
183
+ var radii;
184
+ var cpx = null;
185
+ var cpy = null;
186
+ var qcpx = null;
187
+ var qcpy = null;
188
+ var startPoint = null;
189
+ var currentPoint = null;
190
+ ctx.beginPath();
191
+ for (var i = 0; i < commands.length; ++i) {
192
+ pathType = commands[i][0];
193
+ // Reset control point if command is not cubic
194
+ if (pathType !== "S" && pathType !== "s" && pathType !== "C" && pathType !== "c") {
195
+ cpx = null;
196
+ cpy = null;
197
+ }
198
+ if (pathType !== "T" && pathType !== "t" && pathType !== "Q" && pathType !== "q") {
199
+ qcpx = null;
200
+ qcpy = null;
201
+ }
202
+ var c = void 0;
203
+ switch (pathType) {
204
+ case "m":
205
+ case "M":
206
+ c = commands[i];
207
+ if (pathType === "m") {
208
+ x += c[1];
209
+ y += c[2];
210
+ }
211
+ else {
212
+ x = c[1];
213
+ y = c[2];
214
+ }
215
+ if (pathType === "M" || !startPoint) {
216
+ startPoint = { x: x, y: y };
217
+ }
218
+ ctx.moveTo(x, y);
219
+ break;
220
+ case "l":
221
+ c = commands[i];
222
+ x += c[1];
223
+ y += c[2];
224
+ ctx.lineTo(x, y);
225
+ break;
226
+ case "L":
227
+ c = commands[i];
228
+ x = c[1];
229
+ y = c[2];
230
+ ctx.lineTo(x, y);
231
+ break;
232
+ case "H":
233
+ c = commands[i];
234
+ x = c[1];
235
+ ctx.lineTo(x, y);
236
+ break;
237
+ case "h":
238
+ c = commands[i];
239
+ x += c[1];
240
+ ctx.lineTo(x, y);
241
+ break;
242
+ case "V":
243
+ c = commands[i];
244
+ y = c[1];
245
+ ctx.lineTo(x, y);
246
+ break;
247
+ case "v":
248
+ c = commands[i];
249
+ y += c[1];
250
+ ctx.lineTo(x, y);
251
+ break;
252
+ case "a":
253
+ case "A":
254
+ c = commands[i];
255
+ if (currentPoint === null) {
256
+ throw new Error("This should never happen");
257
+ }
258
+ if (pathType === "a") {
259
+ x += c[6];
260
+ y += c[7];
261
+ }
262
+ else {
263
+ x = c[6];
264
+ y = c[7];
265
+ }
266
+ rx = c[1]; // rx
267
+ ry = c[2]; // ry
268
+ angle = (c[3] * Math.PI) / 180;
269
+ largeArcFlag = !!c[4];
270
+ sweepFlag = !!c[5];
271
+ endPoint = { x: x, y: y };
272
+ // https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
273
+ midPoint = {
274
+ x: (currentPoint.x - endPoint.x) / 2,
275
+ y: (currentPoint.y - endPoint.y) / 2
276
+ };
277
+ rotatePoint(midPoint, -angle);
278
+ // radius correction
279
+ lambda = (midPoint.x * midPoint.x) / (rx * rx) + (midPoint.y * midPoint.y) / (ry * ry);
280
+ if (lambda > 1) {
281
+ lambda = Math.sqrt(lambda);
282
+ rx *= lambda;
283
+ ry *= lambda;
284
+ }
285
+ centerPoint = {
286
+ x: (rx * midPoint.y) / ry,
287
+ y: -(ry * midPoint.x) / rx
288
+ };
289
+ t1 = rx * rx * ry * ry;
290
+ t2 = rx * rx * midPoint.y * midPoint.y + ry * ry * midPoint.x * midPoint.x;
291
+ if (sweepFlag !== largeArcFlag) {
292
+ scalePoint(centerPoint, Math.sqrt((t1 - t2) / t2) || 0);
293
+ }
294
+ else {
295
+ scalePoint(centerPoint, -Math.sqrt((t1 - t2) / t2) || 0);
296
+ }
297
+ startAngle = Math.atan2((midPoint.y - centerPoint.y) / ry, (midPoint.x - centerPoint.x) / rx);
298
+ endAngle = Math.atan2(-(midPoint.y + centerPoint.y) / ry, -(midPoint.x + centerPoint.x) / rx);
299
+ rotatePoint(centerPoint, angle);
300
+ translatePoint(centerPoint, (endPoint.x + currentPoint.x) / 2, (endPoint.y + currentPoint.y) / 2);
301
+ ctx.save();
302
+ ctx.translate(centerPoint.x, centerPoint.y);
303
+ ctx.rotate(angle);
304
+ ctx.scale(rx, ry);
305
+ ctx.arc(0, 0, 1, startAngle, endAngle, !sweepFlag);
306
+ ctx.restore();
307
+ break;
308
+ case "C":
309
+ c = commands[i];
310
+ cpx = c[3]; // Last control point
311
+ cpy = c[4];
312
+ x = c[5];
313
+ y = c[6];
314
+ ctx.bezierCurveTo(c[1], c[2], cpx, cpy, x, y);
315
+ break;
316
+ case "c":
317
+ c = commands[i];
318
+ ctx.bezierCurveTo(c[1] + x, c[2] + y, c[3] + x, c[4] + y, c[5] + x, c[6] + y);
319
+ cpx = c[3] + x; // Last control point
320
+ cpy = c[4] + y;
321
+ x += c[5];
322
+ y += c[6];
323
+ break;
324
+ case "S":
325
+ c = commands[i];
326
+ if (cpx === null || cpy === null) {
327
+ cpx = x;
328
+ cpy = y;
329
+ }
330
+ ctx.bezierCurveTo(2 * x - cpx, 2 * y - cpy, c[1], c[2], c[3], c[4]);
331
+ cpx = c[1]; // last control point
332
+ cpy = c[2];
333
+ x = c[3];
334
+ y = c[4];
335
+ break;
336
+ case "s":
337
+ c = commands[i];
338
+ if (cpx === null || cpy === null) {
339
+ cpx = x;
340
+ cpy = y;
341
+ }
342
+ ctx.bezierCurveTo(2 * x - cpx, 2 * y - cpy, c[1] + x, c[2] + y, c[3] + x, c[4] + y);
343
+ cpx = c[1] + x; // last control point
344
+ cpy = c[2] + y;
345
+ x += c[3];
346
+ y += c[4];
347
+ break;
348
+ case "Q":
349
+ c = commands[i];
350
+ qcpx = c[1]; // last control point
351
+ qcpy = c[2];
352
+ x = c[3];
353
+ y = c[4];
354
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
355
+ break;
356
+ case "q":
357
+ c = commands[i];
358
+ qcpx = c[1] + x; // last control point
359
+ qcpy = c[2] + y;
360
+ x += c[3];
361
+ y += c[4];
362
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
363
+ break;
364
+ case "T":
365
+ c = commands[i];
366
+ if (qcpx === null || qcpy === null) {
367
+ qcpx = x;
368
+ qcpy = y;
369
+ }
370
+ qcpx = 2 * x - qcpx; // last control point
371
+ qcpy = 2 * y - qcpy;
372
+ x = c[1];
373
+ y = c[2];
374
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
375
+ break;
376
+ case "t":
377
+ c = commands[i];
378
+ if (qcpx === null || qcpy === null) {
379
+ qcpx = x;
380
+ qcpy = y;
381
+ }
382
+ qcpx = 2 * x - qcpx; // last control point
383
+ qcpy = 2 * y - qcpy;
384
+ x += c[1];
385
+ y += c[2];
386
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
387
+ break;
388
+ case "z":
389
+ case "Z":
390
+ if (startPoint) {
391
+ x = startPoint.x;
392
+ y = startPoint.y;
393
+ }
394
+ startPoint = null;
395
+ ctx.closePath();
396
+ break;
397
+ case "AC": // arc
398
+ c = commands[i];
399
+ x = c[1];
400
+ y = c[2];
401
+ r = c[3];
402
+ startAngle = c[4];
403
+ endAngle = c[5];
404
+ ccw = c[6];
405
+ ctx.arc(x, y, r, startAngle, endAngle, ccw);
406
+ break;
407
+ case "AT": // arcTo
408
+ c = commands[i];
409
+ x1 = c[1];
410
+ y1 = c[2];
411
+ x = c[3];
412
+ y = c[4];
413
+ r = c[5];
414
+ ctx.arcTo(x1, y1, x, y, r);
415
+ break;
416
+ case "E": // ellipse
417
+ c = commands[i];
418
+ x = c[1];
419
+ y = c[2];
420
+ rx = c[3];
421
+ ry = c[4];
422
+ angle = c[5];
423
+ startAngle = c[6];
424
+ endAngle = c[7];
425
+ ccw = c[8];
426
+ ctx.save();
427
+ ctx.translate(x, y);
428
+ ctx.rotate(angle);
429
+ ctx.scale(rx, ry);
430
+ ctx.arc(0, 0, 1, startAngle, endAngle, ccw);
431
+ ctx.restore();
432
+ break;
433
+ case "R": // rect
434
+ c = commands[i];
435
+ x = c[1];
436
+ y = c[2];
437
+ w = c[3];
438
+ h = c[4];
439
+ startPoint = { x: x, y: y };
440
+ ctx.rect(x, y, w, h);
441
+ break;
442
+ case "RR": // roundedRect
443
+ c = commands[i];
444
+ x = c[1];
445
+ y = c[2];
446
+ w = c[3];
447
+ h = c[4];
448
+ radii = c[5];
449
+ startPoint = { x: x, y: y };
450
+ ctx.roundRect(x, y, w, h, radii);
451
+ break;
452
+ }
453
+ if (!currentPoint) {
454
+ currentPoint = { x: x, y: y };
455
+ }
456
+ else {
457
+ currentPoint.x = x;
458
+ currentPoint.y = y;
459
+ }
460
+ }
461
+ }
462
+ /**
463
+ * Polyfills CanvasRenderingContext2D stroke, fill and isPointInPath so that they support Path2D objects.
464
+ * @param {WindowLike} window - window like object containing a CanvasRenderingContext2D constructor
465
+ */
466
+ function polyfillPath2D(window) {
467
+ if (!window || !window.CanvasRenderingContext2D || window.Path2D)
468
+ return;
469
+ var CanvasRenderingContext2D = window.CanvasRenderingContext2D;
470
+ /* eslint-disable @typescript-eslint/unbound-method */
471
+ // setting unbound functions here. Make sure this is set in function call later
472
+ var cFill = CanvasRenderingContext2D.prototype.fill;
473
+ var cStroke = CanvasRenderingContext2D.prototype.stroke;
474
+ var cIsPointInPath = CanvasRenderingContext2D.prototype.isPointInPath;
475
+ /* eslint-enable @typescript-eslint/unbound-method */
476
+ CanvasRenderingContext2D.prototype.fill = function fill() {
477
+ var args = [];
478
+ for (var _i = 0; _i < arguments.length; _i++) {
479
+ args[_i] = arguments[_i];
480
+ }
481
+ if (args[0] instanceof Path2D) {
482
+ var path = args[0];
483
+ var fillRule = args[1] || "nonzero";
484
+ buildPath(this, path.commands);
485
+ cFill.apply(this, [fillRule]);
486
+ }
487
+ else {
488
+ var fillRule = args[0] || "nonzero";
489
+ return cFill.apply(this, [fillRule]);
490
+ }
491
+ };
492
+ CanvasRenderingContext2D.prototype.stroke = function stroke(path) {
493
+ if (path) {
494
+ buildPath(this, path.commands);
495
+ }
496
+ cStroke.apply(this);
497
+ };
498
+ CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath() {
499
+ var args = [];
500
+ for (var _i = 0; _i < arguments.length; _i++) {
501
+ args[_i] = arguments[_i];
502
+ }
503
+ if (args[0] instanceof Path2D) {
504
+ // first argument is a Path2D object
505
+ var path = args[0];
506
+ var x = args[1];
507
+ var y = args[2];
508
+ var fillRule = args[3] || "nonzero";
509
+ buildPath(this, path.commands);
510
+ return cIsPointInPath.apply(this, [x, y, fillRule]);
511
+ }
512
+ else {
513
+ return cIsPointInPath.apply(this, args);
514
+ }
515
+ };
516
+ window.Path2D = Path2D;
517
+ }
518
+
519
+ function roundRect(x, y, width, height, radii) {
520
+ var _this = this;
521
+ if (radii === void 0) { radii = 0; }
522
+ if (typeof radii === "number") {
523
+ // eslint-disable-next-line no-param-reassign
524
+ radii = [radii];
525
+ }
526
+ // check for range error
527
+ if (Array.isArray(radii)) {
528
+ if (radii.length === 0 || radii.length > 4) {
529
+ throw new RangeError("Failed to execute 'roundRect' on '".concat(this.constructor.name, "': ").concat(radii.length, " radii provided. Between one and four radii are necessary."));
530
+ }
531
+ radii.forEach(function (v) {
532
+ if (v < 0) {
533
+ throw new RangeError("Failed to execute 'roundRect' on '".concat(_this.constructor.name, "': Radius value ").concat(v, " is negative."));
534
+ }
535
+ });
536
+ }
537
+ else {
538
+ return;
539
+ }
540
+ if (radii.length === 1 && radii[0] === 0) {
541
+ return this.rect(x, y, width, height);
542
+ }
543
+ // set the corners
544
+ // tl = top left radius
545
+ // tr = top right radius
546
+ // br = bottom right radius
547
+ // bl = bottom left radius
548
+ var minRadius = Math.min(width, height) / 2;
549
+ var tr, br, bl;
550
+ var tl = (tr = br = bl = Math.min(minRadius, radii[0]));
551
+ if (radii.length === 2) {
552
+ tr = bl = Math.min(minRadius, radii[1]);
553
+ }
554
+ if (radii.length === 3) {
555
+ tr = bl = Math.min(minRadius, radii[1]);
556
+ br = Math.min(minRadius, radii[2]);
557
+ }
558
+ if (radii.length === 4) {
559
+ tr = Math.min(minRadius, radii[1]);
560
+ br = Math.min(minRadius, radii[2]);
561
+ bl = Math.min(minRadius, radii[3]);
562
+ }
563
+ // begin with closing current path
564
+ // this.closePath();
565
+ // let's draw the rounded rectangle
566
+ this.moveTo(x, y + height - bl);
567
+ this.arcTo(x, y, x + tl, y, tl);
568
+ this.arcTo(x + width, y, x + width, y + tr, tr);
569
+ this.arcTo(x + width, y + height, x + width - br, y + height, br);
570
+ this.arcTo(x, y + height, x, y + height - bl, bl);
571
+ // and move to rects control point for further path drawing
572
+ this.moveTo(x, y);
573
+ }
574
+ /**
575
+ * Polyfills roundRect on CanvasRenderingContext2D and Path2D
576
+ * @param {WindowLike} window - window like object containing both CanvasRenderingContext2D and Path2D constructor
577
+ */
578
+ function polyfillRoundRect(window) {
579
+ if (!window || !window.CanvasRenderingContext2D)
580
+ return;
581
+ var CanvasRenderingContext2D = window.CanvasRenderingContext2D, Path2D = window.Path2D;
582
+ // polyfill unsupported roundRect for e.g. firefox https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect#browser_compatibility
583
+ if (CanvasRenderingContext2D && !CanvasRenderingContext2D.prototype.roundRect) {
584
+ CanvasRenderingContext2D.prototype.roundRect = roundRect;
585
+ }
586
+ if (Path2D && !Path2D.prototype.roundRect) {
587
+ Path2D.prototype.roundRect = roundRect;
588
+ }
589
+ }
590
+
591
+ export { Path2D, parsePath, polyfillPath2D, polyfillRoundRect };
@@ -588,4 +588,5 @@ function polyfillRoundRect(window) {
588
588
  }
589
589
  }
590
590
 
591
- export { Path2D, parsePath, polyfillPath2D, polyfillRoundRect };
591
+ polyfillPath2D(window);
592
+ polyfillRoundRect(window);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "path2d-polyfill",
3
- "version": "2.0.0-beta.0",
3
+ "version": "2.0.1",
4
4
  "description": "Polyfills Path2D api for canvas rendering",
5
5
  "scripts": {
6
6
  "build": "rm -rf dist/* && rollup -c",
@@ -15,9 +15,17 @@
15
15
  "prepare": "husky install",
16
16
  "release": "release-it"
17
17
  },
18
+ "main": "dist/path2d-node.cjs",
18
19
  "browser": "dist/path2d-polyfill.min.js",
19
- "module": "dist/path2d-polyfill.esm.js",
20
- "main": "dist/path2d-polyfill.cjs.js",
20
+ "exports": {
21
+ "node": {
22
+ "import": "./dist/path2d-node.mjs",
23
+ "require": "./dist/path2d-node.cjs"
24
+ },
25
+ "development": "./dist/path2d-polyfill.dev.js",
26
+ "module": "./dist/path2d-polyfill.esm.js",
27
+ "default": "./dist/path2d-polyfill.min.js"
28
+ },
21
29
  "files": [
22
30
  "dist"
23
31
  ],
@@ -28,7 +36,8 @@
28
36
  "keywords": [
29
37
  "Path2D",
30
38
  "polyfill",
31
- "canvas"
39
+ "canvas",
40
+ "roundRect"
32
41
  ],
33
42
  "author": "nilzona",
34
43
  "license": "MIT",
@@ -37,8 +46,6 @@
37
46
  },
38
47
  "homepage": "https://github.com/nilzona/path2d-polyfill#readme",
39
48
  "devDependencies": {
40
- "@babel/core": "7.20.7",
41
- "@babel/preset-env": "7.20.2",
42
49
  "@commitlint/cli": "17.3.0",
43
50
  "@commitlint/config-conventional": "17.3.0",
44
51
  "@release-it/conventional-changelog": "5.1.1",