svg-path-interpolator 0.2.3 → 0.3.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,312 +0,0 @@
1
- const rotatePoint = require('./utils').rotatePoint;
2
- const degToRads = require('./utils').degToRads;
3
- const isNullOrUndefined = require('./utils').isNullOrUndefined;
4
- const SVGTransform = require('./SVGTransform');
5
-
6
- // Calculators
7
- // https://pomax.github.io/bezierinfo/
8
- function calculateLinear(t, p1, p2) {
9
- return p1 + t * (p2 - p1);
10
- }
11
-
12
- function calculatePointQuadratic(t, p1, p2, p3) {
13
- const oneMinusT = 1 - t;
14
- return (oneMinusT * oneMinusT) * p1 + 2 * oneMinusT * t * p2 + (t * t) * p3;
15
- }
16
-
17
- function calculatePointCubic(t, p1, p2, p3, p4) {
18
- const t2 = t * t;
19
- const t3 = t2 * t;
20
- const oneMinusT = 1 - t;
21
-
22
- return p1 * Math.pow(oneMinusT, 3) + p2 * 3 * (oneMinusT * oneMinusT) * t + p3 * 3 * oneMinusT * t2 + p4 * t3;
23
- }
24
-
25
- function calculateCoordinatesLinear(points, minDistance, roundToNearest, sampleFrequency) {
26
- const pts = [];
27
- let [startX, startY] = points.splice(0, 2);
28
-
29
- for (let i = 0; i < points.length; i += 2) {
30
- let endX = points[i];
31
- let endY = points[i + 1];
32
- let t = 0;
33
- let lastX = startX;
34
- let lastY = startY;
35
- while (t <= 1.0000000000000007) {
36
- const x = calculateLinear(t, startX, endX);
37
- const y = calculateLinear(t, startY, endY);
38
-
39
- const deltaX = x - lastX;
40
- const deltaY = y - lastY;
41
- const dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
42
- if (Math.abs(dist) > minDistance) {
43
- pts.push(x - (x % roundToNearest), y - (y % roundToNearest));
44
- lastX = x;
45
- lastY = y;
46
- }
47
- t += sampleFrequency
48
- }
49
- startX = endX;
50
- startY = endY;
51
- }
52
- return pts;
53
- }
54
-
55
- function calculateCoordinatesQuad(points, minDistance, roundToNearest, sampleFrequency) {
56
- const pts = [];
57
- let [startX, startY] = points.splice(0, 2);
58
-
59
- for (let i = 0; i < points.length; i += 4) {
60
- let ctrl1x = points[i];
61
- let ctrl1y = points[i + 1];
62
- let endX = points[i + 2];
63
- let endY = points[i + 3];
64
-
65
- let t = 0;
66
- let lastX = startX;
67
- let lastY = startY;
68
- while (t <= 1.0000000000000007) {
69
- const x = calculatePointQuadratic(t, startX, ctrl1x, endX);
70
- const y = calculatePointQuadratic(t, startY, ctrl1y, endY);
71
-
72
- const deltaX = x - lastX;
73
- const deltaY = y - lastY;
74
- const dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
75
- if (Math.abs(dist) > minDistance) {
76
- pts.push(x - (x % roundToNearest), y - (y % roundToNearest));
77
- lastX = x;
78
- lastY = y;
79
- }
80
- t += sampleFrequency;
81
- }
82
- startX = endX;
83
- startY = endY;
84
- }
85
-
86
- return pts;
87
- }
88
-
89
- function calculateCoordinatesCubic(points, minDistance, roundToNearest, sampleFrequency) {
90
- const pts = [];
91
- let [startX, startY] = points.splice(0, 2);
92
-
93
- for (let i = 0; i < points.length; i += 6) {
94
- let ctrl1x = points[i];
95
- let ctrl1y = points[i + 1];
96
- let ctrl2x = points[i + 2];
97
- let ctrl2y = points[i + 3];
98
- let endX = points[i + 4];
99
- let endY = points[i + 5];
100
- let t = 0;
101
- let lastX = startX;
102
- let lastY = startY;
103
- while (t <= 1.0000000000000007) {
104
- const x = calculatePointCubic(t, startX, ctrl1x, ctrl2x, endX);
105
- const y = calculatePointCubic(t, startY, ctrl1y, ctrl2y, endY);
106
-
107
- const deltaX = x - lastX;
108
- const deltaY = y - lastY;
109
- const dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
110
- if (Math.abs(dist) > minDistance) {
111
- pts.push(x - (x % roundToNearest), y - (y % roundToNearest));
112
- lastX = x;
113
- lastY = y;
114
- }
115
- t += sampleFrequency;
116
- }
117
- startX = endX;
118
- startY = endY;
119
- }
120
-
121
- return pts;
122
- }
123
-
124
- function calculateCoordinatesSmoothCubic(points, minDistance, roundToNearest, sampleFrequency) {
125
- const pts = [];
126
- let [startX, startY, previousCtrl2x, previousCtrl2y] = points.splice(0, 4);
127
-
128
- for (let i = 0; i < points.length; i += 4) {
129
- const ctrl2x = points[i];
130
- const ctrl2y = points[i + 1];
131
- const endX = points[i + 2];
132
- const endY = points[i + 3];
133
- if (isNullOrUndefined(previousCtrl2x) || isNullOrUndefined(previousCtrl2y)) {
134
- previousCtrl2x = startX;
135
- previousCtrl2y = startY;
136
- }
137
-
138
- let svgTransform = new SVGTransform(1, 0, 0, 1, previousCtrl2x - startX, previousCtrl2y - startY).inverse();
139
- let {x:ctrl1x, y:ctrl1y} = svgTransform.map(startX, startY);
140
-
141
- const interpolatedPts = calculateCoordinatesCubic([startX, startY, ctrl1x, ctrl1y, ctrl2x, ctrl2y, endX, endY], minDistance, roundToNearest, sampleFrequency, 6);
142
- pts.push(...interpolatedPts);
143
-
144
- startX = endX;
145
- startY = endY;
146
- previousCtrl2x = ctrl2x;
147
- previousCtrl2y = ctrl2y;
148
- }
149
- return pts;
150
- }
151
-
152
- function calculateCoordinatesArc(points, minDistance, roundToNearest, sampleFrequency) {
153
- const pts = [];
154
- let [startX, startY] = points.splice(0, 2);
155
-
156
- for (let i = 0; i < points.length; i += 7) {
157
- let rx = points[i];
158
- let ry = points[i + 1];
159
- let angle = points[i + 2];
160
- let largeArc = points[i + 3];
161
- let sweep = points[i + 4];
162
- let endX = points[i + 5];
163
- let endY = points[i + 6];
164
- // If the endpoints (x1, y1) and (x2, y2) are identical, then
165
- // this is equivalent to omitting the elliptical arc segment entirely.
166
- if (startX === endX && startY === endY) {
167
- continue;
168
- }
169
- // If rx = 0 or ry = 0 then this arc is treated as a straight
170
- // line segment (a "lineto") joining the endpoints.
171
- if (rx === 0 || ry === 0) {
172
- pts.push(...calculateCoordinatesLinear([startX, startY, endX, endY], minDistance, roundToNearest, sampleFrequency));
173
- continue;
174
- }
175
- // If rx or ry have negative signs, these are dropped;
176
- // the absolute value is used instead.
177
- if (rx < 0) {
178
- rx *= -1;
179
- }
180
- if (ry < 0) {
181
- ry *= -1;
182
- }
183
-
184
- const beziers = decomposeArcToCubic({x: startX, y: startY}, angle, rx, ry, largeArc, sweep, {x: endX, y: endY});
185
- // Triplet points - start of new bezier is end of last
186
- for (let i = 0; i < beziers.length; i += 3) {
187
- const ctrlPt1 = beziers[i];
188
- const ctrlPt2 = beziers[i + 1];
189
- const endPoint = beziers[i + 2];
190
- const points = [startX, startY, ctrlPt1.x, ctrlPt1.y, ctrlPt2.x, ctrlPt2.y, endPoint.x, endPoint.y];
191
- const interpolatedPoints = calculateCoordinatesCubic(points, minDistance, roundToNearest, sampleFrequency);
192
- pts.push(...interpolatedPoints);
193
-
194
- startX = endPoint.x;
195
- startY = endPoint.y;
196
- }
197
- startX = endX;
198
- startY = endY;
199
- }
200
-
201
- return pts;
202
- }
203
-
204
- function decomposeArcToCubic(point1, rotationInDegrees, rx, ry, largeArcFlag, sweepFlag, point2) {
205
- //----------------------------
206
- // https://github.com/WebKit/webkit/blob/master/Source/WebCore/svg/SVGPathParser.cpp
207
- //----------------------------
208
- // Conversion from endpoint to center parameterization
209
- // https://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
210
- const angleRads = degToRads(rotationInDegrees);
211
- const midPointDistance = {x: (point1.x - point2.x) * .5, y: (point1.y - point2.y) * .5};
212
- const transformedMidPoint = rotatePoint(0, 0, midPointDistance.x, midPointDistance.y, -angleRads, -angleRads);
213
- const squareRx = rx * rx;
214
- const squareRy = ry * ry;
215
- const squareX = transformedMidPoint.x * transformedMidPoint.x;
216
- const squareY = transformedMidPoint.y * transformedMidPoint.y;
217
-
218
- // Check if the radii are big enough to draw the arc, scale radii if not.
219
- // http://www.w3.org/TR/SVG/implnote.html#ArcCorrectionOutOfRangeRadii
220
- const radiiScale = squareX / squareRx + squareY / squareRy;
221
- if (radiiScale > 1) {
222
- const sqrtRadiiScale = Math.sqrt(radiiScale);
223
- rx *= sqrtRadiiScale;
224
- ry *= sqrtRadiiScale;
225
- }
226
- // Apply scale
227
- point1 = {x: point1.x * (1 / rx), y: point1.y * (1 / ry)};
228
- point2 = {x: point2.x * (1 / rx), y: point2.y * (1 / ry)};
229
- // Apply rotation
230
- point1 = rotatePoint(0, 0, point1.x, point1.y, -angleRads, -angleRads);
231
- point2 = rotatePoint(0, 0, point2.x, point2.y, -angleRads, -angleRads);
232
-
233
- const delta = {x: point2.x - point1.x, y: point2.y - point1.y};
234
- const d = delta.x * delta.x + delta.y * delta.y;
235
- const scaleFactorSquared = Math.max(1 / d - 0.25, 0);
236
- let scaleFactor = Math.sqrt(scaleFactorSquared);
237
- if (sweepFlag === largeArcFlag) {
238
- scaleFactor = -scaleFactor;
239
- }
240
- delta.x *= scaleFactor;
241
- delta.y *= scaleFactor;
242
- let centerPoint = {x: (point1.x + point2.x) * .5, y: (point1.y + point2.y) * .5};
243
- // https://github.com/WebKit/webkit/blob/master/Source/WebCore/svg/SVGPathParser.cpp#L465
244
- centerPoint.x -= delta.y;
245
- centerPoint.y += delta.x;
246
-
247
- const theta1 = Math.atan2(point1.y - centerPoint.y, point1.x - centerPoint.x);
248
- const theta2 = Math.atan2(point2.y - centerPoint.y, point2.x - centerPoint.x);
249
-
250
- let thetaArc = theta2 - theta1;
251
- if (thetaArc < 0 && sweepFlag)
252
- thetaArc += 2 * Math.PI;
253
- else if (thetaArc > 0 && !sweepFlag)
254
- thetaArc -= 2 * Math.PI;
255
-
256
- // Some results of atan2 on some platform implementations are not exact enough. So that we get more
257
- // cubic curves than expected here. Adding 0.001f reduces the count of segments to the correct count.
258
- const cubicBeziers = []; // Triplet points - Assumes the start point is the end point from the previous command
259
- const segments = Math.ceil(Math.abs(thetaArc / ((Math.PI / 2) + 0.001)));
260
- for (let i = 0; i < segments; i++) {
261
- const startTheta = theta1 + i * thetaArc / segments;
262
- const endTheta = theta1 + (i + 1) * thetaArc / segments;
263
- const t = (8 / 6) * Math.tan(0.25 * (endTheta - startTheta));
264
- if (!isFinite(t)) {
265
- return cubicBeziers;
266
- }
267
- const sinStartTheta = Math.sin(startTheta);
268
- const cosStartTheta = Math.cos(startTheta);
269
- const sinEndTheta = Math.sin(endTheta);
270
- const cosEndTheta = Math.cos(endTheta);
271
-
272
- point1 = {x: cosStartTheta - t * sinStartTheta, y: sinStartTheta + t * cosStartTheta};
273
- point1.x += centerPoint.x;
274
- point1.y += centerPoint.y;
275
-
276
- let targetPoint = {x: cosEndTheta, y: sinEndTheta};
277
- targetPoint.x += centerPoint.x;
278
- targetPoint.y += centerPoint.y;
279
-
280
- point2 = Object.assign({}, targetPoint);
281
- point2.x += t * sinEndTheta;
282
- point2.y += -t * cosEndTheta;
283
-
284
- // rotate and scale
285
- point1 = rotatePoint(0, 0, point1.x, point1.y, angleRads, angleRads);
286
- point1.x *= rx;
287
- point1.y *= ry;
288
-
289
- point2 = rotatePoint(0, 0, point2.x, point2.y, angleRads, angleRads);
290
- point2.x *= rx;
291
- point2.y *= ry;
292
-
293
- targetPoint = rotatePoint(0, 0, targetPoint.x, targetPoint.y, angleRads, angleRads);
294
- targetPoint.x *= rx;
295
- targetPoint.y *= ry;
296
-
297
- cubicBeziers.push(point1, point2, targetPoint);
298
- }
299
- return cubicBeziers;
300
- }
301
-
302
- module.exports = {
303
- a: calculateCoordinatesArc,
304
- c: calculateCoordinatesCubic,
305
- h: calculateCoordinatesLinear,
306
- l: calculateCoordinatesLinear,
307
- m: calculateCoordinatesLinear,
308
- q: calculateCoordinatesQuad,
309
- s: calculateCoordinatesSmoothCubic,
310
- v: calculateCoordinatesLinear,
311
- z: calculateCoordinatesLinear
312
- };
package/src/math/utils.js DELETED
@@ -1,40 +0,0 @@
1
- function degToRads(deg){
2
- return Math.PI * deg / 180;
3
- }
4
-
5
- function radToDeg(rad){
6
- return rad * 180 / Math.PI;
7
- }
8
-
9
- function isNullOrUndefined(value){
10
- return Boolean(value === undefined || value === null);
11
- }
12
-
13
- /**
14
- * Rotates a point around the given origin
15
- * by the specified radians and returns the
16
- * rotated point.
17
- *
18
- * @param originX The x coordinate of the point to rotate around.
19
- * @param originY The y coordinate of the point to rotate around.
20
- * @param x The x coordinate of the point to be rotated.
21
- * @param y The y coordinate of the point to be rotated.
22
- * @param radiansX Radians to rotate along the x axis.
23
- * @param radiansY Radians to rotate along the y axis.
24
- *
25
- * @returns {Object} The point with the rotated coordinates.
26
- */
27
- function rotatePoint(originX, originY, x, y, radiansX, radiansY) {
28
- const v = {x: x - originX, y: y - originY};
29
- const vx = (v.x * Math.cos(radiansX)) - (v.y * Math.sin(radiansX));
30
- const vy = (v.x * Math.sin(radiansY)) + (v.y * Math.cos(radiansY));
31
- return {x: vx + originX, y: vy + originY};
32
- }
33
-
34
-
35
- module.exports = {
36
- degToRads,
37
- isNullOrUndefined,
38
- radToDeg,
39
- rotatePoint
40
- };