@margoengine/loaders 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1198 -3
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +161 -0
- package/dist/dae.js +633 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf.d.ts +81 -0
- package/dist/dxf.js +774 -0
- package/dist/engine.d.ts +36 -0
- package/dist/engine.js +117 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +55 -0
- package/dist/gcode.js +278 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +120 -0
- package/dist/mtl.js +469 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +63 -0
- package/dist/nrrd.js +743 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +143 -0
- package/dist/obj.js +456 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +27 -0
- package/dist/render.js +153 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +42 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +514 -0
- package/dist/threemf-materials.d.ts +75 -0
- package/dist/threemf-materials.js +314 -0
- package/dist/threemf-mesh.d.ts +60 -0
- package/dist/threemf-mesh.js +201 -0
- package/dist/threemf.d.ts +101 -0
- package/dist/threemf.js +415 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +246 -6
package/dist/lottie.js
ADDED
|
@@ -0,0 +1,893 @@
|
|
|
1
|
+
import { hexColor } from '@margoengine/math/color';
|
|
2
|
+
import { outlineShapes } from './outline.js';
|
|
3
|
+
import { LoaderError } from './types.js';
|
|
4
|
+
import { toText } from './util.js';
|
|
5
|
+
const F = 'Lottie';
|
|
6
|
+
const MAX_NESTING = 64;
|
|
7
|
+
/** Circle by four cubic curves: the handle length of one quarter turn, 4 (sqrt 2 - 1) / 3. */
|
|
8
|
+
const KAPPA = 0.5522847498307936;
|
|
9
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
10
|
+
const list = (value) => (Array.isArray(value) ? value.filter(isObject) : []);
|
|
11
|
+
const num = (value, fallback) => typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
12
|
+
const str = (value, fallback = '') => (typeof value === 'string' ? value : fallback);
|
|
13
|
+
const IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
14
|
+
const multiply = (m, n) => [
|
|
15
|
+
m[0] * n[0] + m[2] * n[1],
|
|
16
|
+
m[1] * n[0] + m[3] * n[1],
|
|
17
|
+
m[0] * n[2] + m[2] * n[3],
|
|
18
|
+
m[1] * n[2] + m[3] * n[3],
|
|
19
|
+
m[0] * n[4] + m[2] * n[5] + m[4],
|
|
20
|
+
m[1] * n[4] + m[3] * n[5] + m[5],
|
|
21
|
+
];
|
|
22
|
+
const apply = (m, x, y) => [
|
|
23
|
+
m[0] * x + m[2] * y + m[4],
|
|
24
|
+
m[1] * x + m[3] * y + m[5],
|
|
25
|
+
];
|
|
26
|
+
const meanScale = (m) => Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
|
|
27
|
+
/** The y of a CSS-style cubic ease from (0,0) to (1,1) with handles (x1,y1) and (x2,y2), at x. */
|
|
28
|
+
function ease(x1, y1, x2, y2, x) {
|
|
29
|
+
if (x <= 0)
|
|
30
|
+
return 0;
|
|
31
|
+
if (x >= 1)
|
|
32
|
+
return 1;
|
|
33
|
+
const curve = (a, b, t) => 3 * (1 - t) * (1 - t) * t * a + 3 * (1 - t) * t * t * b + t * t * t;
|
|
34
|
+
// x rises with t for handles inside 0..1, so bisection always finds it; 40 halvings pass double precision of frames.
|
|
35
|
+
let low = 0, high = 1;
|
|
36
|
+
for (let step = 0; step < 40; step++) {
|
|
37
|
+
const middle = (low + high) / 2;
|
|
38
|
+
if (curve(x1, x2, middle) < x)
|
|
39
|
+
low = middle;
|
|
40
|
+
else
|
|
41
|
+
high = middle;
|
|
42
|
+
}
|
|
43
|
+
return curve(y1, y2, (low + high) / 2);
|
|
44
|
+
}
|
|
45
|
+
const pick = (value, index, fallback) => Array.isArray(value) ? num(value[index] ?? value[0], fallback) : num(value, fallback);
|
|
46
|
+
const asBezier = (value) => {
|
|
47
|
+
const source = Array.isArray(value) ? value[0] : value;
|
|
48
|
+
if (!isObject(source) || !Array.isArray(source.v))
|
|
49
|
+
return null;
|
|
50
|
+
const points = (name) => source.v.map((_, n) => {
|
|
51
|
+
const point = source[name]?.[n];
|
|
52
|
+
return Array.isArray(point) ? [num(point[0], 0), num(point[1], 0)] : [0, 0];
|
|
53
|
+
});
|
|
54
|
+
return { v: points('v'), i: points('i'), o: points('o'), c: source.c === true };
|
|
55
|
+
};
|
|
56
|
+
const asSample = (value) => {
|
|
57
|
+
const path = asBezier(value);
|
|
58
|
+
if (path)
|
|
59
|
+
return path;
|
|
60
|
+
if (typeof value === 'number')
|
|
61
|
+
return [value];
|
|
62
|
+
if (Array.isArray(value))
|
|
63
|
+
return value.map((item) => num(item, 0));
|
|
64
|
+
return [0];
|
|
65
|
+
};
|
|
66
|
+
const blend = (a, b, t) => {
|
|
67
|
+
if (Array.isArray(a) && Array.isArray(b))
|
|
68
|
+
return a.map((value, n) => value + ((b[n] ?? value) - value) * t(n));
|
|
69
|
+
if (Array.isArray(a) || Array.isArray(b))
|
|
70
|
+
return a;
|
|
71
|
+
if (a.v.length !== b.v.length)
|
|
72
|
+
return a;
|
|
73
|
+
const mix = (p, q) => p.map((point, n) => [point[0] + (q[n][0] - point[0]) * t(0), point[1] + (q[n][1] - point[1]) * t(0)]);
|
|
74
|
+
return { v: mix(a.v, b.v), i: mix(a.i, b.i), o: mix(a.o, b.o), c: a.c };
|
|
75
|
+
};
|
|
76
|
+
/** Steps of the table that turns distance along a spatial curve into its parameter. */
|
|
77
|
+
const CURVE_STEPS = 128;
|
|
78
|
+
/**
|
|
79
|
+
* A position on the spatial curve of `key`. The ease gives the share of the distance travelled, as an After Effects
|
|
80
|
+
* motion path does, so a straight path with zero handles is a plain straight move.
|
|
81
|
+
*/
|
|
82
|
+
function alongCurve(key, start, end, share) {
|
|
83
|
+
const { out, in: back } = key.spatial, point = (t) => {
|
|
84
|
+
const u = 1 - t;
|
|
85
|
+
return start.map((p0, n) => {
|
|
86
|
+
const p3 = end[n] ?? p0;
|
|
87
|
+
return (u * u * u * p0 + 3 * u * u * t * (p0 + (out[n] ?? 0)) + 3 * u * t * t * (p3 + (back[n] ?? 0)) + t * t * t * p3);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
let lengths = key.lengths;
|
|
91
|
+
if (!lengths) {
|
|
92
|
+
lengths = key.lengths = new Float64Array(CURVE_STEPS + 1);
|
|
93
|
+
let before = point(0);
|
|
94
|
+
for (let n = 1; n <= CURVE_STEPS; n++) {
|
|
95
|
+
const here = point(n / CURVE_STEPS);
|
|
96
|
+
lengths[n] = lengths[n - 1] + Math.hypot(...here.map((value, k) => value - before[k]));
|
|
97
|
+
before = here;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const total = lengths[CURVE_STEPS];
|
|
101
|
+
if (!(total > 0))
|
|
102
|
+
return start;
|
|
103
|
+
const wanted = share * total;
|
|
104
|
+
// An ease that overshoots runs on along the curve's end.
|
|
105
|
+
let low = 0;
|
|
106
|
+
if (wanted >= total)
|
|
107
|
+
low = CURVE_STEPS - 1;
|
|
108
|
+
else if (wanted > 0) {
|
|
109
|
+
let high = CURVE_STEPS;
|
|
110
|
+
while (high - low > 1) {
|
|
111
|
+
const middle = (low + high) >> 1;
|
|
112
|
+
if (lengths[middle] <= wanted)
|
|
113
|
+
low = middle;
|
|
114
|
+
else
|
|
115
|
+
high = middle;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const span = lengths[low + 1] - lengths[low], a = point(low / CURVE_STEPS), b = point((low + 1) / CURVE_STEPS), t = span > 0 ? (wanted - lengths[low]) / span : 0;
|
|
119
|
+
return a.map((value, k) => value + (b[k] - value) * t);
|
|
120
|
+
}
|
|
121
|
+
/** A property: a constant, or keyframes with their easing. */
|
|
122
|
+
class Property {
|
|
123
|
+
constructor(raw, fallback) {
|
|
124
|
+
this.keys = [];
|
|
125
|
+
const source = isObject(raw) ? raw : {}, value = source.k;
|
|
126
|
+
this.constant = value === undefined ? fallback : asSample(value);
|
|
127
|
+
if (!Array.isArray(value) || !isObject(value[0]) || value[0].t === undefined)
|
|
128
|
+
return;
|
|
129
|
+
const keys = list(value);
|
|
130
|
+
keys.forEach((key, n) => {
|
|
131
|
+
const next = keys[n + 1], start = key.s === undefined ? asSample((keys[n - 1] ?? key).e) : asSample(key.s), end = key.e !== undefined ? asSample(key.e) : next?.s !== undefined ? asSample(next.s) : null, ein = isObject(key.i) ? key.i : {}, eout = isObject(key.o) ? key.o : {}, out = key.to, back = key.ti;
|
|
132
|
+
this.keys.push({
|
|
133
|
+
time: num(key.t, 0),
|
|
134
|
+
start,
|
|
135
|
+
end,
|
|
136
|
+
hold: key.h === 1,
|
|
137
|
+
easing: (d) => [pick(eout.x, d, 0), pick(eout.y, d, 0), pick(ein.x, d, 1), pick(ein.y, d, 1)],
|
|
138
|
+
spatial: Array.isArray(out) && Array.isArray(back)
|
|
139
|
+
? { out: out.map((item) => num(item, 0)), in: back.map((item) => num(item, 0)) }
|
|
140
|
+
: null,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
get animated() {
|
|
145
|
+
return this.keys.length > 0;
|
|
146
|
+
}
|
|
147
|
+
at(frame) {
|
|
148
|
+
const { keys } = this;
|
|
149
|
+
if (!keys.length)
|
|
150
|
+
return this.constant;
|
|
151
|
+
if (frame <= keys[0].time)
|
|
152
|
+
return keys[0].start;
|
|
153
|
+
let index = keys.length - 1;
|
|
154
|
+
while (index > 0 && keys[index].time > frame)
|
|
155
|
+
index--;
|
|
156
|
+
const key = keys[index], next = keys[index + 1];
|
|
157
|
+
if (!next || key.end === null)
|
|
158
|
+
return key.end && !next ? key.end : key.start;
|
|
159
|
+
if (key.hold)
|
|
160
|
+
return key.start;
|
|
161
|
+
const span = next.time - key.time, x = span > 0 ? (frame - key.time) / span : 1, eased = (d) => {
|
|
162
|
+
const [x1, y1, x2, y2] = key.easing(d);
|
|
163
|
+
return ease(x1, y1, x2, y2, x);
|
|
164
|
+
};
|
|
165
|
+
const { start, end } = key;
|
|
166
|
+
if (key.spatial && Array.isArray(start) && Array.isArray(end))
|
|
167
|
+
return alongCurve(key, start, end, eased(0));
|
|
168
|
+
return blend(start, end, eased);
|
|
169
|
+
}
|
|
170
|
+
numbers(frame) {
|
|
171
|
+
const value = this.at(frame);
|
|
172
|
+
return Array.isArray(value) ? value : [0];
|
|
173
|
+
}
|
|
174
|
+
number(frame) {
|
|
175
|
+
return this.numbers(frame)[0] ?? 0;
|
|
176
|
+
}
|
|
177
|
+
path(frame) {
|
|
178
|
+
const value = this.at(frame);
|
|
179
|
+
return Array.isArray(value) ? null : value;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/** A layer or group transform: anchor, position, scale, rotation, skew, opacity. */
|
|
183
|
+
class Transform {
|
|
184
|
+
constructor(raw) {
|
|
185
|
+
const source = isObject(raw) ? raw : {}, position = isObject(source.p) ? source.p : {};
|
|
186
|
+
this.anchor = new Property(source.a, [0, 0, 0]);
|
|
187
|
+
this.axes = position.s === true ? [new Property(position.x, [0]), new Property(position.y, [0])] : null;
|
|
188
|
+
this.position = this.axes ? null : new Property(source.p, [0, 0, 0]);
|
|
189
|
+
this.scale = new Property(source.s, [100, 100, 100]);
|
|
190
|
+
this.rotation = new Property(source.r ?? source.rz, [0]);
|
|
191
|
+
this.skew = new Property(source.sk, [0]);
|
|
192
|
+
this.skewAxis = new Property(source.sa, [0]);
|
|
193
|
+
this.alpha = new Property(source.o, [100]);
|
|
194
|
+
}
|
|
195
|
+
matrix(frame) {
|
|
196
|
+
const [ax = 0, ay = 0] = this.anchor.numbers(frame), [px = 0, py = 0] = this.axes
|
|
197
|
+
? [this.axes[0].number(frame), this.axes[1].number(frame)]
|
|
198
|
+
: this.position.numbers(frame), [sx = 100, sy = 100] = this.scale.numbers(frame), turn = (this.rotation.number(frame) * Math.PI) / 180, skew = (-this.skew.number(frame) * Math.PI) / 180, axis = (this.skewAxis.number(frame) * Math.PI) / 180, cos = Math.cos(turn), sin = Math.sin(turn);
|
|
199
|
+
let local = [cos, sin, -sin, cos, px, py];
|
|
200
|
+
if (skew !== 0) {
|
|
201
|
+
const c = Math.cos(axis), s = Math.sin(axis);
|
|
202
|
+
local = multiply(local, [c, s, -s, c, 0, 0]);
|
|
203
|
+
local = multiply(local, [1, 0, Math.tan(skew), 1, 0, 0]);
|
|
204
|
+
local = multiply(local, [c, -s, s, c, 0, 0]);
|
|
205
|
+
}
|
|
206
|
+
local = multiply(local, [sx / 100, 0, 0, sy / 100, 0, 0]);
|
|
207
|
+
return multiply(local, [1, 0, 0, 1, -ax, -ay]);
|
|
208
|
+
}
|
|
209
|
+
opacity(frame) {
|
|
210
|
+
return this.alpha.number(frame) / 100;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** Bezier vertices of an axis-aligned rectangle, clockwise from the top right, as After Effects draws it. */
|
|
214
|
+
function rectangle(cx, cy, w, h, radius) {
|
|
215
|
+
const x = w / 2, y = h / 2, r = Math.min(radius, x, y), k = r * (1 - KAPPA);
|
|
216
|
+
if (r <= 0) {
|
|
217
|
+
const v = [
|
|
218
|
+
[cx + x, cy - y],
|
|
219
|
+
[cx + x, cy + y],
|
|
220
|
+
[cx - x, cy + y],
|
|
221
|
+
[cx - x, cy - y],
|
|
222
|
+
];
|
|
223
|
+
return { v, i: v.map(() => [0, 0]), o: v.map(() => [0, 0]), c: true };
|
|
224
|
+
}
|
|
225
|
+
const h0 = r - k, v = [
|
|
226
|
+
[cx + x - r, cy - y],
|
|
227
|
+
[cx + x, cy - y + r],
|
|
228
|
+
[cx + x, cy + y - r],
|
|
229
|
+
[cx + x - r, cy + y],
|
|
230
|
+
[cx - x + r, cy + y],
|
|
231
|
+
[cx - x, cy + y - r],
|
|
232
|
+
[cx - x, cy - y + r],
|
|
233
|
+
[cx - x + r, cy - y],
|
|
234
|
+
], o = [
|
|
235
|
+
[h0, 0],
|
|
236
|
+
[0, 0],
|
|
237
|
+
[0, h0],
|
|
238
|
+
[0, 0],
|
|
239
|
+
[-h0, 0],
|
|
240
|
+
[0, 0],
|
|
241
|
+
[0, -h0],
|
|
242
|
+
[0, 0],
|
|
243
|
+
], i = [
|
|
244
|
+
[0, 0],
|
|
245
|
+
[0, -h0],
|
|
246
|
+
[0, 0],
|
|
247
|
+
[h0, 0],
|
|
248
|
+
[0, 0],
|
|
249
|
+
[0, h0],
|
|
250
|
+
[0, 0],
|
|
251
|
+
[-h0, 0],
|
|
252
|
+
];
|
|
253
|
+
return { v, i, o, c: true };
|
|
254
|
+
}
|
|
255
|
+
function ellipse(cx, cy, w, h) {
|
|
256
|
+
const x = w / 2, y = h / 2, kx = x * KAPPA, ky = y * KAPPA;
|
|
257
|
+
return {
|
|
258
|
+
v: [
|
|
259
|
+
[cx, cy - y],
|
|
260
|
+
[cx + x, cy],
|
|
261
|
+
[cx, cy + y],
|
|
262
|
+
[cx - x, cy],
|
|
263
|
+
],
|
|
264
|
+
i: [
|
|
265
|
+
[-kx, 0],
|
|
266
|
+
[0, -ky],
|
|
267
|
+
[kx, 0],
|
|
268
|
+
[0, ky],
|
|
269
|
+
],
|
|
270
|
+
o: [
|
|
271
|
+
[kx, 0],
|
|
272
|
+
[0, ky],
|
|
273
|
+
[-kx, 0],
|
|
274
|
+
[0, -ky],
|
|
275
|
+
],
|
|
276
|
+
c: true,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/** Polygon or star, its first point straight up from the centre before `rotation` (radians). */
|
|
280
|
+
function star(cx, cy, points, outer, inner, rotation) {
|
|
281
|
+
const v = [], corners = inner === null ? points : points * 2;
|
|
282
|
+
for (let n = 0; n < corners; n++) {
|
|
283
|
+
const radius = inner === null || n % 2 === 0 ? outer : inner, angle = rotation - Math.PI / 2 + (n * 2 * Math.PI) / corners;
|
|
284
|
+
v.push([cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)]);
|
|
285
|
+
}
|
|
286
|
+
return { v, i: v.map(() => [0, 0]), o: v.map(() => [0, 0]), c: true };
|
|
287
|
+
}
|
|
288
|
+
const reversed = (path) => ({
|
|
289
|
+
v: [...path.v].reverse(),
|
|
290
|
+
i: [...path.o].reverse(),
|
|
291
|
+
o: [...path.i].reverse(),
|
|
292
|
+
c: path.c,
|
|
293
|
+
});
|
|
294
|
+
function pieces(path) {
|
|
295
|
+
const out = [], count = path.v.length, last = path.c ? count : count - 1;
|
|
296
|
+
for (let n = 0; n < last; n++) {
|
|
297
|
+
const next = (n + 1) % count, a = path.v[n], b = path.v[next], ho = path.o[n], hi = path.i[next];
|
|
298
|
+
out.push([a[0], a[1], a[0] + ho[0], a[1] + ho[1], b[0] + hi[0], b[1] + hi[1], b[0], b[1]]);
|
|
299
|
+
}
|
|
300
|
+
return out;
|
|
301
|
+
}
|
|
302
|
+
const pointOn = (p, t) => {
|
|
303
|
+
const u = 1 - t, a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t;
|
|
304
|
+
return [a * p[0] + b * p[2] + c * p[4] + d * p[6], a * p[1] + b * p[3] + c * p[5] + d * p[7]];
|
|
305
|
+
};
|
|
306
|
+
/** Chords measured along a piece. A trim lands within a hundredth of a unit on a path 100 units long. */
|
|
307
|
+
const CHORDS = 64;
|
|
308
|
+
/** Distance along a piece at each of `CHORDS` even steps of its parameter, starting at 0. */
|
|
309
|
+
function lengths(piece) {
|
|
310
|
+
const table = new Float64Array(CHORDS + 1);
|
|
311
|
+
let [x, y] = pointOn(piece, 0);
|
|
312
|
+
for (let n = 1; n <= CHORDS; n++) {
|
|
313
|
+
const [nx, ny] = pointOn(piece, n / CHORDS);
|
|
314
|
+
table[n] = table[n - 1] + Math.hypot(nx - x, ny - y);
|
|
315
|
+
x = nx;
|
|
316
|
+
y = ny;
|
|
317
|
+
}
|
|
318
|
+
return table;
|
|
319
|
+
}
|
|
320
|
+
/** The parameter at `distance` along a piece: a curve's speed varies, so distance is not the parameter. */
|
|
321
|
+
function parameterAt(table, distance) {
|
|
322
|
+
let n = 1;
|
|
323
|
+
while (n < CHORDS && table[n] < distance)
|
|
324
|
+
n++;
|
|
325
|
+
const span = table[n] - table[n - 1];
|
|
326
|
+
return (n - 1 + (span > 0 ? (distance - table[n - 1]) / span : 0)) / CHORDS;
|
|
327
|
+
}
|
|
328
|
+
/** The part of a piece between parameters `t0` and `t1`, by de Casteljau subdivision. */
|
|
329
|
+
function cut(p, t0, t1) {
|
|
330
|
+
const split = (q, t) => {
|
|
331
|
+
const lerp = (a, b) => a + (b - a) * t, [x0, y0, x1, y1, x2, y2, x3, y3] = q, ax = lerp(x0, x1), ay = lerp(y0, y1), bx = lerp(x1, x2), by = lerp(y1, y2), cx = lerp(x2, x3), cy = lerp(y2, y3), dx = lerp(ax, bx), dy = lerp(ay, by), ex = lerp(bx, cx), ey = lerp(by, cy), fx = lerp(dx, ex), fy = lerp(dy, ey);
|
|
332
|
+
return [
|
|
333
|
+
[x0, y0, ax, ay, dx, dy, fx, fy],
|
|
334
|
+
[fx, fy, ex, ey, cx, cy, x3, y3],
|
|
335
|
+
];
|
|
336
|
+
};
|
|
337
|
+
const tail = t0 > 0 ? split(p, t0)[1] : p;
|
|
338
|
+
return t1 < 1 ? split(tail, (t1 - t0) / (1 - t0))[0] : tail;
|
|
339
|
+
}
|
|
340
|
+
/** The stretch of a path from `from` to `to`, as fractions of its length; `from` past `to` wraps over the end. */
|
|
341
|
+
function trim(parts, from, to) {
|
|
342
|
+
const tables = parts.map(lengths), total = tables.reduce((sum, table) => sum + table[CHORDS], 0);
|
|
343
|
+
if (total === 0)
|
|
344
|
+
return [];
|
|
345
|
+
const take = (a, b) => {
|
|
346
|
+
const out = [];
|
|
347
|
+
let at = 0;
|
|
348
|
+
parts.forEach((part, n) => {
|
|
349
|
+
const table = tables[n], size = table[CHORDS], start = Math.max(a * total - at, 0), end = Math.min(b * total - at, size);
|
|
350
|
+
if (size > 0 && end > start)
|
|
351
|
+
out.push(cut(part, parameterAt(table, start), parameterAt(table, end)));
|
|
352
|
+
at += size;
|
|
353
|
+
});
|
|
354
|
+
return out;
|
|
355
|
+
};
|
|
356
|
+
return from <= to ? [take(from, to)] : [take(from, 1), take(0, to)];
|
|
357
|
+
}
|
|
358
|
+
const runsOf = (path) => ({ pieces: pieces(path), closed: path.c });
|
|
359
|
+
function commandsOf(runs, matrix) {
|
|
360
|
+
const commands = [];
|
|
361
|
+
for (const { pieces: parts, closed } of runs) {
|
|
362
|
+
if (!parts.length)
|
|
363
|
+
continue;
|
|
364
|
+
const [sx, sy] = apply(matrix, parts[0][0], parts[0][1]);
|
|
365
|
+
commands.push(['M', sx, sy]);
|
|
366
|
+
for (const p of parts) {
|
|
367
|
+
const [ax, ay] = apply(matrix, p[2], p[3]), [bx, by] = apply(matrix, p[4], p[5]), [cx, cy] = apply(matrix, p[6], p[7]), straight = p[2] === p[0] && p[3] === p[1] && p[4] === p[6] && p[5] === p[7];
|
|
368
|
+
commands.push(straight ? ['L', cx, cy] : ['C', ax, ay, bx, by, cx, cy]);
|
|
369
|
+
}
|
|
370
|
+
if (closed)
|
|
371
|
+
commands.push(['Z']);
|
|
372
|
+
}
|
|
373
|
+
return commands;
|
|
374
|
+
}
|
|
375
|
+
const CAPS = ['butt', 'round', 'square'];
|
|
376
|
+
const JOINS = ['miter', 'round', 'bevel'];
|
|
377
|
+
/** A sequential trim: every run in turn is one line, and `span` (null for none) is the stretch of it that stays. */
|
|
378
|
+
function trimInTurn(shapes, span) {
|
|
379
|
+
const sizeOf = (run) => run.pieces.reduce((sum, piece) => sum + lengths(piece)[CHORDS], 0), total = shapes.reduce((sum, shape) => sum + shape.runs.reduce((all, run) => all + sizeOf(run), 0), 0);
|
|
380
|
+
const stretches = !span
|
|
381
|
+
? []
|
|
382
|
+
: span[0] <= span[1]
|
|
383
|
+
? [[span[0], span[1]]]
|
|
384
|
+
: [
|
|
385
|
+
[span[0], 1],
|
|
386
|
+
[0, span[1]],
|
|
387
|
+
];
|
|
388
|
+
let at = 0;
|
|
389
|
+
for (const shape of shapes) {
|
|
390
|
+
shape.runs = shape.runs.flatMap((run) => {
|
|
391
|
+
const size = sizeOf(run), kept = [];
|
|
392
|
+
for (const [from, to] of size > 0 ? stretches : []) {
|
|
393
|
+
const a = Math.max((from * total - at) / size, 0), b = Math.min((to * total - at) / size, 1);
|
|
394
|
+
if (b > a)
|
|
395
|
+
for (const pieces of trim(run.pieces, a, b))
|
|
396
|
+
kept.push({ pieces, closed: false });
|
|
397
|
+
}
|
|
398
|
+
at += size;
|
|
399
|
+
return kept;
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
/** The shape items of one list, compiled once, then walked per frame. */
|
|
404
|
+
class ShapeList {
|
|
405
|
+
constructor(raw, note, depth) {
|
|
406
|
+
this.steps = [];
|
|
407
|
+
this.transform = null;
|
|
408
|
+
if (depth > MAX_NESTING)
|
|
409
|
+
throw new LoaderError(F, 'range', 'shape groups nest too deep');
|
|
410
|
+
// A list is walked first to last: paints reach the paths before them, and a group's transform covers the group.
|
|
411
|
+
for (const item of list(raw)) {
|
|
412
|
+
if (item.hd === true)
|
|
413
|
+
continue;
|
|
414
|
+
const compile = this.compile(item, note, depth);
|
|
415
|
+
if (compile)
|
|
416
|
+
this.steps.push(compile);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
compile(item, note, depth) {
|
|
420
|
+
const reverse = item.d === 3;
|
|
421
|
+
const push = (make) => (frame, state, matrix) => {
|
|
422
|
+
const path = make(frame);
|
|
423
|
+
if (path)
|
|
424
|
+
state.gathered.push({ runs: [runsOf(reverse ? reversed(path) : path)], matrix });
|
|
425
|
+
};
|
|
426
|
+
switch (item.ty) {
|
|
427
|
+
case 'gr': {
|
|
428
|
+
const inner = new ShapeList(item.it, note, depth + 1);
|
|
429
|
+
return (frame, state, matrix, opacity) => inner.walk(frame, state, matrix, opacity);
|
|
430
|
+
}
|
|
431
|
+
case 'tr':
|
|
432
|
+
this.transform = new Transform(item);
|
|
433
|
+
return null;
|
|
434
|
+
case 'sh': {
|
|
435
|
+
const shape = new Property(item.ks, [0]);
|
|
436
|
+
return push((frame) => shape.path(frame));
|
|
437
|
+
}
|
|
438
|
+
case 'rc': {
|
|
439
|
+
const position = new Property(item.p, [0, 0]), size = new Property(item.s, [0, 0]), radius = new Property(item.r, [0]);
|
|
440
|
+
return push((frame) => {
|
|
441
|
+
const [x = 0, y = 0] = position.numbers(frame), [w = 0, h = 0] = size.numbers(frame);
|
|
442
|
+
return rectangle(x, y, w, h, radius.number(frame));
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
case 'el': {
|
|
446
|
+
const position = new Property(item.p, [0, 0]), size = new Property(item.s, [0, 0]);
|
|
447
|
+
return push((frame) => {
|
|
448
|
+
const [x = 0, y = 0] = position.numbers(frame), [w = 0, h = 0] = size.numbers(frame);
|
|
449
|
+
return ellipse(x, y, w, h);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
case 'sr': {
|
|
453
|
+
const position = new Property(item.p, [0, 0]), points = new Property(item.pt, [5]), rotation = new Property(item.r, [0]), outer = new Property(item.or, [0]), inner = new Property(item.ir, [0]), polygon = item.sy === 2;
|
|
454
|
+
if (num(item.is?.k, 0) || num(item.os?.k, 0))
|
|
455
|
+
note('star roundness');
|
|
456
|
+
return push((frame) => {
|
|
457
|
+
const [x = 0, y = 0] = position.numbers(frame), count = Math.max(3, Math.round(points.number(frame)));
|
|
458
|
+
return star(x, y, count, outer.number(frame), polygon ? null : inner.number(frame), (rotation.number(frame) * Math.PI) / 180);
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
case 'fl':
|
|
462
|
+
case 'st':
|
|
463
|
+
case 'gf':
|
|
464
|
+
case 'gs':
|
|
465
|
+
return this.paint(item);
|
|
466
|
+
case 'tm': {
|
|
467
|
+
const start = new Property(item.s, [0]), end = new Property(item.e, [100]), offset = new Property(item.o, [0]);
|
|
468
|
+
const sequential = item.m === 2;
|
|
469
|
+
return (frame, state) => {
|
|
470
|
+
const wrap = (v) => v - Math.floor(v), shift = offset.number(frame) / 360, low = Math.min(start.number(frame), end.number(frame)) / 100, high = Math.max(start.number(frame), end.number(frame)) / 100;
|
|
471
|
+
if (high - low >= 1)
|
|
472
|
+
return;
|
|
473
|
+
const from = wrap(low + shift), to = wrap(high + shift);
|
|
474
|
+
if (sequential)
|
|
475
|
+
trimInTurn(state.gathered, high <= low ? null : [from, to]);
|
|
476
|
+
else
|
|
477
|
+
for (const shape of state.gathered)
|
|
478
|
+
shape.runs =
|
|
479
|
+
high <= low
|
|
480
|
+
? []
|
|
481
|
+
: shape.runs.flatMap((run) => trim(run.pieces, from, to).map((pieces) => ({ pieces, closed: false })));
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
default:
|
|
485
|
+
note(`shape item ${str(item.ty, 'unknown')}`);
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
paint(item) {
|
|
490
|
+
const type = item.ty, stroked = type === 'st' || type === 'gs', gradient = type === 'gf' || type === 'gs', color = new Property(item.c, [0, 0, 0]), alpha = new Property(item.o, [100]), width = new Property(item.w, [1]), start = new Property(item.s, [0, 0]), end = new Property(item.e, [0, 0]), stops = isObject(item.g) ? new Property(item.g.k, [0]) : null, stopCount = isObject(item.g) ? num(item.g.p, 0) : 0, dashes = list(item.d).map((dash) => ({ kind: str(dash.n), value: new Property(dash.v, [0]) })), fillRule = item.r === 2 ? 'evenodd' : 'nonzero', lineCap = CAPS[num(item.lc, 2) - 1] ?? 'round', lineJoin = JOINS[num(item.lj, 2) - 1] ?? 'round', miterLimit = num(item.ml, 4), radial = item.t === 2;
|
|
491
|
+
return (frame, state, place, opacity) => {
|
|
492
|
+
const reached = [...state.gathered];
|
|
493
|
+
if (!reached.length)
|
|
494
|
+
return;
|
|
495
|
+
// Stroke width, dashes and gradient ends follow the transforms over the style, whatever the shape sits under.
|
|
496
|
+
const scale = meanScale(place), rgb = color.numbers(frame);
|
|
497
|
+
let paint = null;
|
|
498
|
+
if (gradient && stops) {
|
|
499
|
+
const [sx = 0, sy = 0] = start.numbers(frame), [ex = 0, ey = 0] = end.numbers(frame);
|
|
500
|
+
paint = {
|
|
501
|
+
kind: radial ? 'radial' : 'linear',
|
|
502
|
+
start: apply(place, sx, sy),
|
|
503
|
+
end: apply(place, ex, ey),
|
|
504
|
+
stops: gradientStops(stops.numbers(frame), stopCount),
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
let dash = null;
|
|
508
|
+
if (dashes.length) {
|
|
509
|
+
const pattern = dashes.filter((d) => d.kind !== 'o').map((d) => d.value.number(frame) * scale), offset = dashes.find((d) => d.kind === 'o')?.value.number(frame) ?? 0;
|
|
510
|
+
dash = { pattern, offset: offset * scale };
|
|
511
|
+
}
|
|
512
|
+
const solid = [rgb[0] ?? 0, rgb[1] ?? 0, rgb[2] ?? 0];
|
|
513
|
+
const share = opacity * (alpha.number(frame) / 100), strokeWidth = stroked ? width.number(frame) * scale : 0;
|
|
514
|
+
state.items.push(() => {
|
|
515
|
+
const commands = reached.flatMap(({ runs, matrix }) => commandsOf(runs, matrix));
|
|
516
|
+
if (!commands.length)
|
|
517
|
+
return null;
|
|
518
|
+
return makePath({
|
|
519
|
+
commands,
|
|
520
|
+
fill: !stroked && !gradient ? solid : null,
|
|
521
|
+
gradient: paint,
|
|
522
|
+
stroke: stroked && !gradient ? solid : null,
|
|
523
|
+
opacity: share,
|
|
524
|
+
fillRule,
|
|
525
|
+
strokeWidth,
|
|
526
|
+
lineCap,
|
|
527
|
+
lineJoin,
|
|
528
|
+
miterLimit,
|
|
529
|
+
dash,
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
/** Runs the list; adds its paths and paints to `state`, its paths under its own transform. */
|
|
535
|
+
walk(frame, state, matrix, opacity) {
|
|
536
|
+
const local = this.transform, here = local ? multiply(matrix, local.matrix(frame)) : matrix, alpha = local ? opacity * local.opacity(frame) : opacity, own = { gathered: [], items: state.items };
|
|
537
|
+
for (const step of this.steps)
|
|
538
|
+
step(frame, own, here, alpha);
|
|
539
|
+
state.gathered.push(...own.gathered);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/** Gradient stops from a flat array: `count` of offset, r, g, b, then optional pairs of offset, alpha. */
|
|
543
|
+
function gradientStops(flat, count) {
|
|
544
|
+
const stops = [], alphas = flat.slice(count * 4), alphaAt = (offset) => {
|
|
545
|
+
if (alphas.length < 2)
|
|
546
|
+
return 1;
|
|
547
|
+
let before = 0;
|
|
548
|
+
for (let n = 0; n + 3 < alphas.length; n += 2) {
|
|
549
|
+
if (alphas[n + 2] >= offset) {
|
|
550
|
+
const span = alphas[n + 2] - alphas[n], t = span > 0 ? Math.min(1, Math.max(0, (offset - alphas[n]) / span)) : 0;
|
|
551
|
+
return alphas[n + 1] + (alphas[n + 3] - alphas[n + 1]) * t;
|
|
552
|
+
}
|
|
553
|
+
before = n + 2;
|
|
554
|
+
}
|
|
555
|
+
return alphas[before + 1];
|
|
556
|
+
};
|
|
557
|
+
for (let n = 0; n < count; n++) {
|
|
558
|
+
const at = flat[n * 4] ?? 0;
|
|
559
|
+
stops.push([at, flat[n * 4 + 1] ?? 0, flat[n * 4 + 2] ?? 0, flat[n * 4 + 3] ?? 0, alphaAt(at)]);
|
|
560
|
+
}
|
|
561
|
+
return stops;
|
|
562
|
+
}
|
|
563
|
+
function makePath(fields) {
|
|
564
|
+
return {
|
|
565
|
+
kind: 'path',
|
|
566
|
+
...fields,
|
|
567
|
+
toShapes(options = {}) {
|
|
568
|
+
return outlineShapes(fields.commands, fields.fillRule, options.flipY ? (x, y) => [x, -y] : (x, y) => [x, y]);
|
|
569
|
+
},
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
/** Reads layers; precompositions resolve through `assets`, refusing one that contains itself. */
|
|
573
|
+
class Compiler {
|
|
574
|
+
constructor(assets, maxLayers) {
|
|
575
|
+
this.assets = assets;
|
|
576
|
+
this.maxLayers = maxLayers;
|
|
577
|
+
this.layers = 0;
|
|
578
|
+
this.unsupported = new Set();
|
|
579
|
+
this.note = (feature) => void this.unsupported.add(feature);
|
|
580
|
+
}
|
|
581
|
+
compile(raw, open) {
|
|
582
|
+
if (open.length > MAX_NESTING)
|
|
583
|
+
throw new LoaderError(F, 'range', 'precompositions nest too deep');
|
|
584
|
+
return list(raw).flatMap((item) => {
|
|
585
|
+
if (++this.layers > this.maxLayers)
|
|
586
|
+
throw new LoaderError(F, 'limit', `more than ${this.maxLayers} layers`);
|
|
587
|
+
return this.layer(item, open);
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
layer(item, open) {
|
|
591
|
+
const type = item.ty, layer = {
|
|
592
|
+
index: typeof item.ind === 'number' ? item.ind : null,
|
|
593
|
+
parent: typeof item.parent === 'number' ? item.parent : null,
|
|
594
|
+
inFrame: num(item.ip, 0),
|
|
595
|
+
outFrame: num(item.op, Number.POSITIVE_INFINITY),
|
|
596
|
+
startTime: num(item.st, 0),
|
|
597
|
+
stretch: num(item.sr, 1) || 1,
|
|
598
|
+
hidden: item.hd === true,
|
|
599
|
+
transform: new Transform(item.ks),
|
|
600
|
+
shapes: null,
|
|
601
|
+
solid: null,
|
|
602
|
+
image: null,
|
|
603
|
+
precomp: null,
|
|
604
|
+
};
|
|
605
|
+
if (Array.isArray(item.masksProperties) && item.masksProperties.length)
|
|
606
|
+
this.note('mask');
|
|
607
|
+
if (item.tt)
|
|
608
|
+
this.note('track matte');
|
|
609
|
+
if (Array.isArray(item.ef) && item.ef.length)
|
|
610
|
+
this.note('effect');
|
|
611
|
+
if (item.ddd === 1)
|
|
612
|
+
this.note('3D layer');
|
|
613
|
+
if (num(item.bm, 0) !== 0)
|
|
614
|
+
this.note('blend mode');
|
|
615
|
+
switch (type) {
|
|
616
|
+
case 0: {
|
|
617
|
+
const id = str(item.refId), asset = this.assets.get(id);
|
|
618
|
+
if (!asset)
|
|
619
|
+
throw new LoaderError(F, 'syntax', `layer refers to missing asset ${id}`);
|
|
620
|
+
if (open.includes(id))
|
|
621
|
+
throw new LoaderError(F, 'range', `precomposition ${id} contains itself`);
|
|
622
|
+
layer.precomp = {
|
|
623
|
+
layers: this.compile(asset.layers, [...open, id]),
|
|
624
|
+
remap: item.tm === undefined ? null : new Property(item.tm, [0]),
|
|
625
|
+
width: num(item.w, 0),
|
|
626
|
+
height: num(item.h, 0),
|
|
627
|
+
};
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
case 1:
|
|
631
|
+
{
|
|
632
|
+
const [r = 0, g = 0, b = 0] = hexColor(str(item.sc)) ?? [];
|
|
633
|
+
layer.solid = { width: num(item.sw, 0), height: num(item.sh, 0), color: [r, g, b] };
|
|
634
|
+
}
|
|
635
|
+
break;
|
|
636
|
+
case 2:
|
|
637
|
+
layer.image = str(item.refId);
|
|
638
|
+
break;
|
|
639
|
+
case 3:
|
|
640
|
+
break;
|
|
641
|
+
case 4:
|
|
642
|
+
layer.shapes = new ShapeList(item.shapes, this.note, 0);
|
|
643
|
+
break;
|
|
644
|
+
case 5:
|
|
645
|
+
this.note('text layer');
|
|
646
|
+
break;
|
|
647
|
+
default:
|
|
648
|
+
this.note(`layer type ${String(type)}`);
|
|
649
|
+
}
|
|
650
|
+
return [layer];
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/** A layer's matrix through its parents. */
|
|
654
|
+
const parentMatrix = (layer, all, frame) => {
|
|
655
|
+
let matrix = layer.transform.matrix(frame);
|
|
656
|
+
const seen = new Set([layer]);
|
|
657
|
+
for (let at = layer; at.parent !== null;) {
|
|
658
|
+
const parent = all.find((other) => other.index === at.parent);
|
|
659
|
+
if (!parent || seen.has(parent))
|
|
660
|
+
break;
|
|
661
|
+
seen.add(parent);
|
|
662
|
+
matrix = multiply(parent.transform.matrix(frame), matrix);
|
|
663
|
+
at = parent;
|
|
664
|
+
}
|
|
665
|
+
return matrix;
|
|
666
|
+
};
|
|
667
|
+
function collect(layers, frame, rate, sizes, matrix, opacity, out) {
|
|
668
|
+
// The first layer is on top, so the last is drawn first.
|
|
669
|
+
for (let n = layers.length - 1; n >= 0; n--) {
|
|
670
|
+
const layer = layers[n];
|
|
671
|
+
if (layer.hidden || frame < layer.inFrame || frame >= layer.outFrame)
|
|
672
|
+
continue;
|
|
673
|
+
// A layer's own keyframes are written in the time of its composition, so `st` and `sr` move only what a
|
|
674
|
+
// precomposition plays inside it: a file with `st: -44` keys its layer at 44 to 89, its own in and out frames.
|
|
675
|
+
const here = multiply(matrix, parentMatrix(layer, layers, frame)), alpha = opacity * layer.transform.opacity(frame);
|
|
676
|
+
if (layer.shapes) {
|
|
677
|
+
const walked = { gathered: [], items: [] };
|
|
678
|
+
layer.shapes.walk(frame, walked, here, alpha);
|
|
679
|
+
// Items are in paint order, top first.
|
|
680
|
+
for (const item of walked.items.reverse()) {
|
|
681
|
+
const path = item();
|
|
682
|
+
if (path)
|
|
683
|
+
out.push(path);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
else if (layer.solid) {
|
|
687
|
+
const { width, height, color } = layer.solid;
|
|
688
|
+
out.push(makePath({
|
|
689
|
+
commands: commandsOf([runsOf(rectangle(width / 2, height / 2, width, height, 0))], here),
|
|
690
|
+
fill: color,
|
|
691
|
+
gradient: null,
|
|
692
|
+
stroke: null,
|
|
693
|
+
opacity: alpha,
|
|
694
|
+
fillRule: 'nonzero',
|
|
695
|
+
strokeWidth: 0,
|
|
696
|
+
lineCap: 'butt',
|
|
697
|
+
lineJoin: 'miter',
|
|
698
|
+
miterLimit: 4,
|
|
699
|
+
dash: null,
|
|
700
|
+
}));
|
|
701
|
+
}
|
|
702
|
+
else if (layer.image !== null) {
|
|
703
|
+
const asset = sizes.get(layer.image);
|
|
704
|
+
if (asset)
|
|
705
|
+
out.push({
|
|
706
|
+
kind: 'image',
|
|
707
|
+
asset: asset.id,
|
|
708
|
+
matrix: here,
|
|
709
|
+
opacity: alpha,
|
|
710
|
+
width: asset.width,
|
|
711
|
+
height: asset.height,
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
else if (layer.precomp) {
|
|
715
|
+
const { remap, layers: inner, width, height } = layer.precomp, time = remap ? remap.number(frame) * rate : (frame - layer.startTime) / layer.stretch, first = out.length;
|
|
716
|
+
collect(inner, time, rate, sizes, here, alpha, out);
|
|
717
|
+
// Lottie specification, precomposition layer: `w` and `h` are the width and height of its clipping rect.
|
|
718
|
+
if (width > 0 && height > 0 && out.length > first) {
|
|
719
|
+
const rect = commandsOf([runsOf(rectangle(width / 2, height / 2, width, height, 0))], here);
|
|
720
|
+
for (let k = first; k < out.length; k++) {
|
|
721
|
+
const item = out[k];
|
|
722
|
+
item.clip = item.clip ? [rect, ...item.clip] : [rect];
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
const cssColor = (r, g, b, a) => `rgba(${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)},${a})`;
|
|
729
|
+
function trace(context, commands) {
|
|
730
|
+
context.beginPath();
|
|
731
|
+
for (const c of commands) {
|
|
732
|
+
if (c[0] === 'M')
|
|
733
|
+
context.moveTo(c[1], c[2]);
|
|
734
|
+
else if (c[0] === 'L')
|
|
735
|
+
context.lineTo(c[1], c[2]);
|
|
736
|
+
else if (c[0] === 'C')
|
|
737
|
+
context.bezierCurveTo(c[1], c[2], c[3], c[4], c[5], c[6]);
|
|
738
|
+
else if (c[0] === 'Q')
|
|
739
|
+
context.quadraticCurveTo(c[1], c[2], c[3], c[4]);
|
|
740
|
+
else
|
|
741
|
+
context.closePath();
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
function gradientPaint(context, { kind, start, end, stops }) {
|
|
745
|
+
const paint = kind === 'linear'
|
|
746
|
+
? context.createLinearGradient(start[0], start[1], end[0], end[1])
|
|
747
|
+
: context.createRadialGradient(start[0], start[1], 0, start[0], start[1], Math.hypot(end[0] - start[0], end[1] - start[1]));
|
|
748
|
+
for (const [offset, r, g, b, a] of stops)
|
|
749
|
+
paint.addColorStop(Math.min(1, Math.max(0, offset)), cssColor(r, g, b, a));
|
|
750
|
+
return paint;
|
|
751
|
+
}
|
|
752
|
+
function paintPath(context, path) {
|
|
753
|
+
context.save();
|
|
754
|
+
context.globalAlpha = path.opacity;
|
|
755
|
+
trace(context, path.commands);
|
|
756
|
+
if (path.strokeWidth > 0) {
|
|
757
|
+
context.lineWidth = path.strokeWidth;
|
|
758
|
+
context.lineCap = path.lineCap;
|
|
759
|
+
context.lineJoin = path.lineJoin;
|
|
760
|
+
context.miterLimit = path.miterLimit;
|
|
761
|
+
if (path.dash) {
|
|
762
|
+
context.setLineDash(path.dash.pattern);
|
|
763
|
+
context.lineDashOffset = path.dash.offset;
|
|
764
|
+
}
|
|
765
|
+
const [r = 0, g = 0, b = 0] = path.stroke ?? [];
|
|
766
|
+
context.strokeStyle = path.gradient ? gradientPaint(context, path.gradient) : cssColor(r, g, b, 1);
|
|
767
|
+
context.stroke();
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
const [r = 0, g = 0, b = 0] = path.fill ?? [];
|
|
771
|
+
context.fillStyle = path.gradient ? gradientPaint(context, path.gradient) : cssColor(r, g, b, 1);
|
|
772
|
+
context.fill(path.fillRule);
|
|
773
|
+
}
|
|
774
|
+
context.restore();
|
|
775
|
+
}
|
|
776
|
+
/** Whether the file's exporter wrote fill and stroke colours from 0 to 255: Bodymovin before 4.1.9. */
|
|
777
|
+
function bytesColours(version) {
|
|
778
|
+
if (!/^\d+\.\d+\.\d+/.test(version))
|
|
779
|
+
return false;
|
|
780
|
+
const [major = 0, minor = 0, patch = 0] = version.split('.').map(Number);
|
|
781
|
+
return major < 4 || (major === 4 && (minor < 1 || (minor === 1 && patch < 9)));
|
|
782
|
+
}
|
|
783
|
+
/** Rewrites the fill and stroke colours under `value` to 0..1, in a file of {@link bytesColours}. */
|
|
784
|
+
function scaleColours(value) {
|
|
785
|
+
if (Array.isArray(value))
|
|
786
|
+
for (const item of value)
|
|
787
|
+
scaleColours(item);
|
|
788
|
+
if (!isObject(value))
|
|
789
|
+
return;
|
|
790
|
+
if ((value.ty === 'fl' || value.ty === 'st') && isObject(value.c)) {
|
|
791
|
+
const scaled = (colour) => Array.isArray(colour) ? colour.map((part) => (typeof part === 'number' ? part / 255 : part)) : colour;
|
|
792
|
+
const keys = value.c.k;
|
|
793
|
+
if (Array.isArray(keys) && isObject(keys[0]))
|
|
794
|
+
for (const key of list(keys)) {
|
|
795
|
+
key.s = scaled(key.s);
|
|
796
|
+
key.e = scaled(key.e);
|
|
797
|
+
}
|
|
798
|
+
else
|
|
799
|
+
value.c.k = scaled(keys);
|
|
800
|
+
}
|
|
801
|
+
for (const name of ['layers', 'assets', 'shapes', 'it'])
|
|
802
|
+
scaleColours(value[name]);
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Reads a Lottie JSON animation. `frame()` gives the vector shapes of any frame, for `toShapes()` or your own drawing,
|
|
806
|
+
* and `draw()` paints a frame onto a 2D canvas context, which uploads as a texture. What the file uses and a frame
|
|
807
|
+
* leaves out is listed in `unsupported`.
|
|
808
|
+
*
|
|
809
|
+
* @throws LoaderError code `syntax` for bad JSON or a missing asset, `header` when the root is not a Lottie animation,
|
|
810
|
+
* `range` for a precomposition that contains itself or nesting past 64, `limit` over `maxLayers`.
|
|
811
|
+
*/
|
|
812
|
+
export function parseLottie(input, options = {}) {
|
|
813
|
+
let root;
|
|
814
|
+
try {
|
|
815
|
+
root = JSON.parse(toText(input, F));
|
|
816
|
+
}
|
|
817
|
+
catch (error) {
|
|
818
|
+
if (error instanceof LoaderError)
|
|
819
|
+
throw error;
|
|
820
|
+
throw new LoaderError(F, 'syntax', `bad JSON (${error.message})`);
|
|
821
|
+
}
|
|
822
|
+
if (!isObject(root) || !Array.isArray(root.layers) || typeof root.fr !== 'number' || !(root.fr > 0))
|
|
823
|
+
throw new LoaderError(F, 'header', 'not a Lottie animation: it needs layers and a frame rate');
|
|
824
|
+
if (bytesColours(str(root.v)))
|
|
825
|
+
scaleColours(root);
|
|
826
|
+
const assets = new Map(), images = [];
|
|
827
|
+
for (const asset of list(root.assets)) {
|
|
828
|
+
const id = str(asset.id);
|
|
829
|
+
if (Array.isArray(asset.layers))
|
|
830
|
+
assets.set(id, asset);
|
|
831
|
+
else if (typeof asset.p === 'string') {
|
|
832
|
+
const embedded = asset.e === 1 || asset.p.startsWith('data:');
|
|
833
|
+
images.push({
|
|
834
|
+
id,
|
|
835
|
+
width: num(asset.w, 0),
|
|
836
|
+
height: num(asset.h, 0),
|
|
837
|
+
path: embedded ? asset.p : str(asset.u) + asset.p,
|
|
838
|
+
embedded,
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
const compiler = new Compiler(assets, options.maxLayers ?? 100_000), layers = compiler.compile(root.layers, []), rate = root.fr, inFrame = num(root.ip, 0), outFrame = num(root.op, inFrame), sizes = new Map(images.map((image) => [image.id, image]));
|
|
843
|
+
const frame = (at) => {
|
|
844
|
+
const out = [];
|
|
845
|
+
collect(layers, at, rate, sizes, IDENTITY, 1, out);
|
|
846
|
+
return out;
|
|
847
|
+
};
|
|
848
|
+
return {
|
|
849
|
+
version: str(root.v),
|
|
850
|
+
name: str(root.nm),
|
|
851
|
+
width: num(root.w, 0),
|
|
852
|
+
height: num(root.h, 0),
|
|
853
|
+
frameRate: rate,
|
|
854
|
+
inFrame,
|
|
855
|
+
outFrame,
|
|
856
|
+
duration: (outFrame - inFrame) / rate,
|
|
857
|
+
images,
|
|
858
|
+
markers: list(root.markers).map((marker) => ({
|
|
859
|
+
name: str(marker.cm),
|
|
860
|
+
time: num(marker.tm, 0),
|
|
861
|
+
duration: num(marker.dr, 0),
|
|
862
|
+
})),
|
|
863
|
+
unsupported: [...compiler.unsupported],
|
|
864
|
+
frame,
|
|
865
|
+
draw(context, at, drawOptions = {}) {
|
|
866
|
+
for (const item of frame(at)) {
|
|
867
|
+
const image = item.kind === 'image' ? drawOptions.images?.[item.asset] : null;
|
|
868
|
+
if (image === undefined)
|
|
869
|
+
continue;
|
|
870
|
+
if (item.clip) {
|
|
871
|
+
if (!context.clip)
|
|
872
|
+
throw new LoaderError(F, 'unsupported', 'the canvas has no clip(); precompositions clip');
|
|
873
|
+
context.save();
|
|
874
|
+
for (const outline of item.clip) {
|
|
875
|
+
trace(context, outline);
|
|
876
|
+
context.clip();
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (item.kind === 'path')
|
|
880
|
+
paintPath(context, item);
|
|
881
|
+
else {
|
|
882
|
+
context.save();
|
|
883
|
+
context.globalAlpha = item.opacity;
|
|
884
|
+
context.transform(...item.matrix);
|
|
885
|
+
context.drawImage(image, 0, 0, item.width, item.height);
|
|
886
|
+
context.restore();
|
|
887
|
+
}
|
|
888
|
+
if (item.clip)
|
|
889
|
+
context.restore();
|
|
890
|
+
}
|
|
891
|
+
},
|
|
892
|
+
};
|
|
893
|
+
}
|