path2d-polyfill 1.1.5 → 1.2.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.
@@ -1,14 +1,14 @@
1
- const parsePath = require('./parse-path');
1
+ const parsePath = require("./parse-path");
2
2
 
3
3
  /**
4
4
  * Work around for https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8438884/
5
5
  * @ignore
6
6
  */
7
7
  function supportsSvgPathArgument(window) {
8
- const canvas = window.document.createElement('canvas');
9
- const g = canvas.getContext('2d');
10
- const p = new window.Path2D('M0 0 L1 1');
11
- g.strokeStyle = 'red';
8
+ const canvas = window.document.createElement("canvas");
9
+ const g = canvas.getContext("2d");
10
+ const p = new window.Path2D("M0 0 L1 1");
11
+ g.strokeStyle = "red";
12
12
  g.lineWidth = 1;
13
13
  g.stroke(p);
14
14
  const imgData = g.getImageData(0, 0, 1, 1);
@@ -33,7 +33,7 @@ function scalePoint(point, s) {
33
33
  }
34
34
 
35
35
  function polyFillPath2D(window) {
36
- if (typeof window === 'undefined' || !window.CanvasRenderingContext2D) {
36
+ if (typeof window === "undefined" || !window.CanvasRenderingContext2D) {
37
37
  return;
38
38
  }
39
39
  if (window.Path2D && supportsSvgPathArgument(window)) {
@@ -63,39 +63,39 @@ function polyFillPath2D(window) {
63
63
  }
64
64
 
65
65
  moveTo(x, y) {
66
- this.segments.push(['M', x, y]);
66
+ this.segments.push(["M", x, y]);
67
67
  }
68
68
 
69
69
  lineTo(x, y) {
70
- this.segments.push(['L', x, y]);
70
+ this.segments.push(["L", x, y]);
71
71
  }
72
72
 
73
73
  arc(x, y, r, start, end, ccw) {
74
- this.segments.push(['AC', x, y, r, start, end, !!ccw]);
74
+ this.segments.push(["AC", x, y, r, start, end, !!ccw]);
75
75
  }
76
76
 
77
77
  arcTo(x1, y1, x2, y2, r) {
78
- this.segments.push(['AT', x1, y1, x2, y2, r]);
78
+ this.segments.push(["AT", x1, y1, x2, y2, r]);
79
79
  }
80
80
 
81
81
  ellipse(x, y, rx, ry, angle, start, end, ccw) {
82
- this.segments.push(['E', x, y, rx, ry, angle, start, end, !!ccw]);
82
+ this.segments.push(["E", x, y, rx, ry, angle, start, end, !!ccw]);
83
83
  }
84
84
 
85
85
  closePath() {
86
- this.segments.push(['Z']);
86
+ this.segments.push(["Z"]);
87
87
  }
88
88
 
89
89
  bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
90
- this.segments.push(['C', cp1x, cp1y, cp2x, cp2y, x, y]);
90
+ this.segments.push(["C", cp1x, cp1y, cp2x, cp2y, x, y]);
91
91
  }
92
92
 
93
93
  quadraticCurveTo(cpx, cpy, x, y) {
94
- this.segments.push(['Q', cpx, cpy, x, y]);
94
+ this.segments.push(["Q", cpx, cpy, x, y]);
95
95
  }
96
96
 
97
97
  rect(x, y, width, height) {
98
- this.segments.push(['R', x, y, width, height]);
98
+ this.segments.push(["R", x, y, width, height]);
99
99
  }
100
100
  }
101
101
 
@@ -135,30 +135,20 @@ function polyFillPath2D(window) {
135
135
  pathType = s[0];
136
136
 
137
137
  // Reset control point if command is not cubic
138
- if (
139
- pathType !== 'S' &&
140
- pathType !== 's' &&
141
- pathType !== 'C' &&
142
- pathType !== 'c'
143
- ) {
138
+ if (pathType !== "S" && pathType !== "s" && pathType !== "C" && pathType !== "c") {
144
139
  cpx = null;
145
140
  cpy = null;
146
141
  }
147
142
 
148
- if (
149
- pathType !== 'T' &&
150
- pathType !== 't' &&
151
- pathType !== 'Q' &&
152
- pathType !== 'q'
153
- ) {
143
+ if (pathType !== "T" && pathType !== "t" && pathType !== "Q" && pathType !== "q") {
154
144
  qcpx = null;
155
145
  qcpy = null;
156
146
  }
157
147
 
158
148
  switch (pathType) {
159
- case 'm':
160
- case 'M':
161
- if (pathType === 'm') {
149
+ case "m":
150
+ case "M":
151
+ if (pathType === "m") {
162
152
  x += s[1];
163
153
  y += s[2];
164
154
  } else {
@@ -166,41 +156,41 @@ function polyFillPath2D(window) {
166
156
  y = s[2];
167
157
  }
168
158
 
169
- if (pathType === 'M' || !startPoint) {
159
+ if (pathType === "M" || !startPoint) {
170
160
  startPoint = { x, y };
171
161
  }
172
162
 
173
163
  canvas.moveTo(x, y);
174
164
  break;
175
- case 'l':
165
+ case "l":
176
166
  x += s[1];
177
167
  y += s[2];
178
168
  canvas.lineTo(x, y);
179
169
  break;
180
- case 'L':
170
+ case "L":
181
171
  x = s[1];
182
172
  y = s[2];
183
173
  canvas.lineTo(x, y);
184
174
  break;
185
- case 'H':
175
+ case "H":
186
176
  x = s[1];
187
177
  canvas.lineTo(x, y);
188
178
  break;
189
- case 'h':
179
+ case "h":
190
180
  x += s[1];
191
181
  canvas.lineTo(x, y);
192
182
  break;
193
- case 'V':
183
+ case "V":
194
184
  y = s[1];
195
185
  canvas.lineTo(x, y);
196
186
  break;
197
- case 'v':
187
+ case "v":
198
188
  y += s[1];
199
189
  canvas.lineTo(x, y);
200
190
  break;
201
- case 'a':
202
- case 'A':
203
- if (pathType === 'a') {
191
+ case "a":
192
+ case "A":
193
+ if (pathType === "a") {
204
194
  x += s[6];
205
195
  y += s[7];
206
196
  } else {
@@ -224,9 +214,7 @@ function polyFillPath2D(window) {
224
214
  rotatePoint(midPoint, -angle);
225
215
 
226
216
  // radius correction
227
- lambda =
228
- (midPoint.x * midPoint.x) / (rx * rx) +
229
- (midPoint.y * midPoint.y) / (ry * ry);
217
+ lambda = (midPoint.x * midPoint.x) / (rx * rx) + (midPoint.y * midPoint.y) / (ry * ry);
230
218
  if (lambda > 1) {
231
219
  lambda = Math.sqrt(lambda);
232
220
  rx *= lambda;
@@ -238,30 +226,18 @@ function polyFillPath2D(window) {
238
226
  y: -(ry * midPoint.x) / rx,
239
227
  };
240
228
  t1 = rx * rx * ry * ry;
241
- t2 =
242
- rx * rx * midPoint.y * midPoint.y +
243
- ry * ry * midPoint.x * midPoint.x;
229
+ t2 = rx * rx * midPoint.y * midPoint.y + ry * ry * midPoint.x * midPoint.x;
244
230
  if (sweepFlag !== largeArcFlag) {
245
231
  scalePoint(centerPoint, Math.sqrt((t1 - t2) / t2) || 0);
246
232
  } else {
247
233
  scalePoint(centerPoint, -Math.sqrt((t1 - t2) / t2) || 0);
248
234
  }
249
235
 
250
- startAngle = Math.atan2(
251
- (midPoint.y - centerPoint.y) / ry,
252
- (midPoint.x - centerPoint.x) / rx
253
- );
254
- endAngle = Math.atan2(
255
- -(midPoint.y + centerPoint.y) / ry,
256
- -(midPoint.x + centerPoint.x) / rx
257
- );
236
+ startAngle = Math.atan2((midPoint.y - centerPoint.y) / ry, (midPoint.x - centerPoint.x) / rx);
237
+ endAngle = Math.atan2(-(midPoint.y + centerPoint.y) / ry, -(midPoint.x + centerPoint.x) / rx);
258
238
 
259
239
  rotatePoint(centerPoint, angle);
260
- translatePoint(
261
- centerPoint,
262
- (endPoint.x + currentPoint.x) / 2,
263
- (endPoint.y + currentPoint.y) / 2
264
- );
240
+ translatePoint(centerPoint, (endPoint.x + currentPoint.x) / 2, (endPoint.y + currentPoint.y) / 2);
265
241
 
266
242
  canvas.save();
267
243
  canvas.translate(centerPoint.x, centerPoint.y);
@@ -270,80 +246,59 @@ function polyFillPath2D(window) {
270
246
  canvas.arc(0, 0, 1, startAngle, endAngle, !sweepFlag);
271
247
  canvas.restore();
272
248
  break;
273
- case 'C':
249
+ case "C":
274
250
  cpx = s[3]; // Last control point
275
251
  cpy = s[4];
276
252
  x = s[5];
277
253
  y = s[6];
278
254
  canvas.bezierCurveTo(s[1], s[2], cpx, cpy, x, y);
279
255
  break;
280
- case 'c':
281
- canvas.bezierCurveTo(
282
- s[1] + x,
283
- s[2] + y,
284
- s[3] + x,
285
- s[4] + y,
286
- s[5] + x,
287
- s[6] + y
288
- );
256
+ case "c":
257
+ canvas.bezierCurveTo(s[1] + x, s[2] + y, s[3] + x, s[4] + y, s[5] + x, s[6] + y);
289
258
  cpx = s[3] + x; // Last control point
290
259
  cpy = s[4] + y;
291
260
  x += s[5];
292
261
  y += s[6];
293
262
  break;
294
- case 'S':
263
+ case "S":
295
264
  if (cpx === null || cpy === null) {
296
265
  cpx = x;
297
266
  cpy = y;
298
267
  }
299
268
 
300
- canvas.bezierCurveTo(
301
- 2 * x - cpx,
302
- 2 * y - cpy,
303
- s[1],
304
- s[2],
305
- s[3],
306
- s[4]
307
- );
269
+ canvas.bezierCurveTo(2 * x - cpx, 2 * y - cpy, s[1], s[2], s[3], s[4]);
308
270
  cpx = s[1]; // last control point
309
271
  cpy = s[2];
310
272
  x = s[3];
311
273
  y = s[4];
312
274
  break;
313
- case 's':
275
+ case "s":
314
276
  if (cpx === null || cpy === null) {
315
277
  cpx = x;
316
278
  cpy = y;
317
279
  }
318
280
 
319
- canvas.bezierCurveTo(
320
- 2 * x - cpx,
321
- 2 * y - cpy,
322
- s[1] + x,
323
- s[2] + y,
324
- s[3] + x,
325
- s[4] + y
326
- );
281
+ canvas.bezierCurveTo(2 * x - cpx, 2 * y - cpy, s[1] + x, s[2] + y, s[3] + x, s[4] + y);
327
282
  cpx = s[1] + x; // last control point
328
283
  cpy = s[2] + y;
329
284
  x += s[3];
330
285
  y += s[4];
331
286
  break;
332
- case 'Q':
287
+ case "Q":
333
288
  qcpx = s[1]; // last control point
334
289
  qcpy = s[2];
335
290
  x = s[3];
336
291
  y = s[4];
337
292
  canvas.quadraticCurveTo(qcpx, qcpy, x, y);
338
293
  break;
339
- case 'q':
294
+ case "q":
340
295
  qcpx = s[1] + x; // last control point
341
296
  qcpy = s[2] + y;
342
297
  x += s[3];
343
298
  y += s[4];
344
299
  canvas.quadraticCurveTo(qcpx, qcpy, x, y);
345
300
  break;
346
- case 'T':
301
+ case "T":
347
302
  if (qcpx === null || qcpy === null) {
348
303
  qcpx = x;
349
304
  qcpy = y;
@@ -354,7 +309,7 @@ function polyFillPath2D(window) {
354
309
  y = s[2];
355
310
  canvas.quadraticCurveTo(qcpx, qcpy, x, y);
356
311
  break;
357
- case 't':
312
+ case "t":
358
313
  if (qcpx === null || qcpy === null) {
359
314
  qcpx = x;
360
315
  qcpy = y;
@@ -365,14 +320,14 @@ function polyFillPath2D(window) {
365
320
  y += s[2];
366
321
  canvas.quadraticCurveTo(qcpx, qcpy, x, y);
367
322
  break;
368
- case 'z':
369
- case 'Z':
323
+ case "z":
324
+ case "Z":
370
325
  x = startPoint.x;
371
326
  y = startPoint.y;
372
327
  startPoint = undefined;
373
328
  canvas.closePath();
374
329
  break;
375
- case 'AC': // arc
330
+ case "AC": // arc
376
331
  x = s[1];
377
332
  y = s[2];
378
333
  r = s[3];
@@ -381,7 +336,7 @@ function polyFillPath2D(window) {
381
336
  ccw = s[6];
382
337
  canvas.arc(x, y, r, startAngle, endAngle, ccw);
383
338
  break;
384
- case 'AT': // arcTo
339
+ case "AT": // arcTo
385
340
  x1 = s[1];
386
341
  y1 = s[2];
387
342
  x = s[3];
@@ -389,7 +344,7 @@ function polyFillPath2D(window) {
389
344
  r = s[5];
390
345
  canvas.arcTo(x1, y1, x, y, r);
391
346
  break;
392
- case 'E': // ellipse
347
+ case "E": // ellipse
393
348
  x = s[1];
394
349
  y = s[2];
395
350
  rx = s[3];
@@ -405,7 +360,7 @@ function polyFillPath2D(window) {
405
360
  canvas.arc(0, 0, 1, startAngle, endAngle, ccw);
406
361
  canvas.restore();
407
362
  break;
408
- case 'R': // rect
363
+ case "R": // rect
409
364
  x = s[1];
410
365
  y = s[2];
411
366
  w = s[3];
@@ -426,11 +381,8 @@ function polyFillPath2D(window) {
426
381
  const cStroke = window.CanvasRenderingContext2D.prototype.stroke;
427
382
 
428
383
  window.CanvasRenderingContext2D.prototype.fill = function fill(...args) {
429
- let fillRule = 'nonzero';
430
- if (
431
- args.length === 0 ||
432
- (args.length === 1 && typeof args[0] === 'string')
433
- ) {
384
+ let fillRule = "nonzero";
385
+ if (args.length === 0 || (args.length === 1 && typeof args[0] === "string")) {
434
386
  cFill.apply(this, args);
435
387
  return;
436
388
  }
@@ -451,18 +403,15 @@ function polyFillPath2D(window) {
451
403
  cStroke.call(this);
452
404
  };
453
405
 
454
- const cIsPointInPath =
455
- window.CanvasRenderingContext2D.prototype.isPointInPath;
406
+ const cIsPointInPath = window.CanvasRenderingContext2D.prototype.isPointInPath;
456
407
 
457
- window.CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath(
458
- ...args
459
- ) {
408
+ window.CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath(...args) {
460
409
  // let fillRule = 'nonzero';
461
- if (args[0].constructor.name === 'Path2D') {
410
+ if (args[0].constructor.name === "Path2D") {
462
411
  // first argument is a Path2D object
463
412
  const x = args[1];
464
413
  const y = args[2];
465
- const fillRule = args[3] || 'nonzero';
414
+ const fillRule = args[3] || "nonzero";
466
415
  const path = args[0];
467
416
  buildPath(this, path.segments);
468
417
  return cIsPointInPath.apply(this, [x, y, fillRule]);