@remotion/paths 3.2.11
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/.prettierrc.js +14 -0
- package/LICENSE.md +7 -0
- package/README.md +25 -0
- package/dist/arc.d.ts +12 -0
- package/dist/arc.js +231 -0
- package/dist/bezier-functions.d.ts +11 -0
- package/dist/bezier-functions.js +138 -0
- package/dist/bezier-values.d.ts +3 -0
- package/dist/bezier-values.js +693 -0
- package/dist/bezier.d.ts +26 -0
- package/dist/bezier.js +81 -0
- package/dist/construct.d.ts +7 -0
- package/dist/construct.js +308 -0
- package/dist/evolve-path.d.ts +10 -0
- package/dist/evolve-path.js +17 -0
- package/dist/get-length.d.ts +6 -0
- package/dist/get-length.js +15 -0
- package/dist/get-part-at-length.d.ts +4 -0
- package/dist/get-part-at-length.js +20 -0
- package/dist/get-parts.d.ts +7 -0
- package/dist/get-parts.js +31 -0
- package/dist/get-point-at-length.d.ts +7 -0
- package/dist/get-point-at-length.js +24 -0
- package/dist/get-properties-at-length.d.ts +1 -0
- package/dist/get-properties-at-length.js +23 -0
- package/dist/get-reversed-path.d.ts +12 -0
- package/dist/get-reversed-path.js +159 -0
- package/dist/get-tangent-at-length.d.ts +7 -0
- package/dist/get-tangent-at-length.js +24 -0
- package/dist/helpers/arc.d.ts +12 -0
- package/dist/helpers/arc.js +226 -0
- package/dist/helpers/bezier-functions.d.ts +11 -0
- package/dist/helpers/bezier-functions.js +135 -0
- package/dist/helpers/bezier-values.d.ts +3 -0
- package/dist/helpers/bezier-values.js +694 -0
- package/dist/helpers/bezier.d.ts +26 -0
- package/dist/helpers/bezier.js +82 -0
- package/dist/helpers/construct.d.ts +7 -0
- package/dist/helpers/construct.js +309 -0
- package/dist/helpers/get-part-at-length.d.ts +4 -0
- package/dist/helpers/get-part-at-length.js +20 -0
- package/dist/helpers/linear.d.ts +7 -0
- package/dist/helpers/linear.js +30 -0
- package/dist/helpers/parse.d.ts +2 -0
- package/dist/helpers/parse.js +49 -0
- package/dist/helpers/split-curve.d.ts +47 -0
- package/dist/helpers/split-curve.js +190 -0
- package/dist/helpers/types.d.ts +22 -0
- package/dist/helpers/types.js +3 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +19 -0
- package/dist/interpolate-path.d.ts +8 -0
- package/dist/interpolate-path.js +366 -0
- package/dist/linear.d.ts +7 -0
- package/dist/linear.js +31 -0
- package/dist/normalize-path.d.ts +6 -0
- package/dist/normalize-path.js +300 -0
- package/dist/parse.d.ts +2 -0
- package/dist/parse.js +48 -0
- package/dist/reverse-path.d.ts +12 -0
- package/dist/reverse-path.js +148 -0
- package/dist/types.d.ts +26 -0
- package/dist/types.js +2 -0
- package/package.json +39 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* https://github.com/Pomax/svg-path-reverse
|
|
4
|
+
*
|
|
5
|
+
* This code is in the public domain, except in jurisdictions that do
|
|
6
|
+
* not recognise the public domain, where this code is MIT licensed.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.reversePath = void 0;
|
|
10
|
+
const normalize_path_1 = require("./normalize-path");
|
|
11
|
+
/**
|
|
12
|
+
* Normalise an SVG path to absolute coordinates
|
|
13
|
+
* and full commands, rather than relative coordinates
|
|
14
|
+
* and/or shortcut commands.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Reverse an SVG path.
|
|
18
|
+
* As long as the input path is normalised, this is actually really
|
|
19
|
+
* simple to do. As all pathing commands are symmetrical, meaning
|
|
20
|
+
* that they render the same when you reverse the coordinate order,
|
|
21
|
+
* the grand trick here is to reverse the path (making sure to keep
|
|
22
|
+
* coordinates ordered pairwise) and shift the operators left by
|
|
23
|
+
* one or two coordinate pairs depending on the operator:
|
|
24
|
+
*
|
|
25
|
+
* - Z is removed (after noting it existed),
|
|
26
|
+
* - L moves to 2 spots earlier (skipping one coordinate),
|
|
27
|
+
* - Q moves to 2 spots earlier (skipping one coordinate),
|
|
28
|
+
* - C moves to 4 spots earlier (skipping two coordinates)
|
|
29
|
+
* and its arguments get reversed,
|
|
30
|
+
* - the path start becomes M.
|
|
31
|
+
* - the path end becomes Z iff it was there to begin with.
|
|
32
|
+
*/
|
|
33
|
+
function reverseNormalizedPath(normalized) {
|
|
34
|
+
const terms = normalized.trim().split(' ');
|
|
35
|
+
let term;
|
|
36
|
+
const tlen = terms.length;
|
|
37
|
+
const tlen1 = tlen - 1;
|
|
38
|
+
let t;
|
|
39
|
+
const reversed = [];
|
|
40
|
+
let x;
|
|
41
|
+
let y;
|
|
42
|
+
let pair;
|
|
43
|
+
let pairs;
|
|
44
|
+
let shift;
|
|
45
|
+
const matcher = /[QAZLCM]/;
|
|
46
|
+
const closed = terms.slice(-1)[0].toUpperCase() === 'Z';
|
|
47
|
+
for (t = 0; t < tlen; t++) {
|
|
48
|
+
term = terms[t];
|
|
49
|
+
// Is this an operator? If it is, run through its
|
|
50
|
+
// argument list, which we know is fixed length.
|
|
51
|
+
if (matcher.test(term)) {
|
|
52
|
+
// Arc processing relies on not-just-coordinates
|
|
53
|
+
if (term === 'A') {
|
|
54
|
+
reversed.push(terms[t + 5] === '0' ? '1' : '0');
|
|
55
|
+
reversed.push(terms[t + 4]);
|
|
56
|
+
reversed.push(terms[t + 3]);
|
|
57
|
+
reversed.push(terms[t + 2]);
|
|
58
|
+
reversed.push(terms[t + 1]);
|
|
59
|
+
reversed.push(term);
|
|
60
|
+
reversed.push(terms[t + 7]);
|
|
61
|
+
reversed.push(terms[t + 6]);
|
|
62
|
+
t += 7;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
// how many coordinate pairs do we need to read,
|
|
66
|
+
// and by how many pairs should this operator be
|
|
67
|
+
// shifted left?
|
|
68
|
+
else if (term === 'C') {
|
|
69
|
+
pairs = 3;
|
|
70
|
+
shift = 2;
|
|
71
|
+
}
|
|
72
|
+
else if (term === 'Q') {
|
|
73
|
+
pairs = 2;
|
|
74
|
+
shift = 1;
|
|
75
|
+
}
|
|
76
|
+
else if (term === 'L') {
|
|
77
|
+
pairs = 1;
|
|
78
|
+
shift = 1;
|
|
79
|
+
}
|
|
80
|
+
else if (term === 'M') {
|
|
81
|
+
pairs = 1;
|
|
82
|
+
shift = 0;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
// do the argument reading and operator shifting
|
|
88
|
+
if (pairs === shift) {
|
|
89
|
+
reversed.push(term);
|
|
90
|
+
}
|
|
91
|
+
for (pair = 0; pair < pairs; pair++) {
|
|
92
|
+
if (pair === shift) {
|
|
93
|
+
reversed.push(term);
|
|
94
|
+
}
|
|
95
|
+
x = terms[++t];
|
|
96
|
+
y = terms[++t];
|
|
97
|
+
reversed.push(y);
|
|
98
|
+
reversed.push(x);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// the code has been set up so that every time we
|
|
102
|
+
// iterate because of the for() operation, the term
|
|
103
|
+
// we see is a pathing operator, not a number. As
|
|
104
|
+
// such, if we get to this "else" the path is malformed.
|
|
105
|
+
else {
|
|
106
|
+
const pre = terms.slice(Math.max(t - 3, 0), 3).join(' ');
|
|
107
|
+
const post = terms.slice(t + 1, Math.min(t + 4, tlen1)).join(' ');
|
|
108
|
+
const range = pre + ' [' + term + '] ' + post;
|
|
109
|
+
throw new Error('Error while trying to reverse normalized SVG path, at position ' +
|
|
110
|
+
t +
|
|
111
|
+
' (' +
|
|
112
|
+
range +
|
|
113
|
+
').\n' +
|
|
114
|
+
"Either the path is not normalised, or it's malformed.");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
reversed.push('M');
|
|
118
|
+
// generating the reversed path string involves
|
|
119
|
+
// running through our transformed terms in reverse.
|
|
120
|
+
let revstring = '';
|
|
121
|
+
const rlen1 = reversed.length - 1;
|
|
122
|
+
let r;
|
|
123
|
+
for (r = rlen1; r > 0; r--) {
|
|
124
|
+
revstring += reversed[r] + ' ';
|
|
125
|
+
}
|
|
126
|
+
if (closed)
|
|
127
|
+
revstring += 'Z';
|
|
128
|
+
revstring = revstring.replace(/M M/g, 'Z M');
|
|
129
|
+
return revstring;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* This is the function that you'll actually want to
|
|
133
|
+
* make use of, because it lets you reverse individual
|
|
134
|
+
* subpaths in some <path> "d" attribute.
|
|
135
|
+
*/
|
|
136
|
+
const reversePath = (_path, _subpath) => {
|
|
137
|
+
const subpath = _subpath !== null && _subpath !== void 0 ? _subpath : false;
|
|
138
|
+
const path = (0, normalize_path_1.normalizePath)(_path);
|
|
139
|
+
let paths = path.replace(/M/g, '|M').split('|');
|
|
140
|
+
let revpath;
|
|
141
|
+
paths.splice(0, 1);
|
|
142
|
+
if (subpath !== false && subpath >= paths.length) {
|
|
143
|
+
return path;
|
|
144
|
+
}
|
|
145
|
+
if (subpath === false) {
|
|
146
|
+
paths = paths.map((spath) => {
|
|
147
|
+
return reverseNormalizedPath(spath.trim());
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const spath = paths[subpath];
|
|
152
|
+
if (spath) {
|
|
153
|
+
revpath = reverseNormalizedPath(spath.trim());
|
|
154
|
+
paths[subpath] = revpath;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return paths.join(' ').replace(/ +/g, ' ').trim();
|
|
158
|
+
};
|
|
159
|
+
exports.reversePath = reversePath;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets tangent values x and y of a point which is on an SVG path
|
|
3
|
+
* @param {string} path A valid SVG path
|
|
4
|
+
* @param {number} length The length at which the tangent should be sampled
|
|
5
|
+
* @link https://remotion.dev/docs/paths/get-tangent-at-length
|
|
6
|
+
*/
|
|
7
|
+
export declare const getTangentAtLength: (path: string, length: number) => import("./helpers/types").Point;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTangentAtLength = void 0;
|
|
4
|
+
const construct_1 = require("./helpers/construct");
|
|
5
|
+
const get_part_at_length_1 = require("./helpers/get-part-at-length");
|
|
6
|
+
/**
|
|
7
|
+
* Gets tangent values x and y of a point which is on an SVG path
|
|
8
|
+
* @param {string} path A valid SVG path
|
|
9
|
+
* @param {number} length The length at which the tangent should be sampled
|
|
10
|
+
* @link https://remotion.dev/docs/paths/get-tangent-at-length
|
|
11
|
+
*/
|
|
12
|
+
const getTangentAtLength = (path, length) => {
|
|
13
|
+
const constructed = (0, construct_1.construct)(path);
|
|
14
|
+
const fractionPart = (0, get_part_at_length_1.getPartAtLength)(path, length);
|
|
15
|
+
const functionAtPart = constructed.functions[fractionPart.i];
|
|
16
|
+
if (functionAtPart) {
|
|
17
|
+
return functionAtPart.getTangentAtLength(fractionPart.fraction);
|
|
18
|
+
}
|
|
19
|
+
if (constructed.initial_point) {
|
|
20
|
+
return { x: 0, y: 0 };
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Wrong function at this part.');
|
|
23
|
+
};
|
|
24
|
+
exports.getTangentAtLength = getTangentAtLength;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Properties } from './types';
|
|
2
|
+
export declare const makeArc: ({ x0, y0, rx, ry, xAxisRotate, LargeArcFlag, SweepFlag, x1, y1, }: {
|
|
3
|
+
x0: number;
|
|
4
|
+
y0: number;
|
|
5
|
+
rx: number;
|
|
6
|
+
ry: number;
|
|
7
|
+
xAxisRotate: number;
|
|
8
|
+
LargeArcFlag: boolean;
|
|
9
|
+
SweepFlag: boolean;
|
|
10
|
+
x1: number;
|
|
11
|
+
y1: number;
|
|
12
|
+
}) => Properties;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copied from: https://github.com/rveciana/svg-path-properties
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.makeArc = void 0;
|
|
5
|
+
const makeArc = ({ x0, y0, rx, ry, xAxisRotate, LargeArcFlag, SweepFlag, x1, y1, }) => {
|
|
6
|
+
const lengthProperties = approximateArcLengthOfCurve(300, (t) => {
|
|
7
|
+
return pointOnEllipticalArc({
|
|
8
|
+
p0: { x: x0, y: y0 },
|
|
9
|
+
rx,
|
|
10
|
+
ry,
|
|
11
|
+
xAxisRotation: xAxisRotate,
|
|
12
|
+
largeArcFlag: LargeArcFlag,
|
|
13
|
+
sweepFlag: SweepFlag,
|
|
14
|
+
p1: { x: x1, y: y1 },
|
|
15
|
+
t,
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
const length = lengthProperties.arcLength;
|
|
19
|
+
const getTotalLength = () => {
|
|
20
|
+
return length;
|
|
21
|
+
};
|
|
22
|
+
const getPointAtLength = (fractionLength) => {
|
|
23
|
+
if (fractionLength < 0) {
|
|
24
|
+
fractionLength = 0;
|
|
25
|
+
}
|
|
26
|
+
else if (fractionLength > length) {
|
|
27
|
+
fractionLength = length;
|
|
28
|
+
}
|
|
29
|
+
const position = pointOnEllipticalArc({
|
|
30
|
+
p0: { x: x0, y: y0 },
|
|
31
|
+
rx,
|
|
32
|
+
ry,
|
|
33
|
+
xAxisRotation: xAxisRotate,
|
|
34
|
+
largeArcFlag: LargeArcFlag,
|
|
35
|
+
sweepFlag: SweepFlag,
|
|
36
|
+
p1: { x: x1, y: y1 },
|
|
37
|
+
t: fractionLength / length,
|
|
38
|
+
});
|
|
39
|
+
return { x: position.x, y: position.y };
|
|
40
|
+
};
|
|
41
|
+
const getTangentAtLength = (fractionLength) => {
|
|
42
|
+
if (fractionLength < 0) {
|
|
43
|
+
fractionLength = 0;
|
|
44
|
+
}
|
|
45
|
+
else if (fractionLength > length) {
|
|
46
|
+
fractionLength = length;
|
|
47
|
+
}
|
|
48
|
+
const point_dist = 0.05; // needs testing
|
|
49
|
+
const p1 = getPointAtLength(fractionLength);
|
|
50
|
+
let p2;
|
|
51
|
+
if (fractionLength < 0) {
|
|
52
|
+
fractionLength = 0;
|
|
53
|
+
}
|
|
54
|
+
else if (fractionLength > length) {
|
|
55
|
+
fractionLength = length;
|
|
56
|
+
}
|
|
57
|
+
if (fractionLength < length - point_dist) {
|
|
58
|
+
p2 = getPointAtLength(fractionLength + point_dist);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
p2 = getPointAtLength(fractionLength - point_dist);
|
|
62
|
+
}
|
|
63
|
+
const xDist = p2.x - p1.x;
|
|
64
|
+
const yDist = p2.y - p1.y;
|
|
65
|
+
const dist = Math.sqrt(xDist * xDist + yDist * yDist);
|
|
66
|
+
if (fractionLength < length - point_dist) {
|
|
67
|
+
return { x: -xDist / dist, y: -yDist / dist };
|
|
68
|
+
}
|
|
69
|
+
return { x: xDist / dist, y: yDist / dist };
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
getPointAtLength,
|
|
73
|
+
getTangentAtLength,
|
|
74
|
+
getTotalLength,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
exports.makeArc = makeArc;
|
|
78
|
+
const pointOnEllipticalArc = ({ p0, rx, ry, xAxisRotation, largeArcFlag, sweepFlag, p1, t, }) => {
|
|
79
|
+
// In accordance to: http://www.w3.org/TR/SVG/implnote.html#ArcOutOfRangeParameters
|
|
80
|
+
rx = Math.abs(rx);
|
|
81
|
+
ry = Math.abs(ry);
|
|
82
|
+
xAxisRotation = mod(xAxisRotation, 360);
|
|
83
|
+
const xAxisRotationRadians = toRadians(xAxisRotation);
|
|
84
|
+
// If the endpoints are identical, then this is equivalent to omitting the elliptical arc segment entirely.
|
|
85
|
+
if (p0.x === p1.x && p0.y === p1.y) {
|
|
86
|
+
return { x: p0.x, y: p0.y, ellipticalArcAngle: 0 }; // Check if angle is correct
|
|
87
|
+
}
|
|
88
|
+
// If rx = 0 or ry = 0 then this arc is treated as a straight line segment joining the endpoints.
|
|
89
|
+
if (rx === 0 || ry === 0) {
|
|
90
|
+
// return this.pointOnLine(p0, p1, t);
|
|
91
|
+
return { x: 0, y: 0, ellipticalArcAngle: 0 }; // Check if angle is correct
|
|
92
|
+
}
|
|
93
|
+
// Following "Conversion from endpoint to center parameterization"
|
|
94
|
+
// http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
|
|
95
|
+
// Step #1: Compute transformedPoint
|
|
96
|
+
const dx = (p0.x - p1.x) / 2;
|
|
97
|
+
const dy = (p0.y - p1.y) / 2;
|
|
98
|
+
const transformedPoint = {
|
|
99
|
+
x: Math.cos(xAxisRotationRadians) * dx + Math.sin(xAxisRotationRadians) * dy,
|
|
100
|
+
y: -Math.sin(xAxisRotationRadians) * dx +
|
|
101
|
+
Math.cos(xAxisRotationRadians) * dy,
|
|
102
|
+
};
|
|
103
|
+
// Ensure radii are large enough
|
|
104
|
+
const radiiCheck = transformedPoint.x ** 2 / rx ** 2 + transformedPoint.y ** 2 / ry ** 2;
|
|
105
|
+
if (radiiCheck > 1) {
|
|
106
|
+
rx *= Math.sqrt(radiiCheck);
|
|
107
|
+
ry *= Math.sqrt(radiiCheck);
|
|
108
|
+
}
|
|
109
|
+
// Step #2: Compute transformedCenter
|
|
110
|
+
const cSquareNumerator = rx ** 2 * ry ** 2 -
|
|
111
|
+
rx ** 2 * transformedPoint.y ** 2 -
|
|
112
|
+
ry ** 2 * transformedPoint.x ** 2;
|
|
113
|
+
const cSquareRootDenom = rx ** 2 * transformedPoint.y ** 2 + ry ** 2 * transformedPoint.x ** 2;
|
|
114
|
+
let cRadicand = cSquareNumerator / cSquareRootDenom;
|
|
115
|
+
// Make sure this never drops below zero because of precision
|
|
116
|
+
cRadicand = cRadicand < 0 ? 0 : cRadicand;
|
|
117
|
+
const cCoef = (largeArcFlag === sweepFlag ? -1 : 1) * Math.sqrt(cRadicand);
|
|
118
|
+
const transformedCenter = {
|
|
119
|
+
x: cCoef * ((rx * transformedPoint.y) / ry),
|
|
120
|
+
y: cCoef * (-(ry * transformedPoint.x) / rx),
|
|
121
|
+
};
|
|
122
|
+
// Step #3: Compute center
|
|
123
|
+
const center = {
|
|
124
|
+
x: Math.cos(xAxisRotationRadians) * transformedCenter.x -
|
|
125
|
+
Math.sin(xAxisRotationRadians) * transformedCenter.y +
|
|
126
|
+
(p0.x + p1.x) / 2,
|
|
127
|
+
y: Math.sin(xAxisRotationRadians) * transformedCenter.x +
|
|
128
|
+
Math.cos(xAxisRotationRadians) * transformedCenter.y +
|
|
129
|
+
(p0.y + p1.y) / 2,
|
|
130
|
+
};
|
|
131
|
+
// Step #4: Compute start/sweep angles
|
|
132
|
+
// Start angle of the elliptical arc prior to the stretch and rotate operations.
|
|
133
|
+
// Difference between the start and end angles
|
|
134
|
+
const startVector = {
|
|
135
|
+
x: (transformedPoint.x - transformedCenter.x) / rx,
|
|
136
|
+
y: (transformedPoint.y - transformedCenter.y) / ry,
|
|
137
|
+
};
|
|
138
|
+
const startAngle = angleBetween({
|
|
139
|
+
x: 1,
|
|
140
|
+
y: 0,
|
|
141
|
+
}, startVector);
|
|
142
|
+
const endVector = {
|
|
143
|
+
x: (-transformedPoint.x - transformedCenter.x) / rx,
|
|
144
|
+
y: (-transformedPoint.y - transformedCenter.y) / ry,
|
|
145
|
+
};
|
|
146
|
+
let sweepAngle = angleBetween(startVector, endVector);
|
|
147
|
+
if (!sweepFlag && sweepAngle > 0) {
|
|
148
|
+
sweepAngle -= 2 * Math.PI;
|
|
149
|
+
}
|
|
150
|
+
else if (sweepFlag && sweepAngle < 0) {
|
|
151
|
+
sweepAngle += 2 * Math.PI;
|
|
152
|
+
}
|
|
153
|
+
// We use % instead of `mod(..)` because we want it to be -360deg to 360deg(but actually in radians)
|
|
154
|
+
sweepAngle %= 2 * Math.PI;
|
|
155
|
+
// From http://www.w3.org/TR/SVG/implnote.html#ArcParameterizationAlternatives
|
|
156
|
+
const angle = startAngle + sweepAngle * t;
|
|
157
|
+
const ellipseComponentX = rx * Math.cos(angle);
|
|
158
|
+
const ellipseComponentY = ry * Math.sin(angle);
|
|
159
|
+
const point = {
|
|
160
|
+
x: Math.cos(xAxisRotationRadians) * ellipseComponentX -
|
|
161
|
+
Math.sin(xAxisRotationRadians) * ellipseComponentY +
|
|
162
|
+
center.x,
|
|
163
|
+
y: Math.sin(xAxisRotationRadians) * ellipseComponentX +
|
|
164
|
+
Math.cos(xAxisRotationRadians) * ellipseComponentY +
|
|
165
|
+
center.y,
|
|
166
|
+
ellipticalArcStartAngle: startAngle,
|
|
167
|
+
ellipticalArcEndAngle: startAngle + sweepAngle,
|
|
168
|
+
ellipticalArcAngle: angle,
|
|
169
|
+
ellipticalArcCenter: center,
|
|
170
|
+
resultantRx: rx,
|
|
171
|
+
resultantRy: ry,
|
|
172
|
+
};
|
|
173
|
+
return point;
|
|
174
|
+
};
|
|
175
|
+
const approximateArcLengthOfCurve = (resolution, pointOnCurveFunc) => {
|
|
176
|
+
// Resolution is the number of segments we use
|
|
177
|
+
resolution = resolution ? resolution : 500;
|
|
178
|
+
let resultantArcLength = 0;
|
|
179
|
+
const arcLengthMap = [];
|
|
180
|
+
const approximationLines = [];
|
|
181
|
+
let prevPoint = pointOnCurveFunc(0);
|
|
182
|
+
let nextPoint;
|
|
183
|
+
for (let i = 0; i < resolution; i++) {
|
|
184
|
+
const t = clamp(i * (1 / resolution), 0, 1);
|
|
185
|
+
nextPoint = pointOnCurveFunc(t);
|
|
186
|
+
resultantArcLength += distance(prevPoint, nextPoint);
|
|
187
|
+
approximationLines.push([prevPoint, nextPoint]);
|
|
188
|
+
arcLengthMap.push({
|
|
189
|
+
t,
|
|
190
|
+
arcLength: resultantArcLength,
|
|
191
|
+
});
|
|
192
|
+
prevPoint = nextPoint;
|
|
193
|
+
}
|
|
194
|
+
// Last stretch to the endpoint
|
|
195
|
+
nextPoint = pointOnCurveFunc(1);
|
|
196
|
+
approximationLines.push([prevPoint, nextPoint]);
|
|
197
|
+
resultantArcLength += distance(prevPoint, nextPoint);
|
|
198
|
+
arcLengthMap.push({
|
|
199
|
+
t: 1,
|
|
200
|
+
arcLength: resultantArcLength,
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
arcLength: resultantArcLength,
|
|
204
|
+
arcLengthMap,
|
|
205
|
+
approximationLines,
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
const mod = (x, m) => {
|
|
209
|
+
return ((x % m) + m) % m;
|
|
210
|
+
};
|
|
211
|
+
const toRadians = (angle) => {
|
|
212
|
+
return angle * (Math.PI / 180);
|
|
213
|
+
};
|
|
214
|
+
const distance = (p0, p1) => {
|
|
215
|
+
return Math.sqrt((p1.x - p0.x) ** 2 + (p1.y - p0.y) ** 2);
|
|
216
|
+
};
|
|
217
|
+
const clamp = (val, min, max) => {
|
|
218
|
+
return Math.min(Math.max(val, min), max);
|
|
219
|
+
};
|
|
220
|
+
const angleBetween = (v0, v1) => {
|
|
221
|
+
const p = v0.x * v1.x + v0.y * v1.y;
|
|
222
|
+
const n = Math.sqrt((v0.x ** 2 + v0.y ** 2) * (v1.x ** 2 + v1.y ** 2));
|
|
223
|
+
const sign = v0.x * v1.y - v0.y * v1.x < 0 ? -1 : 1;
|
|
224
|
+
const angle = sign * Math.acos(p / n);
|
|
225
|
+
return angle;
|
|
226
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Point } from './types';
|
|
2
|
+
export declare const cubicPoint: (xs: number[], ys: number[], t: number) => Point;
|
|
3
|
+
export declare const cubicDerivative: (xs: number[], ys: number[], t: number) => Point;
|
|
4
|
+
export declare const getCubicArcLength: (xs: number[], ys: number[], t: number) => number;
|
|
5
|
+
export declare const quadraticPoint: (xs: number[], ys: number[], t: number) => Point;
|
|
6
|
+
export declare const getQuadraticArcLength: (xs: number[], ys: number[], t: number) => number;
|
|
7
|
+
export declare const quadraticDerivative: (xs: number[], ys: number[], t: number) => {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
};
|
|
11
|
+
export declare const t2length: (length: number, totalLength: number, func: (t: number) => number) => number;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.t2length = exports.quadraticDerivative = exports.getQuadraticArcLength = exports.quadraticPoint = exports.getCubicArcLength = exports.cubicDerivative = exports.cubicPoint = void 0;
|
|
4
|
+
const bezier_values_1 = require("./bezier-values");
|
|
5
|
+
const cubicPoint = (xs, ys, t) => {
|
|
6
|
+
const x = (1 - t) * (1 - t) * (1 - t) * xs[0] +
|
|
7
|
+
3 * (1 - t) * (1 - t) * t * xs[1] +
|
|
8
|
+
3 * (1 - t) * t * t * xs[2] +
|
|
9
|
+
t * t * t * xs[3];
|
|
10
|
+
const y = (1 - t) * (1 - t) * (1 - t) * ys[0] +
|
|
11
|
+
3 * (1 - t) * (1 - t) * t * ys[1] +
|
|
12
|
+
3 * (1 - t) * t * t * ys[2] +
|
|
13
|
+
t * t * t * ys[3];
|
|
14
|
+
return { x, y };
|
|
15
|
+
};
|
|
16
|
+
exports.cubicPoint = cubicPoint;
|
|
17
|
+
const cubicDerivative = (xs, ys, t) => {
|
|
18
|
+
const derivative = (0, exports.quadraticPoint)([3 * (xs[1] - xs[0]), 3 * (xs[2] - xs[1]), 3 * (xs[3] - xs[2])], [3 * (ys[1] - ys[0]), 3 * (ys[2] - ys[1]), 3 * (ys[3] - ys[2])], t);
|
|
19
|
+
return derivative;
|
|
20
|
+
};
|
|
21
|
+
exports.cubicDerivative = cubicDerivative;
|
|
22
|
+
const getCubicArcLength = (xs, ys, t) => {
|
|
23
|
+
let correctedT;
|
|
24
|
+
const n = 20;
|
|
25
|
+
const z = t / 2;
|
|
26
|
+
let sum = 0;
|
|
27
|
+
for (let i = 0; i < n; i++) {
|
|
28
|
+
correctedT = z * bezier_values_1.tValues[n][i] + z;
|
|
29
|
+
sum += bezier_values_1.cValues[n][i] * bFunc(xs, ys, correctedT);
|
|
30
|
+
}
|
|
31
|
+
return z * sum;
|
|
32
|
+
};
|
|
33
|
+
exports.getCubicArcLength = getCubicArcLength;
|
|
34
|
+
const quadraticPoint = (xs, ys, t) => {
|
|
35
|
+
const x = (1 - t) * (1 - t) * xs[0] + 2 * (1 - t) * t * xs[1] + t * t * xs[2];
|
|
36
|
+
const y = (1 - t) * (1 - t) * ys[0] + 2 * (1 - t) * t * ys[1] + t * t * ys[2];
|
|
37
|
+
return { x, y };
|
|
38
|
+
};
|
|
39
|
+
exports.quadraticPoint = quadraticPoint;
|
|
40
|
+
const getQuadraticArcLength = (xs, ys, t) => {
|
|
41
|
+
if (t === undefined) {
|
|
42
|
+
t = 1;
|
|
43
|
+
}
|
|
44
|
+
const ax = xs[0] - 2 * xs[1] + xs[2];
|
|
45
|
+
const ay = ys[0] - 2 * ys[1] + ys[2];
|
|
46
|
+
const bx = 2 * xs[1] - 2 * xs[0];
|
|
47
|
+
const by = 2 * ys[1] - 2 * ys[0];
|
|
48
|
+
const A = 4 * (ax * ax + ay * ay);
|
|
49
|
+
const B = 4 * (ax * bx + ay * by);
|
|
50
|
+
const C = bx * bx + by * by;
|
|
51
|
+
if (A === 0) {
|
|
52
|
+
return t * Math.sqrt((xs[2] - xs[0]) ** 2 + (ys[2] - ys[0]) ** 2);
|
|
53
|
+
}
|
|
54
|
+
const b = B / (2 * A);
|
|
55
|
+
const c = C / A;
|
|
56
|
+
const u = t + b;
|
|
57
|
+
const k = c - b * b;
|
|
58
|
+
const uuk = u * u + k > 0 ? Math.sqrt(u * u + k) : 0;
|
|
59
|
+
const bbk = b * b + k > 0 ? Math.sqrt(b * b + k) : 0;
|
|
60
|
+
const term = b + Math.sqrt(b * b + k) === 0
|
|
61
|
+
? 0
|
|
62
|
+
: k * Math.log(Math.abs((u + uuk) / (b + bbk)));
|
|
63
|
+
return (Math.sqrt(A) / 2) * (u * uuk - b * bbk + term);
|
|
64
|
+
};
|
|
65
|
+
exports.getQuadraticArcLength = getQuadraticArcLength;
|
|
66
|
+
const quadraticDerivative = (xs, ys, t) => {
|
|
67
|
+
return {
|
|
68
|
+
x: (1 - t) * 2 * (xs[1] - xs[0]) + t * 2 * (xs[2] - xs[1]),
|
|
69
|
+
y: (1 - t) * 2 * (ys[1] - ys[0]) + t * 2 * (ys[2] - ys[1]),
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
exports.quadraticDerivative = quadraticDerivative;
|
|
73
|
+
function bFunc(xs, ys, t) {
|
|
74
|
+
const xbase = getDerivative(1, t, xs);
|
|
75
|
+
const ybase = getDerivative(1, t, ys);
|
|
76
|
+
const combined = xbase * xbase + ybase * ybase;
|
|
77
|
+
return Math.sqrt(combined);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Compute the curve derivative (hodograph) at t.
|
|
81
|
+
*/
|
|
82
|
+
const getDerivative = (derivative, t, vs) => {
|
|
83
|
+
// the derivative of any 't'-less function is zero.
|
|
84
|
+
const n = vs.length - 1;
|
|
85
|
+
let value;
|
|
86
|
+
if (n === 0) {
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
// direct values? compute!
|
|
90
|
+
if (derivative === 0) {
|
|
91
|
+
value = 0;
|
|
92
|
+
for (let k = 0; k <= n; k++) {
|
|
93
|
+
value += bezier_values_1.binomialCoefficients[n][k] * (1 - t) ** (n - k) * t ** k * vs[k];
|
|
94
|
+
}
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
// Still some derivative? go down one order, then try
|
|
98
|
+
// for the lower order curve's.
|
|
99
|
+
const _vs = new Array(n);
|
|
100
|
+
for (let k = 0; k < n; k++) {
|
|
101
|
+
_vs[k] = n * (vs[k + 1] - vs[k]);
|
|
102
|
+
}
|
|
103
|
+
return getDerivative(derivative - 1, t, _vs);
|
|
104
|
+
};
|
|
105
|
+
const t2length = (length, totalLength, func) => {
|
|
106
|
+
let error = 1;
|
|
107
|
+
let t = length / totalLength;
|
|
108
|
+
let step = (length - func(t)) / totalLength;
|
|
109
|
+
let numIterations = 0;
|
|
110
|
+
while (error > 0.001) {
|
|
111
|
+
const increasedTLength = func(t + step);
|
|
112
|
+
const increasedTError = Math.abs(length - increasedTLength) / totalLength;
|
|
113
|
+
if (increasedTError < error) {
|
|
114
|
+
error = increasedTError;
|
|
115
|
+
t += step;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const decreasedTLength = func(t - step);
|
|
119
|
+
const decreasedTError = Math.abs(length - decreasedTLength) / totalLength;
|
|
120
|
+
if (decreasedTError < error) {
|
|
121
|
+
error = decreasedTError;
|
|
122
|
+
t -= step;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
step /= 2;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
numIterations++;
|
|
129
|
+
if (numIterations > 500) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return t;
|
|
134
|
+
};
|
|
135
|
+
exports.t2length = t2length;
|