@algorisys/tinyfly 0.64.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 +21 -0
- package/README.md +672 -0
- package/bin/tinyfly.mjs +59 -0
- package/lib/addons/adapters.js +756 -0
- package/lib/addons/dom-adapter-ChrbtkLC.js +243 -0
- package/lib/addons/drivers.js +72 -0
- package/lib/addons/export.js +1169 -0
- package/lib/addons/gsap-compat.js +2576 -0
- package/lib/addons/interaction.js +225 -0
- package/lib/addons/smooth-scroll-DaSacemU.js +671 -0
- package/lib/addons/teach.js +215 -0
- package/lib/browser/tinyfly.iife.js +2 -0
- package/lib/browser/tinyfly.js +6372 -0
- package/lib/browser/tinyfly.umd.js +2 -0
- package/lib/cli/tools.js +2325 -0
- package/lib/embed/tinyfly-embed.iife.js +18 -0
- package/lib/embed/tinyfly-embed.js +2316 -0
- package/lib/engine/tinyfly-engine.js +1972 -0
- package/lib/engine/tinyfly-engine.umd.cjs +2 -0
- package/lib/frameworks/react.js +32 -0
- package/lib/frameworks/solid.js +27 -0
- package/lib/frameworks/svelte.js +15 -0
- package/lib/frameworks/vue.js +29 -0
- package/lib/player/tinyfly-player.es.js +2306 -0
- package/lib/player/tinyfly-player.iife.js +2 -0
- package/lib/player/tinyfly-player.umd.js +2 -0
- package/lib/types/adapters/canvas/canvas-adapter.d.ts +238 -0
- package/lib/types/adapters/canvas/index.d.ts +1 -0
- package/lib/types/adapters/dom/dom-adapter.d.ts +50 -0
- package/lib/types/adapters/dom/flip.d.ts +60 -0
- package/lib/types/adapters/dom/index.d.ts +2 -0
- package/lib/types/adapters/filter-utils.d.ts +32 -0
- package/lib/types/adapters/index.d.ts +5 -0
- package/lib/types/adapters/shine-utils.d.ts +26 -0
- package/lib/types/adapters/svg/index.d.ts +1 -0
- package/lib/types/adapters/svg/svg-adapter.d.ts +47 -0
- package/lib/types/adapters/svg-transform-box.d.ts +13 -0
- package/lib/types/adapters/text-content.d.ts +11 -0
- package/lib/types/adapters/webgl/index.d.ts +1 -0
- package/lib/types/adapters/webgl/webgl-adapter.d.ts +69 -0
- package/lib/types/browser/index.d.ts +41 -0
- package/lib/types/compat/gsap/custom-eases.d.ts +28 -0
- package/lib/types/compat/gsap/defaults.d.ts +19 -0
- package/lib/types/compat/gsap/draw-svg-vars.d.ts +36 -0
- package/lib/types/compat/gsap/ease-map.d.ts +51 -0
- package/lib/types/compat/gsap/image-sequence.d.ts +61 -0
- package/lib/types/compat/gsap/index.d.ts +65 -0
- package/lib/types/compat/gsap/inertia-vars.d.ts +39 -0
- package/lib/types/compat/gsap/keyframes-vars.d.ts +8 -0
- package/lib/types/compat/gsap/layout.d.ts +6 -0
- package/lib/types/compat/gsap/live-context.d.ts +87 -0
- package/lib/types/compat/gsap/live-draggable.d.ts +75 -0
- package/lib/types/compat/gsap/live-flip.d.ts +73 -0
- package/lib/types/compat/gsap/live-morph.d.ts +18 -0
- package/lib/types/compat/gsap/live-motion-path.d.ts +33 -0
- package/lib/types/compat/gsap/live-scroll-to.d.ts +52 -0
- package/lib/types/compat/gsap/live-scroll.d.ts +108 -0
- package/lib/types/compat/gsap/live-transition.d.ts +40 -0
- package/lib/types/compat/gsap/live.d.ts +341 -0
- package/lib/types/compat/gsap/morph-vars.d.ts +21 -0
- package/lib/types/compat/gsap/motion-path-vars.d.ts +38 -0
- package/lib/types/compat/gsap/playhead-crossings.d.ts +47 -0
- package/lib/types/compat/gsap/position.d.ts +37 -0
- package/lib/types/compat/gsap/quick-play.d.ts +38 -0
- package/lib/types/compat/gsap/split-text.d.ts +66 -0
- package/lib/types/compat/gsap/spring-vars.d.ts +34 -0
- package/lib/types/compat/gsap/stage.d.ts +182 -0
- package/lib/types/compat/gsap/stagger-grid.d.ts +30 -0
- package/lib/types/compat/gsap/text-vars.d.ts +39 -0
- package/lib/types/compat/gsap/timeline.d.ts +161 -0
- package/lib/types/compat/gsap/utils.d.ts +60 -0
- package/lib/types/compat/gsap/vars.d.ts +79 -0
- package/lib/types/drivers/index.d.ts +8 -0
- package/lib/types/drivers/scroll-driver.d.ts +218 -0
- package/lib/types/drivers/scroll-markers.d.ts +41 -0
- package/lib/types/drivers/scroll-math.d.ts +87 -0
- package/lib/types/drivers/scroll-pin.d.ts +45 -0
- package/lib/types/drivers/scroll-snap.d.ts +47 -0
- package/lib/types/drivers/smooth-scroll.d.ts +118 -0
- package/lib/types/drivers/types.d.ts +23 -0
- package/lib/types/drivers/visibility-driver.d.ts +56 -0
- package/lib/types/embed/controls.d.ts +46 -0
- package/lib/types/embed/index.d.ts +17 -0
- package/lib/types/embed/mount.d.ts +41 -0
- package/lib/types/embed/tools.d.ts +33 -0
- package/lib/types/engine/authoring/custom-ease.d.ts +49 -0
- package/lib/types/engine/authoring/index.d.ts +3 -0
- package/lib/types/engine/authoring/random.d.ts +38 -0
- package/lib/types/engine/authoring/resolve-values.d.ts +57 -0
- package/lib/types/engine/core/bake.d.ts +66 -0
- package/lib/types/engine/core/clock.d.ts +44 -0
- package/lib/types/engine/core/index.d.ts +8 -0
- package/lib/types/engine/core/inertia.d.ts +30 -0
- package/lib/types/engine/core/playhead-crossings.d.ts +47 -0
- package/lib/types/engine/core/spring.d.ts +132 -0
- package/lib/types/engine/core/stagger.d.ts +40 -0
- package/lib/types/engine/core/timeline.d.ts +203 -0
- package/lib/types/engine/core/track.d.ts +90 -0
- package/lib/types/engine/export/byte-writer.d.ts +36 -0
- package/lib/types/engine/export/css.d.ts +31 -0
- package/lib/types/engine/export/gif-decode.test-helper.d.ts +24 -0
- package/lib/types/engine/export/gif.d.ts +134 -0
- package/lib/types/engine/export/index.d.ts +9 -0
- package/lib/types/engine/export/lottie.d.ts +104 -0
- package/lib/types/engine/export/mp4.d.ts +64 -0
- package/lib/types/engine/export/quantize.d.ts +56 -0
- package/lib/types/engine/export/sprite-sheet.d.ts +46 -0
- package/lib/types/engine/export/video.d.ts +99 -0
- package/lib/types/engine/export/webp.d.ts +82 -0
- package/lib/types/engine/index.d.ts +7 -0
- package/lib/types/engine/interpolation/easing.d.ts +65 -0
- package/lib/types/engine/interpolation/index.d.ts +2 -0
- package/lib/types/engine/interpolation/interpolators.d.ts +25 -0
- package/lib/types/engine/path/index.d.ts +11 -0
- package/lib/types/engine/path/motion-path.d.ts +24 -0
- package/lib/types/engine/path/path-builders.d.ts +38 -0
- package/lib/types/engine/path/path-morph.d.ts +47 -0
- package/lib/types/engine/path/path-utils.d.ts +73 -0
- package/lib/types/engine/serialization/index.d.ts +1 -0
- package/lib/types/engine/serialization/json.d.ts +30 -0
- package/lib/types/engine/text/index.d.ts +2 -0
- package/lib/types/engine/text/text-value.d.ts +16 -0
- package/lib/types/engine/types.d.ts +401 -0
- package/lib/types/frameworks/react.d.ts +35 -0
- package/lib/types/frameworks/solid.d.ts +16 -0
- package/lib/types/frameworks/svelte.d.ts +19 -0
- package/lib/types/frameworks/vue.d.ts +27 -0
- package/lib/types/interaction/draggable.d.ts +133 -0
- package/lib/types/interaction/index.d.ts +3 -0
- package/lib/types/interaction/observer.d.ts +80 -0
- package/lib/types/interaction/snap.d.ts +30 -0
- package/lib/types/player/index.d.ts +7 -0
- package/lib/types/player/media-sync.d.ts +60 -0
- package/lib/types/player/player.d.ts +238 -0
- package/lib/types/player/sequence-types.d.ts +68 -0
- package/lib/types/player/sequencer.d.ts +91 -0
- package/lib/types/teach/diagrams.d.ts +137 -0
- package/lib/types/teach/index.d.ts +11 -0
- package/lib/types/teach/lesson.d.ts +55 -0
- package/package.json +170 -0
package/lib/cli/tools.js
ADDED
|
@@ -0,0 +1,2325 @@
|
|
|
1
|
+
function isCubicBezierEasing(easing) {
|
|
2
|
+
return typeof easing === "object" && easing !== null && easing.type === "cubic-bezier";
|
|
3
|
+
}
|
|
4
|
+
function isParametricEasing(easing) {
|
|
5
|
+
return typeof easing === "object" && easing !== null && easing.type !== "cubic-bezier";
|
|
6
|
+
}
|
|
7
|
+
const FORMAT_VERSION = 1;
|
|
8
|
+
function isTextTrack(track) {
|
|
9
|
+
return track.property === "text" && "textConfig" in track;
|
|
10
|
+
}
|
|
11
|
+
function isInertiaTrack(track) {
|
|
12
|
+
return track.kind === "inertia" && "inertia" in track;
|
|
13
|
+
}
|
|
14
|
+
function isSpringTrack(track) {
|
|
15
|
+
return track.kind === "spring" && "spring" in track;
|
|
16
|
+
}
|
|
17
|
+
function isMotionPathTrack(track) {
|
|
18
|
+
return track.property === "motionPath" && "motionPathConfig" in track;
|
|
19
|
+
}
|
|
20
|
+
function staggerDistance(index, count, from = "start") {
|
|
21
|
+
if (count <= 1) return 0;
|
|
22
|
+
if (typeof from === "number") {
|
|
23
|
+
const anchor = Math.max(0, Math.min(count - 1, from));
|
|
24
|
+
return Math.abs(index - anchor);
|
|
25
|
+
}
|
|
26
|
+
switch (from) {
|
|
27
|
+
case "end":
|
|
28
|
+
return count - 1 - index;
|
|
29
|
+
case "center":
|
|
30
|
+
return Math.abs(index - (count - 1) / 2);
|
|
31
|
+
case "edges":
|
|
32
|
+
return (count - 1) / 2 - Math.abs(index - (count - 1) / 2);
|
|
33
|
+
case "start":
|
|
34
|
+
default:
|
|
35
|
+
return index;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function maxStaggerDistance(count, from = "start") {
|
|
39
|
+
if (count <= 1) return 0;
|
|
40
|
+
let max = 0;
|
|
41
|
+
for (let i = 0; i < count; i++) {
|
|
42
|
+
max = Math.max(max, staggerDistance(i, count, from));
|
|
43
|
+
}
|
|
44
|
+
return max;
|
|
45
|
+
}
|
|
46
|
+
function staggerOffset(index, count, config) {
|
|
47
|
+
if (config.offsets) return config.offsets[index] ?? 0;
|
|
48
|
+
const from = config.from ?? "start";
|
|
49
|
+
const distance = staggerDistance(index, count, from);
|
|
50
|
+
if (config.amount !== void 0) {
|
|
51
|
+
const max = maxStaggerDistance(count, from);
|
|
52
|
+
if (max === 0) return 0;
|
|
53
|
+
return config.amount * distance / max;
|
|
54
|
+
}
|
|
55
|
+
if (config.each !== void 0) {
|
|
56
|
+
return config.each * distance;
|
|
57
|
+
}
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
function staggerOffsets(count, config) {
|
|
61
|
+
return Array.from({ length: count }, (_, i) => staggerOffset(i, count, config));
|
|
62
|
+
}
|
|
63
|
+
function staggerSpan(count, config) {
|
|
64
|
+
if (count <= 1) return 0;
|
|
65
|
+
return Math.max(...staggerOffsets(count, config));
|
|
66
|
+
}
|
|
67
|
+
const SPRING_STEP_MS = 1;
|
|
68
|
+
const SPRING_MAX_DURATION_MS = 6e4;
|
|
69
|
+
const MAX_STEPS = SPRING_MAX_DURATION_MS / SPRING_STEP_MS;
|
|
70
|
+
const DEFAULT_SPRING = {
|
|
71
|
+
stiffness: 180,
|
|
72
|
+
damping: 12,
|
|
73
|
+
mass: 1,
|
|
74
|
+
velocity: 0,
|
|
75
|
+
restDelta: 0.01,
|
|
76
|
+
restSpeed: 0.1
|
|
77
|
+
};
|
|
78
|
+
class SpringSampler {
|
|
79
|
+
from;
|
|
80
|
+
to;
|
|
81
|
+
stiffness;
|
|
82
|
+
damping;
|
|
83
|
+
mass;
|
|
84
|
+
restDelta;
|
|
85
|
+
restSpeed;
|
|
86
|
+
/** value[i] is the spring's position at time i * SPRING_STEP_MS */
|
|
87
|
+
/**
|
|
88
|
+
* Travel distance, used to scale the rest thresholds.
|
|
89
|
+
*
|
|
90
|
+
* Without this the thresholds are absolute, and a spring animating `scale`
|
|
91
|
+
* from 0 to 1 hits them ~100x sooner than one animating `x` from 0 to 100 —
|
|
92
|
+
* so the small one is declared "settled" at its first pass through the
|
|
93
|
+
* target and never shows the overshoot at all. Scaling by travel makes
|
|
94
|
+
* settling depend on the spring's parameters, not on the units of whatever
|
|
95
|
+
* property it happens to drive.
|
|
96
|
+
*/
|
|
97
|
+
distance;
|
|
98
|
+
samples;
|
|
99
|
+
velocity;
|
|
100
|
+
/** Once at rest we stop simulating; every later time returns `to`. */
|
|
101
|
+
settledStep = null;
|
|
102
|
+
constructor(config) {
|
|
103
|
+
this.from = config.from;
|
|
104
|
+
this.to = config.to;
|
|
105
|
+
this.stiffness = config.stiffness ?? DEFAULT_SPRING.stiffness;
|
|
106
|
+
this.damping = config.damping ?? DEFAULT_SPRING.damping;
|
|
107
|
+
this.mass = config.mass ?? DEFAULT_SPRING.mass;
|
|
108
|
+
this.restDelta = config.restDelta ?? DEFAULT_SPRING.restDelta;
|
|
109
|
+
this.restSpeed = config.restSpeed ?? DEFAULT_SPRING.restSpeed;
|
|
110
|
+
this.distance = Math.abs(this.to - this.from) || 1;
|
|
111
|
+
this.samples = [this.from];
|
|
112
|
+
this.velocity = config.velocity ?? DEFAULT_SPRING.velocity;
|
|
113
|
+
if (this.isAtRest(this.from)) {
|
|
114
|
+
this.settledStep = 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Whether a position/velocity pair counts as settled.
|
|
119
|
+
*
|
|
120
|
+
* Both thresholds are fractions of the spring's travel distance:
|
|
121
|
+
* `restDelta` as a fraction of the distance, and `restSpeed` as a fraction
|
|
122
|
+
* of the distance per second. That keeps settling scale-invariant.
|
|
123
|
+
*/
|
|
124
|
+
isAtRest(position) {
|
|
125
|
+
return Math.abs(position - this.to) < this.restDelta * this.distance && Math.abs(this.velocity) < this.restSpeed * this.distance;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Position at `timeMs`. Times before 0 clamp to the start value; times past
|
|
129
|
+
* settling return the target exactly.
|
|
130
|
+
*/
|
|
131
|
+
valueAt(timeMs) {
|
|
132
|
+
if (timeMs <= 0) return this.from;
|
|
133
|
+
const step = Math.floor(timeMs / SPRING_STEP_MS);
|
|
134
|
+
this.simulateTo(step + 1);
|
|
135
|
+
if (this.settledStep !== null && step >= this.settledStep) {
|
|
136
|
+
return this.to;
|
|
137
|
+
}
|
|
138
|
+
const a = this.samples[Math.min(step, this.samples.length - 1)];
|
|
139
|
+
const b = this.samples[Math.min(step + 1, this.samples.length - 1)];
|
|
140
|
+
const frac = timeMs / SPRING_STEP_MS - step;
|
|
141
|
+
return a + (b - a) * frac;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* How long the spring takes to settle, in milliseconds — the natural duration
|
|
145
|
+
* of a spring track. Runs the simulation to completion once.
|
|
146
|
+
*/
|
|
147
|
+
settleTime() {
|
|
148
|
+
this.simulateTo(MAX_STEPS + 1);
|
|
149
|
+
if (this.settledStep !== null) {
|
|
150
|
+
return this.settledStep * SPRING_STEP_MS;
|
|
151
|
+
}
|
|
152
|
+
return SPRING_MAX_DURATION_MS;
|
|
153
|
+
}
|
|
154
|
+
/** Advance the cached simulation until it holds at least `steps` samples. */
|
|
155
|
+
simulateTo(steps) {
|
|
156
|
+
if (this.settledStep !== null) return;
|
|
157
|
+
const limit = Math.min(steps, MAX_STEPS + 1);
|
|
158
|
+
const dt = SPRING_STEP_MS / 1e3;
|
|
159
|
+
while (this.samples.length < limit) {
|
|
160
|
+
const position = this.samples[this.samples.length - 1];
|
|
161
|
+
const displacement = position - this.to;
|
|
162
|
+
const springForce = -this.stiffness * displacement;
|
|
163
|
+
const dampingForce = -this.damping * this.velocity;
|
|
164
|
+
const acceleration = (springForce + dampingForce) / this.mass;
|
|
165
|
+
this.velocity += acceleration * dt;
|
|
166
|
+
const next = position + this.velocity * dt;
|
|
167
|
+
this.samples.push(next);
|
|
168
|
+
if (this.isAtRest(next)) {
|
|
169
|
+
this.settledStep = this.samples.length - 1;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (this.samples.length > MAX_STEPS) {
|
|
174
|
+
this.settledStep = MAX_STEPS;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const DEFAULT_INERTIA_FRICTION = 4;
|
|
179
|
+
const REST_FRACTION = 2e-3;
|
|
180
|
+
const MIN_REST_DELTA = 1e-4;
|
|
181
|
+
const INERTIA_MAX_DURATION_MS = 6e4;
|
|
182
|
+
function frictionOf(config) {
|
|
183
|
+
const k = config.friction ?? DEFAULT_INERTIA_FRICTION;
|
|
184
|
+
return k > 0 ? k : DEFAULT_INERTIA_FRICTION;
|
|
185
|
+
}
|
|
186
|
+
function naturalRest(config) {
|
|
187
|
+
return config.from + config.velocity / frictionOf(config);
|
|
188
|
+
}
|
|
189
|
+
function snap(value, end) {
|
|
190
|
+
if (end === void 0) return value;
|
|
191
|
+
if (typeof end === "number") {
|
|
192
|
+
return end > 0 ? Math.round(value / end) * end : value;
|
|
193
|
+
}
|
|
194
|
+
if (end.length === 0) return value;
|
|
195
|
+
let best = end[0];
|
|
196
|
+
for (const candidate of end) {
|
|
197
|
+
if (Math.abs(candidate - value) < Math.abs(best - value)) best = candidate;
|
|
198
|
+
}
|
|
199
|
+
return best;
|
|
200
|
+
}
|
|
201
|
+
function inertiaRest(config) {
|
|
202
|
+
let rest = snap(naturalRest(config), config.end);
|
|
203
|
+
if (config.min !== void 0) rest = Math.max(config.min, rest);
|
|
204
|
+
if (config.max !== void 0) rest = Math.min(config.max, rest);
|
|
205
|
+
return rest;
|
|
206
|
+
}
|
|
207
|
+
function inertiaDuration(config) {
|
|
208
|
+
const distance = Math.abs(inertiaRest(config) - config.from);
|
|
209
|
+
if (distance === 0) return 0;
|
|
210
|
+
const restDelta = config.restDelta ?? Math.max(MIN_REST_DELTA, distance * REST_FRACTION);
|
|
211
|
+
if (restDelta >= distance) return 0;
|
|
212
|
+
const seconds = Math.log(distance / restDelta) / frictionOf(config);
|
|
213
|
+
return Math.min(INERTIA_MAX_DURATION_MS, seconds * 1e3);
|
|
214
|
+
}
|
|
215
|
+
function inertiaValueAt(config, timeMs) {
|
|
216
|
+
if (timeMs <= 0) return config.from;
|
|
217
|
+
const rest = inertiaRest(config);
|
|
218
|
+
if (timeMs >= inertiaDuration(config)) return rest;
|
|
219
|
+
const k = frictionOf(config);
|
|
220
|
+
return config.from + (rest - config.from) * (1 - Math.exp(-k * timeMs / 1e3));
|
|
221
|
+
}
|
|
222
|
+
const linear = (t) => t;
|
|
223
|
+
const easeInQuad = (t) => t * t;
|
|
224
|
+
const easeOutQuad = (t) => 1 - (1 - t) * (1 - t);
|
|
225
|
+
const easeInOutQuad = (t) => t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
|
|
226
|
+
const easeInCubic = (t) => t * t * t;
|
|
227
|
+
const easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
|
|
228
|
+
const easeInOutCubic = (t) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
229
|
+
const easeIn = easeInCubic;
|
|
230
|
+
const easeOut = easeOutCubic;
|
|
231
|
+
const easeInOut = easeInOutCubic;
|
|
232
|
+
const easingMap = {
|
|
233
|
+
"linear": linear,
|
|
234
|
+
"ease-in": easeIn,
|
|
235
|
+
"ease-out": easeOut,
|
|
236
|
+
"ease-in-out": easeInOut,
|
|
237
|
+
"ease-in-quad": easeInQuad,
|
|
238
|
+
"ease-out-quad": easeOutQuad,
|
|
239
|
+
"ease-in-out-quad": easeInOutQuad,
|
|
240
|
+
"ease-in-cubic": easeInCubic,
|
|
241
|
+
"ease-out-cubic": easeOutCubic,
|
|
242
|
+
"ease-in-out-cubic": easeInOutCubic
|
|
243
|
+
};
|
|
244
|
+
function createCubicBezier(points) {
|
|
245
|
+
const [cp1x, cp1y, cp2x, cp2y] = points;
|
|
246
|
+
const cx = 3 * cp1x;
|
|
247
|
+
const bx = 3 * (cp2x - cp1x) - cx;
|
|
248
|
+
const ax = 1 - cx - bx;
|
|
249
|
+
const cy = 3 * cp1y;
|
|
250
|
+
const by = 3 * (cp2y - cp1y) - cy;
|
|
251
|
+
const ay = 1 - cy - by;
|
|
252
|
+
const sampleX = (t) => ((ax * t + bx) * t + cx) * t;
|
|
253
|
+
const sampleY = (t) => ((ay * t + by) * t + cy) * t;
|
|
254
|
+
const sampleDerivativeX = (t) => (3 * ax * t + 2 * bx) * t + cx;
|
|
255
|
+
const solveCurveX = (x) => {
|
|
256
|
+
let t = x;
|
|
257
|
+
for (let i = 0; i < 8; i++) {
|
|
258
|
+
const currentX = sampleX(t) - x;
|
|
259
|
+
if (Math.abs(currentX) < 1e-7) {
|
|
260
|
+
return t;
|
|
261
|
+
}
|
|
262
|
+
const derivative = sampleDerivativeX(t);
|
|
263
|
+
if (Math.abs(derivative) < 1e-7) {
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
t -= currentX / derivative;
|
|
267
|
+
}
|
|
268
|
+
let low = 0;
|
|
269
|
+
let high = 1;
|
|
270
|
+
t = x;
|
|
271
|
+
while (low < high) {
|
|
272
|
+
const currentX = sampleX(t);
|
|
273
|
+
if (Math.abs(currentX - x) < 1e-7) {
|
|
274
|
+
return t;
|
|
275
|
+
}
|
|
276
|
+
if (x > currentX) {
|
|
277
|
+
low = t;
|
|
278
|
+
} else {
|
|
279
|
+
high = t;
|
|
280
|
+
}
|
|
281
|
+
t = (low + high) / 2;
|
|
282
|
+
}
|
|
283
|
+
return t;
|
|
284
|
+
};
|
|
285
|
+
return (x) => {
|
|
286
|
+
if (x <= 0) return 0;
|
|
287
|
+
if (x >= 1) return 1;
|
|
288
|
+
const t = solveCurveX(x);
|
|
289
|
+
return sampleY(t);
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function withMode(out, mode = "out") {
|
|
293
|
+
if (mode === "out") return out;
|
|
294
|
+
const easeIn2 = (t) => 1 - out(1 - t);
|
|
295
|
+
if (mode === "in") return easeIn2;
|
|
296
|
+
return (t) => t < 0.5 ? easeIn2(t * 2) / 2 : out(t * 2 - 1) / 2 + 0.5;
|
|
297
|
+
}
|
|
298
|
+
function elasticOut(amplitude = 1, period = 0.3) {
|
|
299
|
+
const a = Math.max(1, amplitude);
|
|
300
|
+
const s = period / (2 * Math.PI) * Math.asin(1 / a);
|
|
301
|
+
return (t) => {
|
|
302
|
+
if (t <= 0) return 0;
|
|
303
|
+
if (t >= 1) return 1;
|
|
304
|
+
return a * Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / period) + 1;
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
const bounceOut = (t) => {
|
|
308
|
+
const n = 7.5625;
|
|
309
|
+
const d = 2.75;
|
|
310
|
+
if (t <= 0) return 0;
|
|
311
|
+
if (t >= 1) return 1;
|
|
312
|
+
if (t < 1 / d) return n * t * t;
|
|
313
|
+
if (t < 2 / d) {
|
|
314
|
+
const t22 = t - 1.5 / d;
|
|
315
|
+
return n * t22 * t22 + 0.75;
|
|
316
|
+
}
|
|
317
|
+
if (t < 2.5 / d) {
|
|
318
|
+
const t22 = t - 2.25 / d;
|
|
319
|
+
return n * t22 * t22 + 0.9375;
|
|
320
|
+
}
|
|
321
|
+
const t2 = t - 2.625 / d;
|
|
322
|
+
return n * t2 * t2 + 0.984375;
|
|
323
|
+
};
|
|
324
|
+
function backOut(overshoot = 1.70158) {
|
|
325
|
+
return (t) => {
|
|
326
|
+
if (t <= 0) return 0;
|
|
327
|
+
if (t >= 1) return 1;
|
|
328
|
+
const p = t - 1;
|
|
329
|
+
return p * p * ((overshoot + 1) * p + overshoot) + 1;
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
function stepsEasing(count, position = "end") {
|
|
333
|
+
const n = Math.max(1, Math.floor(count));
|
|
334
|
+
return (t) => {
|
|
335
|
+
if (t >= 1) return 1;
|
|
336
|
+
if (t <= 0) return position === "start" || position === "both" ? position === "start" ? 1 / n : 1 / (n + 1) : 0;
|
|
337
|
+
const step = Math.floor(t * n);
|
|
338
|
+
switch (position) {
|
|
339
|
+
case "start":
|
|
340
|
+
return Math.min(1, (step + 1) / n);
|
|
341
|
+
case "both":
|
|
342
|
+
return (step + 1) / (n + 1);
|
|
343
|
+
case "none":
|
|
344
|
+
return n === 1 ? 0 : Math.min(1, step / (n - 1));
|
|
345
|
+
default:
|
|
346
|
+
return step / n;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function parametricEasing(easing) {
|
|
351
|
+
switch (easing.type) {
|
|
352
|
+
case "steps":
|
|
353
|
+
return stepsEasing(easing.count, easing.position);
|
|
354
|
+
case "elastic":
|
|
355
|
+
return withMode(elasticOut(easing.amplitude, easing.period), easing.mode);
|
|
356
|
+
case "bounce":
|
|
357
|
+
return withMode(bounceOut, easing.mode);
|
|
358
|
+
case "back":
|
|
359
|
+
return withMode(backOut(easing.overshoot), easing.mode);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function getEasingFunction(type) {
|
|
363
|
+
if (type === void 0) {
|
|
364
|
+
return linear;
|
|
365
|
+
}
|
|
366
|
+
if (isCubicBezierEasing(type)) {
|
|
367
|
+
return createCubicBezier(type.points);
|
|
368
|
+
}
|
|
369
|
+
if (isParametricEasing(type)) {
|
|
370
|
+
return parametricEasing(type);
|
|
371
|
+
}
|
|
372
|
+
return easingMap[type] ?? linear;
|
|
373
|
+
}
|
|
374
|
+
const LENGTH_SAMPLES = 32;
|
|
375
|
+
const PATH_CACHE_LIMIT = 256;
|
|
376
|
+
const pathCache = /* @__PURE__ */ new Map();
|
|
377
|
+
const COMMAND = /[MmLlHhVvCcSsQqTtAaZz]/;
|
|
378
|
+
const NUMBER = /^[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/;
|
|
379
|
+
const ARG_COUNT = {
|
|
380
|
+
M: 2,
|
|
381
|
+
L: 2,
|
|
382
|
+
H: 1,
|
|
383
|
+
V: 1,
|
|
384
|
+
C: 6,
|
|
385
|
+
S: 4,
|
|
386
|
+
Q: 4,
|
|
387
|
+
T: 2,
|
|
388
|
+
A: 7,
|
|
389
|
+
Z: 0
|
|
390
|
+
};
|
|
391
|
+
function tokenize(pathData) {
|
|
392
|
+
const commands = [];
|
|
393
|
+
let i = 0;
|
|
394
|
+
let current = null;
|
|
395
|
+
const skipSeparators = () => {
|
|
396
|
+
while (i < pathData.length && /[\s,]/.test(pathData[i])) i++;
|
|
397
|
+
};
|
|
398
|
+
while (i < pathData.length) {
|
|
399
|
+
skipSeparators();
|
|
400
|
+
if (i >= pathData.length) break;
|
|
401
|
+
const char = pathData[i];
|
|
402
|
+
if (COMMAND.test(char)) {
|
|
403
|
+
current = { type: char, args: [] };
|
|
404
|
+
commands.push(current);
|
|
405
|
+
i++;
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (!current) break;
|
|
409
|
+
const isArc = current.type === "A" || current.type === "a";
|
|
410
|
+
const argIndex = current.args.length % 7;
|
|
411
|
+
if (isArc && (argIndex === 3 || argIndex === 4)) {
|
|
412
|
+
if (char !== "0" && char !== "1") break;
|
|
413
|
+
current.args.push(char === "1" ? 1 : 0);
|
|
414
|
+
i++;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const match = NUMBER.exec(pathData.slice(i));
|
|
418
|
+
if (!match) break;
|
|
419
|
+
current.args.push(parseFloat(match[0]));
|
|
420
|
+
i += match[0].length;
|
|
421
|
+
}
|
|
422
|
+
return commands;
|
|
423
|
+
}
|
|
424
|
+
function arcToCubics(x1, y1, rxIn, ryIn, rotationDeg, largeArc, sweep, x2, y2) {
|
|
425
|
+
if (x1 === x2 && y1 === y2) return [];
|
|
426
|
+
let rx = Math.abs(rxIn);
|
|
427
|
+
let ry = Math.abs(ryIn);
|
|
428
|
+
if (rx === 0 || ry === 0) return [[x1, y1, x2, y2, x2, y2]];
|
|
429
|
+
const phi = rotationDeg * Math.PI / 180;
|
|
430
|
+
const cosPhi = Math.cos(phi);
|
|
431
|
+
const sinPhi = Math.sin(phi);
|
|
432
|
+
const dx = (x1 - x2) / 2;
|
|
433
|
+
const dy = (y1 - y2) / 2;
|
|
434
|
+
const x1p = cosPhi * dx + sinPhi * dy;
|
|
435
|
+
const y1p = -sinPhi * dx + cosPhi * dy;
|
|
436
|
+
const lambda = x1p * x1p / (rx * rx) + y1p * y1p / (ry * ry);
|
|
437
|
+
if (lambda > 1) {
|
|
438
|
+
const s = Math.sqrt(lambda);
|
|
439
|
+
rx *= s;
|
|
440
|
+
ry *= s;
|
|
441
|
+
}
|
|
442
|
+
const sign = largeArc === sweep ? -1 : 1;
|
|
443
|
+
const numerator = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
|
444
|
+
const denominator = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
|
|
445
|
+
const coef = sign * Math.sqrt(Math.max(0, numerator / denominator));
|
|
446
|
+
const cxp = coef * rx * y1p / ry;
|
|
447
|
+
const cyp = -coef * ry * x1p / rx;
|
|
448
|
+
const cx = cosPhi * cxp - sinPhi * cyp + (x1 + x2) / 2;
|
|
449
|
+
const cy = sinPhi * cxp + cosPhi * cyp + (y1 + y2) / 2;
|
|
450
|
+
const angle = (ux, uy, vx, vy) => {
|
|
451
|
+
const dot = ux * vx + uy * vy;
|
|
452
|
+
const len = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
|
|
453
|
+
const a = Math.acos(Math.max(-1, Math.min(1, dot / len)));
|
|
454
|
+
return ux * vy - uy * vx < 0 ? -a : a;
|
|
455
|
+
};
|
|
456
|
+
const theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
457
|
+
let delta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
|
|
458
|
+
if (!sweep && delta > 0) delta -= 2 * Math.PI;
|
|
459
|
+
if (sweep && delta < 0) delta += 2 * Math.PI;
|
|
460
|
+
const pieces = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 2)));
|
|
461
|
+
const step = delta / pieces;
|
|
462
|
+
const k = 4 / 3 * Math.tan(step / 4);
|
|
463
|
+
const pointAt = (theta) => {
|
|
464
|
+
const ex = rx * Math.cos(theta);
|
|
465
|
+
const ey = ry * Math.sin(theta);
|
|
466
|
+
return [cosPhi * ex - sinPhi * ey + cx, sinPhi * ex + cosPhi * ey + cy];
|
|
467
|
+
};
|
|
468
|
+
const derivativeAt = (theta) => {
|
|
469
|
+
const ex = -rx * Math.sin(theta);
|
|
470
|
+
const ey = ry * Math.cos(theta);
|
|
471
|
+
return [cosPhi * ex - sinPhi * ey, sinPhi * ex + cosPhi * ey];
|
|
472
|
+
};
|
|
473
|
+
const cubics = [];
|
|
474
|
+
for (let p = 0; p < pieces; p++) {
|
|
475
|
+
const a = theta1 + p * step;
|
|
476
|
+
const b = a + step;
|
|
477
|
+
const [ax, ay] = pointAt(a);
|
|
478
|
+
const [bx, by] = p === pieces - 1 ? [x2, y2] : pointAt(b);
|
|
479
|
+
const [dax, day] = derivativeAt(a);
|
|
480
|
+
const [dbx, dby] = derivativeAt(b);
|
|
481
|
+
cubics.push([ax + k * dax, ay + k * day, bx - k * dbx, by - k * dby, bx, by]);
|
|
482
|
+
}
|
|
483
|
+
return cubics;
|
|
484
|
+
}
|
|
485
|
+
function cubicPoint(p0, p1, p2, p3, t) {
|
|
486
|
+
const mt = 1 - t;
|
|
487
|
+
return mt * mt * mt * p0 + 3 * mt * mt * t * p1 + 3 * mt * t * t * p2 + t * t * t * p3;
|
|
488
|
+
}
|
|
489
|
+
function cubicDerivative(p0, p1, p2, p3, t) {
|
|
490
|
+
const mt = 1 - t;
|
|
491
|
+
return 3 * mt * mt * (p1 - p0) + 6 * mt * t * (p2 - p1) + 3 * t * t * (p3 - p2);
|
|
492
|
+
}
|
|
493
|
+
function lineSegment(x0, y0, x1, y1) {
|
|
494
|
+
return {
|
|
495
|
+
subpath: 0,
|
|
496
|
+
type: "L",
|
|
497
|
+
points: [x1, y1],
|
|
498
|
+
startX: x0,
|
|
499
|
+
startY: y0,
|
|
500
|
+
endX: x1,
|
|
501
|
+
endY: y1,
|
|
502
|
+
length: Math.hypot(x1 - x0, y1 - y0)
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
function cubicSegment(x0, y0, c) {
|
|
506
|
+
const [c1x, c1y, c2x, c2y, x1, y1] = c;
|
|
507
|
+
const lengths = [0];
|
|
508
|
+
let prevX = x0;
|
|
509
|
+
let prevY = y0;
|
|
510
|
+
let total = 0;
|
|
511
|
+
for (let i = 1; i <= LENGTH_SAMPLES; i++) {
|
|
512
|
+
const t = i / LENGTH_SAMPLES;
|
|
513
|
+
const x = cubicPoint(x0, c1x, c2x, x1, t);
|
|
514
|
+
const y = cubicPoint(y0, c1y, c2y, y1, t);
|
|
515
|
+
total += Math.hypot(x - prevX, y - prevY);
|
|
516
|
+
lengths.push(total);
|
|
517
|
+
prevX = x;
|
|
518
|
+
prevY = y;
|
|
519
|
+
}
|
|
520
|
+
return {
|
|
521
|
+
subpath: 0,
|
|
522
|
+
type: "C",
|
|
523
|
+
points: [c1x, c1y, c2x, c2y, x1, y1],
|
|
524
|
+
startX: x0,
|
|
525
|
+
startY: y0,
|
|
526
|
+
endX: x1,
|
|
527
|
+
endY: y1,
|
|
528
|
+
length: total,
|
|
529
|
+
lengths
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
function parsePath(pathData) {
|
|
533
|
+
const cached = pathCache.get(pathData);
|
|
534
|
+
if (cached) return cached;
|
|
535
|
+
const segments = [];
|
|
536
|
+
let x = 0;
|
|
537
|
+
let y = 0;
|
|
538
|
+
let subpathX = 0;
|
|
539
|
+
let subpathY = 0;
|
|
540
|
+
let lastCubicControl = null;
|
|
541
|
+
let lastQuadControl = null;
|
|
542
|
+
let subpath = -1;
|
|
543
|
+
const closedSubpaths = /* @__PURE__ */ new Set();
|
|
544
|
+
const push = (segment) => {
|
|
545
|
+
if (subpath < 0) subpath = 0;
|
|
546
|
+
segment.subpath = subpath;
|
|
547
|
+
segments.push(segment);
|
|
548
|
+
};
|
|
549
|
+
for (const { type, args } of tokenize(pathData)) {
|
|
550
|
+
const absType = type.toUpperCase();
|
|
551
|
+
const relative = type !== absType;
|
|
552
|
+
const count = ARG_COUNT[absType];
|
|
553
|
+
if (absType === "Z") {
|
|
554
|
+
if (x !== subpathX || y !== subpathY) push(lineSegment(x, y, subpathX, subpathY));
|
|
555
|
+
if (subpath >= 0) closedSubpaths.add(subpath);
|
|
556
|
+
x = subpathX;
|
|
557
|
+
y = subpathY;
|
|
558
|
+
lastCubicControl = lastQuadControl = null;
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
for (let i = 0; i + count <= args.length; i += count) {
|
|
562
|
+
const a = args.slice(i, i + count);
|
|
563
|
+
const ox = relative ? x : 0;
|
|
564
|
+
const oy = relative ? y : 0;
|
|
565
|
+
let nextCubic = null;
|
|
566
|
+
let nextQuad = null;
|
|
567
|
+
switch (absType) {
|
|
568
|
+
case "M":
|
|
569
|
+
if (i === 0) {
|
|
570
|
+
x = a[0] + ox;
|
|
571
|
+
y = a[1] + oy;
|
|
572
|
+
subpathX = x;
|
|
573
|
+
subpathY = y;
|
|
574
|
+
if (subpath < 0 || segments[segments.length - 1]?.subpath === subpath) subpath++;
|
|
575
|
+
} else {
|
|
576
|
+
push(lineSegment(x, y, a[0] + ox, a[1] + oy));
|
|
577
|
+
x = a[0] + ox;
|
|
578
|
+
y = a[1] + oy;
|
|
579
|
+
}
|
|
580
|
+
break;
|
|
581
|
+
case "L":
|
|
582
|
+
push(lineSegment(x, y, a[0] + ox, a[1] + oy));
|
|
583
|
+
x = a[0] + ox;
|
|
584
|
+
y = a[1] + oy;
|
|
585
|
+
break;
|
|
586
|
+
case "H":
|
|
587
|
+
push(lineSegment(x, y, a[0] + ox, y));
|
|
588
|
+
x = a[0] + ox;
|
|
589
|
+
break;
|
|
590
|
+
case "V":
|
|
591
|
+
push(lineSegment(x, y, x, a[0] + oy));
|
|
592
|
+
y = a[0] + oy;
|
|
593
|
+
break;
|
|
594
|
+
case "C": {
|
|
595
|
+
const c = [a[0] + ox, a[1] + oy, a[2] + ox, a[3] + oy, a[4] + ox, a[5] + oy];
|
|
596
|
+
push(cubicSegment(x, y, c));
|
|
597
|
+
nextCubic = [c[2], c[3]];
|
|
598
|
+
x = c[4];
|
|
599
|
+
y = c[5];
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
case "S": {
|
|
603
|
+
const [rx, ry] = lastCubicControl ? [2 * x - lastCubicControl[0], 2 * y - lastCubicControl[1]] : [x, y];
|
|
604
|
+
const c = [rx, ry, a[0] + ox, a[1] + oy, a[2] + ox, a[3] + oy];
|
|
605
|
+
push(cubicSegment(x, y, c));
|
|
606
|
+
nextCubic = [c[2], c[3]];
|
|
607
|
+
x = c[4];
|
|
608
|
+
y = c[5];
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
case "Q":
|
|
612
|
+
case "T": {
|
|
613
|
+
let qx = x;
|
|
614
|
+
let qy = y;
|
|
615
|
+
if (absType === "Q") {
|
|
616
|
+
qx = a[0] + ox;
|
|
617
|
+
qy = a[1] + oy;
|
|
618
|
+
} else if (lastQuadControl) {
|
|
619
|
+
qx = 2 * x - lastQuadControl[0];
|
|
620
|
+
qy = 2 * y - lastQuadControl[1];
|
|
621
|
+
}
|
|
622
|
+
const endX = absType === "Q" ? a[2] + ox : a[0] + ox;
|
|
623
|
+
const endY = absType === "Q" ? a[3] + oy : a[1] + oy;
|
|
624
|
+
push(
|
|
625
|
+
cubicSegment(x, y, [
|
|
626
|
+
x + 2 / 3 * (qx - x),
|
|
627
|
+
y + 2 / 3 * (qy - y),
|
|
628
|
+
endX + 2 / 3 * (qx - endX),
|
|
629
|
+
endY + 2 / 3 * (qy - endY),
|
|
630
|
+
endX,
|
|
631
|
+
endY
|
|
632
|
+
])
|
|
633
|
+
);
|
|
634
|
+
nextQuad = [qx, qy];
|
|
635
|
+
x = endX;
|
|
636
|
+
y = endY;
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
639
|
+
case "A": {
|
|
640
|
+
const endX = a[5] + ox;
|
|
641
|
+
const endY = a[6] + oy;
|
|
642
|
+
let startX = x;
|
|
643
|
+
let startY = y;
|
|
644
|
+
for (const c of arcToCubics(x, y, a[0], a[1], a[2], a[3], a[4], endX, endY)) {
|
|
645
|
+
push(cubicSegment(startX, startY, c));
|
|
646
|
+
startX = c[4];
|
|
647
|
+
startY = c[5];
|
|
648
|
+
}
|
|
649
|
+
x = endX;
|
|
650
|
+
y = endY;
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
lastCubicControl = nextCubic;
|
|
655
|
+
lastQuadControl = nextQuad;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
const totalLength = segments.reduce((sum, seg) => sum + seg.length, 0);
|
|
659
|
+
const subpaths = [];
|
|
660
|
+
for (let i = 0; i < segments.length; ) {
|
|
661
|
+
const index = segments[i].subpath;
|
|
662
|
+
let end = i;
|
|
663
|
+
let length = 0;
|
|
664
|
+
while (end < segments.length && segments[end].subpath === index) length += segments[end++].length;
|
|
665
|
+
const first = segments[i];
|
|
666
|
+
const last = segments[end - 1];
|
|
667
|
+
const closed = closedSubpaths.has(index) || Math.abs(last.endX - first.startX) < 1e-9 && Math.abs(last.endY - first.startY) < 1e-9;
|
|
668
|
+
subpaths.push({ start: i, end, length, closed });
|
|
669
|
+
i = end;
|
|
670
|
+
}
|
|
671
|
+
const parsed = { segments, totalLength, subpaths };
|
|
672
|
+
if (pathCache.size >= PATH_CACHE_LIMIT) {
|
|
673
|
+
pathCache.delete(pathCache.keys().next().value);
|
|
674
|
+
}
|
|
675
|
+
pathCache.set(pathData, parsed);
|
|
676
|
+
return parsed;
|
|
677
|
+
}
|
|
678
|
+
function tAtDistance(segment, distance) {
|
|
679
|
+
const lengths = segment.lengths;
|
|
680
|
+
if (distance <= 0) return 0;
|
|
681
|
+
if (distance >= segment.length) return 1;
|
|
682
|
+
let low = 0;
|
|
683
|
+
let high = lengths.length - 1;
|
|
684
|
+
while (low < high - 1) {
|
|
685
|
+
const mid = low + high >> 1;
|
|
686
|
+
if (lengths[mid] < distance) low = mid;
|
|
687
|
+
else high = mid;
|
|
688
|
+
}
|
|
689
|
+
const span = lengths[high] - lengths[low];
|
|
690
|
+
const fraction = span > 0 ? (distance - lengths[low]) / span : 0;
|
|
691
|
+
return (low + fraction) / (lengths.length - 1);
|
|
692
|
+
}
|
|
693
|
+
function pointOnSegment(segment, distance) {
|
|
694
|
+
if (segment.type === "L") {
|
|
695
|
+
const f = segment.length > 0 ? Math.max(0, Math.min(1, distance / segment.length)) : 0;
|
|
696
|
+
return {
|
|
697
|
+
x: segment.startX + (segment.endX - segment.startX) * f,
|
|
698
|
+
y: segment.startY + (segment.endY - segment.startY) * f,
|
|
699
|
+
angle: Math.atan2(segment.endY - segment.startY, segment.endX - segment.startX) * 180 / Math.PI
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
const [c1x, c1y, c2x, c2y, x1, y1] = segment.points;
|
|
703
|
+
const t = tAtDistance(segment, distance);
|
|
704
|
+
let dx = cubicDerivative(segment.startX, c1x, c2x, x1, t);
|
|
705
|
+
let dy = cubicDerivative(segment.startY, c1y, c2y, y1, t);
|
|
706
|
+
if (Math.hypot(dx, dy) < 1e-9) {
|
|
707
|
+
const nudge = t < 0.5 ? Math.min(1, t + 1e-3) : Math.max(0, t - 1e-3);
|
|
708
|
+
const nx = cubicPoint(segment.startX, c1x, c2x, x1, nudge);
|
|
709
|
+
const ny = cubicPoint(segment.startY, c1y, c2y, y1, nudge);
|
|
710
|
+
const px = cubicPoint(segment.startX, c1x, c2x, x1, t);
|
|
711
|
+
const py = cubicPoint(segment.startY, c1y, c2y, y1, t);
|
|
712
|
+
dx = t < 0.5 ? nx - px : px - nx;
|
|
713
|
+
dy = t < 0.5 ? ny - py : py - ny;
|
|
714
|
+
}
|
|
715
|
+
return {
|
|
716
|
+
x: cubicPoint(segment.startX, c1x, c2x, x1, t),
|
|
717
|
+
y: cubicPoint(segment.startY, c1y, c2y, y1, t),
|
|
718
|
+
angle: Math.atan2(dy, dx) * 180 / Math.PI
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
function pointAtDistance(segments, distance, start = 0, end = segments.length) {
|
|
722
|
+
if (end <= start) return { x: 0, y: 0, angle: 0 };
|
|
723
|
+
let accumulated = 0;
|
|
724
|
+
for (let i = start; i < end; i++) {
|
|
725
|
+
const segment = segments[i];
|
|
726
|
+
if (accumulated + segment.length >= distance || i === end - 1) {
|
|
727
|
+
return pointOnSegment(segment, distance - accumulated);
|
|
728
|
+
}
|
|
729
|
+
accumulated += segment.length;
|
|
730
|
+
}
|
|
731
|
+
return { x: 0, y: 0, angle: 0 };
|
|
732
|
+
}
|
|
733
|
+
function getPointAtProgress(pathData, progress) {
|
|
734
|
+
const { segments, totalLength } = parsePath(pathData);
|
|
735
|
+
return pointAtDistance(segments, Math.max(0, Math.min(1, progress)) * totalLength);
|
|
736
|
+
}
|
|
737
|
+
const MIN_SAMPLES = 24;
|
|
738
|
+
const MAX_SAMPLES = 320;
|
|
739
|
+
const SAMPLE_SPACING = 2.5;
|
|
740
|
+
const ALIGN_SAMPLES = 72;
|
|
741
|
+
const PLAN_TOLERANCE = 0.2;
|
|
742
|
+
const PLAN_CACHE_LIMIT = 128;
|
|
743
|
+
const planCache = /* @__PURE__ */ new Map();
|
|
744
|
+
let planCount = 0;
|
|
745
|
+
let lastPlan;
|
|
746
|
+
const round = (n) => Math.round(n * 100) / 100;
|
|
747
|
+
function runsOf(pathData, merge) {
|
|
748
|
+
const { segments, subpaths, totalLength } = parsePath(pathData);
|
|
749
|
+
if (segments.length === 0) return [];
|
|
750
|
+
if (merge) {
|
|
751
|
+
const allClosed = subpaths.every((s) => s.closed);
|
|
752
|
+
return [{ segments, start: 0, end: segments.length, length: totalLength, closed: allClosed }];
|
|
753
|
+
}
|
|
754
|
+
return subpaths.filter((s) => s.length > 0).map((s) => ({ segments, start: s.start, end: s.end, length: s.length, closed: s.closed }));
|
|
755
|
+
}
|
|
756
|
+
function pointOnRun(run, fraction) {
|
|
757
|
+
const f = run.closed ? (fraction % 1 + 1) % 1 : Math.max(0, Math.min(1, fraction));
|
|
758
|
+
const p = pointAtDistance(run.segments, f * run.length, run.start, run.end);
|
|
759
|
+
return [p.x, p.y];
|
|
760
|
+
}
|
|
761
|
+
function boundariesOf(run) {
|
|
762
|
+
const fractions = [];
|
|
763
|
+
let accumulated = 0;
|
|
764
|
+
for (let i = run.start; i < run.end; i++) {
|
|
765
|
+
accumulated += run.segments[i].length;
|
|
766
|
+
if (run.length > 0) fractions.push(accumulated / run.length);
|
|
767
|
+
}
|
|
768
|
+
return fractions;
|
|
769
|
+
}
|
|
770
|
+
function uniformSamples(run, count) {
|
|
771
|
+
const points = [];
|
|
772
|
+
for (let i = 0; i < count; i++) {
|
|
773
|
+
points.push(pointOnRun(run, run.closed ? i / count : i / (count - 1)));
|
|
774
|
+
}
|
|
775
|
+
return points;
|
|
776
|
+
}
|
|
777
|
+
function centred(points) {
|
|
778
|
+
let cx = 0;
|
|
779
|
+
let cy = 0;
|
|
780
|
+
for (const [x, y] of points) {
|
|
781
|
+
cx += x;
|
|
782
|
+
cy += y;
|
|
783
|
+
}
|
|
784
|
+
cx /= points.length;
|
|
785
|
+
cy /= points.length;
|
|
786
|
+
return points.map(([x, y]) => [x - cx, y - cy]);
|
|
787
|
+
}
|
|
788
|
+
function chooseAlignment(from, to, forced) {
|
|
789
|
+
const closed = from.closed && to.closed;
|
|
790
|
+
if (forced !== void 0) {
|
|
791
|
+
return { offset: closed ? Math.abs(forced) % ALIGN_SAMPLES / ALIGN_SAMPLES : 0, reversed: forced < 0 };
|
|
792
|
+
}
|
|
793
|
+
const a = centred(uniformSamples(from, ALIGN_SAMPLES));
|
|
794
|
+
const b = centred(uniformSamples(to, ALIGN_SAMPLES));
|
|
795
|
+
const n = ALIGN_SAMPLES;
|
|
796
|
+
let best = { offset: 0, reversed: false };
|
|
797
|
+
let bestCost = Infinity;
|
|
798
|
+
for (const reversed of [false, true]) {
|
|
799
|
+
const offsets = closed ? n : 1;
|
|
800
|
+
for (let k = 0; k < offsets; k++) {
|
|
801
|
+
let cost = 0;
|
|
802
|
+
for (let i = 0; i < n && cost < bestCost; i++) {
|
|
803
|
+
const j = closed ? reversed ? (k - i + n) % n : (i + k) % n : reversed ? n - 1 - i : i;
|
|
804
|
+
const dx = a[i][0] - b[j][0];
|
|
805
|
+
const dy = a[i][1] - b[j][1];
|
|
806
|
+
cost += dx * dx + dy * dy;
|
|
807
|
+
}
|
|
808
|
+
if (cost < bestCost) {
|
|
809
|
+
bestCost = cost;
|
|
810
|
+
best = { offset: closed ? k / n : 0, reversed };
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return best;
|
|
815
|
+
}
|
|
816
|
+
function mapFraction(f, alignment, closed) {
|
|
817
|
+
if (!closed) return alignment.reversed ? 1 - f : f;
|
|
818
|
+
const mapped = alignment.reversed ? alignment.offset - f : f + alignment.offset;
|
|
819
|
+
return (mapped % 1 + 1) % 1;
|
|
820
|
+
}
|
|
821
|
+
function unmapFraction(g, alignment, closed) {
|
|
822
|
+
if (!closed) return alignment.reversed ? 1 - g : g;
|
|
823
|
+
const f = alignment.reversed ? alignment.offset - g : g - alignment.offset;
|
|
824
|
+
return (f % 1 + 1) % 1;
|
|
825
|
+
}
|
|
826
|
+
function planPair(from, to, options) {
|
|
827
|
+
const closed = from.closed && to.closed;
|
|
828
|
+
const alignment = chooseAlignment(from, to, options.shapeIndex);
|
|
829
|
+
const count = Math.max(
|
|
830
|
+
MIN_SAMPLES,
|
|
831
|
+
Math.min(MAX_SAMPLES, Math.ceil(Math.max(from.length, to.length) / SAMPLE_SPACING))
|
|
832
|
+
);
|
|
833
|
+
const fractions = /* @__PURE__ */ new Set();
|
|
834
|
+
const add = (f) => fractions.add(Math.round(f * 1e7) / 1e7);
|
|
835
|
+
for (let i = 0; i <= count; i++) add(i / count);
|
|
836
|
+
for (const f of boundariesOf(from)) add(f);
|
|
837
|
+
for (const g of boundariesOf(to)) add(unmapFraction(g, alignment, closed));
|
|
838
|
+
let sorted = [...fractions].sort((x, y) => x - y);
|
|
839
|
+
if (closed) sorted = sorted.filter((f) => f < 1);
|
|
840
|
+
const fromPoints = [];
|
|
841
|
+
const toPoints = [];
|
|
842
|
+
for (const f of sorted) {
|
|
843
|
+
fromPoints.push(...pointOnRun(from, f));
|
|
844
|
+
toPoints.push(...pointOnRun(to, mapFraction(f, alignment, closed)));
|
|
845
|
+
}
|
|
846
|
+
return thinPair({ from: fromPoints, to: toPoints, closed });
|
|
847
|
+
}
|
|
848
|
+
function thinPair(pair) {
|
|
849
|
+
const n = pair.from.length / 2;
|
|
850
|
+
if (n <= 3) return pair;
|
|
851
|
+
const keep = new Uint8Array(n);
|
|
852
|
+
keep[0] = 1;
|
|
853
|
+
keep[n - 1] = 1;
|
|
854
|
+
const stack = [[0, n - 1]];
|
|
855
|
+
while (stack.length > 0) {
|
|
856
|
+
const [first, last] = stack.pop();
|
|
857
|
+
let worst = -1;
|
|
858
|
+
let worstDistance = PLAN_TOLERANCE;
|
|
859
|
+
for (let i = first + 1; i < last; i++) {
|
|
860
|
+
const d = Math.max(chordDistance(pair.from, first, last, i), chordDistance(pair.to, first, last, i));
|
|
861
|
+
if (d > worstDistance) {
|
|
862
|
+
worstDistance = d;
|
|
863
|
+
worst = i;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
if (worst !== -1) {
|
|
867
|
+
keep[worst] = 1;
|
|
868
|
+
stack.push([first, worst], [worst, last]);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
const from = [];
|
|
872
|
+
const to = [];
|
|
873
|
+
for (let i = 0; i < n; i++) {
|
|
874
|
+
if (!keep[i]) continue;
|
|
875
|
+
from.push(pair.from[i * 2], pair.from[i * 2 + 1]);
|
|
876
|
+
to.push(pair.to[i * 2], pair.to[i * 2 + 1]);
|
|
877
|
+
}
|
|
878
|
+
return { from, to, closed: pair.closed };
|
|
879
|
+
}
|
|
880
|
+
function chordDistance(points, a, b, i) {
|
|
881
|
+
const ax = points[a * 2];
|
|
882
|
+
const ay = points[a * 2 + 1];
|
|
883
|
+
const dx = points[b * 2] - ax;
|
|
884
|
+
const dy = points[b * 2 + 1] - ay;
|
|
885
|
+
const px = points[i * 2] - ax;
|
|
886
|
+
const py = points[i * 2 + 1] - ay;
|
|
887
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
888
|
+
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, (px * dx + py * dy) / lengthSquared));
|
|
889
|
+
return Math.hypot(px - t * dx, py - t * dy);
|
|
890
|
+
}
|
|
891
|
+
function planFor(from, to, options) {
|
|
892
|
+
const shapeIndex = options.shapeIndex;
|
|
893
|
+
if (lastPlan && lastPlan.from === from && lastPlan.to === to && lastPlan.shapeIndex === shapeIndex) return lastPlan.plan;
|
|
894
|
+
const byFrom = planCache.get(String(shapeIndex ?? "auto"))?.get(from);
|
|
895
|
+
const cached = byFrom?.get(to);
|
|
896
|
+
if (cached) {
|
|
897
|
+
lastPlan = { from, to, shapeIndex, plan: cached };
|
|
898
|
+
return cached;
|
|
899
|
+
}
|
|
900
|
+
const separate = parsePath(from).subpaths.filter((s) => s.length > 0).length === parsePath(to).subpaths.filter((s) => s.length > 0).length;
|
|
901
|
+
const fromRuns = runsOf(from, !separate);
|
|
902
|
+
const toRuns = runsOf(to, !separate);
|
|
903
|
+
const plan = {
|
|
904
|
+
pairs: fromRuns.map((run, i) => planPair(run, toRuns[i], options))
|
|
905
|
+
};
|
|
906
|
+
if (planCount >= PLAN_CACHE_LIMIT) {
|
|
907
|
+
planCache.clear();
|
|
908
|
+
planCount = 0;
|
|
909
|
+
}
|
|
910
|
+
const indexKey = String(shapeIndex ?? "auto");
|
|
911
|
+
const byIndex = planCache.get(indexKey) ?? /* @__PURE__ */ new Map();
|
|
912
|
+
planCache.set(indexKey, byIndex);
|
|
913
|
+
const targets = byIndex.get(from) ?? /* @__PURE__ */ new Map();
|
|
914
|
+
byIndex.set(from, targets);
|
|
915
|
+
targets.set(to, plan);
|
|
916
|
+
planCount++;
|
|
917
|
+
lastPlan = { from, to, shapeIndex, plan };
|
|
918
|
+
return plan;
|
|
919
|
+
}
|
|
920
|
+
function morphPath(from, to, progress, options = {}) {
|
|
921
|
+
if (!from) return to;
|
|
922
|
+
if (!to) return from;
|
|
923
|
+
const p = Math.max(0, Math.min(1, progress));
|
|
924
|
+
if (p === 0) return from;
|
|
925
|
+
if (p === 1) return to;
|
|
926
|
+
const plan = planFor(from, to, options);
|
|
927
|
+
if (plan.pairs.length === 0) return p < 0.5 ? from : to;
|
|
928
|
+
let d = "";
|
|
929
|
+
for (const pair of plan.pairs) {
|
|
930
|
+
for (let i = 0; i < pair.from.length; i += 2) {
|
|
931
|
+
const x = round(pair.from[i] + (pair.to[i] - pair.from[i]) * p);
|
|
932
|
+
const y = round(pair.from[i + 1] + (pair.to[i + 1] - pair.from[i + 1]) * p);
|
|
933
|
+
d += `${i === 0 ? d ? " M" : "M" : " L"}${x} ${y}`;
|
|
934
|
+
}
|
|
935
|
+
if (pair.closed) d += " Z";
|
|
936
|
+
}
|
|
937
|
+
return d;
|
|
938
|
+
}
|
|
939
|
+
function isPathData(value) {
|
|
940
|
+
return /^\s*[Mm]\s*[-+]?(?:\d|\.\d)/.test(value);
|
|
941
|
+
}
|
|
942
|
+
const interpolateNumber = (from, to, progress) => {
|
|
943
|
+
return from + (to - from) * progress;
|
|
944
|
+
};
|
|
945
|
+
const MAX_COLOR_CACHE = 512;
|
|
946
|
+
const hexCache = /* @__PURE__ */ new Map();
|
|
947
|
+
const rgbCache = /* @__PURE__ */ new Map();
|
|
948
|
+
function parseHex(hex) {
|
|
949
|
+
const cached = hexCache.get(hex);
|
|
950
|
+
if (cached) return cached;
|
|
951
|
+
const cleaned = hex.replace("#", "");
|
|
952
|
+
const parsed = [
|
|
953
|
+
parseInt(cleaned.slice(0, 2), 16),
|
|
954
|
+
parseInt(cleaned.slice(2, 4), 16),
|
|
955
|
+
parseInt(cleaned.slice(4, 6), 16)
|
|
956
|
+
];
|
|
957
|
+
if (hexCache.size < MAX_COLOR_CACHE) hexCache.set(hex, parsed);
|
|
958
|
+
return parsed;
|
|
959
|
+
}
|
|
960
|
+
const isHex = (s) => s.charCodeAt(0) === 35;
|
|
961
|
+
const isRgb = (s) => s.startsWith("rgb");
|
|
962
|
+
const isRgba = (s) => s.startsWith("rgba");
|
|
963
|
+
const RGB_PATTERN = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)/;
|
|
964
|
+
const toHexPart = (n) => Math.round(n).toString(16).padStart(2, "0");
|
|
965
|
+
function toHex(r, g, b) {
|
|
966
|
+
return `#${toHexPart(r)}${toHexPart(g)}${toHexPart(b)}`;
|
|
967
|
+
}
|
|
968
|
+
function parseRgb(color) {
|
|
969
|
+
const cached = rgbCache.get(color);
|
|
970
|
+
if (cached) return cached;
|
|
971
|
+
const match = color.match(RGB_PATTERN);
|
|
972
|
+
if (!match) {
|
|
973
|
+
throw new Error(`Invalid rgb color: ${color}`);
|
|
974
|
+
}
|
|
975
|
+
const r = parseInt(match[1], 10);
|
|
976
|
+
const g = parseInt(match[2], 10);
|
|
977
|
+
const b = parseInt(match[3], 10);
|
|
978
|
+
const parsed = match[4] !== void 0 ? [r, g, b, parseFloat(match[4])] : [r, g, b];
|
|
979
|
+
if (rgbCache.size < MAX_COLOR_CACHE) rgbCache.set(color, parsed);
|
|
980
|
+
return parsed;
|
|
981
|
+
}
|
|
982
|
+
const interpolateColor = (from, to, progress) => {
|
|
983
|
+
if (isHex(from) && isHex(to)) {
|
|
984
|
+
const [r1, g1, b1] = parseHex(from);
|
|
985
|
+
const [r2, g2, b2] = parseHex(to);
|
|
986
|
+
const r = interpolateNumber(r1, r2, progress);
|
|
987
|
+
const g = interpolateNumber(g1, g2, progress);
|
|
988
|
+
const b = interpolateNumber(b1, b2, progress);
|
|
989
|
+
return toHex(r, g, b);
|
|
990
|
+
}
|
|
991
|
+
if ((isRgb(from) || isRgba(from)) && (isRgb(to) || isRgba(to))) {
|
|
992
|
+
const fromParts = parseRgb(from);
|
|
993
|
+
const toParts = parseRgb(to);
|
|
994
|
+
const r = Math.round(interpolateNumber(fromParts[0], toParts[0], progress));
|
|
995
|
+
const g = Math.round(interpolateNumber(fromParts[1], toParts[1], progress));
|
|
996
|
+
const b = Math.round(interpolateNumber(fromParts[2], toParts[2], progress));
|
|
997
|
+
if (fromParts.length === 4 || toParts.length === 4) {
|
|
998
|
+
const a1 = fromParts[3] ?? 1;
|
|
999
|
+
const a2 = toParts[3] ?? 1;
|
|
1000
|
+
const a = interpolateNumber(a1, a2, progress);
|
|
1001
|
+
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
1002
|
+
}
|
|
1003
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
1004
|
+
}
|
|
1005
|
+
return progress < 1 ? from : to;
|
|
1006
|
+
};
|
|
1007
|
+
const interpolateArray = (from, to, progress) => {
|
|
1008
|
+
const length = Math.min(from.length, to.length);
|
|
1009
|
+
const result = [];
|
|
1010
|
+
for (let i = 0; i < length; i++) {
|
|
1011
|
+
result.push(interpolateNumber(from[i], to[i], progress));
|
|
1012
|
+
}
|
|
1013
|
+
return result;
|
|
1014
|
+
};
|
|
1015
|
+
const interpolateString = (from, to, progress) => {
|
|
1016
|
+
return progress < 1 ? from : to;
|
|
1017
|
+
};
|
|
1018
|
+
const interpolatePathString = (from, to, progress) => {
|
|
1019
|
+
return morphPath(from, to, progress);
|
|
1020
|
+
};
|
|
1021
|
+
function getInterpolator(sampleValue) {
|
|
1022
|
+
if (typeof sampleValue === "number") {
|
|
1023
|
+
return interpolateNumber;
|
|
1024
|
+
}
|
|
1025
|
+
if (Array.isArray(sampleValue)) {
|
|
1026
|
+
return interpolateArray;
|
|
1027
|
+
}
|
|
1028
|
+
if (typeof sampleValue === "string") {
|
|
1029
|
+
if (sampleValue.startsWith("#") || sampleValue.startsWith("rgb")) {
|
|
1030
|
+
return interpolateColor;
|
|
1031
|
+
}
|
|
1032
|
+
if (isPathData(sampleValue)) {
|
|
1033
|
+
return interpolatePathString;
|
|
1034
|
+
}
|
|
1035
|
+
return interpolateString;
|
|
1036
|
+
}
|
|
1037
|
+
return interpolateString;
|
|
1038
|
+
}
|
|
1039
|
+
function createTrack(options) {
|
|
1040
|
+
const sortedKeyframes = [...options.keyframes].sort((a, b) => a.time - b.time);
|
|
1041
|
+
return {
|
|
1042
|
+
...options,
|
|
1043
|
+
keyframes: sortedKeyframes
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
function trackTargets(track) {
|
|
1047
|
+
return track.targets && track.targets.length > 0 ? track.targets : [track.target];
|
|
1048
|
+
}
|
|
1049
|
+
function offsetFor(index, count, delay, stagger) {
|
|
1050
|
+
const base = delay ?? 0;
|
|
1051
|
+
if (!stagger || count <= 1) return base;
|
|
1052
|
+
return base + staggerOffset(index, count, stagger);
|
|
1053
|
+
}
|
|
1054
|
+
class TrackPlayer {
|
|
1055
|
+
track;
|
|
1056
|
+
targets;
|
|
1057
|
+
constructor(track) {
|
|
1058
|
+
this.track = track;
|
|
1059
|
+
this.targets = trackTargets(track);
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Get the interpolated value at a specific time.
|
|
1063
|
+
*
|
|
1064
|
+
* For a multi-target track this returns the *first* target's value; callers
|
|
1065
|
+
* that need every target should use `getTargetValues`.
|
|
1066
|
+
*/
|
|
1067
|
+
getValueAtTime(time) {
|
|
1068
|
+
return this.valueForOffset(time - offsetFor(0, this.targets.length, this.track.delay, this.track.stagger));
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Every target's value at a specific time, in target order.
|
|
1072
|
+
*
|
|
1073
|
+
* Single-target tracks yield one entry; staggered tracks yield one per target,
|
|
1074
|
+
* each sampled at its own offset time.
|
|
1075
|
+
*/
|
|
1076
|
+
getTargetValues(time) {
|
|
1077
|
+
const count = this.targets.length;
|
|
1078
|
+
const out = [];
|
|
1079
|
+
for (let i = 0; i < count; i++) {
|
|
1080
|
+
const offset = offsetFor(i, count, this.track.delay, this.track.stagger);
|
|
1081
|
+
const value = this.valueForOffset(time - offset);
|
|
1082
|
+
if (value === void 0) continue;
|
|
1083
|
+
out.push({ target: this.targets[i], value, start: offset + this.track.keyframes[0].time });
|
|
1084
|
+
}
|
|
1085
|
+
return out;
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Get the duration of this track — the last keyframe, plus any delay, the
|
|
1089
|
+
* widest stagger offset, and any trailing hold.
|
|
1090
|
+
*/
|
|
1091
|
+
getDuration() {
|
|
1092
|
+
const { keyframes } = this.track;
|
|
1093
|
+
if (keyframes.length === 0) {
|
|
1094
|
+
return 0;
|
|
1095
|
+
}
|
|
1096
|
+
const last = keyframes[keyframes.length - 1].time;
|
|
1097
|
+
const spread = this.track.stagger ? staggerSpan(this.targets.length, this.track.stagger) : 0;
|
|
1098
|
+
return last + (this.track.delay ?? 0) + spread + (this.track.endDelay ?? 0);
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Get the track metadata.
|
|
1102
|
+
*/
|
|
1103
|
+
getTrack() {
|
|
1104
|
+
return this.track;
|
|
1105
|
+
}
|
|
1106
|
+
/** Interpolated value at a time already shifted into the track's own frame. */
|
|
1107
|
+
valueForOffset(time) {
|
|
1108
|
+
const { keyframes } = this.track;
|
|
1109
|
+
if (keyframes.length === 0) {
|
|
1110
|
+
return void 0;
|
|
1111
|
+
}
|
|
1112
|
+
if (keyframes.length === 1) {
|
|
1113
|
+
return keyframes[0].value;
|
|
1114
|
+
}
|
|
1115
|
+
if (time <= keyframes[0].time) {
|
|
1116
|
+
return keyframes[0].value;
|
|
1117
|
+
}
|
|
1118
|
+
if (time >= keyframes[keyframes.length - 1].time) {
|
|
1119
|
+
return keyframes[keyframes.length - 1].value;
|
|
1120
|
+
}
|
|
1121
|
+
const { from, to } = this.findSurroundingKeyframes(time);
|
|
1122
|
+
if (!from || !to) {
|
|
1123
|
+
return void 0;
|
|
1124
|
+
}
|
|
1125
|
+
if (from.time === time) {
|
|
1126
|
+
return from.value;
|
|
1127
|
+
}
|
|
1128
|
+
const segmentDuration = to.time - from.time;
|
|
1129
|
+
const segmentProgress = (time - from.time) / segmentDuration;
|
|
1130
|
+
const easingFn = getEasingFunction(to.easing);
|
|
1131
|
+
const easedProgress = easingFn(segmentProgress);
|
|
1132
|
+
const interpolator = getInterpolator(from.value);
|
|
1133
|
+
return interpolator(from.value, to.value, easedProgress);
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Find the keyframes surrounding a given time.
|
|
1137
|
+
*/
|
|
1138
|
+
findSurroundingKeyframes(time) {
|
|
1139
|
+
const { keyframes } = this.track;
|
|
1140
|
+
for (let i = 0; i < keyframes.length - 1; i++) {
|
|
1141
|
+
if (time >= keyframes[i].time && time <= keyframes[i + 1].time) {
|
|
1142
|
+
return { from: keyframes[i], to: keyframes[i + 1] };
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
return { from: null, to: null };
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
class SpringTrackPlayer {
|
|
1149
|
+
track;
|
|
1150
|
+
targets;
|
|
1151
|
+
sampler;
|
|
1152
|
+
constructor(track) {
|
|
1153
|
+
this.track = track;
|
|
1154
|
+
this.targets = trackTargets(track);
|
|
1155
|
+
this.sampler = new SpringSampler(track.spring);
|
|
1156
|
+
}
|
|
1157
|
+
getValueAtTime(time) {
|
|
1158
|
+
return this.sampler.valueAt(time - offsetFor(0, this.targets.length, this.track.delay, this.track.stagger));
|
|
1159
|
+
}
|
|
1160
|
+
getTargetValues(time) {
|
|
1161
|
+
const count = this.targets.length;
|
|
1162
|
+
const out = [];
|
|
1163
|
+
for (let i = 0; i < count; i++) {
|
|
1164
|
+
const offset = offsetFor(i, count, this.track.delay, this.track.stagger);
|
|
1165
|
+
out.push({ target: this.targets[i], value: this.sampler.valueAt(time - offset), start: offset });
|
|
1166
|
+
}
|
|
1167
|
+
return out;
|
|
1168
|
+
}
|
|
1169
|
+
/** Settle time plus delay and the widest stagger offset. */
|
|
1170
|
+
getDuration() {
|
|
1171
|
+
const spread = this.track.stagger ? staggerSpan(this.targets.length, this.track.stagger) : 0;
|
|
1172
|
+
return this.sampler.settleTime() + (this.track.delay ?? 0) + spread;
|
|
1173
|
+
}
|
|
1174
|
+
getTrack() {
|
|
1175
|
+
return this.track;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
class InertiaTrackPlayer {
|
|
1179
|
+
track;
|
|
1180
|
+
targets;
|
|
1181
|
+
duration;
|
|
1182
|
+
constructor(track) {
|
|
1183
|
+
this.track = track;
|
|
1184
|
+
this.targets = trackTargets(track);
|
|
1185
|
+
this.duration = inertiaDuration(track.inertia);
|
|
1186
|
+
}
|
|
1187
|
+
getValueAtTime(time) {
|
|
1188
|
+
return inertiaValueAt(this.track.inertia, time - offsetFor(0, this.targets.length, this.track.delay, this.track.stagger));
|
|
1189
|
+
}
|
|
1190
|
+
getTargetValues(time) {
|
|
1191
|
+
const count = this.targets.length;
|
|
1192
|
+
const out = [];
|
|
1193
|
+
for (let i = 0; i < count; i++) {
|
|
1194
|
+
const offset = offsetFor(i, count, this.track.delay, this.track.stagger);
|
|
1195
|
+
out.push({ target: this.targets[i], value: inertiaValueAt(this.track.inertia, time - offset), start: offset });
|
|
1196
|
+
}
|
|
1197
|
+
return out;
|
|
1198
|
+
}
|
|
1199
|
+
/** Settle time plus delay and the widest stagger offset. */
|
|
1200
|
+
getDuration() {
|
|
1201
|
+
const spread = this.track.stagger ? staggerSpan(this.targets.length, this.track.stagger) : 0;
|
|
1202
|
+
return this.duration + (this.track.delay ?? 0) + spread;
|
|
1203
|
+
}
|
|
1204
|
+
getTrack() {
|
|
1205
|
+
return this.track;
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
function getMotionPathPoint(config, progress) {
|
|
1209
|
+
const point = { ...getPointAtProgress(config.pathData, progress) };
|
|
1210
|
+
if (config.matrix) {
|
|
1211
|
+
const [a, b, c, d, e, f] = config.matrix;
|
|
1212
|
+
const { x, y } = point;
|
|
1213
|
+
point.x = a * x + c * y + e;
|
|
1214
|
+
point.y = b * x + d * y + f;
|
|
1215
|
+
const radians = point.angle * Math.PI / 180;
|
|
1216
|
+
const tx = Math.cos(radians);
|
|
1217
|
+
const ty = Math.sin(radians);
|
|
1218
|
+
point.angle = Math.atan2(b * tx + d * ty, a * tx + c * ty) * 180 / Math.PI;
|
|
1219
|
+
}
|
|
1220
|
+
if (config.autoRotate && config.rotateOffset) {
|
|
1221
|
+
point.angle += config.rotateOffset;
|
|
1222
|
+
}
|
|
1223
|
+
return point;
|
|
1224
|
+
}
|
|
1225
|
+
const CHARACTER_SETS = {
|
|
1226
|
+
upperCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
1227
|
+
lowerCase: "abcdefghijklmnopqrstuvwxyz",
|
|
1228
|
+
upperAndLowerCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
1229
|
+
numbers: "0123456789"
|
|
1230
|
+
};
|
|
1231
|
+
const DEFAULT_REFRESH_RATE = 20;
|
|
1232
|
+
function charactersFor(chars) {
|
|
1233
|
+
const set = CHARACTER_SETS[chars ?? "upperCase"] ?? chars ?? CHARACTER_SETS.upperCase;
|
|
1234
|
+
const list = Array.from(set);
|
|
1235
|
+
return list.length > 0 ? list : Array.from(CHARACTER_SETS.upperCase);
|
|
1236
|
+
}
|
|
1237
|
+
function hash(seed, index, step) {
|
|
1238
|
+
let h = (seed | 0) ^ Math.imul(index + 1, 2654435761) ^ Math.imul(step + 1, 2246822507);
|
|
1239
|
+
h = Math.imul(h ^ h >>> 16, 2146121005);
|
|
1240
|
+
h = Math.imul(h ^ h >>> 15, 2221713035);
|
|
1241
|
+
return (h ^ h >>> 16) >>> 0;
|
|
1242
|
+
}
|
|
1243
|
+
function textAt(config, progress, elapsedMs = 0) {
|
|
1244
|
+
const from = config.from ?? "";
|
|
1245
|
+
const to = config.to;
|
|
1246
|
+
const p = Math.max(0, Math.min(1, progress));
|
|
1247
|
+
if (p <= 0) return from;
|
|
1248
|
+
if (p >= 1) return to;
|
|
1249
|
+
const fromChars = Array.from(from);
|
|
1250
|
+
const toChars = Array.from(to);
|
|
1251
|
+
const rightToLeft = config.rightToLeft ?? false;
|
|
1252
|
+
if (config.mode === "type") {
|
|
1253
|
+
const steps = Math.round(p * Math.max(fromChars.length, toChars.length));
|
|
1254
|
+
if (rightToLeft) {
|
|
1255
|
+
const kept = fromChars.slice(0, Math.max(0, fromChars.length - steps));
|
|
1256
|
+
return kept.join("") + toChars.slice(Math.max(0, toChars.length - steps)).join("");
|
|
1257
|
+
}
|
|
1258
|
+
return toChars.slice(0, steps).join("") + fromChars.slice(steps).join("");
|
|
1259
|
+
}
|
|
1260
|
+
const delay = Math.max(0, Math.min(0.999, config.revealDelay ?? 0));
|
|
1261
|
+
const reveal = Math.max(0, (p - delay) / (1 - delay));
|
|
1262
|
+
const revealed = Math.floor(reveal * toChars.length);
|
|
1263
|
+
const length = config.tweenLength === false ? toChars.length : Math.round(fromChars.length + (toChars.length - fromChars.length) * p);
|
|
1264
|
+
const pool = charactersFor(config.chars);
|
|
1265
|
+
const refreshRate = config.refreshRate ?? DEFAULT_REFRESH_RATE;
|
|
1266
|
+
const step = refreshRate > 0 ? Math.floor(elapsedMs * refreshRate / 1e3) : 0;
|
|
1267
|
+
const seed = config.seed ?? 1;
|
|
1268
|
+
let out = "";
|
|
1269
|
+
for (let i = 0; i < length; i++) {
|
|
1270
|
+
const settled = rightToLeft ? i >= length - revealed : i < revealed;
|
|
1271
|
+
const target = rightToLeft ? toChars[toChars.length - (length - i)] : toChars[i];
|
|
1272
|
+
if (settled && target !== void 0) {
|
|
1273
|
+
out += target;
|
|
1274
|
+
} else if (target === " " || target === "\n") {
|
|
1275
|
+
out += target;
|
|
1276
|
+
} else {
|
|
1277
|
+
out += pool[hash(seed, i, step) % pool.length];
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
return out;
|
|
1281
|
+
}
|
|
1282
|
+
class Timeline {
|
|
1283
|
+
id;
|
|
1284
|
+
name;
|
|
1285
|
+
/** Caption text per language, per marker id */
|
|
1286
|
+
captions;
|
|
1287
|
+
_tracks = [];
|
|
1288
|
+
_trackPlayers = /* @__PURE__ */ new Map();
|
|
1289
|
+
_motionPathTracks = /* @__PURE__ */ new Map();
|
|
1290
|
+
_springTracks = /* @__PURE__ */ new Map();
|
|
1291
|
+
_textTracks = /* @__PURE__ */ new Map();
|
|
1292
|
+
_config;
|
|
1293
|
+
_currentTime = 0;
|
|
1294
|
+
_playbackState = "idle";
|
|
1295
|
+
_direction = "forward";
|
|
1296
|
+
_loopIteration = 0;
|
|
1297
|
+
_explicitDuration;
|
|
1298
|
+
/** Milliseconds still to wait at a loop boundary before the next iteration */
|
|
1299
|
+
_repeatDelayRemaining = 0;
|
|
1300
|
+
/**
|
|
1301
|
+
* A forward loop reached its end with a repeat delay armed: the playhead
|
|
1302
|
+
* holds on the last frame for the delay, then returns to the start.
|
|
1303
|
+
*/
|
|
1304
|
+
_wrapAfterDelay = false;
|
|
1305
|
+
onUpdate = null;
|
|
1306
|
+
onComplete = null;
|
|
1307
|
+
constructor(options) {
|
|
1308
|
+
this.id = options.id;
|
|
1309
|
+
this.name = options.name;
|
|
1310
|
+
this.captions = options.captions;
|
|
1311
|
+
this._config = options.config ?? {};
|
|
1312
|
+
this._explicitDuration = options.config?.duration;
|
|
1313
|
+
if (options.tracks) {
|
|
1314
|
+
for (const track of options.tracks) {
|
|
1315
|
+
this.addTrack(track);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
get tracks() {
|
|
1320
|
+
return [...this._tracks];
|
|
1321
|
+
}
|
|
1322
|
+
get duration() {
|
|
1323
|
+
if (this._explicitDuration !== void 0) {
|
|
1324
|
+
return this._explicitDuration;
|
|
1325
|
+
}
|
|
1326
|
+
return this._calculateDuration();
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Set an explicit timeline duration (ms). Pass `undefined` to fall back to the
|
|
1330
|
+
* duration calculated from the last keyframe across all tracks.
|
|
1331
|
+
*/
|
|
1332
|
+
setDuration(duration) {
|
|
1333
|
+
this._explicitDuration = duration;
|
|
1334
|
+
if (duration !== void 0) {
|
|
1335
|
+
this._config = { ...this._config, duration };
|
|
1336
|
+
} else if (this._config.duration !== void 0) {
|
|
1337
|
+
const config = { ...this._config };
|
|
1338
|
+
delete config.duration;
|
|
1339
|
+
this._config = config;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
get currentTime() {
|
|
1343
|
+
return this._currentTime;
|
|
1344
|
+
}
|
|
1345
|
+
get playbackState() {
|
|
1346
|
+
return this._playbackState;
|
|
1347
|
+
}
|
|
1348
|
+
get direction() {
|
|
1349
|
+
return this._direction;
|
|
1350
|
+
}
|
|
1351
|
+
get loopIteration() {
|
|
1352
|
+
return this._loopIteration;
|
|
1353
|
+
}
|
|
1354
|
+
/** Milliseconds of `repeatDelay` still to wait at a loop boundary (0 when not waiting). */
|
|
1355
|
+
get repeatDelayRemaining() {
|
|
1356
|
+
return this._repeatDelayRemaining;
|
|
1357
|
+
}
|
|
1358
|
+
get speed() {
|
|
1359
|
+
return this._config.speed ?? 1;
|
|
1360
|
+
}
|
|
1361
|
+
set speed(value) {
|
|
1362
|
+
this._config.speed = value;
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Start or resume playback.
|
|
1366
|
+
* If at the end and direction is forward, reset to beginning.
|
|
1367
|
+
* If at the beginning and direction is reverse, reset to end.
|
|
1368
|
+
*/
|
|
1369
|
+
play() {
|
|
1370
|
+
const duration = this.duration;
|
|
1371
|
+
if (this._direction === "forward" && this._currentTime >= duration && duration > 0) {
|
|
1372
|
+
this._currentTime = 0;
|
|
1373
|
+
this._loopIteration = 0;
|
|
1374
|
+
} else if (this._direction === "reverse" && this._currentTime <= 0 && duration > 0) {
|
|
1375
|
+
this._currentTime = duration;
|
|
1376
|
+
this._loopIteration = 0;
|
|
1377
|
+
}
|
|
1378
|
+
this._playbackState = "playing";
|
|
1379
|
+
}
|
|
1380
|
+
/**
|
|
1381
|
+
* Pause playback at current position.
|
|
1382
|
+
*/
|
|
1383
|
+
pause() {
|
|
1384
|
+
this._playbackState = "paused";
|
|
1385
|
+
}
|
|
1386
|
+
/**
|
|
1387
|
+
* Stop playback and reset to beginning.
|
|
1388
|
+
*/
|
|
1389
|
+
stop() {
|
|
1390
|
+
this._playbackState = "idle";
|
|
1391
|
+
this._repeatDelayRemaining = 0;
|
|
1392
|
+
this._wrapAfterDelay = false;
|
|
1393
|
+
this._currentTime = 0;
|
|
1394
|
+
this._loopIteration = 0;
|
|
1395
|
+
this._direction = "forward";
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Seek to a specific time.
|
|
1399
|
+
*/
|
|
1400
|
+
seek(time) {
|
|
1401
|
+
const maxTime = this.duration > 0 ? this.duration : Infinity;
|
|
1402
|
+
this._currentTime = Math.max(0, Math.min(time, maxTime));
|
|
1403
|
+
this._repeatDelayRemaining = 0;
|
|
1404
|
+
this._wrapAfterDelay = false;
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Toggle or set playback direction.
|
|
1408
|
+
*/
|
|
1409
|
+
reverse() {
|
|
1410
|
+
this._direction = this._direction === "forward" ? "reverse" : "forward";
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* Advance the timeline by delta milliseconds.
|
|
1414
|
+
* Call this from your animation loop or clock.
|
|
1415
|
+
*/
|
|
1416
|
+
tick(delta) {
|
|
1417
|
+
if (this._playbackState !== "playing") {
|
|
1418
|
+
return;
|
|
1419
|
+
}
|
|
1420
|
+
const duration = this.duration;
|
|
1421
|
+
if (duration <= 0) {
|
|
1422
|
+
return;
|
|
1423
|
+
}
|
|
1424
|
+
const scaledDelta = delta * this.speed;
|
|
1425
|
+
let timeToProcess = scaledDelta;
|
|
1426
|
+
if (this._repeatDelayRemaining > 0) {
|
|
1427
|
+
const consumed = Math.min(this._repeatDelayRemaining, timeToProcess);
|
|
1428
|
+
this._repeatDelayRemaining -= consumed;
|
|
1429
|
+
timeToProcess -= consumed;
|
|
1430
|
+
if (this._repeatDelayRemaining > 0) {
|
|
1431
|
+
this.onUpdate?.(this.getStateAtTime(this._currentTime));
|
|
1432
|
+
return;
|
|
1433
|
+
}
|
|
1434
|
+
if (this._wrapAfterDelay) {
|
|
1435
|
+
this._wrapAfterDelay = false;
|
|
1436
|
+
this._currentTime = 0;
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
const maxIterations = 1e3;
|
|
1440
|
+
for (let i = 0; i < maxIterations && timeToProcess > 0 && this._playbackState === "playing"; i++) {
|
|
1441
|
+
if (this._direction === "forward") {
|
|
1442
|
+
const timeUntilEnd = duration - this._currentTime;
|
|
1443
|
+
if (timeToProcess >= timeUntilEnd) {
|
|
1444
|
+
timeToProcess -= timeUntilEnd;
|
|
1445
|
+
this._currentTime = duration;
|
|
1446
|
+
if (!this._handleEndReached()) {
|
|
1447
|
+
break;
|
|
1448
|
+
}
|
|
1449
|
+
} else {
|
|
1450
|
+
this._currentTime += timeToProcess;
|
|
1451
|
+
timeToProcess = 0;
|
|
1452
|
+
}
|
|
1453
|
+
} else {
|
|
1454
|
+
const timeUntilStart = this._currentTime;
|
|
1455
|
+
if (timeToProcess >= timeUntilStart) {
|
|
1456
|
+
timeToProcess -= timeUntilStart;
|
|
1457
|
+
this._currentTime = 0;
|
|
1458
|
+
if (!this._handleStartReached()) {
|
|
1459
|
+
break;
|
|
1460
|
+
}
|
|
1461
|
+
} else {
|
|
1462
|
+
this._currentTime -= timeToProcess;
|
|
1463
|
+
timeToProcess = 0;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
const state = this.getStateAtTime(this._currentTime);
|
|
1468
|
+
this.onUpdate?.(state);
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Get the animation state at a specific time.
|
|
1472
|
+
*/
|
|
1473
|
+
getStateAtTime(time) {
|
|
1474
|
+
const values = /* @__PURE__ */ new Map();
|
|
1475
|
+
if (this._hasSharedWrites()) {
|
|
1476
|
+
this._resolveShared(time, values);
|
|
1477
|
+
} else {
|
|
1478
|
+
for (const [trackId, player] of this._trackPlayers) {
|
|
1479
|
+
const property = player.getTrack().property;
|
|
1480
|
+
for (const { target, value, start } of player.getTargetValues(time)) {
|
|
1481
|
+
this._write(values, trackId, target, property, value, time - start);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
return {
|
|
1486
|
+
values,
|
|
1487
|
+
currentTime: this._currentTime,
|
|
1488
|
+
playbackState: this._playbackState,
|
|
1489
|
+
direction: this._direction,
|
|
1490
|
+
loopIteration: this._loopIteration
|
|
1491
|
+
};
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* Several tracks drive the same target+property. Which one applies at `time`:
|
|
1495
|
+
*
|
|
1496
|
+
* 1. Of the tracks that have started (their first keyframe, plus delay and
|
|
1497
|
+
* stagger, is at or before `time`), the one that started LAST.
|
|
1498
|
+
* 2. If none has started yet, the one that starts FIRST — so the value before
|
|
1499
|
+
* anything plays is the first animation's starting value.
|
|
1500
|
+
* 3. Ties on start time go to the track added LAST.
|
|
1501
|
+
*
|
|
1502
|
+
* This is what makes a sequence of tweens on one property play as a sequence:
|
|
1503
|
+
* a later tween holds its starting value, but does not apply it until its
|
|
1504
|
+
* turn. `findConflicts()` reports overlaps by the same rule.
|
|
1505
|
+
*/
|
|
1506
|
+
_resolveShared(time, values) {
|
|
1507
|
+
const chosen = /* @__PURE__ */ new Map();
|
|
1508
|
+
for (const [trackId, player] of this._trackPlayers) {
|
|
1509
|
+
const property = player.getTrack().property;
|
|
1510
|
+
for (const { target, value, start } of player.getTargetValues(time)) {
|
|
1511
|
+
const key = `${target}\0${property}`;
|
|
1512
|
+
const started = start <= time;
|
|
1513
|
+
const current = chosen.get(key);
|
|
1514
|
+
const wins = !current || (started !== current.started ? started : started ? start >= current.start : start <= current.start);
|
|
1515
|
+
if (wins) chosen.set(key, { trackId, target, property, value, start, started });
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
for (const { trackId, target, property, value, start } of chosen.values()) {
|
|
1519
|
+
this._write(values, trackId, target, property, value, time - start);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Write one track's value for a target, expanding the progress of motion paths
|
|
1524
|
+
* (into x/y/rotation) and text tracks (into the string). `elapsed` is the time
|
|
1525
|
+
* since this target's animation on the track started.
|
|
1526
|
+
*/
|
|
1527
|
+
_write(values, trackId, target, property, value, elapsed) {
|
|
1528
|
+
if (value === void 0) return;
|
|
1529
|
+
let targetValues = values.get(target);
|
|
1530
|
+
if (!targetValues) {
|
|
1531
|
+
targetValues = /* @__PURE__ */ new Map();
|
|
1532
|
+
values.set(target, targetValues);
|
|
1533
|
+
}
|
|
1534
|
+
const textTrack = this._textTracks.get(trackId);
|
|
1535
|
+
if (textTrack && typeof value === "number") {
|
|
1536
|
+
targetValues.set("text", textAt(textTrack.textConfig, value, Math.max(0, elapsed)));
|
|
1537
|
+
return;
|
|
1538
|
+
}
|
|
1539
|
+
const motionPathTrack = this._motionPathTracks.get(trackId);
|
|
1540
|
+
if (motionPathTrack && typeof value === "number") {
|
|
1541
|
+
const point = getMotionPathPoint(motionPathTrack.motionPathConfig, value);
|
|
1542
|
+
targetValues.set("motionPathX", point.x);
|
|
1543
|
+
targetValues.set("motionPathY", point.y);
|
|
1544
|
+
if (motionPathTrack.motionPathConfig.autoRotate) {
|
|
1545
|
+
targetValues.set("motionPathRotate", point.angle);
|
|
1546
|
+
}
|
|
1547
|
+
} else {
|
|
1548
|
+
targetValues.set(property, value);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
/** Cached: does any target+property have more than one track? */
|
|
1552
|
+
_sharedWrites = null;
|
|
1553
|
+
_hasSharedWrites() {
|
|
1554
|
+
if (this._sharedWrites === null) {
|
|
1555
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1556
|
+
this._sharedWrites = false;
|
|
1557
|
+
outer: for (const track of this._tracks) {
|
|
1558
|
+
for (const target of trackTargets(track)) {
|
|
1559
|
+
const key = `${target}\0${track.property}`;
|
|
1560
|
+
if (seen.has(key)) {
|
|
1561
|
+
this._sharedWrites = true;
|
|
1562
|
+
break outer;
|
|
1563
|
+
}
|
|
1564
|
+
seen.add(key);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
return this._sharedWrites;
|
|
1569
|
+
}
|
|
1570
|
+
/**
|
|
1571
|
+
* Add a track to the timeline.
|
|
1572
|
+
*/
|
|
1573
|
+
addTrack(track) {
|
|
1574
|
+
this._tracks.push(track);
|
|
1575
|
+
this._sharedWrites = null;
|
|
1576
|
+
if (isInertiaTrack(track)) {
|
|
1577
|
+
this._trackPlayers.set(track.id, new InertiaTrackPlayer(track));
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
if (isSpringTrack(track)) {
|
|
1581
|
+
this._trackPlayers.set(track.id, new SpringTrackPlayer(track));
|
|
1582
|
+
this._springTracks.set(track.id, track);
|
|
1583
|
+
return;
|
|
1584
|
+
}
|
|
1585
|
+
if (isTextTrack(track)) {
|
|
1586
|
+
this._trackPlayers.set(track.id, new TrackPlayer(track));
|
|
1587
|
+
this._textTracks.set(track.id, track);
|
|
1588
|
+
} else if (isMotionPathTrack(track)) {
|
|
1589
|
+
const regularTrack = {
|
|
1590
|
+
id: track.id,
|
|
1591
|
+
target: track.target,
|
|
1592
|
+
property: track.property,
|
|
1593
|
+
keyframes: track.keyframes,
|
|
1594
|
+
delay: track.delay,
|
|
1595
|
+
endDelay: track.endDelay,
|
|
1596
|
+
targets: track.targets,
|
|
1597
|
+
stagger: track.stagger
|
|
1598
|
+
};
|
|
1599
|
+
this._trackPlayers.set(track.id, new TrackPlayer(regularTrack));
|
|
1600
|
+
this._motionPathTracks.set(track.id, track);
|
|
1601
|
+
} else {
|
|
1602
|
+
this._trackPlayers.set(track.id, new TrackPlayer(track));
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
/**
|
|
1606
|
+
* Replace a track with a new version, keeping its place in the track order
|
|
1607
|
+
* (which decides ties when tracks overlap). The new track may have a
|
|
1608
|
+
* different id. Does nothing if no track has `trackId`.
|
|
1609
|
+
*/
|
|
1610
|
+
replaceTrack(trackId, track) {
|
|
1611
|
+
const index = this._tracks.findIndex((t) => t.id === trackId);
|
|
1612
|
+
if (index < 0) return;
|
|
1613
|
+
const after = this._tracks.slice(index + 1);
|
|
1614
|
+
this.removeTrack(trackId);
|
|
1615
|
+
for (const later of after) this.removeTrack(later.id);
|
|
1616
|
+
this.addTrack(track);
|
|
1617
|
+
for (const later of after) this.addTrack(later);
|
|
1618
|
+
}
|
|
1619
|
+
/**
|
|
1620
|
+
* Remove a track by its ID.
|
|
1621
|
+
*/
|
|
1622
|
+
removeTrack(trackId) {
|
|
1623
|
+
this._tracks = this._tracks.filter((t) => t.id !== trackId);
|
|
1624
|
+
this._sharedWrites = null;
|
|
1625
|
+
this._trackPlayers.delete(trackId);
|
|
1626
|
+
this._motionPathTracks.delete(trackId);
|
|
1627
|
+
this._springTracks.delete(trackId);
|
|
1628
|
+
this._textTracks.delete(trackId);
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Tracks matching a filter. All provided fields must match (AND).
|
|
1632
|
+
*
|
|
1633
|
+
* This is the closest principled equivalent to GSAP's per-tween handle: we
|
|
1634
|
+
* have no live tween objects to hold, so a "tween" is addressed by describing
|
|
1635
|
+
* the tracks it produced.
|
|
1636
|
+
*/
|
|
1637
|
+
getTracks(filter = {}) {
|
|
1638
|
+
return this._tracks.filter((track) => this._matches(track, filter));
|
|
1639
|
+
}
|
|
1640
|
+
/**
|
|
1641
|
+
* Remove every track matching a filter. Returns the ids removed.
|
|
1642
|
+
*
|
|
1643
|
+
* `timeline.removeTracks({ target: 'box' })` is the equivalent of killing all
|
|
1644
|
+
* tweens on an element.
|
|
1645
|
+
*/
|
|
1646
|
+
removeTracks(filter = {}) {
|
|
1647
|
+
const doomed = this.getTracks(filter).map((t) => t.id);
|
|
1648
|
+
for (const id of doomed) {
|
|
1649
|
+
this.removeTrack(id);
|
|
1650
|
+
}
|
|
1651
|
+
return doomed;
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* The time span a track is active over: [start, end] in milliseconds.
|
|
1655
|
+
*/
|
|
1656
|
+
getTrackSpan(trackId) {
|
|
1657
|
+
const player = this._trackPlayers.get(trackId);
|
|
1658
|
+
if (!player) return void 0;
|
|
1659
|
+
const track = player.getTrack();
|
|
1660
|
+
const delay = track.delay ?? 0;
|
|
1661
|
+
if (isSpringTrack(track) || isInertiaTrack(track)) {
|
|
1662
|
+
return { from: delay, to: player.getDuration() };
|
|
1663
|
+
}
|
|
1664
|
+
const keyframes = track.keyframes;
|
|
1665
|
+
if (!keyframes || keyframes.length === 0) return void 0;
|
|
1666
|
+
return { from: keyframes[0].time + delay, to: player.getDuration() };
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* Overlapping writes to the same target+property.
|
|
1670
|
+
*
|
|
1671
|
+
* Where two spans overlap, the track that starts later wins from the moment it
|
|
1672
|
+
* starts (ties: the one added later) — see `_resolveShared`. That is
|
|
1673
|
+
* predictable but silent, so an authoring tool should call this and warn,
|
|
1674
|
+
* because a silently discarded stretch of a track looks like a bug.
|
|
1675
|
+
*/
|
|
1676
|
+
findConflicts() {
|
|
1677
|
+
const conflicts = [];
|
|
1678
|
+
for (let i = 0; i < this._tracks.length; i++) {
|
|
1679
|
+
const later = this._tracks[i];
|
|
1680
|
+
const laterSpan = this.getTrackSpan(later.id);
|
|
1681
|
+
if (!laterSpan) continue;
|
|
1682
|
+
for (let j = 0; j < i; j++) {
|
|
1683
|
+
const earlier = this._tracks[j];
|
|
1684
|
+
if (earlier.property !== later.property) continue;
|
|
1685
|
+
const shared = trackTargets(earlier).filter((t) => trackTargets(later).includes(t));
|
|
1686
|
+
if (shared.length === 0) continue;
|
|
1687
|
+
const earlierSpan = this.getTrackSpan(earlier.id);
|
|
1688
|
+
if (!earlierSpan) continue;
|
|
1689
|
+
const overlaps = earlierSpan.from <= laterSpan.to && laterSpan.from <= earlierSpan.to;
|
|
1690
|
+
if (!overlaps) continue;
|
|
1691
|
+
const laterWins = laterSpan.from >= earlierSpan.from;
|
|
1692
|
+
for (const target of shared) {
|
|
1693
|
+
conflicts.push({
|
|
1694
|
+
target,
|
|
1695
|
+
property: later.property,
|
|
1696
|
+
losingTrackId: laterWins ? earlier.id : later.id,
|
|
1697
|
+
winningTrackId: laterWins ? later.id : earlier.id
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
return conflicts;
|
|
1703
|
+
}
|
|
1704
|
+
_matches(track, filter) {
|
|
1705
|
+
if (filter.id !== void 0 && track.id !== filter.id) return false;
|
|
1706
|
+
if (filter.property !== void 0 && track.property !== filter.property) return false;
|
|
1707
|
+
if (filter.target !== void 0 && !trackTargets(track).includes(filter.target)) return false;
|
|
1708
|
+
if (filter.timeRange) {
|
|
1709
|
+
const span = this.getTrackSpan(track.id);
|
|
1710
|
+
if (!span) return false;
|
|
1711
|
+
if (span.to < filter.timeRange.from || span.from > filter.timeRange.to) return false;
|
|
1712
|
+
}
|
|
1713
|
+
return true;
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* Export timeline as a serializable definition.
|
|
1717
|
+
*/
|
|
1718
|
+
toDefinition() {
|
|
1719
|
+
return {
|
|
1720
|
+
formatVersion: FORMAT_VERSION,
|
|
1721
|
+
id: this.id,
|
|
1722
|
+
name: this.name,
|
|
1723
|
+
config: { ...this._config },
|
|
1724
|
+
tracks: [...this._tracks],
|
|
1725
|
+
...this.captions && { captions: this.captions }
|
|
1726
|
+
};
|
|
1727
|
+
}
|
|
1728
|
+
/** The playback configuration, as loaded (read-only). */
|
|
1729
|
+
get config() {
|
|
1730
|
+
return this._config;
|
|
1731
|
+
}
|
|
1732
|
+
/** The timeline's markers, in time order (empty when it has none). */
|
|
1733
|
+
get markers() {
|
|
1734
|
+
return [...this._config.markers ?? []].sort((a, b) => a.time - b.time);
|
|
1735
|
+
}
|
|
1736
|
+
/** Start the between-iterations pause, if the timeline configures one. */
|
|
1737
|
+
_armRepeatDelay() {
|
|
1738
|
+
this._repeatDelayRemaining = this._config.repeatDelay ?? 0;
|
|
1739
|
+
}
|
|
1740
|
+
_calculateDuration() {
|
|
1741
|
+
let maxDuration = 0;
|
|
1742
|
+
for (const [, player] of this._trackPlayers) {
|
|
1743
|
+
maxDuration = Math.max(maxDuration, player.getDuration());
|
|
1744
|
+
}
|
|
1745
|
+
return maxDuration;
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Handle reaching the end of the timeline.
|
|
1749
|
+
* Returns true if we looped and should continue, false if we stopped.
|
|
1750
|
+
*/
|
|
1751
|
+
_handleEndReached() {
|
|
1752
|
+
const loopConfig = this._config.loop ?? 0;
|
|
1753
|
+
if (loopConfig === -1 || this._loopIteration < loopConfig) {
|
|
1754
|
+
this._loopIteration++;
|
|
1755
|
+
this._armRepeatDelay();
|
|
1756
|
+
if (this._config.alternate) {
|
|
1757
|
+
this._direction = "reverse";
|
|
1758
|
+
} else if (this._repeatDelayRemaining > 0) {
|
|
1759
|
+
this._wrapAfterDelay = true;
|
|
1760
|
+
} else {
|
|
1761
|
+
this._currentTime = 0;
|
|
1762
|
+
}
|
|
1763
|
+
return this._repeatDelayRemaining === 0;
|
|
1764
|
+
} else {
|
|
1765
|
+
this._playbackState = "idle";
|
|
1766
|
+
this.onComplete?.();
|
|
1767
|
+
return false;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* Handle reaching the start of the timeline (in reverse).
|
|
1772
|
+
* Returns true if we looped and should continue, false if we stopped.
|
|
1773
|
+
*/
|
|
1774
|
+
_handleStartReached() {
|
|
1775
|
+
const loopConfig = this._config.loop ?? 0;
|
|
1776
|
+
if (this._config.alternate && (loopConfig === -1 || this._loopIteration < loopConfig)) {
|
|
1777
|
+
this._loopIteration++;
|
|
1778
|
+
this._armRepeatDelay();
|
|
1779
|
+
this._direction = "forward";
|
|
1780
|
+
return this._repeatDelayRemaining === 0;
|
|
1781
|
+
} else {
|
|
1782
|
+
this._playbackState = "idle";
|
|
1783
|
+
this.onComplete?.();
|
|
1784
|
+
return false;
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
function copyInertia(inertia) {
|
|
1789
|
+
return { ...inertia, ...Array.isArray(inertia.end) && { end: [...inertia.end] } };
|
|
1790
|
+
}
|
|
1791
|
+
function scheduling(track) {
|
|
1792
|
+
const endDelay = track.endDelay;
|
|
1793
|
+
return {
|
|
1794
|
+
...track.delay !== void 0 && { delay: track.delay },
|
|
1795
|
+
...endDelay !== void 0 && { endDelay },
|
|
1796
|
+
...track.targets !== void 0 && { targets: [...track.targets] },
|
|
1797
|
+
...track.stagger !== void 0 && { stagger: { ...track.stagger } }
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
function deserializeTrack(data) {
|
|
1801
|
+
if (isInertiaTrack(data)) {
|
|
1802
|
+
const inertiaData = data;
|
|
1803
|
+
return {
|
|
1804
|
+
id: inertiaData.id,
|
|
1805
|
+
target: inertiaData.target,
|
|
1806
|
+
property: inertiaData.property,
|
|
1807
|
+
kind: "inertia",
|
|
1808
|
+
inertia: copyInertia(inertiaData.inertia),
|
|
1809
|
+
...scheduling(inertiaData)
|
|
1810
|
+
};
|
|
1811
|
+
}
|
|
1812
|
+
if (isSpringTrack(data)) {
|
|
1813
|
+
const springData = data;
|
|
1814
|
+
return {
|
|
1815
|
+
id: springData.id,
|
|
1816
|
+
target: springData.target,
|
|
1817
|
+
property: springData.property,
|
|
1818
|
+
kind: "spring",
|
|
1819
|
+
spring: { ...springData.spring },
|
|
1820
|
+
...scheduling(springData)
|
|
1821
|
+
};
|
|
1822
|
+
}
|
|
1823
|
+
if (isTextTrack(data)) {
|
|
1824
|
+
const textData = data;
|
|
1825
|
+
return {
|
|
1826
|
+
id: textData.id,
|
|
1827
|
+
target: textData.target,
|
|
1828
|
+
property: "text",
|
|
1829
|
+
textConfig: { ...textData.textConfig },
|
|
1830
|
+
keyframes: [...textData.keyframes].sort((a, b) => a.time - b.time),
|
|
1831
|
+
...scheduling(textData)
|
|
1832
|
+
};
|
|
1833
|
+
}
|
|
1834
|
+
if (data.property === "motionPath" && "motionPathConfig" in data) {
|
|
1835
|
+
const motionData = data;
|
|
1836
|
+
const sortedKeyframes = [...motionData.keyframes].sort((a, b) => a.time - b.time);
|
|
1837
|
+
return {
|
|
1838
|
+
id: motionData.id,
|
|
1839
|
+
target: motionData.target,
|
|
1840
|
+
property: "motionPath",
|
|
1841
|
+
motionPathConfig: { ...motionData.motionPathConfig },
|
|
1842
|
+
keyframes: sortedKeyframes,
|
|
1843
|
+
...scheduling(motionData)
|
|
1844
|
+
};
|
|
1845
|
+
}
|
|
1846
|
+
return createTrack({
|
|
1847
|
+
id: data.id,
|
|
1848
|
+
target: data.target,
|
|
1849
|
+
property: data.property,
|
|
1850
|
+
keyframes: data.keyframes,
|
|
1851
|
+
...scheduling(data)
|
|
1852
|
+
});
|
|
1853
|
+
}
|
|
1854
|
+
function deserializeTimeline(definition) {
|
|
1855
|
+
const version = definition.formatVersion ?? 1;
|
|
1856
|
+
if (version > FORMAT_VERSION) {
|
|
1857
|
+
throw new Error(
|
|
1858
|
+
`tinyfly: this animation uses format version ${version}, but this tinyfly reads up to version ${FORMAT_VERSION}. Update tinyfly to play it.`
|
|
1859
|
+
);
|
|
1860
|
+
}
|
|
1861
|
+
return new Timeline({
|
|
1862
|
+
id: definition.id,
|
|
1863
|
+
name: definition.name,
|
|
1864
|
+
config: definition.config,
|
|
1865
|
+
tracks: definition.tracks.map(deserializeTrack),
|
|
1866
|
+
captions: definition.captions
|
|
1867
|
+
});
|
|
1868
|
+
}
|
|
1869
|
+
const FILTER_PROPERTIES = /* @__PURE__ */ new Set([
|
|
1870
|
+
"blur",
|
|
1871
|
+
"brightness",
|
|
1872
|
+
"glow",
|
|
1873
|
+
"glowColor",
|
|
1874
|
+
"shadowX",
|
|
1875
|
+
"shadowY",
|
|
1876
|
+
"shadowBlur",
|
|
1877
|
+
"shadowColor"
|
|
1878
|
+
]);
|
|
1879
|
+
const DEFAULT_GLOW_COLOR = "#ffffff";
|
|
1880
|
+
const DEFAULT_SHADOW_COLOR = "rgba(0, 0, 0, 0.5)";
|
|
1881
|
+
function composeFilter(v) {
|
|
1882
|
+
const parts = [];
|
|
1883
|
+
if (v.blur !== void 0) {
|
|
1884
|
+
parts.push(`blur(${Math.max(0, v.blur)}px)`);
|
|
1885
|
+
}
|
|
1886
|
+
if (v.brightness !== void 0) {
|
|
1887
|
+
parts.push(`brightness(${Math.max(0, v.brightness)})`);
|
|
1888
|
+
}
|
|
1889
|
+
if (v.glow !== void 0) {
|
|
1890
|
+
parts.push(`drop-shadow(0 0 ${Math.max(0, v.glow)}px ${v.glowColor ?? DEFAULT_GLOW_COLOR})`);
|
|
1891
|
+
}
|
|
1892
|
+
if (v.shadowX !== void 0 || v.shadowY !== void 0 || v.shadowBlur !== void 0) {
|
|
1893
|
+
const x = v.shadowX ?? 0;
|
|
1894
|
+
const y = v.shadowY ?? 0;
|
|
1895
|
+
const blur = Math.max(0, v.shadowBlur ?? 0);
|
|
1896
|
+
parts.push(`drop-shadow(${x}px ${y}px ${blur}px ${v.shadowColor ?? DEFAULT_SHADOW_COLOR})`);
|
|
1897
|
+
}
|
|
1898
|
+
return parts.length > 0 ? parts.join(" ") : null;
|
|
1899
|
+
}
|
|
1900
|
+
function setTextContent(element, value) {
|
|
1901
|
+
const only = element.childNodes.length === 1 ? element.firstChild : null;
|
|
1902
|
+
if (only && only.nodeType === 3) {
|
|
1903
|
+
const textNode = only;
|
|
1904
|
+
if (textNode.data !== value) textNode.data = value;
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
if (element.textContent !== value) element.textContent = value;
|
|
1908
|
+
}
|
|
1909
|
+
function ensureSvgTransformBox(element) {
|
|
1910
|
+
if (!("ownerSVGElement" in element)) return;
|
|
1911
|
+
const style = element.style;
|
|
1912
|
+
if (!style || style.transformBox) return;
|
|
1913
|
+
style.transformBox = "fill-box";
|
|
1914
|
+
if (!style.transformOrigin) style.transformOrigin = "50% 50%";
|
|
1915
|
+
}
|
|
1916
|
+
const PX_PROPERTIES = /* @__PURE__ */ new Set([
|
|
1917
|
+
"width",
|
|
1918
|
+
"height",
|
|
1919
|
+
"top",
|
|
1920
|
+
"right",
|
|
1921
|
+
"bottom",
|
|
1922
|
+
"left",
|
|
1923
|
+
"margin",
|
|
1924
|
+
"marginTop",
|
|
1925
|
+
"marginRight",
|
|
1926
|
+
"marginBottom",
|
|
1927
|
+
"marginLeft",
|
|
1928
|
+
"padding",
|
|
1929
|
+
"paddingTop",
|
|
1930
|
+
"paddingRight",
|
|
1931
|
+
"paddingBottom",
|
|
1932
|
+
"paddingLeft",
|
|
1933
|
+
"borderRadius",
|
|
1934
|
+
"borderWidth",
|
|
1935
|
+
"fontSize",
|
|
1936
|
+
"lineHeight",
|
|
1937
|
+
"letterSpacing",
|
|
1938
|
+
"outlineWidth",
|
|
1939
|
+
"gap",
|
|
1940
|
+
"rowGap",
|
|
1941
|
+
"columnGap"
|
|
1942
|
+
]);
|
|
1943
|
+
const TRANSFORM_PROPERTIES = /* @__PURE__ */ new Set([
|
|
1944
|
+
"x",
|
|
1945
|
+
"y",
|
|
1946
|
+
"z",
|
|
1947
|
+
"rotate",
|
|
1948
|
+
"rotateX",
|
|
1949
|
+
"rotateY",
|
|
1950
|
+
"rotateZ",
|
|
1951
|
+
"scale",
|
|
1952
|
+
"scaleX",
|
|
1953
|
+
"scaleY",
|
|
1954
|
+
"scaleZ",
|
|
1955
|
+
"skewX",
|
|
1956
|
+
"skewY",
|
|
1957
|
+
// Motion path properties
|
|
1958
|
+
"motionPathX",
|
|
1959
|
+
"motionPathY",
|
|
1960
|
+
"motionPathRotate"
|
|
1961
|
+
]);
|
|
1962
|
+
const ORIGIN_PROPERTIES = /* @__PURE__ */ new Set(["originX", "originY"]);
|
|
1963
|
+
const CLIP_PROPERTIES = /* @__PURE__ */ new Set(["clipTop", "clipRight", "clipBottom", "clipLeft"]);
|
|
1964
|
+
const PROPERTY_MAP = {
|
|
1965
|
+
fill: "backgroundColor",
|
|
1966
|
+
stroke: "borderColor",
|
|
1967
|
+
strokeWidth: "borderWidth",
|
|
1968
|
+
color: "color",
|
|
1969
|
+
backgroundColor: "backgroundColor",
|
|
1970
|
+
borderColor: "borderColor"
|
|
1971
|
+
};
|
|
1972
|
+
const SVG_PROPERTY_MAP = {
|
|
1973
|
+
fill: "fill",
|
|
1974
|
+
stroke: "stroke",
|
|
1975
|
+
strokeWidth: "strokeWidth",
|
|
1976
|
+
strokeDasharray: "strokeDasharray",
|
|
1977
|
+
strokeDashoffset: "strokeDashoffset",
|
|
1978
|
+
fillOpacity: "fillOpacity",
|
|
1979
|
+
strokeOpacity: "strokeOpacity"
|
|
1980
|
+
};
|
|
1981
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
1982
|
+
class DOMAdapter {
|
|
1983
|
+
targets = /* @__PURE__ */ new Map();
|
|
1984
|
+
/**
|
|
1985
|
+
* Register an HTML element as an animation target.
|
|
1986
|
+
*/
|
|
1987
|
+
registerTarget(id, element) {
|
|
1988
|
+
this.targets.set(id, element);
|
|
1989
|
+
}
|
|
1990
|
+
/**
|
|
1991
|
+
* Unregister a target by its ID.
|
|
1992
|
+
*/
|
|
1993
|
+
unregisterTarget(id) {
|
|
1994
|
+
this.targets.delete(id);
|
|
1995
|
+
}
|
|
1996
|
+
/**
|
|
1997
|
+
* Get a registered target element.
|
|
1998
|
+
*/
|
|
1999
|
+
getTarget(id) {
|
|
2000
|
+
return this.targets.get(id);
|
|
2001
|
+
}
|
|
2002
|
+
/**
|
|
2003
|
+
* Clear all registered targets.
|
|
2004
|
+
*/
|
|
2005
|
+
clearTargets() {
|
|
2006
|
+
this.targets.clear();
|
|
2007
|
+
}
|
|
2008
|
+
/**
|
|
2009
|
+
* Apply animation state to all registered targets.
|
|
2010
|
+
*/
|
|
2011
|
+
applyState(state) {
|
|
2012
|
+
for (const [targetId, properties] of state.values) {
|
|
2013
|
+
const element = this.targets.get(targetId);
|
|
2014
|
+
if (!element) continue;
|
|
2015
|
+
this.applyProperties(element, properties);
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Apply properties to a single element.
|
|
2020
|
+
*/
|
|
2021
|
+
applyProperties(element, properties) {
|
|
2022
|
+
const transformParts = [];
|
|
2023
|
+
let origin = null;
|
|
2024
|
+
let clip = null;
|
|
2025
|
+
let filter = null;
|
|
2026
|
+
const hasMotionPathX = properties.has("motionPathX");
|
|
2027
|
+
const hasMotionPathY = properties.has("motionPathY");
|
|
2028
|
+
const hasMotionPathRotate = properties.has("motionPathRotate");
|
|
2029
|
+
for (const [property, value] of properties) {
|
|
2030
|
+
if (property === "x" && hasMotionPathX) continue;
|
|
2031
|
+
if (property === "y" && hasMotionPathY) continue;
|
|
2032
|
+
if ((property === "rotate" || property === "rotateZ") && hasMotionPathRotate) continue;
|
|
2033
|
+
if (TRANSFORM_PROPERTIES.has(property)) {
|
|
2034
|
+
const transformValue = this.buildTransformPart(property, value);
|
|
2035
|
+
if (transformValue) {
|
|
2036
|
+
transformParts.push(transformValue);
|
|
2037
|
+
}
|
|
2038
|
+
} else if (ORIGIN_PROPERTIES.has(property)) {
|
|
2039
|
+
if (typeof value === "number") (origin ??= {})[property] = value;
|
|
2040
|
+
} else if (CLIP_PROPERTIES.has(property)) {
|
|
2041
|
+
if (typeof value === "number") (clip ??= {})[property] = value;
|
|
2042
|
+
} else if (FILTER_PROPERTIES.has(property)) {
|
|
2043
|
+
(filter ??= {})[property] = value;
|
|
2044
|
+
} else if (property === "perspective") ;
|
|
2045
|
+
else if (property === "shine") ;
|
|
2046
|
+
else if (property === "text" && typeof value === "string") {
|
|
2047
|
+
setTextContent(element, value);
|
|
2048
|
+
} else if (property === "d" && typeof value === "string") {
|
|
2049
|
+
const el = element;
|
|
2050
|
+
const path = el.tagName?.toLowerCase() === "path" ? el : el.querySelector?.("path");
|
|
2051
|
+
path?.setAttribute?.("d", value);
|
|
2052
|
+
} else {
|
|
2053
|
+
this.applyStyleProperty(element, property, value);
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
const shine = properties.get("shine");
|
|
2057
|
+
if (typeof shine === "number") {
|
|
2058
|
+
this.applyShine(element, shine);
|
|
2059
|
+
}
|
|
2060
|
+
const perspective = properties.get("perspective");
|
|
2061
|
+
if (typeof perspective === "number") {
|
|
2062
|
+
transformParts.unshift(`perspective(${perspective}px)`);
|
|
2063
|
+
}
|
|
2064
|
+
if (transformParts.length > 0) {
|
|
2065
|
+
element.style.transform = transformParts.join(" ");
|
|
2066
|
+
ensureSvgTransformBox(element);
|
|
2067
|
+
}
|
|
2068
|
+
if (origin) {
|
|
2069
|
+
const ox = origin.originX ?? 50;
|
|
2070
|
+
const oy = origin.originY ?? 50;
|
|
2071
|
+
element.style.transformOrigin = `${ox}% ${oy}%`;
|
|
2072
|
+
}
|
|
2073
|
+
if (clip) {
|
|
2074
|
+
const t = clip.clipTop ?? 0;
|
|
2075
|
+
const r = clip.clipRight ?? 0;
|
|
2076
|
+
const b = clip.clipBottom ?? 0;
|
|
2077
|
+
const l = clip.clipLeft ?? 0;
|
|
2078
|
+
element.style.clipPath = `inset(${t}% ${r}% ${b}% ${l}%)`;
|
|
2079
|
+
}
|
|
2080
|
+
if (filter) {
|
|
2081
|
+
const composed = composeFilter(filter);
|
|
2082
|
+
if (composed) element.style.filter = composed;
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
/**
|
|
2086
|
+
* Build a transform function string for a property.
|
|
2087
|
+
*/
|
|
2088
|
+
buildTransformPart(property, value) {
|
|
2089
|
+
if (typeof value !== "number") return null;
|
|
2090
|
+
switch (property) {
|
|
2091
|
+
case "x":
|
|
2092
|
+
case "motionPathX":
|
|
2093
|
+
return `translateX(${value}px)`;
|
|
2094
|
+
case "y":
|
|
2095
|
+
case "motionPathY":
|
|
2096
|
+
return `translateY(${value}px)`;
|
|
2097
|
+
case "z":
|
|
2098
|
+
return `translateZ(${value}px)`;
|
|
2099
|
+
case "rotate":
|
|
2100
|
+
case "rotateZ":
|
|
2101
|
+
case "motionPathRotate":
|
|
2102
|
+
return `rotate(${value}deg)`;
|
|
2103
|
+
case "rotateX":
|
|
2104
|
+
return `rotateX(${value}deg)`;
|
|
2105
|
+
case "rotateY":
|
|
2106
|
+
return `rotateY(${value}deg)`;
|
|
2107
|
+
case "scale":
|
|
2108
|
+
return `scale(${value})`;
|
|
2109
|
+
case "scaleX":
|
|
2110
|
+
return `scaleX(${value})`;
|
|
2111
|
+
case "scaleY":
|
|
2112
|
+
return `scaleY(${value})`;
|
|
2113
|
+
case "scaleZ":
|
|
2114
|
+
return `scaleZ(${value})`;
|
|
2115
|
+
case "skewX":
|
|
2116
|
+
return `skewX(${value}deg)`;
|
|
2117
|
+
case "skewY":
|
|
2118
|
+
return `skewY(${value}deg)`;
|
|
2119
|
+
default:
|
|
2120
|
+
return null;
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
/**
|
|
2124
|
+
* Apply a shine sweep to a (text) element.
|
|
2125
|
+
*
|
|
2126
|
+
* `progress` runs 0..1 as the highlight travels from just off the left edge to
|
|
2127
|
+
* just off the right. Two layered, text-clipped gradients are used: a moving
|
|
2128
|
+
* white highlight band on top of a solid layer of the element's base colour,
|
|
2129
|
+
* so the text stays visible while the sheen passes over the glyphs.
|
|
2130
|
+
*/
|
|
2131
|
+
applyShine(element, progress) {
|
|
2132
|
+
if (!element.dataset.shineBase) {
|
|
2133
|
+
element.dataset.shineBase = element.style.color || "currentColor";
|
|
2134
|
+
}
|
|
2135
|
+
const base = element.dataset.shineBase;
|
|
2136
|
+
const position = -20 + progress * 140;
|
|
2137
|
+
const style = element.style;
|
|
2138
|
+
style.color = "transparent";
|
|
2139
|
+
style.backgroundImage = `linear-gradient(105deg, transparent 40%, rgba(255, 255, 255, 0.9) 50%, transparent 60%), linear-gradient(${base}, ${base})`;
|
|
2140
|
+
style.backgroundSize = "250% 100%, 100% 100%";
|
|
2141
|
+
style.backgroundPosition = `${position}% 0, 0 0`;
|
|
2142
|
+
style.backgroundRepeat = "no-repeat";
|
|
2143
|
+
style.webkitBackgroundClip = "text";
|
|
2144
|
+
style.backgroundClip = "text";
|
|
2145
|
+
}
|
|
2146
|
+
/**
|
|
2147
|
+
* Apply a single style property to an element.
|
|
2148
|
+
*/
|
|
2149
|
+
applyStyleProperty(element, property, value) {
|
|
2150
|
+
let cssProperty;
|
|
2151
|
+
const svg = element.namespaceURI === SVG_NAMESPACE;
|
|
2152
|
+
if (svg && property in SVG_PROPERTY_MAP) {
|
|
2153
|
+
const svgValue = Array.isArray(value) ? value.join(", ") : String(value);
|
|
2154
|
+
element.style[SVG_PROPERTY_MAP[property]] = svgValue;
|
|
2155
|
+
return;
|
|
2156
|
+
} else if (property === "fill" && element.dataset?.elementType === "text") {
|
|
2157
|
+
cssProperty = "color";
|
|
2158
|
+
} else {
|
|
2159
|
+
cssProperty = PROPERTY_MAP[property] ?? property;
|
|
2160
|
+
}
|
|
2161
|
+
let cssValue;
|
|
2162
|
+
if (typeof value === "number") {
|
|
2163
|
+
if (PX_PROPERTIES.has(property) || PX_PROPERTIES.has(cssProperty)) {
|
|
2164
|
+
cssValue = `${value}px`;
|
|
2165
|
+
} else {
|
|
2166
|
+
cssValue = String(value);
|
|
2167
|
+
}
|
|
2168
|
+
} else if (Array.isArray(value)) {
|
|
2169
|
+
cssValue = value.join(", ");
|
|
2170
|
+
} else {
|
|
2171
|
+
cssValue = value;
|
|
2172
|
+
}
|
|
2173
|
+
element.style[cssProperty] = cssValue;
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
function targetNamesIn(markup) {
|
|
2177
|
+
const names = /* @__PURE__ */ new Set();
|
|
2178
|
+
for (const match of markup.matchAll(/data-tinyfly\s*=\s*("([^"]*)"|'([^']*)')/g)) names.add(match[2] ?? match[3]);
|
|
2179
|
+
return [...names];
|
|
2180
|
+
}
|
|
2181
|
+
function validateEmbed(definition, options = {}) {
|
|
2182
|
+
const problems = [];
|
|
2183
|
+
const error = (message) => problems.push({ level: "error", message });
|
|
2184
|
+
const warning = (message) => problems.push({ level: "warning", message });
|
|
2185
|
+
if ((definition.formatVersion ?? 1) > FORMAT_VERSION) {
|
|
2186
|
+
error(`format version ${definition.formatVersion} is newer than this tinyfly reads (${FORMAT_VERSION})`);
|
|
2187
|
+
return problems;
|
|
2188
|
+
}
|
|
2189
|
+
let duration = 0;
|
|
2190
|
+
try {
|
|
2191
|
+
duration = deserializeTimeline(definition).duration;
|
|
2192
|
+
} catch (thrown) {
|
|
2193
|
+
error(`the timeline does not load: ${thrown instanceof Error ? thrown.message : String(thrown)}`);
|
|
2194
|
+
return problems;
|
|
2195
|
+
}
|
|
2196
|
+
const explicit = definition.config?.duration;
|
|
2197
|
+
if (explicit !== void 0) {
|
|
2198
|
+
for (const track of definition.tracks) {
|
|
2199
|
+
if (!("keyframes" in track)) continue;
|
|
2200
|
+
const late = track.keyframes.filter((keyframe) => keyframe.time > explicit);
|
|
2201
|
+
if (late.length > 0) error(`track "${track.id}" has a keyframe at ${late[0].time}ms, after the ${explicit}ms duration`);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
const markers = definition.config?.markers ?? [];
|
|
2205
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2206
|
+
markers.forEach((marker, index) => {
|
|
2207
|
+
if (ids.has(marker.id)) error(`marker id "${marker.id}" is used more than once`);
|
|
2208
|
+
ids.add(marker.id);
|
|
2209
|
+
if (marker.time < 0 || marker.time > duration) error(`marker "${marker.id}" at ${marker.time}ms is outside the animation (0–${duration}ms)`);
|
|
2210
|
+
if (index > 0 && marker.time < markers[index - 1].time) error(`marker "${marker.id}" comes before "${markers[index - 1].id}" in time; list markers in order`);
|
|
2211
|
+
});
|
|
2212
|
+
for (const [language, captions] of Object.entries(definition.captions ?? {})) {
|
|
2213
|
+
for (const id of Object.keys(captions)) {
|
|
2214
|
+
if (!ids.has(id)) error(`caption "${id}" (${language}) has no marker with that id`);
|
|
2215
|
+
}
|
|
2216
|
+
for (const id of ids) {
|
|
2217
|
+
if (!(id in captions)) warning(`marker "${id}" has no ${language} caption; its label is shown instead`);
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
if (options.markup !== void 0) {
|
|
2221
|
+
const names = new Set(targetNamesIn(options.markup));
|
|
2222
|
+
const used = /* @__PURE__ */ new Set();
|
|
2223
|
+
for (const track of definition.tracks) {
|
|
2224
|
+
for (const target of [track.target, ...track.targets ?? []]) {
|
|
2225
|
+
used.add(target);
|
|
2226
|
+
if (!names.has(target)) error(`track "${track.id}" targets "${target}", but no element has data-tinyfly="${target}"`);
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
for (const name of names) if (!used.has(name)) warning(`data-tinyfly="${name}" is never animated`);
|
|
2230
|
+
}
|
|
2231
|
+
return problems;
|
|
2232
|
+
}
|
|
2233
|
+
function renderFrame(markup, definition, at = "end") {
|
|
2234
|
+
const timeline = deserializeTimeline(definition);
|
|
2235
|
+
const markers = definition.config?.markers ?? [];
|
|
2236
|
+
const time = at === "start" ? 0 : at === "end" ? timeline.duration : typeof at === "number" ? at : markers.find((marker) => marker.id === at)?.time ?? timeline.duration;
|
|
2237
|
+
const state = timeline.getStateAtTime(Math.max(0, Math.min(timeline.duration, time)));
|
|
2238
|
+
const adapter = new DOMAdapter();
|
|
2239
|
+
const recorders = /* @__PURE__ */ new Map();
|
|
2240
|
+
const inSvg = svgRanges(markup);
|
|
2241
|
+
for (const match of markup.matchAll(START_TAG)) {
|
|
2242
|
+
const name = /data-tinyfly\s*=\s*("([^"]*)"|'([^']*)')/.exec(match[2] ?? "");
|
|
2243
|
+
if (!name) continue;
|
|
2244
|
+
const target = name[2] ?? name[3];
|
|
2245
|
+
if (recorders.has(target)) continue;
|
|
2246
|
+
const standIn = createStandIn(inSvg(match.index ?? 0));
|
|
2247
|
+
recorders.set(target, standIn);
|
|
2248
|
+
adapter.registerTarget(target, standIn);
|
|
2249
|
+
}
|
|
2250
|
+
adapter.applyState(state);
|
|
2251
|
+
let output = "";
|
|
2252
|
+
let last = 0;
|
|
2253
|
+
for (const match of markup.matchAll(START_TAG)) {
|
|
2254
|
+
const [tag, tagName, attributes = "", selfClosing] = match;
|
|
2255
|
+
const start = match.index ?? 0;
|
|
2256
|
+
const name = /data-tinyfly\s*=\s*("([^"]*)"|'([^']*)')/.exec(attributes);
|
|
2257
|
+
const standIn = name ? recorders.get(name[2] ?? name[3]) : void 0;
|
|
2258
|
+
if (!standIn || Object.keys(standIn.style).length === 0 && standIn.text === void 0) continue;
|
|
2259
|
+
output += markup.slice(last, start);
|
|
2260
|
+
const css = Object.entries(standIn.style).map(([property, value]) => `${kebab(property)}: ${value}`).join("; ");
|
|
2261
|
+
const existing = /\sstyle\s*=\s*("([^"]*)"|'([^']*)')/.exec(attributes);
|
|
2262
|
+
let newAttributes = attributes;
|
|
2263
|
+
if (css) {
|
|
2264
|
+
if (existing) {
|
|
2265
|
+
const merged = `${(existing[2] ?? existing[3]).trim().replace(/;?$/, ";")} ${css}`.trim();
|
|
2266
|
+
newAttributes = attributes.replace(existing[0], ` style="${escapeAttribute(merged)}"`);
|
|
2267
|
+
} else {
|
|
2268
|
+
newAttributes = `${attributes} style="${escapeAttribute(css)}"`;
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
output += `<${tagName}${newAttributes}${selfClosing ? " /" : ""}>`;
|
|
2272
|
+
last = start + tag.length;
|
|
2273
|
+
if (standIn.text !== void 0 && !selfClosing) {
|
|
2274
|
+
const close = markup.indexOf(`</${tagName}`, last);
|
|
2275
|
+
const inner = close === -1 ? "" : markup.slice(last, close);
|
|
2276
|
+
if (close !== -1 && !inner.includes("<")) {
|
|
2277
|
+
output += escapeText(standIn.text);
|
|
2278
|
+
last = close;
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
return output + markup.slice(last);
|
|
2283
|
+
}
|
|
2284
|
+
const START_TAG = /<([a-zA-Z][\w:-]*)((?:\s+[^\s=>/]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?)*)\s*(\/?)>/g;
|
|
2285
|
+
function createStandIn(svg) {
|
|
2286
|
+
const standIn = {
|
|
2287
|
+
style: {},
|
|
2288
|
+
dataset: {},
|
|
2289
|
+
namespaceURI: svg ? "http://www.w3.org/2000/svg" : "http://www.w3.org/1999/xhtml",
|
|
2290
|
+
childNodes: [],
|
|
2291
|
+
get textContent() {
|
|
2292
|
+
return standIn.text ?? "";
|
|
2293
|
+
},
|
|
2294
|
+
set textContent(value) {
|
|
2295
|
+
standIn.text = value;
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2298
|
+
if (svg) standIn.ownerSVGElement = null;
|
|
2299
|
+
return standIn;
|
|
2300
|
+
}
|
|
2301
|
+
function svgRanges(markup) {
|
|
2302
|
+
const ranges = [];
|
|
2303
|
+
const opening = /<svg[\s>]/gi;
|
|
2304
|
+
let match;
|
|
2305
|
+
while (match = opening.exec(markup)) {
|
|
2306
|
+
const close = markup.toLowerCase().indexOf("</svg>", match.index);
|
|
2307
|
+
ranges.push([match.index + 1, close === -1 ? markup.length : close]);
|
|
2308
|
+
}
|
|
2309
|
+
return (index) => ranges.some(([from, to]) => index > from && index < to);
|
|
2310
|
+
}
|
|
2311
|
+
function kebab(property) {
|
|
2312
|
+
const dashed = property.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
2313
|
+
return /^(webkit|moz|ms)-/.test(dashed) ? `-${dashed}` : dashed;
|
|
2314
|
+
}
|
|
2315
|
+
function escapeAttribute(value) {
|
|
2316
|
+
return value.replace(/&/g, "&").replace(/"/g, """);
|
|
2317
|
+
}
|
|
2318
|
+
function escapeText(value) {
|
|
2319
|
+
return value.replace(/&/g, "&").replace(/</g, "<");
|
|
2320
|
+
}
|
|
2321
|
+
export {
|
|
2322
|
+
renderFrame,
|
|
2323
|
+
targetNamesIn,
|
|
2324
|
+
validateEmbed
|
|
2325
|
+
};
|