path2d-polyfill 1.2.2 → 2.0.0-beta.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.
- package/README.md +53 -16
- package/dist/path2d-polyfill.cjs.js +596 -0
- package/dist/path2d-polyfill.dev.js +597 -0
- package/dist/path2d-polyfill.esm.js +591 -0
- package/dist/path2d-polyfill.min.js +1 -0
- package/package.json +43 -31
- package/CHANGELOG.md +0 -77
- package/dist/index.esm.js +0 -652
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/src/index.js +0 -11
- package/src/parse-path.js +0 -74
- package/src/path2d-polyfill.js +0 -426
package/src/path2d-polyfill.js
DELETED
|
@@ -1,426 +0,0 @@
|
|
|
1
|
-
const parsePath = require("./parse-path");
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Work around for https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8438884/
|
|
5
|
-
* @ignore
|
|
6
|
-
*/
|
|
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";
|
|
12
|
-
g.lineWidth = 1;
|
|
13
|
-
g.stroke(p);
|
|
14
|
-
const imgData = g.getImageData(0, 0, 1, 1);
|
|
15
|
-
return imgData.data[0] === 255; // Check if pixel is red
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function rotatePoint(point, angle) {
|
|
19
|
-
const nx = point.x * Math.cos(angle) - point.y * Math.sin(angle);
|
|
20
|
-
const ny = point.y * Math.cos(angle) + point.x * Math.sin(angle);
|
|
21
|
-
point.x = nx;
|
|
22
|
-
point.y = ny;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function translatePoint(point, dx, dy) {
|
|
26
|
-
point.x += dx;
|
|
27
|
-
point.y += dy;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function scalePoint(point, s) {
|
|
31
|
-
point.x *= s;
|
|
32
|
-
point.y *= s;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function polyFillPath2D(window) {
|
|
36
|
-
if (typeof window === "undefined" || !window.CanvasRenderingContext2D) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (window.Path2D && supportsSvgPathArgument(window)) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Crates a Path2D polyfill object
|
|
45
|
-
* @constructor
|
|
46
|
-
* @ignore
|
|
47
|
-
* @param {String} path
|
|
48
|
-
*/
|
|
49
|
-
class Path2D {
|
|
50
|
-
constructor(path) {
|
|
51
|
-
this.segments = [];
|
|
52
|
-
if (path && path instanceof Path2D) {
|
|
53
|
-
this.segments.push(...path.segments);
|
|
54
|
-
} else if (path) {
|
|
55
|
-
this.segments = parsePath(path);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
addPath(path) {
|
|
60
|
-
if (path && path instanceof Path2D) {
|
|
61
|
-
this.segments.push(...path.segments);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
moveTo(x, y) {
|
|
66
|
-
this.segments.push(["M", x, y]);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
lineTo(x, y) {
|
|
70
|
-
this.segments.push(["L", x, y]);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
arc(x, y, r, start, end, ccw) {
|
|
74
|
-
this.segments.push(["AC", x, y, r, start, end, !!ccw]);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
arcTo(x1, y1, x2, y2, r) {
|
|
78
|
-
this.segments.push(["AT", x1, y1, x2, y2, r]);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
ellipse(x, y, rx, ry, angle, start, end, ccw) {
|
|
82
|
-
this.segments.push(["E", x, y, rx, ry, angle, start, end, !!ccw]);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
closePath() {
|
|
86
|
-
this.segments.push(["Z"]);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
90
|
-
this.segments.push(["C", cp1x, cp1y, cp2x, cp2y, x, y]);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
quadraticCurveTo(cpx, cpy, x, y) {
|
|
94
|
-
this.segments.push(["Q", cpx, cpy, x, y]);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
rect(x, y, width, height) {
|
|
98
|
-
this.segments.push(["R", x, y, width, height]);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function buildPath(canvas, segments) {
|
|
103
|
-
let endAngle;
|
|
104
|
-
let startAngle;
|
|
105
|
-
let largeArcFlag;
|
|
106
|
-
let sweepFlag;
|
|
107
|
-
let endPoint;
|
|
108
|
-
let midPoint;
|
|
109
|
-
let angle;
|
|
110
|
-
let lambda;
|
|
111
|
-
let t1;
|
|
112
|
-
let t2;
|
|
113
|
-
let x;
|
|
114
|
-
let x1;
|
|
115
|
-
let y;
|
|
116
|
-
let y1;
|
|
117
|
-
let r;
|
|
118
|
-
let rx;
|
|
119
|
-
let ry;
|
|
120
|
-
let w;
|
|
121
|
-
let h;
|
|
122
|
-
let pathType;
|
|
123
|
-
let centerPoint;
|
|
124
|
-
let cpx;
|
|
125
|
-
let cpy;
|
|
126
|
-
let qcpx;
|
|
127
|
-
let qcpy;
|
|
128
|
-
let ccw;
|
|
129
|
-
let startPoint = { x: 0, y: 0 };
|
|
130
|
-
const currentPoint = { x: 0, y: 0 };
|
|
131
|
-
|
|
132
|
-
canvas.beginPath();
|
|
133
|
-
for (let i = 0; i < segments.length; ++i) {
|
|
134
|
-
const s = segments[i];
|
|
135
|
-
pathType = s[0];
|
|
136
|
-
|
|
137
|
-
// Reset control point if command is not cubic
|
|
138
|
-
if (pathType !== "S" && pathType !== "s" && pathType !== "C" && pathType !== "c") {
|
|
139
|
-
cpx = null;
|
|
140
|
-
cpy = null;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (pathType !== "T" && pathType !== "t" && pathType !== "Q" && pathType !== "q") {
|
|
144
|
-
qcpx = null;
|
|
145
|
-
qcpy = null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
switch (pathType) {
|
|
149
|
-
case "m":
|
|
150
|
-
case "M":
|
|
151
|
-
if (pathType === "m") {
|
|
152
|
-
x += s[1];
|
|
153
|
-
y += s[2];
|
|
154
|
-
} else {
|
|
155
|
-
x = s[1];
|
|
156
|
-
y = s[2];
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (pathType === "M" || !startPoint) {
|
|
160
|
-
startPoint = { x, y };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
canvas.moveTo(x, y);
|
|
164
|
-
break;
|
|
165
|
-
case "l":
|
|
166
|
-
x += s[1];
|
|
167
|
-
y += s[2];
|
|
168
|
-
canvas.lineTo(x, y);
|
|
169
|
-
break;
|
|
170
|
-
case "L":
|
|
171
|
-
x = s[1];
|
|
172
|
-
y = s[2];
|
|
173
|
-
canvas.lineTo(x, y);
|
|
174
|
-
break;
|
|
175
|
-
case "H":
|
|
176
|
-
x = s[1];
|
|
177
|
-
canvas.lineTo(x, y);
|
|
178
|
-
break;
|
|
179
|
-
case "h":
|
|
180
|
-
x += s[1];
|
|
181
|
-
canvas.lineTo(x, y);
|
|
182
|
-
break;
|
|
183
|
-
case "V":
|
|
184
|
-
y = s[1];
|
|
185
|
-
canvas.lineTo(x, y);
|
|
186
|
-
break;
|
|
187
|
-
case "v":
|
|
188
|
-
y += s[1];
|
|
189
|
-
canvas.lineTo(x, y);
|
|
190
|
-
break;
|
|
191
|
-
case "a":
|
|
192
|
-
case "A":
|
|
193
|
-
if (pathType === "a") {
|
|
194
|
-
x += s[6];
|
|
195
|
-
y += s[7];
|
|
196
|
-
} else {
|
|
197
|
-
x = s[6];
|
|
198
|
-
y = s[7];
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
rx = s[1]; // rx
|
|
202
|
-
ry = s[2]; // ry
|
|
203
|
-
angle = (s[3] * Math.PI) / 180;
|
|
204
|
-
largeArcFlag = !!s[4];
|
|
205
|
-
sweepFlag = !!s[5];
|
|
206
|
-
endPoint = { x, y };
|
|
207
|
-
|
|
208
|
-
// https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
|
|
209
|
-
|
|
210
|
-
midPoint = {
|
|
211
|
-
x: (currentPoint.x - endPoint.x) / 2,
|
|
212
|
-
y: (currentPoint.y - endPoint.y) / 2,
|
|
213
|
-
};
|
|
214
|
-
rotatePoint(midPoint, -angle);
|
|
215
|
-
|
|
216
|
-
// radius correction
|
|
217
|
-
lambda = (midPoint.x * midPoint.x) / (rx * rx) + (midPoint.y * midPoint.y) / (ry * ry);
|
|
218
|
-
if (lambda > 1) {
|
|
219
|
-
lambda = Math.sqrt(lambda);
|
|
220
|
-
rx *= lambda;
|
|
221
|
-
ry *= lambda;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
centerPoint = {
|
|
225
|
-
x: (rx * midPoint.y) / ry,
|
|
226
|
-
y: -(ry * midPoint.x) / rx,
|
|
227
|
-
};
|
|
228
|
-
t1 = rx * rx * ry * ry;
|
|
229
|
-
t2 = rx * rx * midPoint.y * midPoint.y + ry * ry * midPoint.x * midPoint.x;
|
|
230
|
-
if (sweepFlag !== largeArcFlag) {
|
|
231
|
-
scalePoint(centerPoint, Math.sqrt((t1 - t2) / t2) || 0);
|
|
232
|
-
} else {
|
|
233
|
-
scalePoint(centerPoint, -Math.sqrt((t1 - t2) / t2) || 0);
|
|
234
|
-
}
|
|
235
|
-
|
|
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);
|
|
238
|
-
|
|
239
|
-
rotatePoint(centerPoint, angle);
|
|
240
|
-
translatePoint(centerPoint, (endPoint.x + currentPoint.x) / 2, (endPoint.y + currentPoint.y) / 2);
|
|
241
|
-
|
|
242
|
-
canvas.save();
|
|
243
|
-
canvas.translate(centerPoint.x, centerPoint.y);
|
|
244
|
-
canvas.rotate(angle);
|
|
245
|
-
canvas.scale(rx, ry);
|
|
246
|
-
canvas.arc(0, 0, 1, startAngle, endAngle, !sweepFlag);
|
|
247
|
-
canvas.restore();
|
|
248
|
-
break;
|
|
249
|
-
case "C":
|
|
250
|
-
cpx = s[3]; // Last control point
|
|
251
|
-
cpy = s[4];
|
|
252
|
-
x = s[5];
|
|
253
|
-
y = s[6];
|
|
254
|
-
canvas.bezierCurveTo(s[1], s[2], cpx, cpy, x, y);
|
|
255
|
-
break;
|
|
256
|
-
case "c":
|
|
257
|
-
canvas.bezierCurveTo(s[1] + x, s[2] + y, s[3] + x, s[4] + y, s[5] + x, s[6] + y);
|
|
258
|
-
cpx = s[3] + x; // Last control point
|
|
259
|
-
cpy = s[4] + y;
|
|
260
|
-
x += s[5];
|
|
261
|
-
y += s[6];
|
|
262
|
-
break;
|
|
263
|
-
case "S":
|
|
264
|
-
if (cpx === null || cpy === null) {
|
|
265
|
-
cpx = x;
|
|
266
|
-
cpy = y;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
canvas.bezierCurveTo(2 * x - cpx, 2 * y - cpy, s[1], s[2], s[3], s[4]);
|
|
270
|
-
cpx = s[1]; // last control point
|
|
271
|
-
cpy = s[2];
|
|
272
|
-
x = s[3];
|
|
273
|
-
y = s[4];
|
|
274
|
-
break;
|
|
275
|
-
case "s":
|
|
276
|
-
if (cpx === null || cpy === null) {
|
|
277
|
-
cpx = x;
|
|
278
|
-
cpy = y;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
canvas.bezierCurveTo(2 * x - cpx, 2 * y - cpy, s[1] + x, s[2] + y, s[3] + x, s[4] + y);
|
|
282
|
-
cpx = s[1] + x; // last control point
|
|
283
|
-
cpy = s[2] + y;
|
|
284
|
-
x += s[3];
|
|
285
|
-
y += s[4];
|
|
286
|
-
break;
|
|
287
|
-
case "Q":
|
|
288
|
-
qcpx = s[1]; // last control point
|
|
289
|
-
qcpy = s[2];
|
|
290
|
-
x = s[3];
|
|
291
|
-
y = s[4];
|
|
292
|
-
canvas.quadraticCurveTo(qcpx, qcpy, x, y);
|
|
293
|
-
break;
|
|
294
|
-
case "q":
|
|
295
|
-
qcpx = s[1] + x; // last control point
|
|
296
|
-
qcpy = s[2] + y;
|
|
297
|
-
x += s[3];
|
|
298
|
-
y += s[4];
|
|
299
|
-
canvas.quadraticCurveTo(qcpx, qcpy, x, y);
|
|
300
|
-
break;
|
|
301
|
-
case "T":
|
|
302
|
-
if (qcpx === null || qcpy === null) {
|
|
303
|
-
qcpx = x;
|
|
304
|
-
qcpy = y;
|
|
305
|
-
}
|
|
306
|
-
qcpx = 2 * x - qcpx; // last control point
|
|
307
|
-
qcpy = 2 * y - qcpy;
|
|
308
|
-
x = s[1];
|
|
309
|
-
y = s[2];
|
|
310
|
-
canvas.quadraticCurveTo(qcpx, qcpy, x, y);
|
|
311
|
-
break;
|
|
312
|
-
case "t":
|
|
313
|
-
if (qcpx === null || qcpy === null) {
|
|
314
|
-
qcpx = x;
|
|
315
|
-
qcpy = y;
|
|
316
|
-
}
|
|
317
|
-
qcpx = 2 * x - qcpx; // last control point
|
|
318
|
-
qcpy = 2 * y - qcpy;
|
|
319
|
-
x += s[1];
|
|
320
|
-
y += s[2];
|
|
321
|
-
canvas.quadraticCurveTo(qcpx, qcpy, x, y);
|
|
322
|
-
break;
|
|
323
|
-
case "z":
|
|
324
|
-
case "Z":
|
|
325
|
-
x = startPoint.x;
|
|
326
|
-
y = startPoint.y;
|
|
327
|
-
startPoint = undefined;
|
|
328
|
-
canvas.closePath();
|
|
329
|
-
break;
|
|
330
|
-
case "AC": // arc
|
|
331
|
-
x = s[1];
|
|
332
|
-
y = s[2];
|
|
333
|
-
r = s[3];
|
|
334
|
-
startAngle = s[4];
|
|
335
|
-
endAngle = s[5];
|
|
336
|
-
ccw = s[6];
|
|
337
|
-
canvas.arc(x, y, r, startAngle, endAngle, ccw);
|
|
338
|
-
break;
|
|
339
|
-
case "AT": // arcTo
|
|
340
|
-
x1 = s[1];
|
|
341
|
-
y1 = s[2];
|
|
342
|
-
x = s[3];
|
|
343
|
-
y = s[4];
|
|
344
|
-
r = s[5];
|
|
345
|
-
canvas.arcTo(x1, y1, x, y, r);
|
|
346
|
-
break;
|
|
347
|
-
case "E": // ellipse
|
|
348
|
-
x = s[1];
|
|
349
|
-
y = s[2];
|
|
350
|
-
rx = s[3];
|
|
351
|
-
ry = s[4];
|
|
352
|
-
angle = s[5];
|
|
353
|
-
startAngle = s[6];
|
|
354
|
-
endAngle = s[7];
|
|
355
|
-
ccw = s[8];
|
|
356
|
-
canvas.save();
|
|
357
|
-
canvas.translate(x, y);
|
|
358
|
-
canvas.rotate(angle);
|
|
359
|
-
canvas.scale(rx, ry);
|
|
360
|
-
canvas.arc(0, 0, 1, startAngle, endAngle, ccw);
|
|
361
|
-
canvas.restore();
|
|
362
|
-
break;
|
|
363
|
-
case "R": // rect
|
|
364
|
-
x = s[1];
|
|
365
|
-
y = s[2];
|
|
366
|
-
w = s[3];
|
|
367
|
-
h = s[4];
|
|
368
|
-
startPoint = { x, y };
|
|
369
|
-
canvas.rect(x, y, w, h);
|
|
370
|
-
break;
|
|
371
|
-
default:
|
|
372
|
-
// throw new Error(`${pathType} is not implemented`); ?
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
currentPoint.x = x;
|
|
376
|
-
currentPoint.y = y;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
const cFill = window.CanvasRenderingContext2D.prototype.fill;
|
|
381
|
-
const cStroke = window.CanvasRenderingContext2D.prototype.stroke;
|
|
382
|
-
|
|
383
|
-
window.CanvasRenderingContext2D.prototype.fill = function fill(...args) {
|
|
384
|
-
let fillRule = "nonzero";
|
|
385
|
-
if (args.length === 0 || (args.length === 1 && typeof args[0] === "string")) {
|
|
386
|
-
cFill.apply(this, args);
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
if (arguments.length === 2) {
|
|
390
|
-
fillRule = args[1];
|
|
391
|
-
}
|
|
392
|
-
const path = args[0];
|
|
393
|
-
buildPath(this, path.segments);
|
|
394
|
-
cFill.call(this, fillRule);
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
window.CanvasRenderingContext2D.prototype.stroke = function stroke(path) {
|
|
398
|
-
if (!path) {
|
|
399
|
-
cStroke.call(this);
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
buildPath(this, path.segments);
|
|
403
|
-
cStroke.call(this);
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
const cIsPointInPath = window.CanvasRenderingContext2D.prototype.isPointInPath;
|
|
407
|
-
|
|
408
|
-
window.CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath(...args) {
|
|
409
|
-
// let fillRule = 'nonzero';
|
|
410
|
-
if (args[0].constructor.name === "Path2D") {
|
|
411
|
-
// first argument is a Path2D object
|
|
412
|
-
const x = args[1];
|
|
413
|
-
const y = args[2];
|
|
414
|
-
const fillRule = args[3] || "nonzero";
|
|
415
|
-
const path = args[0];
|
|
416
|
-
buildPath(this, path.segments);
|
|
417
|
-
return cIsPointInPath.apply(this, [x, y, fillRule]);
|
|
418
|
-
} else {
|
|
419
|
-
return cIsPointInPath.apply(this, args);
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
window.Path2D = Path2D;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
module.exports = polyFillPath2D;
|