@mkbabb/keyframes.js 1.2.0 → 2.1.1
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/README.md +99 -2
- package/dist/_redirects +1 -0
- package/dist/keyframes.d.ts +615 -108
- package/dist/keyframes.js +1169 -1270
- package/package.json +100 -96
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# keyframes.js 
|
|
2
2
|
|
|
3
|
-
CSS keyframe animations for anything in JavaScript. Specify your keyframes in standards-compliant CSS; animate any object, DOM element, or data structure.
|
|
3
|
+
CSS keyframe animations for anything in JavaScript. Specify your keyframes in standards-compliant CSS; animate any object, DOM element, or data structure. General-purpose interpolation primitives — smoothing, morphing, scroll-driven timelines — ship alongside.
|
|
4
4
|
|
|
5
5
|
[Try the demo here!](https://keyframes.babb.dev/)
|
|
6
6
|
|
|
@@ -51,6 +51,12 @@ Plucked directly from the [`demo/simple`](demo/simple/App.vue) Vue file.
|
|
|
51
51
|
- [Units](#units)
|
|
52
52
|
- [AnimationGroup](#animationgroup)
|
|
53
53
|
- [Presets](#presets)
|
|
54
|
+
- [Web Animations API](#web-animations-api)
|
|
55
|
+
- [Beyond CSS](#beyond-css)
|
|
56
|
+
- [NumericAnimation](#numericanimation)
|
|
57
|
+
- [SmoothProgress](#smoothprogress)
|
|
58
|
+
- [ElementMorph](#elementmorph)
|
|
59
|
+
- [Timeline](#timeline)
|
|
54
60
|
- [Build & Development](#build--development)
|
|
55
61
|
|
|
56
62
|
## Installation
|
|
@@ -68,6 +74,10 @@ src/
|
|
|
68
74
|
├── animation/ # Animation engine: classes, group, presets, interpolation, WAAPI
|
|
69
75
|
│ ├── index.ts # Animation, CSSKeyframesAnimation
|
|
70
76
|
│ ├── group.ts # AnimationGroup — multi-animation compositor
|
|
77
|
+
│ ├── numeric.ts # NumericAnimation — keyframe interpolation over plain objects
|
|
78
|
+
│ ├── smooth.ts # SmoothProgress — exponential smoothing
|
|
79
|
+
│ ├── morph.ts # ElementMorph — position/scale interpolation between elements
|
|
80
|
+
│ ├── timeline.ts # Timeline, ScrollTimeline, ManualTimeline — progress drivers
|
|
71
81
|
│ ├── animations.ts # 30+ preset animations
|
|
72
82
|
│ ├── constants.ts # Types & defaults
|
|
73
83
|
│ ├── utils.ts # Frame calc, value interpolation
|
|
@@ -96,7 +106,7 @@ demo/ # Vue 3 demo apps
|
|
|
96
106
|
├── boxes/ # Vanilla JS: matrix3d transforms
|
|
97
107
|
└── bench/ # Performance benchmarks (rAF vs CSS vs WAAPI)
|
|
98
108
|
|
|
99
|
-
test/ # Vitest (jsdom) —
|
|
109
|
+
test/ # Vitest (jsdom) — 15 suites, 261 tests
|
|
100
110
|
bench/ # Vitest benchmarks — 3 suites
|
|
101
111
|
```
|
|
102
112
|
|
|
@@ -287,6 +297,93 @@ anim.play();
|
|
|
287
297
|
|
|
288
298
|
When `useWAAPI` is `true` (default), eligible animations run on the compositor thread via `Element.animate()`. Eligibility requires: DOM targets, uniform timing function across frames, no computed units, no custom transform function, no color interpolation. Falls back to `requestAnimationFrame` silently.
|
|
289
299
|
|
|
300
|
+
## Beyond CSS
|
|
301
|
+
|
|
302
|
+
The library also ships general-purpose interpolation primitives, decoupled from CSS and the DOM. These compose into a pipeline: `timeline → progress → interpolator → values → apply`.
|
|
303
|
+
|
|
304
|
+
### `NumericAnimation`
|
|
305
|
+
|
|
306
|
+
Keyframe interpolation over plain `{key: number}` objects. Zero-allocation hot path — returns the same object reference each call. Two usage modes: stateless `.at()` queries, or managed `.play()` with rAF-driven playback.
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
// Stateless — drive from your own render loop
|
|
310
|
+
const anim = new NumericAnimation([
|
|
311
|
+
{ x: 0, y: 0, opacity: 0 },
|
|
312
|
+
{ x: 100, y: 200, opacity: 1 },
|
|
313
|
+
]);
|
|
314
|
+
anim.at(0.5); // => { x: 50, y: 100, opacity: 0.5 }
|
|
315
|
+
|
|
316
|
+
// Managed — rAF playback with per-frame callback
|
|
317
|
+
const anim = new NumericAnimation(
|
|
318
|
+
[{ angle: 0 }, { angle: Math.PI * 4 }],
|
|
319
|
+
{ duration: 2500, timingFunction: easeOutCubic },
|
|
320
|
+
);
|
|
321
|
+
await anim.play(({ angle }) => {
|
|
322
|
+
ctx.clearRect(0, 0, w, h);
|
|
323
|
+
drawDial(angle);
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Options: `duration` (ms, required for `.play()`), `timingFunction`, `positions` (explicit keyframe positions as percentages). Call `.stop()` to cancel a running playback — the play promise resolves immediately.
|
|
328
|
+
|
|
329
|
+
### `SmoothProgress`
|
|
330
|
+
|
|
331
|
+
Exponential smoothing for progress values. Frame-rate independent via `tickDt(dt)`.
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
const smooth = new SmoothProgress({ damping: 0.15 });
|
|
335
|
+
smooth.setTarget(1);
|
|
336
|
+
smooth.tick(); // asymptotically approaches 1
|
|
337
|
+
smooth.snap(); // instantly converge
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
Options: `damping` (lerp factor, default 0.1), `snapThreshold` (auto-snap distance, default 0.001), `targetEpsilon` (ignore target changes smaller than this — filters scroll jitter, default 0), `initial`, `clamp`.
|
|
341
|
+
|
|
342
|
+
### `ElementMorph`
|
|
343
|
+
|
|
344
|
+
Interpolates position and scale between two DOM elements (or rects). Produces CSS transforms. Stateless `.apply()` or managed `.play()`.
|
|
345
|
+
|
|
346
|
+
```ts
|
|
347
|
+
// Stateless
|
|
348
|
+
const morph = new ElementMorph(sourceEl, targetEl);
|
|
349
|
+
morph.apply(element, progress); // writes transform + transformOrigin
|
|
350
|
+
|
|
351
|
+
// Managed playback
|
|
352
|
+
const morph = new ElementMorph(sourceEl, targetEl, {
|
|
353
|
+
duration: 400,
|
|
354
|
+
timingFunction: easeOutCubic,
|
|
355
|
+
});
|
|
356
|
+
await morph.play(element);
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Re-measures on demand via `morph.measure(from, to)`. Call `.stop()` to cancel.
|
|
360
|
+
|
|
361
|
+
### `Timeline`
|
|
362
|
+
|
|
363
|
+
Abstract progress driver. Pipeline: `sample() → clamp → easing → boundary snap → smoothing`.
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
const timeline = new ScrollTimeline({
|
|
367
|
+
threshold: 0.35,
|
|
368
|
+
easing: easeOutCubic,
|
|
369
|
+
boundaryEpsilon: 0.005,
|
|
370
|
+
smoothing: { damping: 0.15, targetEpsilon: 0.002 },
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
function update() {
|
|
374
|
+
const p = timeline.tick();
|
|
375
|
+
requestAnimationFrame(update);
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
Options: `easing`, `smoothing` (`SmoothProgressOptions` or `false`), `boundaryEpsilon` (snap eased values within this distance of 0/1 to the boundary — prevents scroll-endpoint oscillation, default 0.005).
|
|
380
|
+
|
|
381
|
+
Subclasses:
|
|
382
|
+
- **`ScrollTimeline`** — scroll position → progress. `threshold` sets viewport fraction for full progress (default 0.35). Injectable `getScrollY`/`getViewportHeight`.
|
|
383
|
+
- **`ManualTimeline`** — externally set value → progress. Smoothing off by default.
|
|
384
|
+
|
|
385
|
+
See [`docs/scroll-morph.md`](docs/scroll-morph.md) for an architecture guide on building jitter-free scroll-driven morph animations.
|
|
386
|
+
|
|
290
387
|
## Build & Development
|
|
291
388
|
|
|
292
389
|
```sh
|
package/dist/_redirects
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/* /index.html 200
|