@bevelo/motion 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/index.cjs +6364 -0
- package/dist/index.d.cts +1832 -0
- package/dist/index.d.ts +1832 -0
- package/dist/index.js +6278 -0
- package/dist/live/flip.cjs +78 -0
- package/dist/live/flip.d.cts +21 -0
- package/dist/live/flip.d.ts +21 -0
- package/dist/live/flip.js +77 -0
- package/dist/live/presence.cjs +7 -0
- package/dist/live/presence.d.cts +2 -0
- package/dist/live/presence.d.ts +2 -0
- package/dist/live/presence.js +1 -0
- package/dist/live/spring-value.cjs +4 -0
- package/dist/live/spring-value.d.cts +41 -0
- package/dist/live/spring-value.d.ts +41 -0
- package/dist/live/spring-value.js +1 -0
- package/dist/presence-BadXSYGX.cjs +499 -0
- package/dist/presence-CbHtzolg.d.cts +126 -0
- package/dist/presence-CbHtzolg.d.ts +126 -0
- package/dist/presence-Dt789xG9.js +470 -0
- package/dist/spring-CfdnEGs4.d.cts +118 -0
- package/dist/spring-CfdnEGs4.d.ts +118 -0
- package/dist/spring-value-D7dvds3M.cjs +373 -0
- package/dist/spring-value-fKpX3pAG.js +338 -0
- package/dist/timeline.cjs +53 -0
- package/dist/timeline.d.cts +61 -0
- package/dist/timeline.d.ts +61 -0
- package/dist/timeline.js +52 -0
- package/package.json +63 -3
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { environmentOf, frameLoopOf } from "@bevelo/runtime";
|
|
2
|
+
// src/spring.ts
|
|
3
|
+
/**
|
|
4
|
+
* Defaults chosen to feel like a good UI spring: ~0.6s to settle, almost no
|
|
5
|
+
* overshoot, no perceptible acceleration delay. They are a single declaration so
|
|
6
|
+
* the runtime value and the type cannot drift apart.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_SPRING = {
|
|
9
|
+
stiffness: 170,
|
|
10
|
+
damping: 26,
|
|
11
|
+
mass: 1,
|
|
12
|
+
from: 0,
|
|
13
|
+
to: 1,
|
|
14
|
+
velocity: 0
|
|
15
|
+
};
|
|
16
|
+
/** Default tolerance for {@link SpringSolution.duration}: a tenth of a pixel at 100dpi. */
|
|
17
|
+
const DEFAULT_EPSILON = .001;
|
|
18
|
+
/** Bisection depth used to refine the first settling time inside a sampled step. */
|
|
19
|
+
const BISECTION_ITERATIONS = 60;
|
|
20
|
+
function requireFinite(name, value) {
|
|
21
|
+
if (!Number.isFinite(value)) throw new RangeError(`spring ${name} must be finite, received ${value}`);
|
|
22
|
+
}
|
|
23
|
+
function requirePositive(name, value) {
|
|
24
|
+
requireFinite(name, value);
|
|
25
|
+
if (value <= 0) throw new RangeError(`spring ${name} must be greater than 0, received ${value}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Solves a spring once, in closed form, for the underdamped, critically damped
|
|
29
|
+
* and overdamped cases, and returns a sampler for the result.
|
|
30
|
+
*
|
|
31
|
+
* Throws a RangeError for non-finite values or a non-positive stiffness, mass or
|
|
32
|
+
* damping: those configs have no finite settling time, and a silent NaN would
|
|
33
|
+
* surface as an element frozen somewhere between two values.
|
|
34
|
+
*/
|
|
35
|
+
function solveSpring(config = {}) {
|
|
36
|
+
const { stiffness = DEFAULT_SPRING.stiffness, damping = DEFAULT_SPRING.damping, mass = DEFAULT_SPRING.mass, from = DEFAULT_SPRING.from, to = DEFAULT_SPRING.to, velocity = DEFAULT_SPRING.velocity } = config;
|
|
37
|
+
requirePositive("stiffness", stiffness);
|
|
38
|
+
requirePositive("mass", mass);
|
|
39
|
+
requirePositive("damping", damping);
|
|
40
|
+
requireFinite("from", from);
|
|
41
|
+
requireFinite("to", to);
|
|
42
|
+
requireFinite("velocity", velocity);
|
|
43
|
+
const displacement = from - to;
|
|
44
|
+
const omega = Math.sqrt(stiffness / mass);
|
|
45
|
+
const discriminant = damping * damping - 4 * mass * stiffness;
|
|
46
|
+
const criticalScale = Math.max(1, damping * damping, 4 * mass * stiffness);
|
|
47
|
+
const isCritical = Math.abs(discriminant) <= 1e-12 * criticalScale;
|
|
48
|
+
let at;
|
|
49
|
+
let velocityAt;
|
|
50
|
+
let decay;
|
|
51
|
+
let omegaD = 0;
|
|
52
|
+
if (isCritical) {
|
|
53
|
+
const rate = omega;
|
|
54
|
+
const linear = velocity + rate * displacement;
|
|
55
|
+
const vBase = -rate * displacement + linear;
|
|
56
|
+
const vSlope = rate * linear;
|
|
57
|
+
at = (seconds) => seconds <= 0 ? from : to + (displacement + linear * seconds) * Math.exp(-rate * seconds);
|
|
58
|
+
velocityAt = (seconds) => seconds <= 0 ? velocity : Math.exp(-rate * seconds) * (vBase - vSlope * seconds);
|
|
59
|
+
decay = rate;
|
|
60
|
+
} else if (discriminant < 0) {
|
|
61
|
+
const alpha = damping / (2 * mass);
|
|
62
|
+
const damped = omega * Math.sqrt(1 - (damping / (2 * Math.sqrt(stiffness * mass))) ** 2);
|
|
63
|
+
const sine = (velocity + alpha * displacement) / damped;
|
|
64
|
+
const vCos = -alpha * displacement + damped * sine;
|
|
65
|
+
const vSin = -alpha * sine - damped * displacement;
|
|
66
|
+
omegaD = damped;
|
|
67
|
+
at = (seconds) => seconds <= 0 ? from : to + Math.exp(-alpha * seconds) * (displacement * Math.cos(damped * seconds) + sine * Math.sin(damped * seconds));
|
|
68
|
+
velocityAt = (seconds) => {
|
|
69
|
+
if (seconds <= 0) return velocity;
|
|
70
|
+
const angle = damped * seconds;
|
|
71
|
+
return Math.exp(-alpha * seconds) * (vCos * Math.cos(angle) + vSin * Math.sin(angle));
|
|
72
|
+
};
|
|
73
|
+
decay = alpha;
|
|
74
|
+
} else {
|
|
75
|
+
const root = Math.sqrt(discriminant);
|
|
76
|
+
const slow = (-damping + root) / (2 * mass);
|
|
77
|
+
const fast = (-damping - root) / (2 * mass);
|
|
78
|
+
const slowCoeff = (velocity - fast * displacement) / (slow - fast);
|
|
79
|
+
const fastCoeff = displacement - slowCoeff;
|
|
80
|
+
const slowVel = slowCoeff * slow;
|
|
81
|
+
const fastVel = fastCoeff * fast;
|
|
82
|
+
at = (seconds) => seconds <= 0 ? from : to + slowCoeff * Math.exp(slow * seconds) + fastCoeff * Math.exp(fast * seconds);
|
|
83
|
+
velocityAt = (seconds) => seconds <= 0 ? velocity : slowVel * Math.exp(slow * seconds) + fastVel * Math.exp(fast * seconds);
|
|
84
|
+
decay = -slow;
|
|
85
|
+
}
|
|
86
|
+
const pointSettled = (s, epsilon) => Math.abs(at(s) - to) <= epsilon && Math.abs(velocityAt(s)) <= epsilon;
|
|
87
|
+
const lookahead = omegaD > 0 ? Math.PI / (2 * omegaD) : 0;
|
|
88
|
+
const settled = (s, epsilon) => pointSettled(s, epsilon) && (lookahead <= 0 || pointSettled(s + lookahead, epsilon));
|
|
89
|
+
let defaultDuration = -1;
|
|
90
|
+
let durationCache;
|
|
91
|
+
const duration = (epsilon = DEFAULT_EPSILON) => {
|
|
92
|
+
if (!Number.isFinite(epsilon) || !(epsilon > 0)) throw new RangeError(`spring duration epsilon must be a finite number > 0, received ${epsilon}`);
|
|
93
|
+
if (epsilon === DEFAULT_EPSILON) {
|
|
94
|
+
if (defaultDuration >= 0) return defaultDuration;
|
|
95
|
+
} else if (durationCache !== void 0) {
|
|
96
|
+
const cached = durationCache.get(epsilon);
|
|
97
|
+
if (cached !== void 0) return cached;
|
|
98
|
+
}
|
|
99
|
+
let result = 0;
|
|
100
|
+
if (!settled(0, epsilon)) {
|
|
101
|
+
const amplitude = Math.max(1, Math.abs(displacement), Math.abs(velocity) / decay);
|
|
102
|
+
let horizon = Math.log(amplitude / epsilon) / decay + 1;
|
|
103
|
+
let guard = 0;
|
|
104
|
+
while (!settled(horizon, epsilon) && guard < 64) {
|
|
105
|
+
horizon *= 2;
|
|
106
|
+
guard += 1;
|
|
107
|
+
}
|
|
108
|
+
const samples = 1024;
|
|
109
|
+
const step = horizon / samples;
|
|
110
|
+
let previous = 0;
|
|
111
|
+
result = horizon;
|
|
112
|
+
for (let index = 1; index <= samples; index += 1) {
|
|
113
|
+
const time = index * step;
|
|
114
|
+
if (settled(time, epsilon)) {
|
|
115
|
+
let low = previous;
|
|
116
|
+
let high = time;
|
|
117
|
+
for (let iteration = 0; iteration < BISECTION_ITERATIONS; iteration += 1) {
|
|
118
|
+
const mid = (low + high) / 2;
|
|
119
|
+
if (settled(mid, epsilon)) high = mid;
|
|
120
|
+
else low = mid;
|
|
121
|
+
}
|
|
122
|
+
result = high;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
previous = time;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (epsilon === DEFAULT_EPSILON) defaultDuration = result;
|
|
129
|
+
else (durationCache ??= /* @__PURE__ */ new Map()).set(epsilon, result);
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
at,
|
|
134
|
+
velocityAt,
|
|
135
|
+
duration
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A spring as an easing over 0..1, for APIs that take an Easing (keyframes, CSS
|
|
140
|
+
* interpolation). The motion is the spring's: sampled at progress * duration().
|
|
141
|
+
*
|
|
142
|
+
* `from` and `to` in the config are ignored: an easing's output range is fixed at
|
|
143
|
+
* 0..1 by definition, and normalising to a caller's arbitrary range here would
|
|
144
|
+
* make the same config mean different things in different call sites.
|
|
145
|
+
*/
|
|
146
|
+
function springEasing(config = {}) {
|
|
147
|
+
const solution = solveSpring({
|
|
148
|
+
...config,
|
|
149
|
+
from: 0,
|
|
150
|
+
to: 1
|
|
151
|
+
});
|
|
152
|
+
const total = solution.duration();
|
|
153
|
+
return (progress) => {
|
|
154
|
+
if (!(progress > 0)) return 0;
|
|
155
|
+
if (progress >= 1) return 1;
|
|
156
|
+
return solution.at(progress * total);
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* A spring described by how long it takes and how much it overshoots —
|
|
161
|
+
* the duration-and-bounce shape a designer actually thinks in.
|
|
162
|
+
*
|
|
163
|
+
* bounce maps to a damping ratio through the underdamped overshoot curve
|
|
164
|
+
* (overshoot = e^(-pi*zeta/sqrt(1-zeta^2)), inverted); the stiffness is then
|
|
165
|
+
* solved so the spring's own settle time lands on `durationMs`. Explicit
|
|
166
|
+
* stiffness/damping fields on a transition still win over this conversion.
|
|
167
|
+
*/
|
|
168
|
+
const DURATION_SPRING_CACHE = /* @__PURE__ */ new Map();
|
|
169
|
+
const DURATION_SPRING_CACHE_MAX = 256;
|
|
170
|
+
function springFromDuration(durationMs, bounce = .25) {
|
|
171
|
+
if (!Number.isFinite(durationMs) || durationMs <= 0) throw new RangeError(`springFromDuration durationMs must be positive, received ${durationMs}`);
|
|
172
|
+
const clamped = Math.min(1, Math.max(0, bounce));
|
|
173
|
+
const key = `${durationMs}:${clamped}`;
|
|
174
|
+
const hit = DURATION_SPRING_CACHE.get(key);
|
|
175
|
+
if (hit !== void 0) return { ...hit };
|
|
176
|
+
const log = clamped <= 0 ? Number.POSITIVE_INFINITY : Math.log(clamped);
|
|
177
|
+
const zeta = clamped <= 0 ? 1 : Math.min(1, Math.max(.05, Math.abs(log) / Math.sqrt(Math.PI ** 2 + log * log)));
|
|
178
|
+
const mass = 1;
|
|
179
|
+
const targetSeconds = durationMs / 1e3;
|
|
180
|
+
let lo = 1;
|
|
181
|
+
let hi = 1e5;
|
|
182
|
+
for (let i = 0; i < 40; i += 1) {
|
|
183
|
+
const mid = Math.sqrt(lo * hi);
|
|
184
|
+
if (solveSpring({
|
|
185
|
+
stiffness: mid,
|
|
186
|
+
damping: 2 * zeta * Math.sqrt(mid * mass),
|
|
187
|
+
mass,
|
|
188
|
+
from: 0,
|
|
189
|
+
to: 1
|
|
190
|
+
}).duration() < targetSeconds) hi = mid;
|
|
191
|
+
else lo = mid;
|
|
192
|
+
}
|
|
193
|
+
const stiffness = Math.sqrt(lo * hi);
|
|
194
|
+
const solved = {
|
|
195
|
+
stiffness,
|
|
196
|
+
damping: 2 * zeta * Math.sqrt(stiffness * mass),
|
|
197
|
+
mass
|
|
198
|
+
};
|
|
199
|
+
if (DURATION_SPRING_CACHE.size >= DURATION_SPRING_CACHE_MAX) {
|
|
200
|
+
const oldest = DURATION_SPRING_CACHE.keys().next().value;
|
|
201
|
+
if (oldest !== void 0) DURATION_SPRING_CACHE.delete(oldest);
|
|
202
|
+
}
|
|
203
|
+
DURATION_SPRING_CACHE.set(key, solved);
|
|
204
|
+
return { ...solved };
|
|
205
|
+
}
|
|
206
|
+
// src/live/spring-value.ts
|
|
207
|
+
/** Samples the analytic solveSpring in the compute pass; onUpdate runs in the write pass. Retarget keeps velocity. */
|
|
208
|
+
function createSpringValue(initial, options) {
|
|
209
|
+
const frame = options.frame ?? frameLoopOf();
|
|
210
|
+
let value = initial;
|
|
211
|
+
let velocity = 0;
|
|
212
|
+
let run = null;
|
|
213
|
+
let dirty = false;
|
|
214
|
+
let jobs = null;
|
|
215
|
+
/** Frame time of the last sample while moving; a retarget starts from it so no frame is held. */
|
|
216
|
+
let lastSample = null;
|
|
217
|
+
function compute(now) {
|
|
218
|
+
if (run === null) return;
|
|
219
|
+
lastSample = now;
|
|
220
|
+
if (run.start === null) run.start = now;
|
|
221
|
+
const seconds = (now - run.start) / 1e3;
|
|
222
|
+
dirty = true;
|
|
223
|
+
if (seconds >= run.duration) {
|
|
224
|
+
value = run.target;
|
|
225
|
+
velocity = 0;
|
|
226
|
+
const finished = run;
|
|
227
|
+
run = null;
|
|
228
|
+
finished.resolve(true);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
value = run.solution.at(seconds);
|
|
232
|
+
velocity = run.solution.velocityAt(seconds);
|
|
233
|
+
}
|
|
234
|
+
function write() {
|
|
235
|
+
if (dirty) {
|
|
236
|
+
dirty = false;
|
|
237
|
+
options.onUpdate(value);
|
|
238
|
+
}
|
|
239
|
+
if (run === null) sleep();
|
|
240
|
+
}
|
|
241
|
+
/** Registers the frame jobs and holds the loop awake while there is work. */
|
|
242
|
+
function wake() {
|
|
243
|
+
if (jobs !== null) return;
|
|
244
|
+
const release = frame.keepAlive();
|
|
245
|
+
const offCompute = frame.job({
|
|
246
|
+
kind: "compute",
|
|
247
|
+
run: compute,
|
|
248
|
+
name: "spring-value"
|
|
249
|
+
});
|
|
250
|
+
const offWrite = frame.job({
|
|
251
|
+
kind: "write",
|
|
252
|
+
run: write,
|
|
253
|
+
name: "spring-value"
|
|
254
|
+
});
|
|
255
|
+
jobs = () => {
|
|
256
|
+
release();
|
|
257
|
+
offCompute();
|
|
258
|
+
offWrite();
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function sleep() {
|
|
262
|
+
jobs?.();
|
|
263
|
+
jobs = null;
|
|
264
|
+
}
|
|
265
|
+
function interrupt() {
|
|
266
|
+
const previous = run;
|
|
267
|
+
run = null;
|
|
268
|
+
previous?.resolve(false);
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
get value() {
|
|
272
|
+
return value;
|
|
273
|
+
},
|
|
274
|
+
get velocity() {
|
|
275
|
+
return velocity;
|
|
276
|
+
},
|
|
277
|
+
to(target, spring) {
|
|
278
|
+
const moving = run !== null && run.start !== null;
|
|
279
|
+
interrupt();
|
|
280
|
+
if (target === value && velocity === 0) return Promise.resolve(true);
|
|
281
|
+
const solution = solveSpring({
|
|
282
|
+
...spring ?? options.spring ?? {},
|
|
283
|
+
from: value,
|
|
284
|
+
to: target,
|
|
285
|
+
velocity
|
|
286
|
+
});
|
|
287
|
+
const epsilon = Math.max(1e-4, Math.abs(target - value) * .001);
|
|
288
|
+
return new Promise((resolve) => {
|
|
289
|
+
run = {
|
|
290
|
+
solution,
|
|
291
|
+
target,
|
|
292
|
+
duration: solution.duration(epsilon),
|
|
293
|
+
resolve,
|
|
294
|
+
start: moving ? lastSample : null
|
|
295
|
+
};
|
|
296
|
+
wake();
|
|
297
|
+
});
|
|
298
|
+
},
|
|
299
|
+
jump(next) {
|
|
300
|
+
interrupt();
|
|
301
|
+
value = next;
|
|
302
|
+
velocity = 0;
|
|
303
|
+
dirty = true;
|
|
304
|
+
wake();
|
|
305
|
+
},
|
|
306
|
+
stop() {
|
|
307
|
+
interrupt();
|
|
308
|
+
velocity = 0;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/** progress 0 -> 1 on enter, 1 -> 0 on exit, through `write`. Reduced motion: jumps. */
|
|
313
|
+
function springRunner(options) {
|
|
314
|
+
const springs = /* @__PURE__ */ new WeakMap();
|
|
315
|
+
return (el, phase, signal) => {
|
|
316
|
+
const target = phase === "entering" ? 1 : 0;
|
|
317
|
+
const doc = el.ownerDocument;
|
|
318
|
+
let spring = springs.get(el);
|
|
319
|
+
if (spring === void 0) {
|
|
320
|
+
spring = createSpringValue(1 - target, {
|
|
321
|
+
onUpdate: (progress) => options.write(el, progress),
|
|
322
|
+
spring: options.spring,
|
|
323
|
+
frame: options.frame ?? frameLoopOf(doc)
|
|
324
|
+
});
|
|
325
|
+
springs.set(el, spring);
|
|
326
|
+
}
|
|
327
|
+
if (environmentOf(doc).reducedMotion()) {
|
|
328
|
+
spring.jump(target);
|
|
329
|
+
return Promise.resolve();
|
|
330
|
+
}
|
|
331
|
+
const running = spring.to(target);
|
|
332
|
+
return new Promise((resolve) => {
|
|
333
|
+
signal.addEventListener("abort", () => resolve(), { once: true });
|
|
334
|
+
running.then(() => resolve());
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
export { springEasing as a, solveSpring as i, springRunner as n, springFromDuration as o, DEFAULT_SPRING as r, createSpringValue as t };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// src/timeline.ts
|
|
3
|
+
/**
|
|
4
|
+
* How far the timeline is zoomed in, in pixels per millisecond. The range is
|
|
5
|
+
* fixed: 1 px/ms is a full-day rail on a wide screen, and 40 px/ms is a
|
|
6
|
+
* frame-level view for motion work. Below 1 the whole duration stops looking
|
|
7
|
+
* like a time axis; above 40 a millisecond is larger than a frame.
|
|
8
|
+
*/
|
|
9
|
+
const MIN_ZOOM = 1;
|
|
10
|
+
const MAX_ZOOM = 40;
|
|
11
|
+
/** A finite value clamped into [min, max]; anything non-finite becomes min. */
|
|
12
|
+
function clamp(value, min, max) {
|
|
13
|
+
if (!Number.isFinite(value)) return min;
|
|
14
|
+
return Math.min(max, Math.max(min, value));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a timeline viewport.
|
|
18
|
+
*
|
|
19
|
+
* A duration is required because every clamp is relative to it, and a broken
|
|
20
|
+
* duration is a caller error: it throws a RangeError rather than silently
|
|
21
|
+
* producing a timeline that cannot be panned. Initial zoom and pan are clamped
|
|
22
|
+
* like later updates, so passing them is never a way to sneak past the rules.
|
|
23
|
+
*/
|
|
24
|
+
function createTimeline(options) {
|
|
25
|
+
if (!Number.isFinite(options.durationMs) || options.durationMs < 0) throw new RangeError("timeline durationMs must be a finite number >= 0, received " + String(options.durationMs));
|
|
26
|
+
let duration = options.durationMs;
|
|
27
|
+
let zoom = clamp(options.zoom ?? MIN_ZOOM, MIN_ZOOM, MAX_ZOOM);
|
|
28
|
+
let pan = clamp(options.panMs ?? 0, 0, duration);
|
|
29
|
+
return {
|
|
30
|
+
scale: () => zoom,
|
|
31
|
+
offset: () => -pan * zoom,
|
|
32
|
+
timeAt(x) {
|
|
33
|
+
if (!Number.isFinite(x)) return 0;
|
|
34
|
+
return clamp(pan + x / zoom, 0, duration);
|
|
35
|
+
},
|
|
36
|
+
xAt: (time) => (time - pan) * zoom,
|
|
37
|
+
zoomTo(factor, anchorX) {
|
|
38
|
+
const anchor = Number.isFinite(anchorX) ? anchorX : 0;
|
|
39
|
+
const time = pan + anchor / zoom;
|
|
40
|
+
zoom = clamp(factor, MIN_ZOOM, MAX_ZOOM);
|
|
41
|
+
pan = clamp(time - anchor / zoom, 0, duration);
|
|
42
|
+
},
|
|
43
|
+
panTo(ms) {
|
|
44
|
+
pan = clamp(ms, 0, duration);
|
|
45
|
+
},
|
|
46
|
+
setDuration(ms) {
|
|
47
|
+
if (!Number.isFinite(ms)) return;
|
|
48
|
+
duration = Math.max(0, ms);
|
|
49
|
+
pan = clamp(pan, 0, duration);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.createTimeline = createTimeline;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Which time the left edge of the viewport shows, in milliseconds. */
|
|
2
|
+
export interface TimelineOptions {
|
|
3
|
+
/** The total span the timeline covers, in milliseconds. Must be finite and >= 0. */
|
|
4
|
+
readonly durationMs: number;
|
|
5
|
+
/** Pixels per millisecond, clamped into [1, 40]. Defaults to 1. */
|
|
6
|
+
readonly zoom?: number;
|
|
7
|
+
/** The time at the left edge, clamped into [0, durationMs]. Defaults to 0. */
|
|
8
|
+
readonly panMs?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A mutable viewport over a duration.
|
|
12
|
+
*
|
|
13
|
+
* Setters return nothing: they are called from pointer handlers, which do not
|
|
14
|
+
* need a value and cannot do anything with a throw. Reads are the two numbers
|
|
15
|
+
* the renderer needs (`scale`, `offset`) plus the two conversions.
|
|
16
|
+
*
|
|
17
|
+
* Clamping rules, all applied at the setter:
|
|
18
|
+
* - zoom is clamped into [1, 40]; a non-finite factor is treated as the minimum.
|
|
19
|
+
* - panMs is clamped into [0, durationMs]; a non-finite pan is treated as 0.
|
|
20
|
+
* - durationMs is clamped to >= 0; a non-finite duration leaves it unchanged.
|
|
21
|
+
* The clamp is the only place time and distance are related: because the
|
|
22
|
+
* viewport width is not part of this model, pan may sit anywhere in the
|
|
23
|
+
* duration, including so close to the end that the rail shows past it.
|
|
24
|
+
*/
|
|
25
|
+
export interface Timeline {
|
|
26
|
+
/** Pixels per millisecond, always within [1, 40]. */
|
|
27
|
+
scale(): number;
|
|
28
|
+
/** The x coordinate of time 0, in pixels; negative once pan has moved right. */
|
|
29
|
+
offset(): number;
|
|
30
|
+
/**
|
|
31
|
+
* The time at a horizontal position, clamped into [0, durationMs].
|
|
32
|
+
*
|
|
33
|
+
* Clamping is what makes scrubbing feel right: a pointer dragged past either
|
|
34
|
+
* end parks the playhead at that end instead of reporting a time outside the
|
|
35
|
+
* track. A non-finite x has no time to report and returns 0, the same "start"
|
|
36
|
+
* answer `insertionFor` gives for a broken coordinate.
|
|
37
|
+
*/
|
|
38
|
+
timeAt(x: number): number;
|
|
39
|
+
/** The horizontal position of a time, in pixels. Linear and unclamped. */
|
|
40
|
+
xAt(time: number): number;
|
|
41
|
+
/**
|
|
42
|
+
* Zooms to `factor` pixels per millisecond while keeping the time under
|
|
43
|
+
* `anchorX` where it is — the "zoom around the pointer" rule. The anchor's
|
|
44
|
+
* time is captured before the zoom changes, then pan is solved from it and
|
|
45
|
+
* clamped, so zooming at either edge is still stable.
|
|
46
|
+
*/
|
|
47
|
+
zoomTo(factor: number, anchorX: number): void;
|
|
48
|
+
/** Moves the viewport so its left edge shows `ms`, clamped into the duration. */
|
|
49
|
+
panTo(ms: number): void;
|
|
50
|
+
/** Changes the duration, clamped to >= 0, and re-clamps pan into the new span. */
|
|
51
|
+
setDuration(ms: number): void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a timeline viewport.
|
|
55
|
+
*
|
|
56
|
+
* A duration is required because every clamp is relative to it, and a broken
|
|
57
|
+
* duration is a caller error: it throws a RangeError rather than silently
|
|
58
|
+
* producing a timeline that cannot be panned. Initial zoom and pan are clamped
|
|
59
|
+
* like later updates, so passing them is never a way to sneak past the rules.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createTimeline(options: TimelineOptions): Timeline;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Which time the left edge of the viewport shows, in milliseconds. */
|
|
2
|
+
export interface TimelineOptions {
|
|
3
|
+
/** The total span the timeline covers, in milliseconds. Must be finite and >= 0. */
|
|
4
|
+
readonly durationMs: number;
|
|
5
|
+
/** Pixels per millisecond, clamped into [1, 40]. Defaults to 1. */
|
|
6
|
+
readonly zoom?: number;
|
|
7
|
+
/** The time at the left edge, clamped into [0, durationMs]. Defaults to 0. */
|
|
8
|
+
readonly panMs?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A mutable viewport over a duration.
|
|
12
|
+
*
|
|
13
|
+
* Setters return nothing: they are called from pointer handlers, which do not
|
|
14
|
+
* need a value and cannot do anything with a throw. Reads are the two numbers
|
|
15
|
+
* the renderer needs (`scale`, `offset`) plus the two conversions.
|
|
16
|
+
*
|
|
17
|
+
* Clamping rules, all applied at the setter:
|
|
18
|
+
* - zoom is clamped into [1, 40]; a non-finite factor is treated as the minimum.
|
|
19
|
+
* - panMs is clamped into [0, durationMs]; a non-finite pan is treated as 0.
|
|
20
|
+
* - durationMs is clamped to >= 0; a non-finite duration leaves it unchanged.
|
|
21
|
+
* The clamp is the only place time and distance are related: because the
|
|
22
|
+
* viewport width is not part of this model, pan may sit anywhere in the
|
|
23
|
+
* duration, including so close to the end that the rail shows past it.
|
|
24
|
+
*/
|
|
25
|
+
export interface Timeline {
|
|
26
|
+
/** Pixels per millisecond, always within [1, 40]. */
|
|
27
|
+
scale(): number;
|
|
28
|
+
/** The x coordinate of time 0, in pixels; negative once pan has moved right. */
|
|
29
|
+
offset(): number;
|
|
30
|
+
/**
|
|
31
|
+
* The time at a horizontal position, clamped into [0, durationMs].
|
|
32
|
+
*
|
|
33
|
+
* Clamping is what makes scrubbing feel right: a pointer dragged past either
|
|
34
|
+
* end parks the playhead at that end instead of reporting a time outside the
|
|
35
|
+
* track. A non-finite x has no time to report and returns 0, the same "start"
|
|
36
|
+
* answer `insertionFor` gives for a broken coordinate.
|
|
37
|
+
*/
|
|
38
|
+
timeAt(x: number): number;
|
|
39
|
+
/** The horizontal position of a time, in pixels. Linear and unclamped. */
|
|
40
|
+
xAt(time: number): number;
|
|
41
|
+
/**
|
|
42
|
+
* Zooms to `factor` pixels per millisecond while keeping the time under
|
|
43
|
+
* `anchorX` where it is — the "zoom around the pointer" rule. The anchor's
|
|
44
|
+
* time is captured before the zoom changes, then pan is solved from it and
|
|
45
|
+
* clamped, so zooming at either edge is still stable.
|
|
46
|
+
*/
|
|
47
|
+
zoomTo(factor: number, anchorX: number): void;
|
|
48
|
+
/** Moves the viewport so its left edge shows `ms`, clamped into the duration. */
|
|
49
|
+
panTo(ms: number): void;
|
|
50
|
+
/** Changes the duration, clamped to >= 0, and re-clamps pan into the new span. */
|
|
51
|
+
setDuration(ms: number): void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a timeline viewport.
|
|
55
|
+
*
|
|
56
|
+
* A duration is required because every clamp is relative to it, and a broken
|
|
57
|
+
* duration is a caller error: it throws a RangeError rather than silently
|
|
58
|
+
* producing a timeline that cannot be panned. Initial zoom and pan are clamped
|
|
59
|
+
* like later updates, so passing them is never a way to sneak past the rules.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createTimeline(options: TimelineOptions): Timeline;
|
package/dist/timeline.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// src/timeline.ts
|
|
2
|
+
/**
|
|
3
|
+
* How far the timeline is zoomed in, in pixels per millisecond. The range is
|
|
4
|
+
* fixed: 1 px/ms is a full-day rail on a wide screen, and 40 px/ms is a
|
|
5
|
+
* frame-level view for motion work. Below 1 the whole duration stops looking
|
|
6
|
+
* like a time axis; above 40 a millisecond is larger than a frame.
|
|
7
|
+
*/
|
|
8
|
+
const MIN_ZOOM = 1;
|
|
9
|
+
const MAX_ZOOM = 40;
|
|
10
|
+
/** A finite value clamped into [min, max]; anything non-finite becomes min. */
|
|
11
|
+
function clamp(value, min, max) {
|
|
12
|
+
if (!Number.isFinite(value)) return min;
|
|
13
|
+
return Math.min(max, Math.max(min, value));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a timeline viewport.
|
|
17
|
+
*
|
|
18
|
+
* A duration is required because every clamp is relative to it, and a broken
|
|
19
|
+
* duration is a caller error: it throws a RangeError rather than silently
|
|
20
|
+
* producing a timeline that cannot be panned. Initial zoom and pan are clamped
|
|
21
|
+
* like later updates, so passing them is never a way to sneak past the rules.
|
|
22
|
+
*/
|
|
23
|
+
function createTimeline(options) {
|
|
24
|
+
if (!Number.isFinite(options.durationMs) || options.durationMs < 0) throw new RangeError("timeline durationMs must be a finite number >= 0, received " + String(options.durationMs));
|
|
25
|
+
let duration = options.durationMs;
|
|
26
|
+
let zoom = clamp(options.zoom ?? MIN_ZOOM, MIN_ZOOM, MAX_ZOOM);
|
|
27
|
+
let pan = clamp(options.panMs ?? 0, 0, duration);
|
|
28
|
+
return {
|
|
29
|
+
scale: () => zoom,
|
|
30
|
+
offset: () => -pan * zoom,
|
|
31
|
+
timeAt(x) {
|
|
32
|
+
if (!Number.isFinite(x)) return 0;
|
|
33
|
+
return clamp(pan + x / zoom, 0, duration);
|
|
34
|
+
},
|
|
35
|
+
xAt: (time) => (time - pan) * zoom,
|
|
36
|
+
zoomTo(factor, anchorX) {
|
|
37
|
+
const anchor = Number.isFinite(anchorX) ? anchorX : 0;
|
|
38
|
+
const time = pan + anchor / zoom;
|
|
39
|
+
zoom = clamp(factor, MIN_ZOOM, MAX_ZOOM);
|
|
40
|
+
pan = clamp(time - anchor / zoom, 0, duration);
|
|
41
|
+
},
|
|
42
|
+
panTo(ms) {
|
|
43
|
+
pan = clamp(ms, 0, duration);
|
|
44
|
+
},
|
|
45
|
+
setDuration(ms) {
|
|
46
|
+
if (!Number.isFinite(ms)) return;
|
|
47
|
+
duration = Math.max(0, ms);
|
|
48
|
+
pan = clamp(pan, 0, duration);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export { createTimeline };
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bevelo/motion",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Animation: springs, easings, keyframes, presence — with a deterministic clock.",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./flip": {
|
|
16
|
+
"types": "./dist/live/flip.d.ts",
|
|
17
|
+
"import": "./dist/live/flip.js",
|
|
18
|
+
"require": "./dist/live/flip.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./presence": {
|
|
21
|
+
"types": "./dist/live/presence.d.ts",
|
|
22
|
+
"import": "./dist/live/presence.js",
|
|
23
|
+
"require": "./dist/live/presence.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./scrub": {
|
|
26
|
+
"types": "./dist/timeline.d.ts",
|
|
27
|
+
"import": "./dist/timeline.js",
|
|
28
|
+
"require": "./dist/timeline.cjs"
|
|
29
|
+
},
|
|
30
|
+
"./spring-value": {
|
|
31
|
+
"types": "./dist/live/spring-value.d.ts",
|
|
32
|
+
"import": "./dist/live/spring-value.js",
|
|
33
|
+
"require": "./dist/live/spring-value.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./timeline": {
|
|
36
|
+
"types": "./dist/timeline.d.ts",
|
|
37
|
+
"import": "./dist/timeline.js",
|
|
38
|
+
"require": "./dist/timeline.cjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@bevelo/runtime": "0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"files": [
|
|
46
|
+
"dist"
|
|
47
|
+
],
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"homepage": "https://github.com/bevelo/bevelo#readme",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/bevelo/bevelo/issues"
|
|
52
|
+
},
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "https://github.com/bevelo/bevelo.git",
|
|
56
|
+
"directory": "packages/motion"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "vp pack",
|
|
63
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
64
|
+
"test": "vp test run"
|
|
65
|
+
}
|
|
6
66
|
}
|