@iyio/svg-charts 0.0.8 → 0.0.10
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/esm/src/lib/SvgBaseChartCtrl.d.ts +7 -2
- package/esm/src/lib/SvgBaseChartCtrl.js +13 -2
- package/esm/src/lib/SvgBaseChartCtrl.js.map +1 -1
- package/esm/src/lib/SvgLineChartCtrl.d.ts +4 -1
- package/esm/src/lib/SvgLineChartCtrl.js +72 -2
- package/esm/src/lib/SvgLineChartCtrl.js.map +1 -1
- package/esm/src/lib/path-intersection.d.ts +35 -0
- package/esm/src/lib/path-intersection.js +657 -0
- package/esm/src/lib/path-intersection.js.map +1 -0
- package/esm/src/lib/svg-charts-types.d.ts +9 -0
- package/package.json +2 -2
- package/src/lib/SvgBaseChartCtrl.d.ts +7 -2
- package/src/lib/SvgBaseChartCtrl.js +13 -2
- package/src/lib/SvgBaseChartCtrl.js.map +1 -1
- package/src/lib/SvgLineChartCtrl.d.ts +4 -1
- package/src/lib/SvgLineChartCtrl.js +71 -1
- package/src/lib/SvgLineChartCtrl.js.map +1 -1
- package/src/lib/path-intersection.d.ts +35 -0
- package/src/lib/path-intersection.js +661 -0
- package/src/lib/path-intersection.js.map +1 -0
- package/src/lib/svg-charts-types.d.ts +9 -0
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable prefer-spread */
|
|
3
|
+
// Sorry I know this file is mess. It is a direct port of old style javascript,
|
|
4
|
+
// but it does work really well. So since it's not broke, I'm not fixing it.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.findPathIntersections = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* This file contains source code adapted from Snap.svg (licensed Apache-2.0).
|
|
9
|
+
*
|
|
10
|
+
* @see https://github.com/adobe-webplatform/Snap.svg/blob/master/src/path.js
|
|
11
|
+
*/
|
|
12
|
+
/* eslint no-fallthrough: "off" */
|
|
13
|
+
const p2s = /,?([a-z]),?/gi, toFloat = parseFloat, math = Math, PI = math.PI, mmin = math.min, mmax = math.max, pow = math.pow, abs = math.abs, pathCommand = /([a-z])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?[\s]*,?[\s]*)+)/ig, pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)[\s]*,?[\s]*/ig;
|
|
14
|
+
const isArray = Array.isArray || function (o) { return o instanceof Array; }; // todo - remove
|
|
15
|
+
const hasProperty = (obj, property) => {
|
|
16
|
+
return Object.prototype.hasOwnProperty.call(obj, property);
|
|
17
|
+
};
|
|
18
|
+
function clone(obj) {
|
|
19
|
+
if (typeof obj == 'function' || Object(obj) !== obj) {
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
const res = new obj.constructor;
|
|
23
|
+
for (const key in obj) {
|
|
24
|
+
if (hasProperty(obj, key)) {
|
|
25
|
+
res[key] = clone(obj[key]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return res;
|
|
29
|
+
}
|
|
30
|
+
function repush(array, item) {
|
|
31
|
+
for (let i = 0, ii = array.length; i < ii; i++)
|
|
32
|
+
if (array[i] === item) {
|
|
33
|
+
return array.push(array.splice(i, 1)[0]);
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
function cacher(f) {
|
|
38
|
+
function newf() {
|
|
39
|
+
// eslint-disable-next-line prefer-rest-params
|
|
40
|
+
const arg = Array.prototype.slice.call(arguments, 0), args = arg.join('\u2400'), cache = newf.cache = newf.cache || {}, count = newf.count = newf.count || [];
|
|
41
|
+
if (hasProperty(cache, args)) {
|
|
42
|
+
repush(count, args);
|
|
43
|
+
return cache[args];
|
|
44
|
+
}
|
|
45
|
+
count.length >= 1e3 && delete cache[count.shift()];
|
|
46
|
+
count.push(args);
|
|
47
|
+
cache[args] = f.apply(0, arg);
|
|
48
|
+
return cache[args];
|
|
49
|
+
}
|
|
50
|
+
return newf;
|
|
51
|
+
}
|
|
52
|
+
function parsePathString(pathString) {
|
|
53
|
+
if (!pathString) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const pth = paths(pathString);
|
|
57
|
+
if (pth.arr) {
|
|
58
|
+
return clone(pth.arr);
|
|
59
|
+
}
|
|
60
|
+
const paramCounts = { a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0 };
|
|
61
|
+
let data = [];
|
|
62
|
+
if (isArray(pathString) && isArray(pathString[0])) { // rough assumption
|
|
63
|
+
data = clone(pathString);
|
|
64
|
+
}
|
|
65
|
+
if (!data.length) {
|
|
66
|
+
String(pathString).replace(pathCommand, function (a, b, c) {
|
|
67
|
+
const params = [];
|
|
68
|
+
let name = b.toLowerCase();
|
|
69
|
+
c.replace(pathValues, function (a, b) {
|
|
70
|
+
b && params.push(+b);
|
|
71
|
+
});
|
|
72
|
+
if (name == 'm' && params.length > 2) {
|
|
73
|
+
data.push([b].concat(params.splice(0, 2)));
|
|
74
|
+
name = 'l';
|
|
75
|
+
b = b == 'm' ? 'l' : 'L';
|
|
76
|
+
}
|
|
77
|
+
while (params.length >= paramCounts[name]) {
|
|
78
|
+
data.push([b].concat(params.splice(0, paramCounts[name])));
|
|
79
|
+
if (!paramCounts[name]) {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return '';
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
data.toString = paths.toString;
|
|
87
|
+
pth.arr = clone(data);
|
|
88
|
+
return data;
|
|
89
|
+
}
|
|
90
|
+
function paths(ps) {
|
|
91
|
+
const p = paths.ps = paths.ps || {};
|
|
92
|
+
if (p[ps]) {
|
|
93
|
+
p[ps].sleep = 100;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
p[ps] = {
|
|
97
|
+
sleep: 100
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
setTimeout(function () {
|
|
101
|
+
for (const key in p) {
|
|
102
|
+
if (hasProperty(p, key) && key != ps) {
|
|
103
|
+
p[key].sleep--;
|
|
104
|
+
!p[key].sleep && delete p[key];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return p[ps];
|
|
109
|
+
}
|
|
110
|
+
function rectBBox(x, y, width, height) {
|
|
111
|
+
if (arguments.length === 1) {
|
|
112
|
+
y = x.y;
|
|
113
|
+
width = x.width;
|
|
114
|
+
height = x.height;
|
|
115
|
+
x = x.x;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
x: x,
|
|
119
|
+
y: y,
|
|
120
|
+
width: width,
|
|
121
|
+
height: height,
|
|
122
|
+
x2: x + width,
|
|
123
|
+
y2: y + height
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function pathToString() {
|
|
127
|
+
return this.join(',').replace(p2s, '$1');
|
|
128
|
+
}
|
|
129
|
+
function pathClone(pathArray) {
|
|
130
|
+
const res = clone(pathArray);
|
|
131
|
+
res.toString = pathToString;
|
|
132
|
+
return res;
|
|
133
|
+
}
|
|
134
|
+
function findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
|
|
135
|
+
const t1 = 1 - t, t13 = pow(t1, 3), t12 = pow(t1, 2), t2 = t * t, t3 = t2 * t, x = t13 * p1x + t12 * 3 * t * c1x + t1 * 3 * t * t * c2x + t3 * p2x, y = t13 * p1y + t12 * 3 * t * c1y + t1 * 3 * t * t * c2y + t3 * p2y;
|
|
136
|
+
return {
|
|
137
|
+
x: fixError(x),
|
|
138
|
+
y: fixError(y)
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function bezierBBox(points) {
|
|
142
|
+
const bbox = curveBBox.apply(null, points);
|
|
143
|
+
return rectBBox(bbox.x0, bbox.y0, bbox.x1 - bbox.x0, bbox.y1 - bbox.y0);
|
|
144
|
+
}
|
|
145
|
+
function isPointInsideBBox(bbox, x, y) {
|
|
146
|
+
return x >= bbox.x &&
|
|
147
|
+
x <= bbox.x + bbox.width &&
|
|
148
|
+
y >= bbox.y &&
|
|
149
|
+
y <= bbox.y + bbox.height;
|
|
150
|
+
}
|
|
151
|
+
function isBBoxIntersect(bbox1, bbox2) {
|
|
152
|
+
bbox1 = rectBBox(bbox1);
|
|
153
|
+
bbox2 = rectBBox(bbox2);
|
|
154
|
+
return isPointInsideBBox(bbox2, bbox1.x, bbox1.y)
|
|
155
|
+
|| isPointInsideBBox(bbox2, bbox1.x2, bbox1.y)
|
|
156
|
+
|| isPointInsideBBox(bbox2, bbox1.x, bbox1.y2)
|
|
157
|
+
|| isPointInsideBBox(bbox2, bbox1.x2, bbox1.y2)
|
|
158
|
+
|| isPointInsideBBox(bbox1, bbox2.x, bbox2.y)
|
|
159
|
+
|| isPointInsideBBox(bbox1, bbox2.x2, bbox2.y)
|
|
160
|
+
|| isPointInsideBBox(bbox1, bbox2.x, bbox2.y2)
|
|
161
|
+
|| isPointInsideBBox(bbox1, bbox2.x2, bbox2.y2)
|
|
162
|
+
|| (bbox1.x < bbox2.x2 && bbox1.x > bbox2.x
|
|
163
|
+
|| bbox2.x < bbox1.x2 && bbox2.x > bbox1.x)
|
|
164
|
+
&& (bbox1.y < bbox2.y2 && bbox1.y > bbox2.y
|
|
165
|
+
|| bbox2.y < bbox1.y2 && bbox2.y > bbox1.y);
|
|
166
|
+
}
|
|
167
|
+
function base3(t, p1, p2, p3, p4) {
|
|
168
|
+
const t1 = -3 * p1 + 9 * p2 - 9 * p3 + 3 * p4, t2 = t * t1 + 6 * p1 - 12 * p2 + 6 * p3;
|
|
169
|
+
return t * t2 - 3 * p1 + 3 * p2;
|
|
170
|
+
}
|
|
171
|
+
function bezlen(x1, y1, x2, y2, x3, y3, x4, y4, z) {
|
|
172
|
+
if (z == null) {
|
|
173
|
+
z = 1;
|
|
174
|
+
}
|
|
175
|
+
z = z > 1 ? 1 : z < 0 ? 0 : z;
|
|
176
|
+
const z2 = z / 2, n = 12, Tvalues = [-.1252, .1252, -.3678, .3678, -.5873, .5873, -.7699, .7699, -.9041, .9041, -.9816, .9816], Cvalues = [0.2491, 0.2491, 0.2335, 0.2335, 0.2032, 0.2032, 0.1601, 0.1601, 0.1069, 0.1069, 0.0472, 0.0472];
|
|
177
|
+
let sum = 0;
|
|
178
|
+
for (let i = 0; i < n; i++) {
|
|
179
|
+
const ct = z2 * Tvalues[i] + z2, xbase = base3(ct, x1, x2, x3, x4), ybase = base3(ct, y1, y2, y3, y4), comb = xbase * xbase + ybase * ybase;
|
|
180
|
+
sum += Cvalues[i] * math.sqrt(comb);
|
|
181
|
+
}
|
|
182
|
+
return z2 * sum;
|
|
183
|
+
}
|
|
184
|
+
function intersectLines(x1, y1, x2, y2, x3, y3, x4, y4) {
|
|
185
|
+
if (mmax(x1, x2) < mmin(x3, x4) ||
|
|
186
|
+
mmin(x1, x2) > mmax(x3, x4) ||
|
|
187
|
+
mmax(y1, y2) < mmin(y3, y4) ||
|
|
188
|
+
mmin(y1, y2) > mmax(y3, y4)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4), ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4), denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
|
192
|
+
if (!denominator) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const px = fixError(nx / denominator), py = fixError(ny / denominator), px2 = +px.toFixed(2), py2 = +py.toFixed(2);
|
|
196
|
+
if (px2 < +mmin(x1, x2).toFixed(2) ||
|
|
197
|
+
px2 > +mmax(x1, x2).toFixed(2) ||
|
|
198
|
+
px2 < +mmin(x3, x4).toFixed(2) ||
|
|
199
|
+
px2 > +mmax(x3, x4).toFixed(2) ||
|
|
200
|
+
py2 < +mmin(y1, y2).toFixed(2) ||
|
|
201
|
+
py2 > +mmax(y1, y2).toFixed(2) ||
|
|
202
|
+
py2 < +mmin(y3, y4).toFixed(2) ||
|
|
203
|
+
py2 > +mmax(y3, y4).toFixed(2)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
return { x: px, y: py };
|
|
207
|
+
}
|
|
208
|
+
function fixError(number) {
|
|
209
|
+
return Math.round(number * 100000000000) / 100000000000;
|
|
210
|
+
}
|
|
211
|
+
function findBezierIntersections(bez1, bez2, justCount) {
|
|
212
|
+
const bbox1 = bezierBBox(bez1), bbox2 = bezierBBox(bez2);
|
|
213
|
+
if (!isBBoxIntersect(bbox1, bbox2)) {
|
|
214
|
+
return justCount ? 0 : [];
|
|
215
|
+
}
|
|
216
|
+
// As an optimization, lines will have only 1 segment
|
|
217
|
+
const l1 = bezlen.apply(0, bez1), l2 = bezlen.apply(0, bez2), n1 = isLine(bez1) ? 1 : ~~(l1 / 5) || 1, n2 = isLine(bez2) ? 1 : ~~(l2 / 5) || 1, dots1 = [], dots2 = [], xy = {};
|
|
218
|
+
let res = justCount ? 0 : [];
|
|
219
|
+
let i = 0;
|
|
220
|
+
for (i = 0; i < n1 + 1; i++) {
|
|
221
|
+
const p = findDotsAtSegment.apply(0, bez1.concat(i / n1));
|
|
222
|
+
dots1.push({ x: p.x, y: p.y, t: i / n1 });
|
|
223
|
+
}
|
|
224
|
+
for (i = 0; i < n2 + 1; i++) {
|
|
225
|
+
const p = findDotsAtSegment.apply(0, bez2.concat(i / n2));
|
|
226
|
+
dots2.push({ x: p.x, y: p.y, t: i / n2 });
|
|
227
|
+
}
|
|
228
|
+
for (i = 0; i < n1; i++) {
|
|
229
|
+
for (let j = 0; j < n2; j++) {
|
|
230
|
+
const di = dots1[i], di1 = dots1[i + 1], dj = dots2[j], dj1 = dots2[j + 1], ci = abs(di1.x - di.x) < .01 ? 'y' : 'x', cj = abs(dj1.x - dj.x) < .01 ? 'y' : 'x', is = intersectLines(di.x, di.y, di1.x, di1.y, dj.x, dj.y, dj1.x, dj1.y);
|
|
231
|
+
let key;
|
|
232
|
+
if (is) {
|
|
233
|
+
key = is.x.toFixed(9) + '#' + is.y.toFixed(9);
|
|
234
|
+
if (xy[key]) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
xy[key] = true;
|
|
238
|
+
const t1 = di.t + abs((is[ci] - di[ci]) / (di1[ci] - di[ci])) * (di1.t - di.t), t2 = dj.t + abs((is[cj] - dj[cj]) / (dj1[cj] - dj[cj])) * (dj1.t - dj.t);
|
|
239
|
+
if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) {
|
|
240
|
+
if (justCount) {
|
|
241
|
+
res++;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
res.push({
|
|
245
|
+
x: is.x,
|
|
246
|
+
y: is.y,
|
|
247
|
+
t1: t1,
|
|
248
|
+
t2: t2
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return res;
|
|
256
|
+
}
|
|
257
|
+
function findPathIntersections(path1, path2, justCount) {
|
|
258
|
+
path1 = pathToCurve(path1);
|
|
259
|
+
path2 = pathToCurve(path2);
|
|
260
|
+
let x1, y1, x2, y2, x1m, y1m, x2m, y2m, bez1, bez2, res = justCount ? 0 : [];
|
|
261
|
+
for (let i = 0, ii = path1.length; i < ii; i++) {
|
|
262
|
+
const pi = path1[i];
|
|
263
|
+
if (pi[0] == 'M') {
|
|
264
|
+
x1 = x1m = pi[1];
|
|
265
|
+
y1 = y1m = pi[2];
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
if (pi[0] == 'C') {
|
|
269
|
+
bez1 = [x1, y1].concat(pi.slice(1));
|
|
270
|
+
x1 = bez1[6];
|
|
271
|
+
y1 = bez1[7];
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
bez1 = [x1, y1, x1, y1, x1m, y1m, x1m, y1m];
|
|
275
|
+
x1 = x1m;
|
|
276
|
+
y1 = y1m;
|
|
277
|
+
}
|
|
278
|
+
for (let j = 0, jj = path2.length; j < jj; j++) {
|
|
279
|
+
const pj = path2[j];
|
|
280
|
+
if (pj[0] == 'M') {
|
|
281
|
+
x2 = x2m = pj[1];
|
|
282
|
+
y2 = y2m = pj[2];
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
if (pj[0] == 'C') {
|
|
286
|
+
bez2 = [x2, y2].concat(pj.slice(1));
|
|
287
|
+
x2 = bez2[6];
|
|
288
|
+
y2 = bez2[7];
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
bez2 = [x2, y2, x2, y2, x2m, y2m, x2m, y2m];
|
|
292
|
+
x2 = x2m;
|
|
293
|
+
y2 = y2m;
|
|
294
|
+
}
|
|
295
|
+
const intr = findBezierIntersections(bez1, bez2, justCount);
|
|
296
|
+
if (justCount) {
|
|
297
|
+
res += intr;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
for (let k = 0, kk = intr.length; k < kk; k++) {
|
|
301
|
+
intr[k].segment1 = i;
|
|
302
|
+
intr[k].segment2 = j;
|
|
303
|
+
intr[k].bez1 = bez1;
|
|
304
|
+
intr[k].bez2 = bez2;
|
|
305
|
+
}
|
|
306
|
+
res = res.concat(intr);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return res;
|
|
313
|
+
}
|
|
314
|
+
exports.findPathIntersections = findPathIntersections;
|
|
315
|
+
function pathToAbsolute(pathArray) {
|
|
316
|
+
const pth = paths(pathArray);
|
|
317
|
+
if (pth.abs) {
|
|
318
|
+
return pathClone(pth.abs);
|
|
319
|
+
}
|
|
320
|
+
if (!isArray(pathArray) || !isArray(pathArray && pathArray[0])) { // rough assumption
|
|
321
|
+
pathArray = parsePathString(pathArray);
|
|
322
|
+
}
|
|
323
|
+
if (!pathArray || !pathArray.length) {
|
|
324
|
+
return [['M', 0, 0]];
|
|
325
|
+
}
|
|
326
|
+
const res = [];
|
|
327
|
+
let x = 0, y = 0, mx = 0, my = 0, start = 0, pa0;
|
|
328
|
+
if (pathArray[0][0] == 'M') {
|
|
329
|
+
x = +pathArray[0][1];
|
|
330
|
+
y = +pathArray[0][2];
|
|
331
|
+
mx = x;
|
|
332
|
+
my = y;
|
|
333
|
+
start++;
|
|
334
|
+
res[0] = ['M', x, y];
|
|
335
|
+
}
|
|
336
|
+
for (let r, pa, i = start, ii = pathArray.length; i < ii; i++) {
|
|
337
|
+
res.push(r = []);
|
|
338
|
+
pa = pathArray[i];
|
|
339
|
+
pa0 = pa[0];
|
|
340
|
+
if (pa0 != pa0.toUpperCase()) {
|
|
341
|
+
r[0] = pa0.toUpperCase();
|
|
342
|
+
switch (r[0]) {
|
|
343
|
+
case 'A':
|
|
344
|
+
r[1] = pa[1];
|
|
345
|
+
r[2] = pa[2];
|
|
346
|
+
r[3] = pa[3];
|
|
347
|
+
r[4] = pa[4];
|
|
348
|
+
r[5] = pa[5];
|
|
349
|
+
r[6] = +pa[6] + x;
|
|
350
|
+
r[7] = +pa[7] + y;
|
|
351
|
+
break;
|
|
352
|
+
case 'V':
|
|
353
|
+
r[1] = +pa[1] + y;
|
|
354
|
+
break;
|
|
355
|
+
case 'H':
|
|
356
|
+
r[1] = +pa[1] + x;
|
|
357
|
+
break;
|
|
358
|
+
default:
|
|
359
|
+
if (r[0] === 'M') {
|
|
360
|
+
mx = +pa[1] + x;
|
|
361
|
+
my = +pa[2] + y;
|
|
362
|
+
}
|
|
363
|
+
for (let j = 1, jj = pa.length; j < jj; j++) {
|
|
364
|
+
r[j] = +pa[j] + ((j % 2) ? x : y);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
for (let k = 0, kk = pa.length; k < kk; k++) {
|
|
370
|
+
r[k] = pa[k];
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
pa0 = pa0.toUpperCase();
|
|
374
|
+
switch (r[0]) {
|
|
375
|
+
case 'Z':
|
|
376
|
+
x = +mx;
|
|
377
|
+
y = +my;
|
|
378
|
+
break;
|
|
379
|
+
case 'H':
|
|
380
|
+
x = r[1];
|
|
381
|
+
break;
|
|
382
|
+
case 'V':
|
|
383
|
+
y = r[1];
|
|
384
|
+
break;
|
|
385
|
+
default:
|
|
386
|
+
if (r[0] === 'M') {
|
|
387
|
+
mx = r[r.length - 2];
|
|
388
|
+
my = r[r.length - 1];
|
|
389
|
+
}
|
|
390
|
+
x = r[r.length - 2];
|
|
391
|
+
y = r[r.length - 1];
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
res.toString = pathToString;
|
|
395
|
+
pth.abs = pathClone(res);
|
|
396
|
+
return res;
|
|
397
|
+
}
|
|
398
|
+
function isLine(bez) {
|
|
399
|
+
return (bez[0] === bez[2] &&
|
|
400
|
+
bez[1] === bez[3] &&
|
|
401
|
+
bez[4] === bez[6] &&
|
|
402
|
+
bez[5] === bez[7]);
|
|
403
|
+
}
|
|
404
|
+
function lineToCurve(x1, y1, x2, y2) {
|
|
405
|
+
return [
|
|
406
|
+
x1, y1, x2,
|
|
407
|
+
y2, x2, y2
|
|
408
|
+
];
|
|
409
|
+
}
|
|
410
|
+
function qubicToCurve(x1, y1, ax, ay, x2, y2) {
|
|
411
|
+
const _13 = 1 / 3, _23 = 2 / 3;
|
|
412
|
+
return [
|
|
413
|
+
_13 * x1 + _23 * ax,
|
|
414
|
+
_13 * y1 + _23 * ay,
|
|
415
|
+
_13 * x2 + _23 * ax,
|
|
416
|
+
_13 * y2 + _23 * ay,
|
|
417
|
+
x2,
|
|
418
|
+
y2
|
|
419
|
+
];
|
|
420
|
+
}
|
|
421
|
+
function arcToCurve(x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
|
|
422
|
+
// for more information of where this math came from visit:
|
|
423
|
+
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
|
|
424
|
+
const _120 = PI * 120 / 180, rad = PI / 180 * (+angle || 0);
|
|
425
|
+
let res = [], xy;
|
|
426
|
+
const rotate = cacher(function (x, y, rad) {
|
|
427
|
+
const X = x * math.cos(rad) - y * math.sin(rad), Y = x * math.sin(rad) + y * math.cos(rad);
|
|
428
|
+
return { x: X, y: Y };
|
|
429
|
+
});
|
|
430
|
+
let f1, f2, cx, cy;
|
|
431
|
+
if (!recursive) {
|
|
432
|
+
xy = rotate(x1, y1, -rad);
|
|
433
|
+
x1 = xy.x;
|
|
434
|
+
y1 = xy.y;
|
|
435
|
+
xy = rotate(x2, y2, -rad);
|
|
436
|
+
x2 = xy.x;
|
|
437
|
+
y2 = xy.y;
|
|
438
|
+
const x = (x1 - x2) / 2, y = (y1 - y2) / 2;
|
|
439
|
+
let h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
|
|
440
|
+
if (h > 1) {
|
|
441
|
+
h = math.sqrt(h);
|
|
442
|
+
rx = h * rx;
|
|
443
|
+
ry = h * ry;
|
|
444
|
+
}
|
|
445
|
+
const rx2 = rx * rx, ry2 = ry * ry, k = (large_arc_flag == sweep_flag ? -1 : 1) *
|
|
446
|
+
math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x)));
|
|
447
|
+
cx = k * rx * y / ry + (x1 + x2) / 2;
|
|
448
|
+
cy = k * -ry * x / rx + (y1 + y2) / 2;
|
|
449
|
+
f1 = math.asin(Number(((y1 - cy) / ry).toFixed(9)));
|
|
450
|
+
f2 = math.asin(Number(((y2 - cy) / ry).toFixed(9)));
|
|
451
|
+
f1 = x1 < cx ? PI - f1 : f1;
|
|
452
|
+
f2 = x2 < cx ? PI - f2 : f2;
|
|
453
|
+
f1 < 0 && (f1 = PI * 2 + f1);
|
|
454
|
+
f2 < 0 && (f2 = PI * 2 + f2);
|
|
455
|
+
if (sweep_flag && f1 > f2) {
|
|
456
|
+
f1 = f1 - PI * 2;
|
|
457
|
+
}
|
|
458
|
+
if (!sweep_flag && f2 > f1) {
|
|
459
|
+
f2 = f2 - PI * 2;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
f1 = recursive[0];
|
|
464
|
+
f2 = recursive[1];
|
|
465
|
+
cx = recursive[2];
|
|
466
|
+
cy = recursive[3];
|
|
467
|
+
}
|
|
468
|
+
let df = f2 - f1;
|
|
469
|
+
if (abs(df) > _120) {
|
|
470
|
+
const f2old = f2, x2old = x2, y2old = y2;
|
|
471
|
+
f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
|
|
472
|
+
x2 = cx + rx * math.cos(f2);
|
|
473
|
+
y2 = cy + ry * math.sin(f2);
|
|
474
|
+
res = arcToCurve(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
|
|
475
|
+
}
|
|
476
|
+
df = f2 - f1;
|
|
477
|
+
const c1 = math.cos(f1), s1 = math.sin(f1), c2 = math.cos(f2), s2 = math.sin(f2), t = math.tan(df / 4), hx = 4 / 3 * rx * t, hy = 4 / 3 * ry * t, m1 = [x1, y1], m2 = [x1 + hx * s1, y1 - hy * c1], m3 = [x2 + hx * s2, y2 - hy * c2], m4 = [x2, y2];
|
|
478
|
+
m2[0] = 2 * m1[0] - m2[0];
|
|
479
|
+
m2[1] = 2 * m1[1] - m2[1];
|
|
480
|
+
if (recursive) {
|
|
481
|
+
return [m2, m3, m4].concat(res);
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
res = [m2, m3, m4].concat(res).join().split(',');
|
|
485
|
+
const newres = [];
|
|
486
|
+
for (let i = 0, ii = res.length; i < ii; i++) {
|
|
487
|
+
newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
|
|
488
|
+
}
|
|
489
|
+
return newres;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
// Returns bounding box of cubic bezier curve.
|
|
493
|
+
// Source: http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
|
|
494
|
+
// Original version: NISHIO Hirokazu
|
|
495
|
+
// Modifications: https://github.com/timo22345
|
|
496
|
+
function curveBBox(x0, y0, x1, y1, x2, y2, x3, y3) {
|
|
497
|
+
const tvalues = [], bounds = [[], []];
|
|
498
|
+
let a, b, c, t, t1, t2, b2ac, sqrtb2ac;
|
|
499
|
+
for (let i = 0; i < 2; ++i) {
|
|
500
|
+
if (i == 0) {
|
|
501
|
+
b = 6 * x0 - 12 * x1 + 6 * x2;
|
|
502
|
+
a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
|
|
503
|
+
c = 3 * x1 - 3 * x0;
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
b = 6 * y0 - 12 * y1 + 6 * y2;
|
|
507
|
+
a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
|
|
508
|
+
c = 3 * y1 - 3 * y0;
|
|
509
|
+
}
|
|
510
|
+
if (abs(a) < 1e-12) {
|
|
511
|
+
if (abs(b) < 1e-12) {
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
t = -c / b;
|
|
515
|
+
if (0 < t && t < 1) {
|
|
516
|
+
tvalues.push(t);
|
|
517
|
+
}
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
b2ac = b * b - 4 * c * a;
|
|
521
|
+
sqrtb2ac = math.sqrt(b2ac);
|
|
522
|
+
if (b2ac < 0) {
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
t1 = (-b + sqrtb2ac) / (2 * a);
|
|
526
|
+
if (0 < t1 && t1 < 1) {
|
|
527
|
+
tvalues.push(t1);
|
|
528
|
+
}
|
|
529
|
+
t2 = (-b - sqrtb2ac) / (2 * a);
|
|
530
|
+
if (0 < t2 && t2 < 1) {
|
|
531
|
+
tvalues.push(t2);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
let j = tvalues.length;
|
|
535
|
+
const jlen = j;
|
|
536
|
+
let mt;
|
|
537
|
+
while (j--) {
|
|
538
|
+
t = tvalues[j];
|
|
539
|
+
mt = 1 - t;
|
|
540
|
+
bounds[0][j] = (mt * mt * mt * x0) + (3 * mt * mt * t * x1) + (3 * mt * t * t * x2) + (t * t * t * x3);
|
|
541
|
+
bounds[1][j] = (mt * mt * mt * y0) + (3 * mt * mt * t * y1) + (3 * mt * t * t * y2) + (t * t * t * y3);
|
|
542
|
+
}
|
|
543
|
+
bounds[0][jlen] = x0;
|
|
544
|
+
bounds[1][jlen] = y0;
|
|
545
|
+
bounds[0][jlen + 1] = x3;
|
|
546
|
+
bounds[1][jlen + 1] = y3;
|
|
547
|
+
bounds[0].length = bounds[1].length = jlen + 2;
|
|
548
|
+
return {
|
|
549
|
+
x0: mmin.apply(0, bounds[0]),
|
|
550
|
+
y0: mmin.apply(0, bounds[1]),
|
|
551
|
+
x1: mmax.apply(0, bounds[0]),
|
|
552
|
+
y1: mmax.apply(0, bounds[1])
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function pathToCurve(path) {
|
|
556
|
+
const pth = paths(path);
|
|
557
|
+
// return cached curve, if existing
|
|
558
|
+
if (pth.curve) {
|
|
559
|
+
return pathClone(pth.curve);
|
|
560
|
+
}
|
|
561
|
+
const curvedPath = pathToAbsolute(path), attrs = { x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null }, processPath = function (path, d, pathCommand) {
|
|
562
|
+
let nx, ny;
|
|
563
|
+
if (!path) {
|
|
564
|
+
return ['C', d.x, d.y, d.x, d.y, d.x, d.y];
|
|
565
|
+
}
|
|
566
|
+
!(path[0] in { T: 1, Q: 1 }) && (d.qx = d.qy = null);
|
|
567
|
+
switch (path[0]) {
|
|
568
|
+
case 'M':
|
|
569
|
+
d.X = path[1];
|
|
570
|
+
d.Y = path[2];
|
|
571
|
+
break;
|
|
572
|
+
case 'A':
|
|
573
|
+
path = ['C'].concat(arcToCurve.apply(0, [d.x, d.y].concat(path.slice(1))));
|
|
574
|
+
break;
|
|
575
|
+
case 'S':
|
|
576
|
+
if (pathCommand == 'C' || pathCommand == 'S') {
|
|
577
|
+
// In 'S' case we have to take into account, if the previous command is C/S.
|
|
578
|
+
nx = d.x * 2 - d.bx;
|
|
579
|
+
// And reflect the previous
|
|
580
|
+
ny = d.y * 2 - d.by;
|
|
581
|
+
// command's control point relative to the current point.
|
|
582
|
+
}
|
|
583
|
+
else {
|
|
584
|
+
// or some else or nothing
|
|
585
|
+
nx = d.x;
|
|
586
|
+
ny = d.y;
|
|
587
|
+
}
|
|
588
|
+
path = ['C', nx, ny].concat(path.slice(1));
|
|
589
|
+
break;
|
|
590
|
+
case 'T':
|
|
591
|
+
if (pathCommand == 'Q' || pathCommand == 'T') {
|
|
592
|
+
// In 'T' case we have to take into account, if the previous command is Q/T.
|
|
593
|
+
d.qx = d.x * 2 - d.qx;
|
|
594
|
+
// And make a reflection similar
|
|
595
|
+
d.qy = d.y * 2 - d.qy;
|
|
596
|
+
// to case 'S'.
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
// or something else or nothing
|
|
600
|
+
d.qx = d.x;
|
|
601
|
+
d.qy = d.y;
|
|
602
|
+
}
|
|
603
|
+
path = ['C'].concat(qubicToCurve(d.x, d.y, d.qx, d.qy, path[1], path[2]));
|
|
604
|
+
break;
|
|
605
|
+
case 'Q':
|
|
606
|
+
d.qx = path[1];
|
|
607
|
+
d.qy = path[2];
|
|
608
|
+
path = ['C'].concat(qubicToCurve(d.x, d.y, path[1], path[2], path[3], path[4]));
|
|
609
|
+
break;
|
|
610
|
+
case 'L':
|
|
611
|
+
path = ['C'].concat(lineToCurve(d.x, d.y, path[1], path[2]));
|
|
612
|
+
break;
|
|
613
|
+
case 'H':
|
|
614
|
+
path = ['C'].concat(lineToCurve(d.x, d.y, path[1], d.y));
|
|
615
|
+
break;
|
|
616
|
+
case 'V':
|
|
617
|
+
path = ['C'].concat(lineToCurve(d.x, d.y, d.x, path[1]));
|
|
618
|
+
break;
|
|
619
|
+
case 'Z':
|
|
620
|
+
path = ['C'].concat(lineToCurve(d.x, d.y, d.X, d.Y));
|
|
621
|
+
break;
|
|
622
|
+
}
|
|
623
|
+
return path;
|
|
624
|
+
}, fixArc = function (pp, i) {
|
|
625
|
+
if (pp[i].length > 7) {
|
|
626
|
+
pp[i].shift();
|
|
627
|
+
const pi = pp[i];
|
|
628
|
+
while (pi.length) {
|
|
629
|
+
pathCommands[i] = 'A'; // if created multiple C:s, their original seg is saved
|
|
630
|
+
pp.splice(i++, 0, ['C'].concat(pi.splice(0, 6)));
|
|
631
|
+
}
|
|
632
|
+
pp.splice(i, 1);
|
|
633
|
+
//ii = curvedPath.length;
|
|
634
|
+
}
|
|
635
|
+
}, pathCommands = []; // path commands of original path p
|
|
636
|
+
let pfirst = '', // temporary holder for original path command
|
|
637
|
+
pathCommand = ''; // holder for previous path command of original path
|
|
638
|
+
for (let i = 0, ii = curvedPath.length; i < ii; i++) {
|
|
639
|
+
curvedPath[i] && (pfirst = curvedPath[i][0]); // save current path command
|
|
640
|
+
if (pfirst != 'C') // C is not saved yet, because it may be result of conversion
|
|
641
|
+
{
|
|
642
|
+
pathCommands[i] = pfirst; // Save current path command
|
|
643
|
+
i && (pathCommand = pathCommands[i - 1]); // Get previous path command pathCommand
|
|
644
|
+
}
|
|
645
|
+
curvedPath[i] = processPath(curvedPath[i], attrs, pathCommand); // Previous path command is inputted to processPath
|
|
646
|
+
if (pathCommands[i] != 'A' && pfirst == 'C')
|
|
647
|
+
pathCommands[i] = 'C'; // A is the only command
|
|
648
|
+
// which may produce multiple C:s
|
|
649
|
+
// so we have to make sure that C is also C in original path
|
|
650
|
+
fixArc(curvedPath, i); // fixArc adds also the right amount of A:s to pathCommands
|
|
651
|
+
const seg = curvedPath[i], seglen = seg.length;
|
|
652
|
+
attrs.x = seg[seglen - 2];
|
|
653
|
+
attrs.y = seg[seglen - 1];
|
|
654
|
+
attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
|
|
655
|
+
attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
|
|
656
|
+
}
|
|
657
|
+
// cache curve
|
|
658
|
+
pth.curve = pathClone(curvedPath);
|
|
659
|
+
return curvedPath;
|
|
660
|
+
}
|
|
661
|
+
//# sourceMappingURL=path-intersection.js.map
|