path2d-polyfill 2.0.1 → 2.1.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.
@@ -1,592 +0,0 @@
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
- polyfillPath2D(window);
592
- polyfillRoundRect(window);