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