motionary 10.5.0 → 10.7.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/AGENTS.md +2 -0
- package/CHANGELOG.md +28 -0
- package/README.md +95 -1
- package/bin/motionary-mcp.mjs +160 -11
- package/dist/chunks/{anim-image-DuxvYhq6.cjs → anim-image-CFGpUX7t.cjs} +2 -2
- package/dist/chunks/{anim-image-DuxvYhq6.cjs.map → anim-image-CFGpUX7t.cjs.map} +1 -1
- package/dist/chunks/{anim-image-BlNPv4JG.js → anim-image-CXDD19FH.js} +2 -2
- package/dist/chunks/{anim-image-BlNPv4JG.js.map → anim-image-CXDD19FH.js.map} +1 -1
- package/dist/chunks/{keyframes-Ce1s05n3.cjs → keyframes-DdwGY1gr.cjs} +2 -2
- package/dist/chunks/{keyframes-Ce1s05n3.cjs.map → keyframes-DdwGY1gr.cjs.map} +1 -1
- package/dist/chunks/{keyframes-B8ITIAVa.js → keyframes-DwR6GPej.js} +2 -2
- package/dist/chunks/{keyframes-B8ITIAVa.js.map → keyframes-DwR6GPej.js.map} +1 -1
- package/dist/chunks/{registry-DXdFb2fJ.js → registry-D87dP_aR.js} +3 -3
- package/dist/chunks/{registry-DXdFb2fJ.js.map → registry-D87dP_aR.js.map} +1 -1
- package/dist/chunks/{registry-VhfPSI0v.cjs → registry-DhKZCitb.cjs} +2 -2
- package/dist/chunks/{registry-VhfPSI0v.cjs.map → registry-DhKZCitb.cjs.map} +1 -1
- package/dist/chunks/{ticker-C5RMHMW7.js → ticker-CJYI5Pxc.js} +2 -2
- package/dist/chunks/{ticker-C5RMHMW7.js.map → ticker-CJYI5Pxc.js.map} +1 -1
- package/dist/chunks/{ticker-DvmC6WLS.cjs → ticker-jD9_A9Rh.cjs} +2 -2
- package/dist/chunks/{ticker-DvmC6WLS.cjs.map → ticker-jD9_A9Rh.cjs.map} +1 -1
- package/dist/chunks/{tween-_BGIl6hK.cjs → tween-Dk0QmNe9.cjs} +2 -2
- package/dist/chunks/{tween-_BGIl6hK.cjs.map → tween-Dk0QmNe9.cjs.map} +1 -1
- package/dist/chunks/{tween-CPtm-EdT.js → tween-_Lld1MsF.js} +2 -2
- package/dist/chunks/{tween-CPtm-EdT.js.map → tween-_Lld1MsF.js.map} +1 -1
- package/dist/components/ai.cjs +314 -0
- package/dist/components/ai.cjs.map +1 -0
- package/dist/components/ai.d.cts +58 -0
- package/dist/components/ai.d.ts +58 -0
- package/dist/components/ai.js +311 -0
- package/dist/components/ai.js.map +1 -0
- package/dist/components/lite.cjs +179 -0
- package/dist/components/lite.cjs.map +1 -1
- package/dist/components/lite.js +176 -1
- package/dist/components/lite.js.map +1 -1
- package/dist/components/tokens.cjs +179 -0
- package/dist/components/tokens.cjs.map +1 -1
- package/dist/components/tokens.d.cts +36 -1
- package/dist/components/tokens.d.ts +36 -1
- package/dist/components/tokens.js +176 -1
- package/dist/components/tokens.js.map +1 -1
- package/dist/components/widgets.cjs +985 -221
- package/dist/components/widgets.cjs.map +1 -1
- package/dist/components/widgets.d.cts +606 -4
- package/dist/components/widgets.d.ts +606 -4
- package/dist/components/widgets.js +976 -222
- package/dist/components/widgets.js.map +1 -1
- package/dist/components.cjs +4 -0
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +36 -1
- package/dist/components.d.ts +36 -1
- package/dist/components.js +1 -1
- package/dist/components.umd.js +2 -2
- package/dist/components.umd.js.map +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/llms-full.txt +221 -1
- package/dist/llms.txt +11 -2
- package/dist/manifest.json +580 -1
- package/dist/runtime/format-apng.cjs +4 -4
- package/dist/runtime/format-apng.js +4 -4
- package/dist/runtime/format-css.cjs +4 -4
- package/dist/runtime/format-css.js +4 -4
- package/dist/runtime/format-gif.cjs +4 -4
- package/dist/runtime/format-gif.js +4 -4
- package/dist/runtime/format-gltf.cjs +1 -1
- package/dist/runtime/format-gltf.js +1 -1
- package/dist/runtime/format-motion.cjs +4 -4
- package/dist/runtime/format-motion.js +4 -4
- package/dist/runtime/format-obj.cjs +1 -1
- package/dist/runtime/format-obj.js +1 -1
- package/dist/runtime/format-scene.cjs +241 -0
- package/dist/runtime/format-scene.cjs.map +1 -0
- package/dist/runtime/format-scene.d.cts +349 -0
- package/dist/runtime/format-scene.d.ts +349 -0
- package/dist/runtime/format-scene.iife.js +2 -0
- package/dist/runtime/format-scene.iife.js.map +1 -0
- package/dist/runtime/format-scene.js +233 -0
- package/dist/runtime/format-scene.js.map +1 -0
- package/dist/runtime/format-sprite.cjs +3 -3
- package/dist/runtime/format-sprite.js +3 -3
- package/dist/runtime/format-svg.cjs +3 -3
- package/dist/runtime/format-svg.js +3 -3
- package/dist/runtime/format-webp.cjs +4 -4
- package/dist/runtime/format-webp.js +4 -4
- package/dist/runtime/gl.cjs +1 -1
- package/dist/runtime/gl.js +1 -1
- package/dist/runtime/physics.cjs +739 -0
- package/dist/runtime/physics.cjs.map +1 -0
- package/dist/runtime/physics.d.cts +250 -0
- package/dist/runtime/physics.d.ts +250 -0
- package/dist/runtime/physics.iife.js +2 -0
- package/dist/runtime/physics.iife.js.map +1 -0
- package/dist/runtime/physics.js +731 -0
- package/dist/runtime/physics.js.map +1 -0
- package/dist/runtime/scroll.cjs +2 -2
- package/dist/runtime/scroll.js +2 -2
- package/dist/runtime/smooth.cjs +1 -1
- package/dist/runtime/smooth.js +1 -1
- package/dist/runtime/text.cjs +1 -1
- package/dist/runtime/text.js +1 -1
- package/dist/runtime/vector.cjs +704 -0
- package/dist/runtime/vector.cjs.map +1 -0
- package/dist/runtime/vector.d.cts +306 -0
- package/dist/runtime/vector.d.ts +306 -0
- package/dist/runtime/vector.iife.js +2 -0
- package/dist/runtime/vector.iife.js.map +1 -0
- package/dist/runtime/vector.js +692 -0
- package/dist/runtime/vector.js.map +1 -0
- package/dist/runtime.cjs +3 -3
- package/dist/runtime.d.cts +1 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.iife.js +1 -1
- package/dist/runtime.iife.js.map +1 -1
- package/dist/runtime.js +5 -5
- package/dist/widgets.umd.js +1 -1
- package/dist/widgets.umd.js.map +1 -1
- package/docs/ROADMAP.md +2 -2
- package/docs/components/README.md +6 -1
- package/docs/components/usa-lottie-player.md +60 -0
- package/docs/components/usa-motion-prompt.md +32 -0
- package/docs/components/usa-physics-playground.md +63 -0
- package/docs/components/usa-rive.md +57 -0
- package/docs/components/usa-token-editor.md +32 -0
- package/docs/components.md +116 -0
- package/docs/runtime/format-scene.md +59 -0
- package/docs/runtime/physics.md +61 -0
- package/docs/runtime/rive.md +55 -0
- package/docs/runtime/vector.md +67 -0
- package/package.json +46 -2
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
import { R as RUNTIME_VERSION } from '../chunks/registry-D87dP_aR.js';
|
|
2
|
+
import { P as Playable } from '../chunks/tween-_Lld1MsF.js';
|
|
3
|
+
import '../chunks/ticker-CJYI5Pxc.js';
|
|
4
|
+
import '../chunks/ease-XN8_0sXu.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `motionary/runtime/vector` (10.6) — a Lottie (bodymovin JSON) + dotLottie
|
|
8
|
+
* player written for Motionary (own renderer on Canvas 2D; no lottie-web
|
|
9
|
+
* code). Supported subset — see the compatibility table in
|
|
10
|
+
* `docs/runtime/vector.md`:
|
|
11
|
+
*
|
|
12
|
+
* - layers: shape, solid, null, image, precomp (with time stretch / time
|
|
13
|
+
* remap), parenting, in / out points, hidden layers;
|
|
14
|
+
* - transforms: anchor, position (incl. split X / Y and spatial bezier
|
|
15
|
+
* paths), scale, rotation, skew, opacity;
|
|
16
|
+
* - shapes: groups, paths, rectangles (rounded), ellipses, stars / polygons;
|
|
17
|
+
* fills (non-zero / even-odd), strokes (caps, joins, dashes), linear and
|
|
18
|
+
* radial gradient fills / strokes, **trim paths** (individually /
|
|
19
|
+
* simultaneously);
|
|
20
|
+
* - **masks** (add, subtract, intersect, inverted, opacity) and **track
|
|
21
|
+
* mattes** (alpha, alpha inverted; luma approximated by alpha);
|
|
22
|
+
* - keyframes: bezier easing per dimension, hold keyframes, v4 (`e`) and v5+
|
|
23
|
+
* (`s` only) files;
|
|
24
|
+
* - **dotLottie** (`.lottie`): zip (stored + deflate via the native
|
|
25
|
+
* `DecompressionStream`), `manifest.json` v1 / v2, several animations,
|
|
26
|
+
* embedded images.
|
|
27
|
+
*
|
|
28
|
+
* Not supported (documented): 3D layers, effects, text layers (10.8),
|
|
29
|
+
* expressions (10.8 subset), merge paths, repeaters. `lottiePlayer()` is a
|
|
30
|
+
* runtime timeline (seek, reverse, scrub with `progress`, markers → labels).
|
|
31
|
+
*/
|
|
32
|
+
// ------------------------------------------------------------------ keyframes
|
|
33
|
+
function bez(x1, y1, x2, y2, x) {
|
|
34
|
+
if (x <= 0)
|
|
35
|
+
return 0;
|
|
36
|
+
if (x >= 1)
|
|
37
|
+
return 1;
|
|
38
|
+
let t = x;
|
|
39
|
+
for (let i = 0; i < 8; i++) {
|
|
40
|
+
const cx = 3 * x1 * t * (1 - t) ** 2 + 3 * x2 * t * t * (1 - t) + t ** 3 - x;
|
|
41
|
+
const d = 3 * x1 * (1 - t) ** 2 + 6 * (x2 - x1) * t * (1 - t) + 3 * (1 - x2) * t * t;
|
|
42
|
+
if (Math.abs(cx) < 1e-5)
|
|
43
|
+
break;
|
|
44
|
+
if (Math.abs(d) < 1e-6)
|
|
45
|
+
break;
|
|
46
|
+
t -= cx / d;
|
|
47
|
+
}
|
|
48
|
+
t = Math.min(1, Math.max(0, t));
|
|
49
|
+
return 3 * y1 * t * (1 - t) ** 2 + 3 * y2 * t * t * (1 - t) + t ** 3;
|
|
50
|
+
}
|
|
51
|
+
const pick = (v, i, d) => (Array.isArray(v) ? v[Math.min(i, v.length - 1)] ?? d : v ?? d);
|
|
52
|
+
const lerpAny = (a, b, p) => {
|
|
53
|
+
if (typeof a === 'number')
|
|
54
|
+
return a + (b - a) * (Array.isArray(p) ? p[0] : p);
|
|
55
|
+
if (Array.isArray(a))
|
|
56
|
+
return a.map((v, i) => (typeof v === 'number' ? v + ((b?.[i] ?? v) - v) * (Array.isArray(p) ? pick(p, i, 0) : p) : lerpAny(v, b?.[i], p)));
|
|
57
|
+
if (a && typeof a === 'object' && a.v)
|
|
58
|
+
return { c: a.c, v: lerpAny(a.v, b.v, p), i: lerpAny(a.i, b.i, p), o: lerpAny(a.o, b.o, p) };
|
|
59
|
+
return a;
|
|
60
|
+
};
|
|
61
|
+
/** Value of an (animated or static) property at frame `f`. */
|
|
62
|
+
function propValue(p, f, fallback) {
|
|
63
|
+
if (!p)
|
|
64
|
+
return fallback;
|
|
65
|
+
if (!p.a && !(Array.isArray(p.k) && p.k.length && typeof p.k[0] === 'object' && 't' in p.k[0]))
|
|
66
|
+
return p.k;
|
|
67
|
+
const ks = p.k;
|
|
68
|
+
if (f <= ks[0].t)
|
|
69
|
+
return unwrap(ks[0].s);
|
|
70
|
+
for (let n = 0; n < ks.length - 1; n++) {
|
|
71
|
+
const a = ks[n], b = ks[n + 1];
|
|
72
|
+
if (f >= b.t)
|
|
73
|
+
continue;
|
|
74
|
+
const s = unwrap(a.s), e = unwrap(a.e !== undefined ? a.e : b.s);
|
|
75
|
+
if (a.h === 1 || e === undefined)
|
|
76
|
+
return s;
|
|
77
|
+
const x = (f - a.t) / (b.t - a.t || 1);
|
|
78
|
+
const dims = Array.isArray(s) ? s.length : 1;
|
|
79
|
+
const ease = Array.from({ length: dims }, (_, i) => bez(pick(a.o?.x, i, 0), pick(a.o?.y, i, 0), pick(a.i?.x, i, 1), pick(a.i?.y, i, 1), x));
|
|
80
|
+
if (a.to && a.ti && Array.isArray(s) && (a.to.some(Boolean) || a.ti.some(Boolean))) {
|
|
81
|
+
// spatial bezier (position paths)
|
|
82
|
+
const t = ease[0], u = 1 - t;
|
|
83
|
+
return s.map((v, i) => u * u * u * v + 3 * u * u * t * (v + (a.to[i] || 0)) + 3 * u * t * t * (e[i] + (a.ti[i] || 0)) + t * t * t * e[i]);
|
|
84
|
+
}
|
|
85
|
+
return lerpAny(s, e, dims > 1 ? ease : ease[0]);
|
|
86
|
+
}
|
|
87
|
+
const last = ks[ks.length - 1];
|
|
88
|
+
return unwrap(last.s !== undefined ? last.s : ks[ks.length - 2]?.e);
|
|
89
|
+
}
|
|
90
|
+
const unwrap = (v) => (Array.isArray(v) && v.length === 1 && typeof v[0] === 'object' ? v[0] : v);
|
|
91
|
+
const I = [1, 0, 0, 1, 0, 0];
|
|
92
|
+
const mul = (m, n) => [m[0] * n[0] + m[2] * n[1], m[1] * n[0] + m[3] * n[1], m[0] * n[2] + m[2] * n[3], m[1] * n[2] + m[3] * n[3], m[0] * n[4] + m[2] * n[5] + m[4], m[1] * n[4] + m[3] * n[5] + m[5]];
|
|
93
|
+
const ap = (m, x, y) => [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
94
|
+
const D = Math.PI / 180;
|
|
95
|
+
/** Transform matrix + opacity (0–1) of a `ks` / `tr` block at frame f. */
|
|
96
|
+
function transformAt(ks, f) {
|
|
97
|
+
if (!ks)
|
|
98
|
+
return { m: I, o: 1 };
|
|
99
|
+
const a = propValue(ks.a, f, [0, 0]) || [0, 0];
|
|
100
|
+
let p;
|
|
101
|
+
if (ks.p?.s)
|
|
102
|
+
p = [propValue(ks.p.x, f, 0), propValue(ks.p.y, f, 0)];
|
|
103
|
+
else
|
|
104
|
+
p = propValue(ks.p, f, [0, 0]) || [0, 0];
|
|
105
|
+
const s = propValue(ks.s, f, [100, 100]) || [100, 100];
|
|
106
|
+
const r = propValue(ks.r ?? ks.rz, f, 0) || 0;
|
|
107
|
+
const sk = propValue(ks.sk, f, 0) || 0, sa = propValue(ks.sa, f, 0) || 0;
|
|
108
|
+
let m = [1, 0, 0, 1, p[0], p[1]];
|
|
109
|
+
if (r)
|
|
110
|
+
m = mul(m, [Math.cos(r * D), Math.sin(r * D), -Math.sin(r * D), Math.cos(r * D), 0, 0]);
|
|
111
|
+
if (sk) {
|
|
112
|
+
const c = Math.cos(sa * D), sn = Math.sin(sa * D), t = Math.tan(-sk * D);
|
|
113
|
+
m = mul(m, mul([c, -sn, sn, c, 0, 0], mul([1, 0, t, 1, 0, 0], [c, sn, -sn, c, 0, 0])));
|
|
114
|
+
}
|
|
115
|
+
m = mul(m, [s[0] / 100, 0, 0, (s[1] ?? s[0]) / 100, 0, 0]);
|
|
116
|
+
m = mul(m, [1, 0, 0, 1, -a[0], -(a[1] || 0)]);
|
|
117
|
+
const o = propValue(ks.o, f, 100);
|
|
118
|
+
return { m, o: (o ?? 100) / 100 };
|
|
119
|
+
}
|
|
120
|
+
const K = 0.5519150244935105;
|
|
121
|
+
function shapePath(it, f) {
|
|
122
|
+
switch (it.ty) {
|
|
123
|
+
case 'sh': {
|
|
124
|
+
const v = propValue(it.ks, f);
|
|
125
|
+
if (!v?.v?.length)
|
|
126
|
+
return [];
|
|
127
|
+
const pts = [v.v[0][0], v.v[0][1]];
|
|
128
|
+
const n = v.v.length;
|
|
129
|
+
for (let i = 1; i < n + (v.c ? 1 : 0); i++) {
|
|
130
|
+
const a = v.v[(i - 1) % n], b = v.v[i % n], ao = v.o[(i - 1) % n] || [0, 0], bi = v.i[i % n] || [0, 0];
|
|
131
|
+
pts.push(a[0] + ao[0], a[1] + ao[1], b[0] + bi[0], b[1] + bi[1], b[0], b[1]);
|
|
132
|
+
}
|
|
133
|
+
return [{ pts, closed: !!v.c }];
|
|
134
|
+
}
|
|
135
|
+
case 'rc': {
|
|
136
|
+
const [px, py] = propValue(it.p, f, [0, 0]), [w, h] = propValue(it.s, f, [0, 0]);
|
|
137
|
+
const r = Math.min(propValue(it.r, f, 0) || 0, w / 2, h / 2), x = px - w / 2, y = py - h / 2;
|
|
138
|
+
if (!r)
|
|
139
|
+
return [{ pts: [x + w, y, x + w, y, x + w, y + h, x + w, y + h, x + w, y + h, x, y + h, x, y + h, x, y + h, x, y, x, y, x, y, x + w, y], closed: true }];
|
|
140
|
+
const k = r * K;
|
|
141
|
+
// starts at top-right, clockwise like After Effects
|
|
142
|
+
return [{ pts: [x + w - r, y, x + w - r + k, y, x + w, y + r - k, x + w, y + r, x + w, y + r, x + w, y + h - r, x + w, y + h - r, x + w, y + h - r + k, x + w - r + k, y + h, x + w - r, y + h, x + w - r, y + h, x + r, y + h, x + r, y + h, x + r - k, y + h, x, y + h - r + k, x, y + h - r, x, y + h - r, x, y + r, x, y + r, x, y + r - k, x + r - k, y, x + r, y, x + r, y, x + w - r, y], closed: true }];
|
|
143
|
+
}
|
|
144
|
+
case 'el': {
|
|
145
|
+
const [cx, cy] = propValue(it.p, f, [0, 0]), [w, h] = propValue(it.s, f, [0, 0]);
|
|
146
|
+
const rx = w / 2, ry = h / 2, kx = rx * K, ky = ry * K;
|
|
147
|
+
return [{ pts: [cx, cy - ry, cx + kx, cy - ry, cx + rx, cy - ky, cx + rx, cy, cx + rx, cy + ky, cx + kx, cy + ry, cx, cy + ry, cx - kx, cy + ry, cx - rx, cy + ky, cx - rx, cy, cx - rx, cy - ky, cx - kx, cy - ry, cx, cy - ry], closed: true }];
|
|
148
|
+
}
|
|
149
|
+
case 'sr': {
|
|
150
|
+
const [cx, cy] = propValue(it.p, f, [0, 0]), n = Math.round(propValue(it.pt, f, 5)), rot = (propValue(it.r, f, 0) || 0) * D;
|
|
151
|
+
const or = propValue(it.or, f, 50), ir = it.sy === 1 ? propValue(it.ir, f, 25) : or;
|
|
152
|
+
const count = it.sy === 1 ? n * 2 : n, pts = [];
|
|
153
|
+
for (let i = 0; i <= count; i++) {
|
|
154
|
+
const ang = rot - Math.PI / 2 + (i * 2 * Math.PI) / count, rr = it.sy === 1 && i % 2 ? ir : or;
|
|
155
|
+
const x = cx + Math.cos(ang) * rr, y = cy + Math.sin(ang) * rr;
|
|
156
|
+
if (!i)
|
|
157
|
+
pts.push(x, y);
|
|
158
|
+
else
|
|
159
|
+
pts.push(pts[pts.length - 2], pts[pts.length - 1], x, y, x, y);
|
|
160
|
+
}
|
|
161
|
+
return [{ pts, closed: true }];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
const txContour = (c, m) => {
|
|
167
|
+
const pts = c.pts.slice();
|
|
168
|
+
for (let i = 0; i < pts.length; i += 2)
|
|
169
|
+
[pts[i], pts[i + 1]] = ap(m, pts[i], pts[i + 1]);
|
|
170
|
+
return { pts, closed: c.closed };
|
|
171
|
+
};
|
|
172
|
+
/** Flatten a contour to a polyline (for trim paths). */
|
|
173
|
+
function flatten(c) {
|
|
174
|
+
const out = [c.pts[0], c.pts[1]];
|
|
175
|
+
for (let i = 2; i < c.pts.length; i += 6) {
|
|
176
|
+
const x0 = c.pts[i - 2], y0 = c.pts[i - 1];
|
|
177
|
+
const [x1, y1, x2, y2, x3, y3] = c.pts.slice(i, i + 6);
|
|
178
|
+
const straight = x1 === x0 && y1 === y0 && x2 === x3 && y2 === y3;
|
|
179
|
+
const steps = straight ? 1 : 16;
|
|
180
|
+
for (let s = 1; s <= steps; s++) {
|
|
181
|
+
const t = s / steps, u = 1 - t;
|
|
182
|
+
out.push(u * u * u * x0 + 3 * u * u * t * x1 + 3 * u * t * t * x2 + t * t * t * x3, u * u * u * y0 + 3 * u * u * t * y1 + 3 * u * t * t * y2 + t * t * t * y3);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
const polyLen = (p) => {
|
|
188
|
+
let l = 0;
|
|
189
|
+
for (let i = 2; i < p.length; i += 2)
|
|
190
|
+
l += Math.hypot(p[i] - p[i - 2], p[i + 1] - p[i - 1]);
|
|
191
|
+
return l;
|
|
192
|
+
};
|
|
193
|
+
/** Cut a polyline to [from, to] (lengths along it) → open polylines (as straight cubic contours). */
|
|
194
|
+
function cutPoly(p, from, to) {
|
|
195
|
+
const out = [];
|
|
196
|
+
let acc = 0;
|
|
197
|
+
for (let i = 2; i < p.length; i += 2) {
|
|
198
|
+
const x0 = p[i - 2], y0 = p[i - 1], x1 = p[i], y1 = p[i + 1], l = Math.hypot(x1 - x0, y1 - y0);
|
|
199
|
+
const a = Math.max(from, acc), b = Math.min(to, acc + l);
|
|
200
|
+
if (b > a && l) {
|
|
201
|
+
const ta = (a - acc) / l, tb = (b - acc) / l;
|
|
202
|
+
const ax = x0 + (x1 - x0) * ta, ay = y0 + (y1 - y0) * ta, bx = x0 + (x1 - x0) * tb, by = y0 + (y1 - y0) * tb;
|
|
203
|
+
if (!out.length)
|
|
204
|
+
out.push(ax, ay);
|
|
205
|
+
out.push(ax, ay, bx, by, bx, by);
|
|
206
|
+
}
|
|
207
|
+
acc += l;
|
|
208
|
+
}
|
|
209
|
+
return out.length ? [{ pts: out, closed: false }] : [];
|
|
210
|
+
}
|
|
211
|
+
/** Apply trim paths to contours ('simultaneously' = per contour, 'individually' = along all of them). */
|
|
212
|
+
function trimContours(cs, start, end, offset, mode) {
|
|
213
|
+
let s = start / 100, e = end / 100;
|
|
214
|
+
if (s > e)
|
|
215
|
+
[s, e] = [e, s];
|
|
216
|
+
const o = ((offset % 360) + 360) % 360 / 360;
|
|
217
|
+
if (e - s >= 1)
|
|
218
|
+
return cs;
|
|
219
|
+
if (e - s <= 0)
|
|
220
|
+
return [];
|
|
221
|
+
const polys = cs.map(flatten), lens = polys.map(polyLen);
|
|
222
|
+
const cut = (poly, L, a, b) => {
|
|
223
|
+
a += o;
|
|
224
|
+
b += o;
|
|
225
|
+
const parts = [];
|
|
226
|
+
for (const k of [-1, 0, 1])
|
|
227
|
+
parts.push(...cutPoly(poly, (a + k) * L, (b + k) * L));
|
|
228
|
+
return parts;
|
|
229
|
+
};
|
|
230
|
+
if (mode === 2) {
|
|
231
|
+
const total = lens.reduce((x, y) => x + y, 0), out = [];
|
|
232
|
+
let acc = 0;
|
|
233
|
+
polys.forEach((p, i) => {
|
|
234
|
+
const a = ((s + o) % 1) * total, b = a + (e - s) * total;
|
|
235
|
+
for (const k of [0, 1])
|
|
236
|
+
out.push(...cutPoly(p, a - acc + k * -total, b - acc + k * -total));
|
|
237
|
+
acc += lens[i];
|
|
238
|
+
});
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
return polys.flatMap((p, i) => cut(p, lens[i], s, e));
|
|
242
|
+
}
|
|
243
|
+
function toPath(cs) {
|
|
244
|
+
const P = new Path2D();
|
|
245
|
+
for (const c of cs) {
|
|
246
|
+
P.moveTo(c.pts[0], c.pts[1]);
|
|
247
|
+
for (let i = 2; i < c.pts.length; i += 6)
|
|
248
|
+
P.bezierCurveTo(c.pts[i], c.pts[i + 1], c.pts[i + 2], c.pts[i + 3], c.pts[i + 4], c.pts[i + 5]);
|
|
249
|
+
if (c.closed)
|
|
250
|
+
P.closePath();
|
|
251
|
+
}
|
|
252
|
+
return P;
|
|
253
|
+
}
|
|
254
|
+
// ------------------------------------------------------------------ colours
|
|
255
|
+
const col = (c, o = 1) => {
|
|
256
|
+
if (!c)
|
|
257
|
+
return 'transparent';
|
|
258
|
+
const big = c.some((v) => v > 1);
|
|
259
|
+
const v = c.map((x) => Math.round(Math.min(1, Math.max(0, big ? x / 255 : x)) * 255));
|
|
260
|
+
return `rgba(${v[0]},${v[1]},${v[2]},${Math.max(0, Math.min(1, (c[3] ?? 1) * o))})`;
|
|
261
|
+
};
|
|
262
|
+
function buildOps(items, f, parent, parentO, ops) {
|
|
263
|
+
const tr = items.find((x) => x.ty === 'tr');
|
|
264
|
+
const { m: tm, o: to } = transformAt(tr, f);
|
|
265
|
+
const m = mul(parent, tm), o = parentO * to;
|
|
266
|
+
const shapes = [];
|
|
267
|
+
const mine = [];
|
|
268
|
+
for (const it of items) {
|
|
269
|
+
if (it.hd)
|
|
270
|
+
continue;
|
|
271
|
+
if (it.ty === 'gr') {
|
|
272
|
+
const sub = [];
|
|
273
|
+
shapes.push(...buildOps(it.it || [], f, m, o, sub));
|
|
274
|
+
mine.push(...sub);
|
|
275
|
+
}
|
|
276
|
+
else if (it.ty === 'sh' || it.ty === 'rc' || it.ty === 'el' || it.ty === 'sr')
|
|
277
|
+
shapes.push({ c: shapePath(it, f).map((c) => txContour(c, m)) });
|
|
278
|
+
else if (it.ty === 'tm') {
|
|
279
|
+
// modifies every path above it (also paths already referenced by styles above — After Effects order)
|
|
280
|
+
const s = propValue(it.s, f, 0), e = propValue(it.e, f, 100), off = propValue(it.o, f, 0);
|
|
281
|
+
if (it.m === 2) {
|
|
282
|
+
const all = trimContours(shapes.flatMap((x) => x.c), s, e, off, 2);
|
|
283
|
+
shapes.forEach((x, i) => (x.c = i ? [] : all));
|
|
284
|
+
}
|
|
285
|
+
else
|
|
286
|
+
shapes.forEach((x) => (x.c = trimContours(x.c, s, e, off, 1)));
|
|
287
|
+
}
|
|
288
|
+
else if (it.ty === 'fl' || it.ty === 'gf')
|
|
289
|
+
mine.push({ kind: 'fill', it, shapes: shapes.slice(), m, o });
|
|
290
|
+
else if (it.ty === 'st' || it.ty === 'gs')
|
|
291
|
+
mine.push({ kind: 'stroke', it, shapes: shapes.slice(), m, o });
|
|
292
|
+
}
|
|
293
|
+
// items listed first paint on top → draw in reverse
|
|
294
|
+
ops.push(...mine.reverse());
|
|
295
|
+
return shapes;
|
|
296
|
+
}
|
|
297
|
+
function paint(ctx, op, f) {
|
|
298
|
+
const it = op.it;
|
|
299
|
+
const paths = op.shapes.flatMap((x) => x.c);
|
|
300
|
+
if (!paths.length)
|
|
301
|
+
return;
|
|
302
|
+
const opacity = (propValue(it.o, f, 100) / 100) * op.o;
|
|
303
|
+
let style;
|
|
304
|
+
if (it.ty === 'gf' || it.ty === 'gs') {
|
|
305
|
+
const sp = ap(op.m, ...propValue(it.s, f, [0, 0])), ep = ap(op.m, ...propValue(it.e, f, [0, 0]));
|
|
306
|
+
const g = it.t === 2 ? ctx.createRadialGradient(sp[0], sp[1], 0, sp[0], sp[1], Math.hypot(ep[0] - sp[0], ep[1] - sp[1])) : ctx.createLinearGradient(sp[0], sp[1], ep[0], ep[1]);
|
|
307
|
+
const n = it.g?.p || 0, k = propValue(it.g?.k, f, []) || [];
|
|
308
|
+
const alphas = k.length >= n * 4 + 2 ? k.slice(n * 4) : null;
|
|
309
|
+
for (let i = 0; i < n; i++) {
|
|
310
|
+
let a = 1;
|
|
311
|
+
if (alphas) {
|
|
312
|
+
// opacity stops: pairs [offset, alpha]; use the nearest one
|
|
313
|
+
let best = 1, bd = Infinity;
|
|
314
|
+
for (let j = 0; j + 1 < alphas.length; j += 2) {
|
|
315
|
+
const d = Math.abs(alphas[j] - k[i * 4]);
|
|
316
|
+
if (d < bd)
|
|
317
|
+
[bd, best] = [d, alphas[j + 1]];
|
|
318
|
+
}
|
|
319
|
+
a = best;
|
|
320
|
+
}
|
|
321
|
+
g.addColorStop(Math.min(1, Math.max(0, k[i * 4])), col([k[i * 4 + 1], k[i * 4 + 2], k[i * 4 + 3], a]));
|
|
322
|
+
}
|
|
323
|
+
style = g;
|
|
324
|
+
}
|
|
325
|
+
else
|
|
326
|
+
style = col(propValue(it.c, f, [0, 0, 0]));
|
|
327
|
+
ctx.save();
|
|
328
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, opacity));
|
|
329
|
+
const path = toPath(paths);
|
|
330
|
+
if (op.kind === 'fill') {
|
|
331
|
+
ctx.fillStyle = style;
|
|
332
|
+
ctx.fill(path, it.r === 2 ? 'evenodd' : 'nonzero');
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
const sc = Math.sqrt(Math.abs(op.m[0] * op.m[3] - op.m[1] * op.m[2])) || 1;
|
|
336
|
+
ctx.strokeStyle = style;
|
|
337
|
+
ctx.lineWidth = propValue(it.w, f, 1) * sc;
|
|
338
|
+
ctx.lineCap = ['butt', 'round', 'square'][(it.lc || 2) - 1] || 'round';
|
|
339
|
+
ctx.lineJoin = ['miter', 'round', 'bevel'][(it.lj || 2) - 1] || 'round';
|
|
340
|
+
ctx.miterLimit = it.ml || 4;
|
|
341
|
+
if (it.d?.length) {
|
|
342
|
+
const dashes = it.d.filter((d) => d.n === 'd' || d.n === 'g').map((d) => propValue(d.v, f, 0) * sc);
|
|
343
|
+
const off = it.d.find((d) => d.n === 'o');
|
|
344
|
+
if (dashes.length && dashes.some((x) => x > 0))
|
|
345
|
+
ctx.setLineDash(dashes.length % 2 ? [...dashes, ...dashes] : dashes);
|
|
346
|
+
if (off)
|
|
347
|
+
ctx.lineDashOffset = -propValue(off.v, f, 0) * sc;
|
|
348
|
+
}
|
|
349
|
+
ctx.stroke(path);
|
|
350
|
+
}
|
|
351
|
+
ctx.restore();
|
|
352
|
+
}
|
|
353
|
+
const scratch = (w, h) => {
|
|
354
|
+
const g = globalThis;
|
|
355
|
+
const c = typeof g.OffscreenCanvas === 'function' ? new g.OffscreenCanvas(w, h) : Object.assign(document.createElement('canvas'), { width: w, height: h });
|
|
356
|
+
return { c, x: c.getContext('2d') };
|
|
357
|
+
};
|
|
358
|
+
const localFrame = (L, f) => (f - (L.st || 0)) / (L.sr || 1);
|
|
359
|
+
function layerMatrix(L, all, f, depth = 0) {
|
|
360
|
+
const own = transformAt(L.ks, localFrame(L, f)).m;
|
|
361
|
+
if (L.parent === undefined || depth > 50)
|
|
362
|
+
return own;
|
|
363
|
+
const P = all.find((x) => x.ind === L.parent);
|
|
364
|
+
return P ? mul(layerMatrix(P, all, f, depth + 1), own) : own;
|
|
365
|
+
}
|
|
366
|
+
function maskPath(L, f, m, ctx, w, h) {
|
|
367
|
+
// composite the masks into the layer's pixels (already drawn in ctx)
|
|
368
|
+
const masks = (L.masksProperties || []).filter((k) => k.mode !== 'n');
|
|
369
|
+
if (!masks.length)
|
|
370
|
+
return;
|
|
371
|
+
const { c: mc, x: mx } = scratch(w, h);
|
|
372
|
+
mx.setTransform(ctx.getTransform());
|
|
373
|
+
masks.forEach((k, i) => {
|
|
374
|
+
const v = propValue(k.pt, f);
|
|
375
|
+
if (!v?.v)
|
|
376
|
+
return;
|
|
377
|
+
const path = toPath(shapePath({ ty: 'sh', ks: { k: v } }, f).map((c) => txContour(c, m)));
|
|
378
|
+
const o = (propValue(k.o, f, 100) ?? 100) / 100;
|
|
379
|
+
mx.save();
|
|
380
|
+
mx.fillStyle = `rgba(0,0,0,${o})`;
|
|
381
|
+
const mode = k.mode;
|
|
382
|
+
if (k.inv) {
|
|
383
|
+
const full = new Path2D();
|
|
384
|
+
full.rect(-1e5, -1e5, 2e5, 2e5);
|
|
385
|
+
full.addPath(path);
|
|
386
|
+
mx.globalCompositeOperation = mode === 's' ? 'destination-out' : mode === 'i' ? 'destination-in' : i ? 'source-over' : 'source-over';
|
|
387
|
+
mx.fill(full, 'evenodd');
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
mx.globalCompositeOperation = mode === 's' ? 'destination-out' : mode === 'i' && i ? 'destination-in' : 'source-over';
|
|
391
|
+
if (mode === 's' && !i) {
|
|
392
|
+
// first mask subtracts from "everything"
|
|
393
|
+
mx.save();
|
|
394
|
+
mx.setTransform(1, 0, 0, 1, 0, 0);
|
|
395
|
+
mx.globalCompositeOperation = 'source-over';
|
|
396
|
+
mx.fillRect(0, 0, w, h);
|
|
397
|
+
mx.restore();
|
|
398
|
+
mx.globalCompositeOperation = 'destination-out';
|
|
399
|
+
}
|
|
400
|
+
mx.fill(path);
|
|
401
|
+
}
|
|
402
|
+
mx.restore();
|
|
403
|
+
});
|
|
404
|
+
ctx.save();
|
|
405
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
406
|
+
ctx.globalCompositeOperation = 'destination-in';
|
|
407
|
+
ctx.drawImage(mc, 0, 0);
|
|
408
|
+
ctx.restore();
|
|
409
|
+
}
|
|
410
|
+
/** Draw frame `f` of an animation (or precomp layer list) into `ctx` with base matrix `base`. */
|
|
411
|
+
function drawLayers(ctx, anim, layers, f, o, base, w, h, depth = 0) {
|
|
412
|
+
if (depth > 16)
|
|
413
|
+
return;
|
|
414
|
+
for (let i = layers.length - 1; i >= 0; i--) {
|
|
415
|
+
const L = layers[i];
|
|
416
|
+
if (L.td || L.hd || L.ty === 3 || f < L.ip || f >= L.op)
|
|
417
|
+
continue;
|
|
418
|
+
const matte = L.tt ? layers[i - 1] : undefined;
|
|
419
|
+
const needsOff = !!(matte || (L.masksProperties && L.masksProperties.length));
|
|
420
|
+
const target = needsOff ? scratch(w, h) : null;
|
|
421
|
+
const x = target ? target.x : ctx;
|
|
422
|
+
x.save();
|
|
423
|
+
x.setTransform(ctx.getTransform());
|
|
424
|
+
const m = mul(base, layerMatrix(L, layers, f));
|
|
425
|
+
const lf = localFrame(L, f);
|
|
426
|
+
x.globalAlpha *= transformAt(L.ks, lf).o; // opacity is not inherited from parents (After Effects)
|
|
427
|
+
if (L.ty === 4) {
|
|
428
|
+
const ops = [];
|
|
429
|
+
buildOps(L.shapes || [], lf, m, 1, ops);
|
|
430
|
+
for (const p of ops)
|
|
431
|
+
paint(x, p, lf);
|
|
432
|
+
}
|
|
433
|
+
else if (L.ty === 1) {
|
|
434
|
+
x.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
435
|
+
x.fillStyle = L.sc || '#000';
|
|
436
|
+
x.fillRect(0, 0, L.sw || 0, L.sh || 0);
|
|
437
|
+
}
|
|
438
|
+
else if (L.ty === 2) {
|
|
439
|
+
const img = o.images?.[L.refId || ''];
|
|
440
|
+
if (img) {
|
|
441
|
+
x.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
442
|
+
x.drawImage(img, 0, 0);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
else if (L.ty === 0) {
|
|
446
|
+
const asset = anim.assets?.find((a) => a.id === L.refId);
|
|
447
|
+
if (asset?.layers) {
|
|
448
|
+
let cf = lf;
|
|
449
|
+
if (L.tm)
|
|
450
|
+
cf = propValue(L.tm, f, 0) * anim.fr;
|
|
451
|
+
x.save();
|
|
452
|
+
x.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
453
|
+
if (L.w && L.h) {
|
|
454
|
+
x.beginPath();
|
|
455
|
+
x.rect(0, 0, L.w, L.h);
|
|
456
|
+
x.clip();
|
|
457
|
+
}
|
|
458
|
+
drawLayers(x, anim, asset.layers, cf, o, I, w, h, depth + 1);
|
|
459
|
+
x.restore();
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (L.masksProperties?.length)
|
|
463
|
+
maskPath(L, lf, m, x, w, h);
|
|
464
|
+
x.restore();
|
|
465
|
+
if (target) {
|
|
466
|
+
if (matte) {
|
|
467
|
+
const mt = scratch(w, h);
|
|
468
|
+
mt.x.setTransform(ctx.getTransform());
|
|
469
|
+
const hidden = { ...matte, td: 0 };
|
|
470
|
+
drawLayers(mt.x, anim, [hidden], f, o, base, w, h, depth + 1);
|
|
471
|
+
target.x.save();
|
|
472
|
+
target.x.setTransform(1, 0, 0, 1, 0, 0);
|
|
473
|
+
target.x.globalCompositeOperation = L.tt === 2 || L.tt === 4 ? 'destination-out' : 'destination-in';
|
|
474
|
+
target.x.drawImage(mt.c, 0, 0);
|
|
475
|
+
target.x.restore();
|
|
476
|
+
}
|
|
477
|
+
ctx.save();
|
|
478
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
479
|
+
ctx.drawImage(target.c, 0, 0);
|
|
480
|
+
ctx.restore();
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/** Render one frame onto a 2D context sized `width × height` (the animation is fitted with `fit`). */
|
|
485
|
+
function renderLottieFrame(ctx, anim, frame, o = {}) {
|
|
486
|
+
const W = o.width ?? ctx.canvas.width, H = o.height ?? ctx.canvas.height;
|
|
487
|
+
ctx.save();
|
|
488
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
489
|
+
ctx.clearRect(0, 0, W, H);
|
|
490
|
+
if (o.background) {
|
|
491
|
+
ctx.fillStyle = o.background;
|
|
492
|
+
ctx.fillRect(0, 0, W, H);
|
|
493
|
+
}
|
|
494
|
+
const sx = W / anim.w, sy = H / anim.h;
|
|
495
|
+
const s = o.fit === 'cover' ? Math.max(sx, sy) : Math.min(sx, sy);
|
|
496
|
+
if (o.fit === 'fill')
|
|
497
|
+
ctx.setTransform(sx, 0, 0, sy, 0, 0);
|
|
498
|
+
else
|
|
499
|
+
ctx.setTransform(s, 0, 0, s, (W - anim.w * s) / 2, (H - anim.h * s) / 2);
|
|
500
|
+
ctx.beginPath();
|
|
501
|
+
ctx.rect(0, 0, anim.w, anim.h);
|
|
502
|
+
ctx.clip();
|
|
503
|
+
drawLayers(ctx, anim, anim.layers, frame, o, I, W, H);
|
|
504
|
+
ctx.restore();
|
|
505
|
+
}
|
|
506
|
+
/** List what a file uses that this renderer skips (3D layers, effects, text, expressions, merge paths, repeaters…). */
|
|
507
|
+
function inspectLottie(anim) {
|
|
508
|
+
const u = new Set();
|
|
509
|
+
const walkShapes = (items) => items?.forEach((it) => {
|
|
510
|
+
if (it.ty === 'gr')
|
|
511
|
+
walkShapes(it.it);
|
|
512
|
+
if (it.ty === 'mm')
|
|
513
|
+
u.add('merge paths');
|
|
514
|
+
if (it.ty === 'rp')
|
|
515
|
+
u.add('repeaters');
|
|
516
|
+
if (it.ty === 'rd')
|
|
517
|
+
u.add('round corners');
|
|
518
|
+
if (it.ty === 'pb')
|
|
519
|
+
u.add('pucker / bloat');
|
|
520
|
+
if (it.ty === 'tw')
|
|
521
|
+
u.add('twist');
|
|
522
|
+
});
|
|
523
|
+
const walk = (ls) => ls.forEach((l) => {
|
|
524
|
+
if (l.ddd)
|
|
525
|
+
u.add('3D layers');
|
|
526
|
+
if (l.ef?.length)
|
|
527
|
+
u.add('effects');
|
|
528
|
+
if (l.ty === 5)
|
|
529
|
+
u.add('text layers');
|
|
530
|
+
if (l.tt === 3 || l.tt === 4)
|
|
531
|
+
u.add('luma mattes (approximated by alpha)');
|
|
532
|
+
if (JSON.stringify(l.ks).includes('"x":"'))
|
|
533
|
+
u.add('expressions');
|
|
534
|
+
walkShapes(l.shapes || []);
|
|
535
|
+
});
|
|
536
|
+
walk(anim.layers);
|
|
537
|
+
anim.assets?.forEach((a) => a.layers && walk(a.layers));
|
|
538
|
+
return { layers: anim.layers.length, unsupported: [...u] };
|
|
539
|
+
}
|
|
540
|
+
/** Read a zip archive's entries (central directory; stored + deflate). Pure; inflating uses `DecompressionStream`. */
|
|
541
|
+
function unzipEntries(input) {
|
|
542
|
+
const b = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
543
|
+
const v = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
544
|
+
let e = b.length - 22;
|
|
545
|
+
while (e >= 0 && v.getUint32(e, true) !== 0x06054b50)
|
|
546
|
+
e--;
|
|
547
|
+
if (e < 0)
|
|
548
|
+
throw new Error('[motionary] vector: not a zip / .lottie file');
|
|
549
|
+
const count = v.getUint16(e + 10, true);
|
|
550
|
+
let p = v.getUint32(e + 16, true);
|
|
551
|
+
const out = [];
|
|
552
|
+
for (let i = 0; i < count; i++) {
|
|
553
|
+
if (v.getUint32(p, true) !== 0x02014b50)
|
|
554
|
+
break;
|
|
555
|
+
const method = v.getUint16(p + 10, true), csize = v.getUint32(p + 20, true), nlen = v.getUint16(p + 28, true), xlen = v.getUint16(p + 30, true), clen = v.getUint16(p + 32, true), lho = v.getUint32(p + 42, true);
|
|
556
|
+
const name = new TextDecoder().decode(b.subarray(p + 46, p + 46 + nlen));
|
|
557
|
+
const lnl = v.getUint16(lho + 26, true), lxl = v.getUint16(lho + 28, true);
|
|
558
|
+
const start = lho + 30 + lnl + lxl;
|
|
559
|
+
out.push({ name, method, data: b.subarray(start, start + csize) });
|
|
560
|
+
p += 46 + nlen + xlen + clen;
|
|
561
|
+
}
|
|
562
|
+
return out;
|
|
563
|
+
}
|
|
564
|
+
async function inflate(e) {
|
|
565
|
+
if (e.method === 0)
|
|
566
|
+
return e.data;
|
|
567
|
+
if (e.method !== 8)
|
|
568
|
+
throw new Error(`[motionary] vector: zip method ${e.method} unsupported (${e.name})`);
|
|
569
|
+
const DS = globalThis.DecompressionStream;
|
|
570
|
+
if (!DS)
|
|
571
|
+
throw new Error('[motionary] vector: DecompressionStream is missing — cannot inflate .lottie entries here');
|
|
572
|
+
const s = new Response(e.data).body.pipeThrough(new DS('deflate-raw'));
|
|
573
|
+
return new Uint8Array(await new Response(s).arrayBuffer());
|
|
574
|
+
}
|
|
575
|
+
/** Unpack a `.lottie` (dotLottie v1 or v2). */
|
|
576
|
+
async function parseDotLottie(input) {
|
|
577
|
+
const entries = unzipEntries(input);
|
|
578
|
+
const files = {};
|
|
579
|
+
await Promise.all(entries.filter((e) => !e.name.endsWith('/')).map(async (e) => (files[e.name.replace(/^\.?\//, '')] = await inflate(e))));
|
|
580
|
+
const td = new TextDecoder();
|
|
581
|
+
const json = (n) => JSON.parse(td.decode(files[n]));
|
|
582
|
+
const manifest = files['manifest.json'] ? json('manifest.json') : { animations: [] };
|
|
583
|
+
const animations = {}, images = {}, themes = {}, stateMachines = {};
|
|
584
|
+
for (const [name, data] of Object.entries(files)) {
|
|
585
|
+
const m = /^(animations|a)\/(.+)\.json$/.exec(name);
|
|
586
|
+
if (m)
|
|
587
|
+
animations[m[2]] = JSON.parse(td.decode(data));
|
|
588
|
+
else if (/^(images|i)\//.test(name))
|
|
589
|
+
images[name.replace(/^(images|i)\//, '')] = data;
|
|
590
|
+
else if (/^(themes|t)\/(.+)\.json$/.test(name))
|
|
591
|
+
themes[name.replace(/^(themes|t)\/|\.json$/g, '')] = JSON.parse(td.decode(data));
|
|
592
|
+
else if (/^(states|s)\/(.+)\.json$/.test(name))
|
|
593
|
+
stateMachines[name.replace(/^(states|s)\/|\.json$/g, '')] = JSON.parse(td.decode(data));
|
|
594
|
+
}
|
|
595
|
+
return { manifest, animations, images, themes, stateMachines };
|
|
596
|
+
}
|
|
597
|
+
/** Decode the images an animation references (embedded data URIs, dotLottie images, or URLs relative to `base`). */
|
|
598
|
+
async function loadLottieImages(anim, o = {}) {
|
|
599
|
+
const out = {};
|
|
600
|
+
await Promise.all((anim.assets || []).filter((a) => a.p && !a.layers).map(async (a) => {
|
|
601
|
+
let blob = null;
|
|
602
|
+
if (a.p.startsWith('data:'))
|
|
603
|
+
blob = await (await fetch(a.p)).blob();
|
|
604
|
+
else {
|
|
605
|
+
const key = Object.keys(o.files || {}).find((k) => k.endsWith('/' + a.p) || k === a.p);
|
|
606
|
+
if (key)
|
|
607
|
+
blob = new Blob([o.files[key]], { type: /\.jpe?g$/i.test(key) ? 'image/jpeg' : /\.webp$/i.test(key) ? 'image/webp' : 'image/png' });
|
|
608
|
+
else if (o.base)
|
|
609
|
+
blob = await (await fetch(new URL((a.u || '') + a.p, o.base).href)).blob();
|
|
610
|
+
}
|
|
611
|
+
if (blob)
|
|
612
|
+
out[a.id] = await createImageBitmap(blob);
|
|
613
|
+
}));
|
|
614
|
+
return out;
|
|
615
|
+
}
|
|
616
|
+
/** Fetch a `.json` Lottie or a `.lottie` file. `animation` picks one of several in a dotLottie (default: the manifest's first / active one). */
|
|
617
|
+
async function loadLottie(src, o = {}) {
|
|
618
|
+
let bytes;
|
|
619
|
+
const base = o.base || (typeof src === 'string' && typeof location !== 'undefined' ? new URL(src, location.href).href : undefined);
|
|
620
|
+
if (typeof src === 'string') {
|
|
621
|
+
const r = await fetch(src);
|
|
622
|
+
if (!r.ok)
|
|
623
|
+
throw new Error(`[motionary] vector: could not load ${src} (${r.status})`);
|
|
624
|
+
bytes = new Uint8Array(await r.arrayBuffer());
|
|
625
|
+
}
|
|
626
|
+
else
|
|
627
|
+
bytes = src instanceof Uint8Array ? src : new Uint8Array(src);
|
|
628
|
+
if (bytes[0] === 0x50 && bytes[1] === 0x4b) {
|
|
629
|
+
const dl = await parseDotLottie(bytes);
|
|
630
|
+
const ids = Object.keys(dl.animations);
|
|
631
|
+
const want = o.animation || dl.manifest.activeAnimationId || dl.manifest.animations?.[0]?.id || ids[0];
|
|
632
|
+
const animation = dl.animations[want] || dl.animations[ids[0]];
|
|
633
|
+
if (!animation)
|
|
634
|
+
throw new Error('[motionary] vector: the .lottie file has no animations');
|
|
635
|
+
const files = {};
|
|
636
|
+
for (const [k, v] of Object.entries(dl.images))
|
|
637
|
+
files[k] = v;
|
|
638
|
+
return { animation, images: await loadLottieImages(animation, { files }), dotLottie: dl };
|
|
639
|
+
}
|
|
640
|
+
const animation = JSON.parse(new TextDecoder().decode(bytes));
|
|
641
|
+
return { animation, images: await loadLottieImages(animation, { base }), dotLottie: null };
|
|
642
|
+
}
|
|
643
|
+
class LPlayer extends Playable {
|
|
644
|
+
constructor(canvas, animation, o) {
|
|
645
|
+
super({ repeat: o.loop === true || o.loop === undefined ? -1 : typeof o.loop === 'number' ? o.loop : 0, yoyo: !!o.bounce });
|
|
646
|
+
this.canvas = canvas;
|
|
647
|
+
this.animation = animation;
|
|
648
|
+
this.o = o;
|
|
649
|
+
this.frame = 0;
|
|
650
|
+
this.markers = {};
|
|
651
|
+
this.timeScale = o.speed || 1;
|
|
652
|
+
const c = canvas.getContext('2d');
|
|
653
|
+
if (!c)
|
|
654
|
+
throw new Error('[motionary] vector: no 2D canvas context');
|
|
655
|
+
this.ctx = c;
|
|
656
|
+
for (const m of animation.markers || [])
|
|
657
|
+
this.markers[m.cm] = [m.tm, m.tm + m.dr];
|
|
658
|
+
this.seg = [animation.ip, animation.op];
|
|
659
|
+
this.setSegment(o.segment || null);
|
|
660
|
+
this.goToFrame(this.seg[0]);
|
|
661
|
+
if (o.autoplay !== false)
|
|
662
|
+
this.play();
|
|
663
|
+
}
|
|
664
|
+
get totalFrames() {
|
|
665
|
+
return this.seg[1] - this.seg[0];
|
|
666
|
+
}
|
|
667
|
+
get duration() {
|
|
668
|
+
return (this.totalFrames / this.animation.fr) * 1000;
|
|
669
|
+
}
|
|
670
|
+
setSegment(s) {
|
|
671
|
+
this.seg = !s ? [this.animation.ip, this.animation.op] : typeof s === 'string' ? this.markers[s] || [this.animation.ip, this.animation.op] : s;
|
|
672
|
+
}
|
|
673
|
+
goToFrame(f) {
|
|
674
|
+
this.frame = f;
|
|
675
|
+
renderLottieFrame(this.ctx, this.animation, f, this.o);
|
|
676
|
+
}
|
|
677
|
+
renderLocal(ms) {
|
|
678
|
+
const f = Math.min(this.seg[0] + (ms / 1000) * this.animation.fr, this.seg[1] - 0.001);
|
|
679
|
+
if (Math.abs(f - this.frame) < 0.01)
|
|
680
|
+
return;
|
|
681
|
+
this.goToFrame(f);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/** Play a Lottie animation on a canvas as a runtime timeline. */
|
|
685
|
+
function lottiePlayer(canvas, animation, o = {}) {
|
|
686
|
+
return new LPlayer(canvas, animation, o);
|
|
687
|
+
}
|
|
688
|
+
/** The module object for `use(vector)`. */
|
|
689
|
+
const vector = { id: 'vector', version: RUNTIME_VERSION, requires: ['core'], api: { loadLottie, parseDotLottie, unzipEntries, lottiePlayer, renderLottieFrame, inspectLottie, propValue, transformAt, trimContours, loadLottieImages } };
|
|
690
|
+
|
|
691
|
+
export { inspectLottie, loadLottie, loadLottieImages, lottiePlayer, parseDotLottie, propValue, renderLottieFrame, transformAt, trimContours, unzipEntries, vector };
|
|
692
|
+
//# sourceMappingURL=vector.js.map
|