@remotion/paths 4.0.0-esm.17 → 4.0.0-newpathfunctions.13
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/dist/get-bounding-box.d.ts +2 -0
- package/dist/get-bounding-box.js +164 -0
- package/dist/helpers/types.d.ts +6 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +5 -1
- package/dist/reset-path.d.ts +1 -0
- package/dist/reset-path.js +10 -0
- package/dist/translate-path.js +1 -1
- package/dist/unarc.d.ts +2 -0
- package/dist/unarc.js +210 -0
- package/dist/unshort.d.ts +2 -0
- package/dist/unshort.js +119 -0
- package/package.json +2 -2
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBoundingBox = void 0;
|
|
4
|
+
const normalize_path_1 = require("./normalize-path");
|
|
5
|
+
const unarc_1 = require("./unarc");
|
|
6
|
+
const unshort_1 = require("./unshort");
|
|
7
|
+
// Precision for consider cubic polynom as quadratic one
|
|
8
|
+
const CBEZIER_MINMAX_EPSILON = 0.00000001;
|
|
9
|
+
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89
|
|
10
|
+
function minmaxQ(A) {
|
|
11
|
+
const min = Math.min(A[0], A[2]);
|
|
12
|
+
const max = Math.max(A[0], A[2]);
|
|
13
|
+
if (A[1] > A[0] ? A[2] >= A[1] : A[2] <= A[1]) {
|
|
14
|
+
// if no extremum in ]0,1[
|
|
15
|
+
return [min, max];
|
|
16
|
+
}
|
|
17
|
+
// check if the extremum E is min or max
|
|
18
|
+
const E = (A[0] * A[2] - A[1] * A[1]) / (A[0] - 2 * A[1] + A[2]);
|
|
19
|
+
return E < min ? [E, max] : [min, E];
|
|
20
|
+
}
|
|
21
|
+
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
|
|
22
|
+
function minmaxC(A) {
|
|
23
|
+
const K = A[0] - 3 * A[1] + 3 * A[2] - A[3];
|
|
24
|
+
// if the polynomial is (almost) quadratic and not cubic
|
|
25
|
+
if (Math.abs(K) < CBEZIER_MINMAX_EPSILON) {
|
|
26
|
+
if (A[0] === A[3] && A[0] === A[1]) {
|
|
27
|
+
// no curve, point targeting same location
|
|
28
|
+
return [A[0], A[3]];
|
|
29
|
+
}
|
|
30
|
+
return minmaxQ([
|
|
31
|
+
A[0],
|
|
32
|
+
-0.5 * A[0] + 1.5 * A[1],
|
|
33
|
+
A[0] - 3 * A[1] + 3 * A[2],
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
// the reduced discriminant of the derivative
|
|
37
|
+
const T = -A[0] * A[2] +
|
|
38
|
+
A[0] * A[3] -
|
|
39
|
+
A[1] * A[2] -
|
|
40
|
+
A[1] * A[3] +
|
|
41
|
+
A[1] * A[1] +
|
|
42
|
+
A[2] * A[2];
|
|
43
|
+
// if the polynomial is monotone in [0,1]
|
|
44
|
+
if (T <= 0) {
|
|
45
|
+
return [Math.min(A[0], A[3]), Math.max(A[0], A[3])];
|
|
46
|
+
}
|
|
47
|
+
const S = Math.sqrt(T);
|
|
48
|
+
// potential extrema
|
|
49
|
+
let min = Math.min(A[0], A[3]);
|
|
50
|
+
let max = Math.max(A[0], A[3]);
|
|
51
|
+
const L = A[0] - 2 * A[1] + A[2];
|
|
52
|
+
// check local extrema
|
|
53
|
+
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) {
|
|
54
|
+
if (R > 0 && R < 1) {
|
|
55
|
+
// if the extrema is for R in [0,1]
|
|
56
|
+
const Q = A[0] * (1 - R) * (1 - R) * (1 - R) +
|
|
57
|
+
A[1] * 3 * (1 - R) * (1 - R) * R +
|
|
58
|
+
A[2] * 3 * (1 - R) * R * R +
|
|
59
|
+
A[3] * R * R * R;
|
|
60
|
+
if (Q < min) {
|
|
61
|
+
min = Q;
|
|
62
|
+
}
|
|
63
|
+
if (Q > max) {
|
|
64
|
+
max = Q;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return [min, max];
|
|
69
|
+
}
|
|
70
|
+
const getBoundingBox = (d) => {
|
|
71
|
+
let minX = Infinity;
|
|
72
|
+
let minY = Infinity;
|
|
73
|
+
let maxX = -Infinity;
|
|
74
|
+
let maxY = -Infinity;
|
|
75
|
+
const abs = (0, normalize_path_1.normalizePath)(d);
|
|
76
|
+
const unarced = (0, unarc_1.unarc)(abs);
|
|
77
|
+
const unshortened = (0, unshort_1.unshort)(unarced);
|
|
78
|
+
let x = 0;
|
|
79
|
+
let y = 0;
|
|
80
|
+
for (const seg of unshortened) {
|
|
81
|
+
switch (seg[0]) {
|
|
82
|
+
case 'M':
|
|
83
|
+
case 'L': {
|
|
84
|
+
if (minX > seg[1]) {
|
|
85
|
+
minX = seg[1];
|
|
86
|
+
}
|
|
87
|
+
if (minY > seg[2]) {
|
|
88
|
+
minY = seg[2];
|
|
89
|
+
}
|
|
90
|
+
if (maxX < seg[1]) {
|
|
91
|
+
maxX = seg[1];
|
|
92
|
+
}
|
|
93
|
+
if (maxY < seg[2]) {
|
|
94
|
+
maxY = seg[2];
|
|
95
|
+
}
|
|
96
|
+
x = seg[1];
|
|
97
|
+
y = seg[2];
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case 'V': {
|
|
101
|
+
if (minY > seg[1]) {
|
|
102
|
+
minY = seg[1];
|
|
103
|
+
}
|
|
104
|
+
if (maxY < seg[1]) {
|
|
105
|
+
maxY = seg[1];
|
|
106
|
+
}
|
|
107
|
+
y = seg[1];
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case 'H': {
|
|
111
|
+
if (minX > seg[1]) {
|
|
112
|
+
minX = seg[1];
|
|
113
|
+
}
|
|
114
|
+
if (maxX < seg[1]) {
|
|
115
|
+
maxX = seg[1];
|
|
116
|
+
}
|
|
117
|
+
x = seg[1];
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'C': {
|
|
121
|
+
const cxMinMax = minmaxC([x, seg[1], seg[3], seg[5]]);
|
|
122
|
+
if (minX > cxMinMax[0]) {
|
|
123
|
+
minX = cxMinMax[0];
|
|
124
|
+
}
|
|
125
|
+
if (maxX < cxMinMax[1]) {
|
|
126
|
+
maxX = cxMinMax[1];
|
|
127
|
+
}
|
|
128
|
+
const cyMinMax = minmaxC([y, seg[2], seg[4], seg[6]]);
|
|
129
|
+
if (minY > cyMinMax[0]) {
|
|
130
|
+
minY = cyMinMax[0];
|
|
131
|
+
}
|
|
132
|
+
if (maxY < cyMinMax[1]) {
|
|
133
|
+
maxY = cyMinMax[1];
|
|
134
|
+
}
|
|
135
|
+
x = seg[5];
|
|
136
|
+
y = seg[6];
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case 'Q': {
|
|
140
|
+
const qxMinMax = minmaxQ([x, seg[1], seg[3]]);
|
|
141
|
+
if (minX > qxMinMax[0]) {
|
|
142
|
+
minX = qxMinMax[0];
|
|
143
|
+
}
|
|
144
|
+
if (maxX < qxMinMax[1]) {
|
|
145
|
+
maxX = qxMinMax[1];
|
|
146
|
+
}
|
|
147
|
+
const qyMinMax = minmaxQ([y, seg[2], seg[4]]);
|
|
148
|
+
if (minY > qyMinMax[0]) {
|
|
149
|
+
minY = qyMinMax[0];
|
|
150
|
+
}
|
|
151
|
+
if (maxY < qyMinMax[1]) {
|
|
152
|
+
maxY = qyMinMax[1];
|
|
153
|
+
}
|
|
154
|
+
x = seg[3];
|
|
155
|
+
y = seg[4];
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
default:
|
|
159
|
+
throw new Error(`Unknown instruction ${seg[0]}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return { x1: minX, y1: minY, x2: maxX, y2: maxY };
|
|
163
|
+
};
|
|
164
|
+
exports.getBoundingBox = getBoundingBox;
|
package/dist/helpers/types.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
export { evolvePath } from './evolve-path';
|
|
2
2
|
export { extendViewBox } from './extend-viewbox';
|
|
3
|
+
export { getBoundingBox } from './get-bounding-box';
|
|
3
4
|
export { getLength } from './get-length';
|
|
4
5
|
export { getParts } from './get-parts';
|
|
5
6
|
export { getPointAtLength } from './get-point-at-length';
|
|
6
7
|
export { getSubpaths } from './get-subpaths';
|
|
7
8
|
export { getTangentAtLength } from './get-tangent-at-length';
|
|
8
|
-
export { Part } from './helpers/types';
|
|
9
|
+
export { BoundingBox, Part } from './helpers/types';
|
|
9
10
|
export { interpolatePath } from './interpolate-path';
|
|
10
11
|
export { normalizePath } from './normalize-path';
|
|
12
|
+
export { resetPath } from './reset-path';
|
|
11
13
|
export { reversePath } from './reverse-path';
|
|
12
14
|
export { translatePath } from './translate-path';
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.translatePath = exports.reversePath = exports.normalizePath = exports.interpolatePath = exports.getTangentAtLength = exports.getSubpaths = exports.getPointAtLength = exports.getParts = exports.getLength = exports.extendViewBox = exports.evolvePath = void 0;
|
|
3
|
+
exports.translatePath = exports.reversePath = exports.resetPath = exports.normalizePath = exports.interpolatePath = exports.getTangentAtLength = exports.getSubpaths = exports.getPointAtLength = exports.getParts = exports.getLength = exports.getBoundingBox = exports.extendViewBox = exports.evolvePath = void 0;
|
|
4
4
|
var evolve_path_1 = require("./evolve-path");
|
|
5
5
|
Object.defineProperty(exports, "evolvePath", { enumerable: true, get: function () { return evolve_path_1.evolvePath; } });
|
|
6
6
|
var extend_viewbox_1 = require("./extend-viewbox");
|
|
7
7
|
Object.defineProperty(exports, "extendViewBox", { enumerable: true, get: function () { return extend_viewbox_1.extendViewBox; } });
|
|
8
|
+
var get_bounding_box_1 = require("./get-bounding-box");
|
|
9
|
+
Object.defineProperty(exports, "getBoundingBox", { enumerable: true, get: function () { return get_bounding_box_1.getBoundingBox; } });
|
|
8
10
|
var get_length_1 = require("./get-length");
|
|
9
11
|
Object.defineProperty(exports, "getLength", { enumerable: true, get: function () { return get_length_1.getLength; } });
|
|
10
12
|
var get_parts_1 = require("./get-parts");
|
|
@@ -19,6 +21,8 @@ var interpolate_path_1 = require("./interpolate-path");
|
|
|
19
21
|
Object.defineProperty(exports, "interpolatePath", { enumerable: true, get: function () { return interpolate_path_1.interpolatePath; } });
|
|
20
22
|
var normalize_path_1 = require("./normalize-path");
|
|
21
23
|
Object.defineProperty(exports, "normalizePath", { enumerable: true, get: function () { return normalize_path_1.normalizePath; } });
|
|
24
|
+
var reset_path_1 = require("./reset-path");
|
|
25
|
+
Object.defineProperty(exports, "resetPath", { enumerable: true, get: function () { return reset_path_1.resetPath; } });
|
|
22
26
|
var reverse_path_1 = require("./reverse-path");
|
|
23
27
|
Object.defineProperty(exports, "reversePath", { enumerable: true, get: function () { return reverse_path_1.reversePath; } });
|
|
24
28
|
var translate_path_1 = require("./translate-path");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const resetPath: (d: string) => string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resetPath = void 0;
|
|
4
|
+
const get_bounding_box_1 = require("./get-bounding-box");
|
|
5
|
+
const translate_path_1 = require("./translate-path");
|
|
6
|
+
const resetPath = (d) => {
|
|
7
|
+
const box = (0, get_bounding_box_1.getBoundingBox)(d);
|
|
8
|
+
return (0, translate_path_1.translatePath)(d, -box.x1, -box.y1);
|
|
9
|
+
};
|
|
10
|
+
exports.resetPath = resetPath;
|
package/dist/translate-path.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.translatePath = void 0;
|
|
|
4
4
|
const parse_1 = require("./helpers/parse");
|
|
5
5
|
const serialize = (path) => {
|
|
6
6
|
return path.reduce((str, seg) => {
|
|
7
|
-
return (str + ' ' + seg[0] + ' ' + seg.slice(1).join('
|
|
7
|
+
return (str + ' ' + seg[0] + ' ' + seg.slice(1).join(' ')).trim();
|
|
8
8
|
}, '');
|
|
9
9
|
};
|
|
10
10
|
const translateSegments = (path, x, y) => {
|
package/dist/unarc.d.ts
ADDED
package/dist/unarc.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unarc = void 0;
|
|
4
|
+
const parse_1 = require("./helpers/parse");
|
|
5
|
+
const TAU = Math.PI * 2;
|
|
6
|
+
function approximate_unit_arc(theta1, delta_theta) {
|
|
7
|
+
const alpha = (4 / 3) * Math.tan(delta_theta / 4);
|
|
8
|
+
const x1 = Math.cos(theta1);
|
|
9
|
+
const y1 = Math.sin(theta1);
|
|
10
|
+
const x2 = Math.cos(theta1 + delta_theta);
|
|
11
|
+
const y2 = Math.sin(theta1 + delta_theta);
|
|
12
|
+
return [
|
|
13
|
+
x1,
|
|
14
|
+
y1,
|
|
15
|
+
x1 - y1 * alpha,
|
|
16
|
+
y1 + x1 * alpha,
|
|
17
|
+
x2 + y2 * alpha,
|
|
18
|
+
y2 - x2 * alpha,
|
|
19
|
+
x2,
|
|
20
|
+
y2,
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
function a2c({ x1, y1, x2, y2, fa, fs, rx, ry, phi, }) {
|
|
24
|
+
const sin_phi = Math.sin((phi * TAU) / 360);
|
|
25
|
+
const cos_phi = Math.cos((phi * TAU) / 360);
|
|
26
|
+
// Make sure radii are valid
|
|
27
|
+
//
|
|
28
|
+
const x1p = (cos_phi * (x1 - x2)) / 2 + (sin_phi * (y1 - y2)) / 2;
|
|
29
|
+
const y1p = (-sin_phi * (x1 - x2)) / 2 + (cos_phi * (y1 - y2)) / 2;
|
|
30
|
+
if (x1p === 0 && y1p === 0) {
|
|
31
|
+
// we're asked to draw line to itself
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
if (rx === 0 || ry === 0) {
|
|
35
|
+
// one of the radii is zero
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
// Compensate out-of-range radii
|
|
39
|
+
//
|
|
40
|
+
rx = Math.abs(rx);
|
|
41
|
+
ry = Math.abs(ry);
|
|
42
|
+
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
43
|
+
if (lambda > 1) {
|
|
44
|
+
rx *= Math.sqrt(lambda);
|
|
45
|
+
ry *= Math.sqrt(lambda);
|
|
46
|
+
}
|
|
47
|
+
// Get center parameters (cx, cy, theta1, delta_theta)
|
|
48
|
+
//
|
|
49
|
+
const cc = get_arc_center({ x1, y1, x2, y2, fa, fs, rx, ry, sin_phi, cos_phi });
|
|
50
|
+
const result = [];
|
|
51
|
+
let theta1 = cc[2];
|
|
52
|
+
let delta_theta = cc[3];
|
|
53
|
+
// Split an arc to multiple segments, so each segment
|
|
54
|
+
// will be less than τ/4 (= 90°)
|
|
55
|
+
//
|
|
56
|
+
const segments = Math.max(Math.ceil(Math.abs(delta_theta) / (TAU / 4)), 1);
|
|
57
|
+
delta_theta /= segments;
|
|
58
|
+
for (let i = 0; i < segments; i++) {
|
|
59
|
+
result.push(approximate_unit_arc(theta1, delta_theta));
|
|
60
|
+
theta1 += delta_theta;
|
|
61
|
+
}
|
|
62
|
+
// We have a bezier approximation of a unit circle,
|
|
63
|
+
// now need to transform back to the original ellipse
|
|
64
|
+
//
|
|
65
|
+
return result.map((curve) => {
|
|
66
|
+
for (let i = 0; i < curve.length; i += 2) {
|
|
67
|
+
let x = curve[i + 0];
|
|
68
|
+
let y = curve[i + 1];
|
|
69
|
+
// scale
|
|
70
|
+
x *= rx;
|
|
71
|
+
y *= ry;
|
|
72
|
+
// rotate
|
|
73
|
+
const xp = cos_phi * x - sin_phi * y;
|
|
74
|
+
const yp = sin_phi * x + cos_phi * y;
|
|
75
|
+
// translate
|
|
76
|
+
curve[i + 0] = xp + cc[0];
|
|
77
|
+
curve[i + 1] = yp + cc[1];
|
|
78
|
+
}
|
|
79
|
+
return curve;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const unarc = (d) => {
|
|
83
|
+
const segments = (0, parse_1.parsePath)(d);
|
|
84
|
+
let x = 0;
|
|
85
|
+
let y = 0;
|
|
86
|
+
const newSegments = segments.map((s) => {
|
|
87
|
+
switch (s[0]) {
|
|
88
|
+
case 'M':
|
|
89
|
+
case 'L': {
|
|
90
|
+
x = s[1];
|
|
91
|
+
y = s[2];
|
|
92
|
+
return [s];
|
|
93
|
+
}
|
|
94
|
+
case 'A': {
|
|
95
|
+
const nextX = s[6];
|
|
96
|
+
const nextY = s[7];
|
|
97
|
+
const new_segments = a2c({
|
|
98
|
+
x1: x,
|
|
99
|
+
y1: y,
|
|
100
|
+
x2: nextX,
|
|
101
|
+
y2: nextY,
|
|
102
|
+
fa: s[4],
|
|
103
|
+
fs: s[5],
|
|
104
|
+
rx: s[1],
|
|
105
|
+
ry: s[2],
|
|
106
|
+
phi: s[3],
|
|
107
|
+
});
|
|
108
|
+
// Degenerated arcs can be ignored by renderer, but should not be dropped
|
|
109
|
+
// to avoid collisions with `S A S` and so on. Replace with empty line.
|
|
110
|
+
if (new_segments.length === 0) {
|
|
111
|
+
return [['L', s[6], s[7]]];
|
|
112
|
+
}
|
|
113
|
+
const result = [];
|
|
114
|
+
new_segments.forEach((_s) => {
|
|
115
|
+
result.push(['C', _s[2], _s[3], _s[4], _s[5], _s[6], _s[7]]);
|
|
116
|
+
});
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
case 'V': {
|
|
120
|
+
y = s[1];
|
|
121
|
+
return [s];
|
|
122
|
+
}
|
|
123
|
+
case 'H': {
|
|
124
|
+
x = s[1];
|
|
125
|
+
return [s];
|
|
126
|
+
}
|
|
127
|
+
case 'C': {
|
|
128
|
+
x = s[5];
|
|
129
|
+
y = s[6];
|
|
130
|
+
return [s];
|
|
131
|
+
}
|
|
132
|
+
case 'Q': {
|
|
133
|
+
x = s[3];
|
|
134
|
+
y = s[4];
|
|
135
|
+
return [s];
|
|
136
|
+
}
|
|
137
|
+
default:
|
|
138
|
+
throw new Error(`Unexpected instruction ${s[0]}`);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
const flatted = newSegments.flat(1);
|
|
142
|
+
return flatted;
|
|
143
|
+
};
|
|
144
|
+
exports.unarc = unarc;
|
|
145
|
+
function get_arc_center({ x1, y1, x2, y2, fa, fs, rx, ry, sin_phi, cos_phi, }) {
|
|
146
|
+
// Step 1.
|
|
147
|
+
//
|
|
148
|
+
// Moving an ellipse so origin will be the middlepoint between our two
|
|
149
|
+
// points. After that, rotate it to line up ellipse axes with coordinate
|
|
150
|
+
// axes.
|
|
151
|
+
//
|
|
152
|
+
const x1p = (cos_phi * (x1 - x2)) / 2 + (sin_phi * (y1 - y2)) / 2;
|
|
153
|
+
const y1p = (-sin_phi * (x1 - x2)) / 2 + (cos_phi * (y1 - y2)) / 2;
|
|
154
|
+
const rx_sq = rx * rx;
|
|
155
|
+
const ry_sq = ry * ry;
|
|
156
|
+
const x1p_sq = x1p * x1p;
|
|
157
|
+
const y1p_sq = y1p * y1p;
|
|
158
|
+
// Step 2.
|
|
159
|
+
//
|
|
160
|
+
// Compute coordinates of the centre of this ellipse (cx', cy')
|
|
161
|
+
// in the new coordinate system.
|
|
162
|
+
//
|
|
163
|
+
let radicant = rx_sq * ry_sq - rx_sq * y1p_sq - ry_sq * x1p_sq;
|
|
164
|
+
if (radicant < 0) {
|
|
165
|
+
// due to rounding errors it might be e.g. -1.3877787807814457e-17
|
|
166
|
+
radicant = 0;
|
|
167
|
+
}
|
|
168
|
+
radicant /= rx_sq * y1p_sq + ry_sq * x1p_sq;
|
|
169
|
+
radicant = Math.sqrt(radicant) * (fa === fs ? -1 : 1);
|
|
170
|
+
const cxp = ((radicant * rx) / ry) * y1p;
|
|
171
|
+
const cyp = ((radicant * -ry) / rx) * x1p;
|
|
172
|
+
// Step 3.
|
|
173
|
+
//
|
|
174
|
+
// Transform back to get centre coordinates (cx, cy) in the original
|
|
175
|
+
// coordinate system.
|
|
176
|
+
//
|
|
177
|
+
const cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2;
|
|
178
|
+
const cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2;
|
|
179
|
+
// Step 4.
|
|
180
|
+
//
|
|
181
|
+
// Compute angles (theta1, delta_theta).
|
|
182
|
+
//
|
|
183
|
+
const v1x = (x1p - cxp) / rx;
|
|
184
|
+
const v1y = (y1p - cyp) / ry;
|
|
185
|
+
const v2x = (-x1p - cxp) / rx;
|
|
186
|
+
const v2y = (-y1p - cyp) / ry;
|
|
187
|
+
const theta1 = unit_vector_angle(1, 0, v1x, v1y);
|
|
188
|
+
let delta_theta = unit_vector_angle(v1x, v1y, v2x, v2y);
|
|
189
|
+
if (fs === 0 && delta_theta > 0) {
|
|
190
|
+
delta_theta -= TAU;
|
|
191
|
+
}
|
|
192
|
+
if (fs === 1 && delta_theta < 0) {
|
|
193
|
+
delta_theta += TAU;
|
|
194
|
+
}
|
|
195
|
+
return [cx, cy, theta1, delta_theta];
|
|
196
|
+
}
|
|
197
|
+
function unit_vector_angle(ux, uy, vx, vy) {
|
|
198
|
+
const sign = ux * vy - uy * vx < 0 ? -1 : 1;
|
|
199
|
+
let dot = ux * vx + uy * vy;
|
|
200
|
+
// Add this to work with arbitrary vectors:
|
|
201
|
+
// dot /= Math.sqrt(ux * ux + uy * uy) * Math.sqrt(vx * vx + vy * vy);
|
|
202
|
+
// rounding errors, e.g. -1.0000000000000002 can screw up this
|
|
203
|
+
if (dot > 1.0) {
|
|
204
|
+
dot = 1.0;
|
|
205
|
+
}
|
|
206
|
+
if (dot < -1.0) {
|
|
207
|
+
dot = -1.0;
|
|
208
|
+
}
|
|
209
|
+
return sign * Math.acos(dot);
|
|
210
|
+
}
|
package/dist/unshort.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unshort = void 0;
|
|
4
|
+
const unshort = function (segments) {
|
|
5
|
+
let prevControlX = 0;
|
|
6
|
+
let prevControlY = 0;
|
|
7
|
+
let curControlX = 0;
|
|
8
|
+
let curControlY = 0;
|
|
9
|
+
let x = 0;
|
|
10
|
+
let y = 0;
|
|
11
|
+
const newSegments = segments.slice(0);
|
|
12
|
+
for (let i = 0; i < newSegments.length; i++) {
|
|
13
|
+
const s = newSegments[i];
|
|
14
|
+
const name = s[0];
|
|
15
|
+
switch (s[0]) {
|
|
16
|
+
case 'M':
|
|
17
|
+
case 'L': {
|
|
18
|
+
x = s[1];
|
|
19
|
+
y = s[2];
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'V': {
|
|
23
|
+
y = s[1];
|
|
24
|
+
newSegments[i] = s;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case 'H': {
|
|
28
|
+
x = s[1];
|
|
29
|
+
newSegments[i] = s;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'C': {
|
|
33
|
+
x = s[5];
|
|
34
|
+
y = s[6];
|
|
35
|
+
newSegments[i] = s;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case 'Q': {
|
|
39
|
+
x = s[3];
|
|
40
|
+
y = s[4];
|
|
41
|
+
newSegments[i] = s;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
default:
|
|
45
|
+
throw new Error('Unexpected command: ' + s[0]);
|
|
46
|
+
}
|
|
47
|
+
const nameUC = name.toUpperCase();
|
|
48
|
+
let isRelative;
|
|
49
|
+
// First command MUST be M|m, it's safe to skip.
|
|
50
|
+
// Protect from access to [-1] for sure.
|
|
51
|
+
if (!i) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (nameUC === 'T') {
|
|
55
|
+
// quadratic curve
|
|
56
|
+
isRelative = name === 't';
|
|
57
|
+
const prevSegment = newSegments[i - 1];
|
|
58
|
+
if (prevSegment[0] === 'Q') {
|
|
59
|
+
prevControlX = prevSegment[1] - x;
|
|
60
|
+
prevControlY = prevSegment[2] - y;
|
|
61
|
+
}
|
|
62
|
+
else if (prevSegment[0] === 'q') {
|
|
63
|
+
prevControlX = prevSegment[1] - prevSegment[3];
|
|
64
|
+
prevControlY = prevSegment[2] - prevSegment[4];
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
prevControlX = 0;
|
|
68
|
+
prevControlY = 0;
|
|
69
|
+
}
|
|
70
|
+
curControlX = -prevControlX;
|
|
71
|
+
curControlY = -prevControlY;
|
|
72
|
+
if (!isRelative) {
|
|
73
|
+
curControlX += x;
|
|
74
|
+
curControlY += y;
|
|
75
|
+
}
|
|
76
|
+
newSegments[i] = [
|
|
77
|
+
isRelative ? 'q' : 'Q',
|
|
78
|
+
curControlX,
|
|
79
|
+
curControlY,
|
|
80
|
+
s[1],
|
|
81
|
+
s[2],
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
else if (nameUC === 'S') {
|
|
85
|
+
// cubic curve
|
|
86
|
+
isRelative = name === 's';
|
|
87
|
+
const prevSegment = newSegments[i - 1];
|
|
88
|
+
if (prevSegment[0] === 'C') {
|
|
89
|
+
prevControlX = prevSegment[3] - x;
|
|
90
|
+
prevControlY = prevSegment[4] - y;
|
|
91
|
+
}
|
|
92
|
+
else if (prevSegment[0] === 'c') {
|
|
93
|
+
prevControlX = prevSegment[3] - prevSegment[5];
|
|
94
|
+
prevControlY = prevSegment[4] - prevSegment[6];
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
prevControlX = 0;
|
|
98
|
+
prevControlY = 0;
|
|
99
|
+
}
|
|
100
|
+
curControlX = -prevControlX;
|
|
101
|
+
curControlY = -prevControlY;
|
|
102
|
+
if (!isRelative) {
|
|
103
|
+
curControlX += x;
|
|
104
|
+
curControlY += y;
|
|
105
|
+
}
|
|
106
|
+
newSegments[i] = [
|
|
107
|
+
isRelative ? 'c' : 'C',
|
|
108
|
+
curControlX,
|
|
109
|
+
curControlY,
|
|
110
|
+
s[1],
|
|
111
|
+
s[2],
|
|
112
|
+
s[3],
|
|
113
|
+
s[4],
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return newSegments;
|
|
118
|
+
};
|
|
119
|
+
exports.unshort = unshort;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remotion/paths",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-newpathfunctions.13+40cc970bd",
|
|
4
4
|
"description": "Utility functions for SVG paths",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "40cc970bd20f2c9fde0c1644f1c9f33cd47f3dc7"
|
|
39
39
|
}
|