path2d-polyfill 1.2.3 → 2.0.0

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.
@@ -0,0 +1,596 @@
1
+ 'use strict';
2
+
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+
18
+ function __spreadArray(to, from, pack) {
19
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
+ if (ar || !(i in from)) {
21
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
+ ar[i] = from[i];
23
+ }
24
+ }
25
+ return to.concat(ar || Array.prototype.slice.call(from));
26
+ }
27
+
28
+ var ARG_LENGTH = {
29
+ a: 7,
30
+ c: 6,
31
+ h: 1,
32
+ l: 2,
33
+ m: 2,
34
+ q: 4,
35
+ s: 4,
36
+ t: 2,
37
+ v: 1,
38
+ z: 0
39
+ };
40
+ var SEGMENT_PATTERN = /([astvzqmhlc])([^astvzqmhlc]*)/gi;
41
+ var NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;
42
+ function parseValues(args) {
43
+ var numbers = args.match(NUMBER);
44
+ return numbers ? numbers.map(Number) : [];
45
+ }
46
+ /**
47
+ * parse an svg path data string. Generates an Array
48
+ * of commands where each command is an Array of the
49
+ * form `[command, arg1, arg2, ...]`
50
+ *
51
+ * https://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation
52
+ * @ignore
53
+ *
54
+ * @param {string} path
55
+ * @returns {array}
56
+ */
57
+ function parsePath(path) {
58
+ var data = [];
59
+ var p = String(path).trim();
60
+ // A path data segment (if there is one) must begin with a "moveto" command
61
+ if (p[0] !== "M" && p[0] !== "m") {
62
+ return data;
63
+ }
64
+ p.replace(SEGMENT_PATTERN, function (_, command, args) {
65
+ var theArgs = parseValues(args);
66
+ var type = command.toLowerCase();
67
+ var theCommand = command;
68
+ // overloaded moveTo
69
+ if (type === "m" && theArgs.length > 2) {
70
+ data.push(__spreadArray([theCommand], theArgs.splice(0, 2), true));
71
+ type = "l";
72
+ theCommand = theCommand === "m" ? "l" : "L";
73
+ }
74
+ // Ignore invalid commands
75
+ if (theArgs.length < ARG_LENGTH[type]) {
76
+ return "";
77
+ }
78
+ data.push(__spreadArray([theCommand], theArgs.splice(0, ARG_LENGTH[type]), true));
79
+ // The command letter can be eliminated on subsequent commands if the
80
+ // same command is used multiple times in a row (e.g., you can drop the
81
+ // second "L" in "M 100 200 L 200 100 L -100 -200" and use
82
+ // "M 100 200 L 200 100 -100 -200" instead).
83
+ while (theArgs.length >= ARG_LENGTH[type] && theArgs.length && ARG_LENGTH[type]) {
84
+ data.push(__spreadArray([theCommand], theArgs.splice(0, ARG_LENGTH[type]), true));
85
+ }
86
+ return "";
87
+ });
88
+ return data;
89
+ }
90
+
91
+ function rotatePoint(point, angle) {
92
+ var nx = point.x * Math.cos(angle) - point.y * Math.sin(angle);
93
+ var ny = point.y * Math.cos(angle) + point.x * Math.sin(angle);
94
+ point.x = nx;
95
+ point.y = ny;
96
+ }
97
+ function translatePoint(point, dx, dy) {
98
+ point.x += dx;
99
+ point.y += dy;
100
+ }
101
+ function scalePoint(point, s) {
102
+ point.x *= s;
103
+ point.y *= s;
104
+ }
105
+ /**
106
+ * Implements a browser's Path2D api
107
+ */
108
+ var Path2D = /** @class */ (function () {
109
+ function Path2D(path) {
110
+ var _a;
111
+ this.commands = [];
112
+ if (path && path instanceof Path2D) {
113
+ (_a = this.commands).push.apply(_a, path.commands);
114
+ }
115
+ else if (path) {
116
+ this.commands = parsePath(path);
117
+ }
118
+ }
119
+ Path2D.prototype.addPath = function (path) {
120
+ var _a;
121
+ if (path && path instanceof Path2D) {
122
+ (_a = this.commands).push.apply(_a, path.commands);
123
+ }
124
+ };
125
+ Path2D.prototype.moveTo = function (x, y) {
126
+ this.commands.push(["M", x, y]);
127
+ };
128
+ Path2D.prototype.lineTo = function (x, y) {
129
+ this.commands.push(["L", x, y]);
130
+ };
131
+ Path2D.prototype.arc = function (x, y, r, start, end, ccw) {
132
+ this.commands.push(["AC", x, y, r, start, end, !!ccw]);
133
+ };
134
+ Path2D.prototype.arcTo = function (x1, y1, x2, y2, r) {
135
+ this.commands.push(["AT", x1, y1, x2, y2, r]);
136
+ };
137
+ Path2D.prototype.ellipse = function (x, y, rx, ry, angle, start, end, ccw) {
138
+ this.commands.push(["E", x, y, rx, ry, angle, start, end, !!ccw]);
139
+ };
140
+ Path2D.prototype.closePath = function () {
141
+ this.commands.push(["Z"]);
142
+ };
143
+ Path2D.prototype.bezierCurveTo = function (cp1x, cp1y, cp2x, cp2y, x, y) {
144
+ this.commands.push(["C", cp1x, cp1y, cp2x, cp2y, x, y]);
145
+ };
146
+ Path2D.prototype.quadraticCurveTo = function (cpx, cpy, x, y) {
147
+ this.commands.push(["Q", cpx, cpy, x, y]);
148
+ };
149
+ Path2D.prototype.rect = function (x, y, width, height) {
150
+ this.commands.push(["R", x, y, width, height]);
151
+ };
152
+ Path2D.prototype.roundRect = function (x, y, width, height, radii) {
153
+ if (typeof radii === "undefined") {
154
+ this.commands.push(["RR", x, y, width, height, 0]);
155
+ }
156
+ else {
157
+ this.commands.push(["RR", x, y, width, height, radii]);
158
+ }
159
+ };
160
+ return Path2D;
161
+ }());
162
+ function buildPath(ctx, commands) {
163
+ var x = 0;
164
+ var y = 0;
165
+ var endAngle;
166
+ var startAngle;
167
+ var largeArcFlag;
168
+ var sweepFlag;
169
+ var endPoint;
170
+ var midPoint;
171
+ var angle;
172
+ var lambda;
173
+ var t1;
174
+ var t2;
175
+ var x1;
176
+ var y1;
177
+ var r;
178
+ var rx;
179
+ var ry;
180
+ var w;
181
+ var h;
182
+ var pathType;
183
+ var centerPoint;
184
+ var ccw;
185
+ var radii;
186
+ var cpx = null;
187
+ var cpy = null;
188
+ var qcpx = null;
189
+ var qcpy = null;
190
+ var startPoint = null;
191
+ var currentPoint = null;
192
+ ctx.beginPath();
193
+ for (var i = 0; i < commands.length; ++i) {
194
+ pathType = commands[i][0];
195
+ // Reset control point if command is not cubic
196
+ if (pathType !== "S" && pathType !== "s" && pathType !== "C" && pathType !== "c") {
197
+ cpx = null;
198
+ cpy = null;
199
+ }
200
+ if (pathType !== "T" && pathType !== "t" && pathType !== "Q" && pathType !== "q") {
201
+ qcpx = null;
202
+ qcpy = null;
203
+ }
204
+ var c = void 0;
205
+ switch (pathType) {
206
+ case "m":
207
+ case "M":
208
+ c = commands[i];
209
+ if (pathType === "m") {
210
+ x += c[1];
211
+ y += c[2];
212
+ }
213
+ else {
214
+ x = c[1];
215
+ y = c[2];
216
+ }
217
+ if (pathType === "M" || !startPoint) {
218
+ startPoint = { x: x, y: y };
219
+ }
220
+ ctx.moveTo(x, y);
221
+ break;
222
+ case "l":
223
+ c = commands[i];
224
+ x += c[1];
225
+ y += c[2];
226
+ ctx.lineTo(x, y);
227
+ break;
228
+ case "L":
229
+ c = commands[i];
230
+ x = c[1];
231
+ y = c[2];
232
+ ctx.lineTo(x, y);
233
+ break;
234
+ case "H":
235
+ c = commands[i];
236
+ x = c[1];
237
+ ctx.lineTo(x, y);
238
+ break;
239
+ case "h":
240
+ c = commands[i];
241
+ x += c[1];
242
+ ctx.lineTo(x, y);
243
+ break;
244
+ case "V":
245
+ c = commands[i];
246
+ y = c[1];
247
+ ctx.lineTo(x, y);
248
+ break;
249
+ case "v":
250
+ c = commands[i];
251
+ y += c[1];
252
+ ctx.lineTo(x, y);
253
+ break;
254
+ case "a":
255
+ case "A":
256
+ c = commands[i];
257
+ if (currentPoint === null) {
258
+ throw new Error("This should never happen");
259
+ }
260
+ if (pathType === "a") {
261
+ x += c[6];
262
+ y += c[7];
263
+ }
264
+ else {
265
+ x = c[6];
266
+ y = c[7];
267
+ }
268
+ rx = c[1]; // rx
269
+ ry = c[2]; // ry
270
+ angle = (c[3] * Math.PI) / 180;
271
+ largeArcFlag = !!c[4];
272
+ sweepFlag = !!c[5];
273
+ endPoint = { x: x, y: y };
274
+ // https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
275
+ midPoint = {
276
+ x: (currentPoint.x - endPoint.x) / 2,
277
+ y: (currentPoint.y - endPoint.y) / 2
278
+ };
279
+ rotatePoint(midPoint, -angle);
280
+ // radius correction
281
+ lambda = (midPoint.x * midPoint.x) / (rx * rx) + (midPoint.y * midPoint.y) / (ry * ry);
282
+ if (lambda > 1) {
283
+ lambda = Math.sqrt(lambda);
284
+ rx *= lambda;
285
+ ry *= lambda;
286
+ }
287
+ centerPoint = {
288
+ x: (rx * midPoint.y) / ry,
289
+ y: -(ry * midPoint.x) / rx
290
+ };
291
+ t1 = rx * rx * ry * ry;
292
+ t2 = rx * rx * midPoint.y * midPoint.y + ry * ry * midPoint.x * midPoint.x;
293
+ if (sweepFlag !== largeArcFlag) {
294
+ scalePoint(centerPoint, Math.sqrt((t1 - t2) / t2) || 0);
295
+ }
296
+ else {
297
+ scalePoint(centerPoint, -Math.sqrt((t1 - t2) / t2) || 0);
298
+ }
299
+ startAngle = Math.atan2((midPoint.y - centerPoint.y) / ry, (midPoint.x - centerPoint.x) / rx);
300
+ endAngle = Math.atan2(-(midPoint.y + centerPoint.y) / ry, -(midPoint.x + centerPoint.x) / rx);
301
+ rotatePoint(centerPoint, angle);
302
+ translatePoint(centerPoint, (endPoint.x + currentPoint.x) / 2, (endPoint.y + currentPoint.y) / 2);
303
+ ctx.save();
304
+ ctx.translate(centerPoint.x, centerPoint.y);
305
+ ctx.rotate(angle);
306
+ ctx.scale(rx, ry);
307
+ ctx.arc(0, 0, 1, startAngle, endAngle, !sweepFlag);
308
+ ctx.restore();
309
+ break;
310
+ case "C":
311
+ c = commands[i];
312
+ cpx = c[3]; // Last control point
313
+ cpy = c[4];
314
+ x = c[5];
315
+ y = c[6];
316
+ ctx.bezierCurveTo(c[1], c[2], cpx, cpy, x, y);
317
+ break;
318
+ case "c":
319
+ c = commands[i];
320
+ ctx.bezierCurveTo(c[1] + x, c[2] + y, c[3] + x, c[4] + y, c[5] + x, c[6] + y);
321
+ cpx = c[3] + x; // Last control point
322
+ cpy = c[4] + y;
323
+ x += c[5];
324
+ y += c[6];
325
+ break;
326
+ case "S":
327
+ c = commands[i];
328
+ if (cpx === null || cpy === null) {
329
+ cpx = x;
330
+ cpy = y;
331
+ }
332
+ ctx.bezierCurveTo(2 * x - cpx, 2 * y - cpy, c[1], c[2], c[3], c[4]);
333
+ cpx = c[1]; // last control point
334
+ cpy = c[2];
335
+ x = c[3];
336
+ y = c[4];
337
+ break;
338
+ case "s":
339
+ c = commands[i];
340
+ if (cpx === null || cpy === null) {
341
+ cpx = x;
342
+ cpy = y;
343
+ }
344
+ ctx.bezierCurveTo(2 * x - cpx, 2 * y - cpy, c[1] + x, c[2] + y, c[3] + x, c[4] + y);
345
+ cpx = c[1] + x; // last control point
346
+ cpy = c[2] + y;
347
+ x += c[3];
348
+ y += c[4];
349
+ break;
350
+ case "Q":
351
+ c = commands[i];
352
+ qcpx = c[1]; // last control point
353
+ qcpy = c[2];
354
+ x = c[3];
355
+ y = c[4];
356
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
357
+ break;
358
+ case "q":
359
+ c = commands[i];
360
+ qcpx = c[1] + x; // last control point
361
+ qcpy = c[2] + y;
362
+ x += c[3];
363
+ y += c[4];
364
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
365
+ break;
366
+ case "T":
367
+ c = commands[i];
368
+ if (qcpx === null || qcpy === null) {
369
+ qcpx = x;
370
+ qcpy = y;
371
+ }
372
+ qcpx = 2 * x - qcpx; // last control point
373
+ qcpy = 2 * y - qcpy;
374
+ x = c[1];
375
+ y = c[2];
376
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
377
+ break;
378
+ case "t":
379
+ c = commands[i];
380
+ if (qcpx === null || qcpy === null) {
381
+ qcpx = x;
382
+ qcpy = y;
383
+ }
384
+ qcpx = 2 * x - qcpx; // last control point
385
+ qcpy = 2 * y - qcpy;
386
+ x += c[1];
387
+ y += c[2];
388
+ ctx.quadraticCurveTo(qcpx, qcpy, x, y);
389
+ break;
390
+ case "z":
391
+ case "Z":
392
+ if (startPoint) {
393
+ x = startPoint.x;
394
+ y = startPoint.y;
395
+ }
396
+ startPoint = null;
397
+ ctx.closePath();
398
+ break;
399
+ case "AC": // arc
400
+ c = commands[i];
401
+ x = c[1];
402
+ y = c[2];
403
+ r = c[3];
404
+ startAngle = c[4];
405
+ endAngle = c[5];
406
+ ccw = c[6];
407
+ ctx.arc(x, y, r, startAngle, endAngle, ccw);
408
+ break;
409
+ case "AT": // arcTo
410
+ c = commands[i];
411
+ x1 = c[1];
412
+ y1 = c[2];
413
+ x = c[3];
414
+ y = c[4];
415
+ r = c[5];
416
+ ctx.arcTo(x1, y1, x, y, r);
417
+ break;
418
+ case "E": // ellipse
419
+ c = commands[i];
420
+ x = c[1];
421
+ y = c[2];
422
+ rx = c[3];
423
+ ry = c[4];
424
+ angle = c[5];
425
+ startAngle = c[6];
426
+ endAngle = c[7];
427
+ ccw = c[8];
428
+ ctx.save();
429
+ ctx.translate(x, y);
430
+ ctx.rotate(angle);
431
+ ctx.scale(rx, ry);
432
+ ctx.arc(0, 0, 1, startAngle, endAngle, ccw);
433
+ ctx.restore();
434
+ break;
435
+ case "R": // rect
436
+ c = commands[i];
437
+ x = c[1];
438
+ y = c[2];
439
+ w = c[3];
440
+ h = c[4];
441
+ startPoint = { x: x, y: y };
442
+ ctx.rect(x, y, w, h);
443
+ break;
444
+ case "RR": // roundedRect
445
+ c = commands[i];
446
+ x = c[1];
447
+ y = c[2];
448
+ w = c[3];
449
+ h = c[4];
450
+ radii = c[5];
451
+ startPoint = { x: x, y: y };
452
+ ctx.roundRect(x, y, w, h, radii);
453
+ break;
454
+ }
455
+ if (!currentPoint) {
456
+ currentPoint = { x: x, y: y };
457
+ }
458
+ else {
459
+ currentPoint.x = x;
460
+ currentPoint.y = y;
461
+ }
462
+ }
463
+ }
464
+ /**
465
+ * Polyfills CanvasRenderingContext2D stroke, fill and isPointInPath so that they support Path2D objects.
466
+ * @param {WindowLike} window - window like object containing a CanvasRenderingContext2D constructor
467
+ */
468
+ function polyfillPath2D(window) {
469
+ if (!window || !window.CanvasRenderingContext2D || window.Path2D)
470
+ return;
471
+ var CanvasRenderingContext2D = window.CanvasRenderingContext2D;
472
+ /* eslint-disable @typescript-eslint/unbound-method */
473
+ // setting unbound functions here. Make sure this is set in function call later
474
+ var cFill = CanvasRenderingContext2D.prototype.fill;
475
+ var cStroke = CanvasRenderingContext2D.prototype.stroke;
476
+ var cIsPointInPath = CanvasRenderingContext2D.prototype.isPointInPath;
477
+ /* eslint-enable @typescript-eslint/unbound-method */
478
+ CanvasRenderingContext2D.prototype.fill = function fill() {
479
+ var args = [];
480
+ for (var _i = 0; _i < arguments.length; _i++) {
481
+ args[_i] = arguments[_i];
482
+ }
483
+ if (args[0] instanceof Path2D) {
484
+ var path = args[0];
485
+ var fillRule = args[1] || "nonzero";
486
+ buildPath(this, path.commands);
487
+ cFill.apply(this, [fillRule]);
488
+ }
489
+ else {
490
+ var fillRule = args[0] || "nonzero";
491
+ return cFill.apply(this, [fillRule]);
492
+ }
493
+ };
494
+ CanvasRenderingContext2D.prototype.stroke = function stroke(path) {
495
+ if (path) {
496
+ buildPath(this, path.commands);
497
+ }
498
+ cStroke.apply(this);
499
+ };
500
+ CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath() {
501
+ var args = [];
502
+ for (var _i = 0; _i < arguments.length; _i++) {
503
+ args[_i] = arguments[_i];
504
+ }
505
+ if (args[0] instanceof Path2D) {
506
+ // first argument is a Path2D object
507
+ var path = args[0];
508
+ var x = args[1];
509
+ var y = args[2];
510
+ var fillRule = args[3] || "nonzero";
511
+ buildPath(this, path.commands);
512
+ return cIsPointInPath.apply(this, [x, y, fillRule]);
513
+ }
514
+ else {
515
+ return cIsPointInPath.apply(this, args);
516
+ }
517
+ };
518
+ window.Path2D = Path2D;
519
+ }
520
+
521
+ function roundRect(x, y, width, height, radii) {
522
+ var _this = this;
523
+ if (radii === void 0) { radii = 0; }
524
+ if (typeof radii === "number") {
525
+ // eslint-disable-next-line no-param-reassign
526
+ radii = [radii];
527
+ }
528
+ // check for range error
529
+ if (Array.isArray(radii)) {
530
+ if (radii.length === 0 || radii.length > 4) {
531
+ throw new RangeError("Failed to execute 'roundRect' on '".concat(this.constructor.name, "': ").concat(radii.length, " radii provided. Between one and four radii are necessary."));
532
+ }
533
+ radii.forEach(function (v) {
534
+ if (v < 0) {
535
+ throw new RangeError("Failed to execute 'roundRect' on '".concat(_this.constructor.name, "': Radius value ").concat(v, " is negative."));
536
+ }
537
+ });
538
+ }
539
+ else {
540
+ return;
541
+ }
542
+ if (radii.length === 1 && radii[0] === 0) {
543
+ return this.rect(x, y, width, height);
544
+ }
545
+ // set the corners
546
+ // tl = top left radius
547
+ // tr = top right radius
548
+ // br = bottom right radius
549
+ // bl = bottom left radius
550
+ var minRadius = Math.min(width, height) / 2;
551
+ var tr, br, bl;
552
+ var tl = (tr = br = bl = Math.min(minRadius, radii[0]));
553
+ if (radii.length === 2) {
554
+ tr = bl = Math.min(minRadius, radii[1]);
555
+ }
556
+ if (radii.length === 3) {
557
+ tr = bl = Math.min(minRadius, radii[1]);
558
+ br = Math.min(minRadius, radii[2]);
559
+ }
560
+ if (radii.length === 4) {
561
+ tr = Math.min(minRadius, radii[1]);
562
+ br = Math.min(minRadius, radii[2]);
563
+ bl = Math.min(minRadius, radii[3]);
564
+ }
565
+ // begin with closing current path
566
+ // this.closePath();
567
+ // let's draw the rounded rectangle
568
+ this.moveTo(x, y + height - bl);
569
+ this.arcTo(x, y, x + tl, y, tl);
570
+ this.arcTo(x + width, y, x + width, y + tr, tr);
571
+ this.arcTo(x + width, y + height, x + width - br, y + height, br);
572
+ this.arcTo(x, y + height, x, y + height - bl, bl);
573
+ // and move to rects control point for further path drawing
574
+ this.moveTo(x, y);
575
+ }
576
+ /**
577
+ * Polyfills roundRect on CanvasRenderingContext2D and Path2D
578
+ * @param {WindowLike} window - window like object containing both CanvasRenderingContext2D and Path2D constructor
579
+ */
580
+ function polyfillRoundRect(window) {
581
+ if (!window || !window.CanvasRenderingContext2D)
582
+ return;
583
+ var CanvasRenderingContext2D = window.CanvasRenderingContext2D, Path2D = window.Path2D;
584
+ // polyfill unsupported roundRect for e.g. firefox https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect#browser_compatibility
585
+ if (CanvasRenderingContext2D && !CanvasRenderingContext2D.prototype.roundRect) {
586
+ CanvasRenderingContext2D.prototype.roundRect = roundRect;
587
+ }
588
+ if (Path2D && !Path2D.prototype.roundRect) {
589
+ Path2D.prototype.roundRect = roundRect;
590
+ }
591
+ }
592
+
593
+ exports.Path2D = Path2D;
594
+ exports.parsePath = parsePath;
595
+ exports.polyfillPath2D = polyfillPath2D;
596
+ exports.polyfillRoundRect = polyfillRoundRect;