@mkbabb/keyframes.js 1.1.0 → 1.1.6

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 CHANGED
@@ -1,12 +1,12 @@
1
1
  # keyframes.js ![image](./assets/cube.png)
2
2
 
3
- Create keyframe animations for **anything** in JavaScript; specify your keyframes in standards-complaint CSS.
3
+ CSS keyframe animations for anything in JavaScript. Specify your keyframes in standards-compliant CSS; animate any object, DOM element, or data structure.
4
4
 
5
- [demo 🌈](https://mkbabb.github.io/keyframes.js/)
5
+ [Try the demo here!](https://keyframes.babb.dev/)
6
6
 
7
- ## Quick Start using `CSS`
7
+ ## Quick Start
8
8
 
9
- #### Create a new `CSSKeyframesAnimation` object:
9
+ Create a `CSSKeyframesAnimation`, feed it CSS `@keyframes`, add targets, play:
10
10
 
11
11
  ```ts
12
12
  const anim = new CSSKeyframesAnimation({
@@ -15,307 +15,301 @@ const anim = new CSSKeyframesAnimation({
15
15
  direction: "alternate",
16
16
  fillMode: "forwards",
17
17
  });
18
- ```
19
-
20
- #### Specify your keyframes in CSS:
21
18
 
22
- ```css
23
- @keyframes mijn-keyframes {
24
- from {
25
- transform: translateX(-100%) translateY(-100%) rotate(0turn);
26
- background-color: #C462D8;
27
- }
28
- 100% {
29
- transform: translateX(50%) translateY(75%) rotate(1turn);
30
- background-color: #E85252;
19
+ anim.fromString(`
20
+ @keyframes mijn-keyframes {
21
+ from {
22
+ transform: translateX(-100%) translateY(-100%) rotate(0turn);
23
+ background-color: #C462D8;
24
+ }
25
+ 100% {
26
+ transform: translateX(50%) translateY(75%) rotate(1turn);
27
+ background-color: #E85252;
28
+ }
31
29
  }
32
- }
33
- ```
34
-
35
- #### Add the keyframes to your animation, and add target elements to animate:
30
+ `);
36
31
 
37
- ```ts
38
- const CSSKeyframes = /* the above string */ ...
39
- anim.fromCSSKeyframes(CSSKeyframes);
40
- anim.addTargets(document.getElementById("myElement"));
41
- ```
42
-
43
- #### Play ▶️
44
-
45
- ```ts
32
+ anim.setTargets(document.getElementById("myElement"));
46
33
  anim.play();
47
34
  ```
48
35
 
49
- This will animate the above element by way of its style properties, as specified in the keyframes. This is only the default behaviour; you can get far more funky with it.
36
+ This animates the element's style properties as specified in the keyframes. The default behavior, but you can get far more inventive with it.
50
37
 
51
- The above is plucked directly from the [`demo/simple`](demo/simple/App.vue) Vue file.
38
+ Plucked directly from the [`demo/simple`](demo/simple/App.vue) Vue file.
52
39
 
53
40
  ## Table of Contents
54
41
 
55
- - [keyframes.js ](#keyframesjs-)
56
- - [Quick Start using `CSS`](#quick-start-using-css)
57
- - [Create a new `CSSKeyframesAnimation` object:](#create-a-new-csskeyframesanimation-object)
58
- - [Specify your keyframes in CSS:](#specify-your-keyframes-in-css)
59
- - [Add the keyframes to your animation, and add target elements to animate:](#add-the-keyframes-to-your-animation-and-add-target-elements-to-animate)
60
- - [Play ▶️](#play-️)
61
- - [Table of Contents](#table-of-contents)
62
- - [Installation](#installation)
63
- - [`Animation`](#animation)
64
- - [`AnimationOptions`:](#animationoptions)
65
- - [The transform function](#the-transform-function)
66
- - [The timing function](#the-timing-function)
67
- - [Step Functions](#step-functions)
68
- - [Bézier Curves](#bézier-curves)
69
- - [Graphing Bézier Curves](#graphing-bézier-curves)
70
- - [Just gimme the `t` value](#just-gimme-the-t-value)
71
- - [`TemplateAnimationFrame`](#templateanimationframe)
72
- - [Reification of a `TemplateAnimationFrame`](#reification-of-a-templateanimationframe)
73
- - [Variable Resolution](#variable-resolution)
74
- - [`CSSKeyframesAnimation`](#csskeyframesanimation)
75
- - [Parsing CSS Keyframes; `keyframes.ts`](#parsing-css-keyframes-keyframests)
76
- - [Units: `units.ts` \& `CSS Units`](#units-unitsts--css-units)
77
- - [Unit class hierarchy](#unit-class-hierarchy)
78
- - [Unit interpolation and resolution](#unit-interpolation-and-resolution)
42
+ - [Installation](#installation)
43
+ - [Project Structure](#project-structure)
44
+ - [Animation](#animation)
45
+ - [AnimationOptions](#animationoptions)
46
+ - [Transform Function](#the-transform-function)
47
+ - [Timing Function](#the-timing-function)
48
+ - [TemplateAnimationFrame](#templateanimationframe)
49
+ - [CSSKeyframesAnimation](#csskeyframesanimation)
50
+ - [Parsing CSS Keyframes](#parsing-css-keyframes)
51
+ - [Units](#units)
52
+ - [AnimationGroup](#animationgroup)
53
+ - [Presets](#presets)
54
+ - [Build & Development](#build--development)
79
55
 
80
56
  ## Installation
81
57
 
82
58
  ```bash
83
- npm install keyframes.ts
59
+ npm install @mkbabb/keyframes.js
84
60
  ```
85
61
 
86
- Which will _mostly_ work both in and out of the browser. Anything that leverages the the `DOM`, of course, won't work outside of the browser (things like `getComputedStyle`, `document`, etc.).
62
+ Works both in and out of the browser. Anything that touches the DOM (`getComputedStyle`, `document`, etc.) won't work in Node.
87
63
 
88
- ## `Animation`
64
+ ## Project Structure
89
65
 
90
- The `Animation` object is the driver behind `CSSKeyFramesAnimation` and `AnimationGroup`.
66
+ ```
67
+ src/
68
+ ├── animation/ # Animation engine: classes, group, presets, interpolation, WAAPI
69
+ │ ├── index.ts # Animation, CSSKeyframesAnimation
70
+ │ ├── group.ts # AnimationGroup — multi-animation compositor
71
+ │ ├── animations.ts # 30+ preset animations
72
+ │ ├── constants.ts # Types & defaults
73
+ │ ├── utils.ts # Frame calc, value interpolation
74
+ │ └── waapi.ts # Web Animations API delegation
75
+ ├── parsing/ # CSS @keyframes parsing & serialization
76
+ │ ├── keyframes.ts # @keyframes grammar (parse-that combinators)
77
+ │ ├── format.ts # Animation → CSS string (Prettier)
78
+ │ ├── units.ts # Re-export: CSS value/color parsers
79
+ │ └── utils.ts # Re-export: parser combinators
80
+ ├── units/ # Value types & normalization
81
+ │ ├── normalize.ts # DOM-aware unit normalization
82
+ │ ├── color/ # Color space classes, converters, normalization
83
+ │ └── *.ts # Re-export barrels from @mkbabb/value.js
84
+ ├── easing.ts # Re-export: easing functions
85
+ ├── math.ts # Re-export: clamp, lerp, bezier
86
+ └── utils.ts # Re-export + memoizeDecorator
87
+
88
+ demo/ # Vue 3 demo apps
89
+ ├── @/ # Shared: animation controls, shadcn-vue UI, styles
90
+ ├── cube/ # 3D cube + AnimationGroup + matrix editor (default demo)
91
+ ├── simple/ # Minimal single-animation example
92
+ ├── square/ # Custom transform function
93
+ ├── amiga/ # 3D animated sphere (Three.js)
94
+ ├── playground/ # Asset playground: drag-and-drop viewport with preset animation binding
95
+ ├── balls/ # Vanilla JS: CSS vars + staggered animations
96
+ ├── boxes/ # Vanilla JS: matrix3d transforms
97
+ └── bench/ # Performance benchmarks (rAF vs CSS vs WAAPI)
98
+
99
+ test/ # Vitest (jsdom) — 11 suites
100
+ bench/ # Vitest benchmarks — 3 suites
101
+ ```
91
102
 
92
- Every `Animation` is composed of (at a high level):
103
+ ## Animation
93
104
 
94
- - options: the options for the animation; `AnimationOptions`
95
- - a transform function: the function to interpolate between keyframes
96
- - a timing function: the function to ease the animation, which can also be set by the `AnimationOptions`
97
- - keyframes: the keyframes for the animation; `TemplateAnimationFrame`
105
+ The `Animation` object drives `CSSKeyframesAnimation` and `AnimationGroup`. It's composed of:
98
106
 
99
- ### [`AnimationOptions`](src/animation.ts#L139):
107
+ - **options** (`AnimationOptions`)
108
+ - **transform function**: interpolates between keyframes
109
+ - **timing function**: eases the animation
110
+ - **keyframes** (`TemplateAnimationFrame`)
100
111
 
101
- - duration: time in milliseconds of the entire animation
102
- - delay: time in milliseconds before the animation starts
103
- - iterationCount: number of times the animation should repeat
104
- - direction: direction of the animation (normal, reverse, alternate, alternate-reverse)
105
- - fillMode: how the animation should apply styles before and after it plays (none, forwards, backwards, both)
106
- - timing function: the timing function to use for easing, tweening, etc., the animation
107
- -
112
+ ### `AnimationOptions`
108
113
 
109
- ### The transform function
114
+ - `duration`: time in milliseconds (default: 1000)
115
+ - `delay`: time in milliseconds before the animation begins (default: 0)
116
+ - `iterationCount`: number of repetitions (default: 1)
117
+ - `direction`: `normal`, `reverse`, `alternate`, `alternate-reverse`
118
+ - `fillMode`: `none`, `forwards`, `backwards`, `both`
119
+ - `timingFunction`: easing function (default: `easeInOutCubic`)
120
+ - `colorSpace`: color interpolation space (default: `oklab`)
121
+ - `hueMethod`: hue interpolation method for cylindrical color spaces (optional)
122
+ - `useWAAPI`: delegate to Web Animations API when eligible (default: `true`)
110
123
 
111
- The type signature of the transform function is as follows:
124
+ ### The transform function
112
125
 
113
126
  ```ts
114
- type TransformFunction<V extends Vars> = (t: number, v: V) => void;
127
+ type TransformFunction<V extends Vars> = (v: V, t: number) => void;
115
128
  ```
116
129
 
117
- And it's called for each timestep `t` of the animation, where `t` is a number between 0 and the duration of the animation. The transform function is responsible for doing whatever you'd like to do with the variables `v` at each timestep `t`.
118
-
119
- The variables `v` are the interpolated values at time `t`, given to you in almost exactly the same form as you originally specified them in the keyframes. Deeply nested objects are supported, as are just about anything else you can think of.
130
+ Called at each timestep `t` (0 to `duration`) with the interpolated variables `v`. The variables arrive in the same shape you specified in your keyframes—deeply nested objects included.
120
131
 
121
- Every value therein is parsed as a CSS value unit, so you can specify things like `1px`, `1em`, `1%`, `1deg`, etc. The library will handle the conversion for you, though two interpolate between two different units, they must be of the same super type (e.g. `px` and `em` are both `length`s, so they can be interpolated; `px` and `deg` are not, so they cannot). See the `collapseNumericType` function within [`units.ts`](src/units.ts) for more information.
132
+ Every value is parsed as a CSS value unit (`1px`, `1em`, `1deg`, etc.). To interpolate between two units they must share a supertype: `px` and `em` are both **length**, so they interpolate; `px` and `deg` are not.
122
133
 
123
134
  ### The timing function
124
135
 
125
- The timing, or easing, tweening, etc., function is responsible for determining how the animation progresses over time. The type signature of the timing function is as follows:
126
-
127
136
  ```ts
128
137
  type TimingFunction = (t: number) => number;
129
138
  ```
130
139
 
131
- Where `t` is a number between 0 and 1, and the return value is also a number between 0 and 1. The timing function is responsible for determining how the animation progresses over time, and can be anything from a simple linear function to a complex Bezier curve.
132
-
133
- All CSS timing functions are supported, and are implemented in [`easing.ts`](src/easing.ts).
134
-
135
- #### Step Functions
140
+ Takes `t` in `[0, 1]`, returns `[0, 1]`. All CSS timing functions are implemented in [`easing.ts`](src/easing.ts).
136
141
 
137
- A special case and multi-parameter variant of a timing function, implemented as `steppedEase`, which takes (in addition to `t`) two parameters:
142
+ #### Step functions
138
143
 
139
- - the number of steps
140
- - the direction, or jump term, of the step
144
+ Implemented as `steppedEase(t, steps, jumpTerm)`:
141
145
 
142
- Valid jump terms are:
146
+ - `jump-none`: the value is held until the end of the step
147
+ - `jump-start` / `start`: step occurs at the start
148
+ - `jump-end` / `end`: step occurs at the end
149
+ - `jump-both` / `both`: steps at both boundaries
143
150
 
144
- - `jump-none`: the step occurs at the start of the step, but the value is held until the end of the step
145
- - `jump-start` | `start`: the step occurs at the start of the step
146
- - `jump-end` | `end`: the step occurs at the end of the step
147
- - `jump-both` | `both`: the step occurs at the start and end of the step
151
+ #### Bezier curves
148
152
 
149
- #### Bézier Curves
153
+ `cubicBezier(t, x1, y1, x2, y2)` implements the cubic case. The general case uses de Casteljau's algorithm iteratively.
150
154
 
151
- Bézier curves are parametric curves defined by a set of control points.
155
+ Both are in [`math.ts`](src/math.ts). For Bezier visualizations, see [this Desmos graph](https://www.desmos.com/calculator/tvivnkflzv) or the `timing-functions` demo.
152
156
 
153
- The `cubicBezier` function implements the special cubic case of the more general Bézier curve, taking in control points for `x1`, `y1`, `x2`, and `y2`, and returning the x and y coordinates of the curve at time `t`.
157
+ `CSSCubicBezier` is the higher-order convenience: takes control points, returns a `t → value` function. CSS's named easings are built from it:
154
158
 
155
159
  ```ts
156
- const [x, y] = cubicBezier(t / duration, 0.09, 0.91, 0.5, 1.5);
160
+ const easeInBounce = (t: number) => CSSCubicBezier(0.09, 0.91, 0.5, 1.5)(t);
157
161
  ```
158
162
 
159
- The general case of calculating a point along a Bézier curve at time `t`, specified at control points `x1, ..., xn`, `y1, ..., yn`, is performed using `deCasteljau`'s algorithm, implemented iteratively as simply the `deCasteljau` function.
163
+ ### `TemplateAnimationFrame`
160
164
 
161
- Both of the above, along with other math utilities, are implemented in [`math.ts`](src/math.ts).
165
+ A template keyframe prior to resolution. Composed of:
162
166
 
163
- #### Graphing Bézier Curves
167
+ - `id`: auto-incremented identifier
168
+ - `start`: start time (CSS time string, number, or percentage)
169
+ - `vars`: the variables to interpolate
170
+ - `transform`: per-keyframe transform function (optional)
171
+ - `timingFunction`: per-keyframe timing function (optional)
164
172
 
165
- If you're interested in more Bézier visualizations, check out [this](https://www.desmos.com/calculator/tvivnkflzv) Desmos graph.
173
+ Once a transform or timing function is specified, it propagates to all subsequent keyframes.
166
174
 
167
- Or use any of the demos in the [`demo`](demo) folder, click on `timing-functions` and then `bezier`.
175
+ #### Reification
168
176
 
169
- #### Just gimme the `t` value
177
+ Template keyframes are reified into concrete keyframes by:
170
178
 
171
- OK ✨
179
+ 1. **Parsing start times**: CSS time formats (`1s`, `100ms`), raw numbers, or percentages (`50%`). All normalized to a percentage of total duration.
180
+ 2. **Resolving functions**: null transforms and timing functions fall back to the `AnimationOptions` defaults.
181
+ 3. **Sorting** by start percentage.
182
+ 4. **Resolving variables** across keyframes—every keyframe ends up with the same variable set.
183
+ 5. **Computing durations** from sorted start/stop times.
172
184
 
173
- `CSSBezier` is the function you're looking for. It's a high-order function that takes in the control points of the Bezier curve and returns a function that takes in a time `t` and returns the value of the Bezier curve at that time.
185
+ #### Variable resolution
174
186
 
175
- For example, CSS's `easeInBounce` is defined as
187
+ Keyframes needn't declare the same variables. The resolver walks backward from each keyframe, seeking the most recent definition of each variable. If none exists, the default value (typically `0`) is used.
176
188
 
177
189
  ```ts
178
- function easeInBounce(t: number) {
179
- t = CSSBezier(0.09, 0.91, 0.5, 1.5)(t);
180
- return t;
181
- }
190
+ const kf1 = { start: "0s", vars: { x: 0, y: 0 } };
191
+ const kf2 = { start: "500ms", vars: { x: 1 } };
192
+ const kf3 = { start: "100%", vars: { x: 0, y: 1 } };
182
193
  ```
183
194
 
184
- ### `TemplateAnimationFrame`
195
+ `kf2` gets `y: 0` from `kf1`. This lets you specify keyframes loosely.
185
196
 
186
- A `TemplateAnimationFrame` object, or template keyframe, is a keyframe that's not yet been resolved to a concrete keyframe. It's composed of:
197
+ ## `CSSKeyframesAnimation`
187
198
 
188
- - id: the unique id of the keyframe; autoincremented number
189
- - start: the start time of the keyframe
190
- - vars: the variables of the keyframe to be interpolated
191
- - transform: the transform function of the keyframe
192
- - timingFunction: the timing function of the keyframe
199
+ An abstraction over `Animation` that parses CSS `@keyframes` into `TemplateAnimationFrame` objects, then adds them to a base `Animation`.
193
200
 
194
- Keyframes can have unique transform and timing functions, but that's not typical: usually you'll specify one transform and timing function for the entire animation (once a transform function is specified, it's used for all keyframes, similarly for the timing function; no need to list it twice).
201
+ Three ways to create keyframes:
195
202
 
196
- #### Reification of a `TemplateAnimationFrame`
203
+ ```ts
204
+ // From CSS string (underlying parser is memoized; results cloned per call)
205
+ anim.fromString(`from { opacity: 0; } to { opacity: 1; }`);
197
206
 
198
- A `TemplateAnimationFrame` is reified into a concrete keyframe by the following process:
207
+ // From keyframe map
208
+ anim.fromKeyframes({ "0%": { opacity: 0 }, "100%": { opacity: 1 } });
199
209
 
200
- - parse the start time: this can be input as a string, which can take on any valid CSS time format (e.g. `1s`, `100ms`, `1.5s`, `1.5ms`, etc.), or as a number, or as a percentage (e.g. `50%`).
201
- - All times are then normalized to a percentage of the total duration of the animation.
202
- - resolve the transform and timing functions if they're null: if they are, they're resolved to the default transform and timing functions specified in the `AnimationOptions`.
210
+ // From variable array
211
+ anim.fromVars([{ opacity: 0 }, { opacity: 1 }]);
212
+ ```
203
213
 
204
- Once all of the `TemplateAnimationFrame` objects have been added to an `Animation`, they're further parsed into a concrete keyframe by the following process:
214
+ The default transform applies interpolated values to `element.style` for each target.
205
215
 
206
- - sort the keyframes by their starting percentage
207
- - resolve the variables for each keyframe
208
- - resolve the keyframes' start and stop times
209
- - calculate the keyframes' duration
216
+ ### Parsing CSS keyframes
210
217
 
211
- ##### Variable Resolution
218
+ [`keyframes.ts`](src/parsing/keyframes.ts) covers most of the CSS spec:
212
219
 
213
- This is done so that every keyframe has the same set of variables, and so that the variables are resolved to their concrete values. Take the following example:
220
+ - `from`, `to`, and percentages
221
+ - Time units (`s`, `ms`)
222
+ - Lengths (`px`, `em`, etc.)
223
+ - Angles (`deg`, `rad`, etc.)
224
+ - Colors (`#fff`, `rgb(255, 255, 255)`, `lab(100, 0, 0)`, `lightblue`, etc.)
225
+ - Transforms (`translateX(100%)`, `rotate(1turn)`, etc.)
226
+ - Variables (`var(--my-var)`)
227
+ - Math expressions (`calc(100% - 10px)`)
228
+ - Any `key: value` pair parseable as a CSS value, function, or list thereof
214
229
 
215
- ```ts
216
- const keyframeVars1 = {
217
- x: 0,
218
- y: 0,
219
- };
220
- const keyframeVars2 = {
221
- z: 0,
222
- };
223
- const keyframeVars3 = {
224
- x: 1,
225
- y: 1,
226
- z: 1,
227
- };
228
- ```
230
+ The parser uses [`@mkbabb/parse-that`](https://github.com/mkbabb/parse-that) and [`@mkbabb/value.js`](https://github.com/mkbabb/value.js) for CSS value parsing. All exported parse functions are memoized.
231
+
232
+ ### Units
233
+
234
+ Thorough unit parsing and resolution, covering:
235
+
236
+ - **length**, **angle**, **time**, **resolution**, **percentage**, **color**
237
+
238
+ See [`units/`](src/units/) and [`parsing/units.ts`](src/parsing/units.ts).
239
+
240
+ A `unit` value takes one of three forms:
229
241
 
230
- Notice that `x` and `y` are defined in the first and third keyframes, but not in the second. We handle this by working through the keyframes backwards and seeking the most recent keyframe that has the variable defined.
242
+ - `ValueUnit`: a value with a string unit and an array of supertypes
243
+ - `FunctionValue`: a function name with an array of `ValueUnit`s
244
+ - `ValueArray`: an array of `ValueUnit`s
231
245
 
232
- If it's not defined in any previous keyframes, we set it to the default value of the variable (usually `0`).
246
+ Each defines `toString()`, `valueOf()`, and `lerp(t, other, target?)`. Any `ValueUnit` variant can interpolate with any other—values are aligned to the shorter array, then interpolated element-wise.
233
247
 
234
- All of this above nets you the ability to specify keyframes in a rather hap-hazard way (perhaps not such a good thing 😅). For example, the below is a valid set of keyframes:
248
+ Units of the same supertype interpolate freely. Supertypes also encode whether a unit is relative or absolute (`px` is absolute; `em` is relative), used to resolve to a common base before interpolation.
249
+
250
+ Interpolation dispatch: numeric values use `lerp`; colors use perceptual interpolation (`oklab` by default, configurable); computed units (`vh`, `vw`, `calc`, `var`) resolve against the live DOM at interpolation time.
251
+
252
+ ## `AnimationGroup`
253
+
254
+ Composites multiple animations with layer blending. Each animation gets a layer config controlling how it merges with others.
235
255
 
236
256
  ```ts
237
- const duration = 1000;
238
- const keyframe1 = {
239
- start: "0s",
240
- vars: {
241
- x: 0,
242
- y: 0,
243
- },
244
- };
245
-
246
- const keyframe2 = {
247
- start: "100%",
248
- vars: {
249
- x: 0,
250
- y: 1,
251
- },
252
- };
253
-
254
- const keyframe3 = {
255
- start: "500ms",
256
- vars: {
257
- x: 1,
258
- },
259
- };
257
+ const group = anim1.group(anim2, anim3);
258
+ group.setTargets(element);
259
+ group.play();
260
260
  ```
261
261
 
262
- ## `CSSKeyframesAnimation`
263
-
264
- An abstraction over the `Animation` object, the `CSSKeyframesAnimation` object is responsible for creating animations from CSS keyframes. This is done by parsing the CSS keyframes into a series of `TemplateAnimationFrame` objects, thereupon adding them to a base `Animation` object.
262
+ Three blend modes:
265
263
 
266
- ### Parsing CSS Keyframes; [`keyframes.ts`](src/parsing/keyframes.ts)
264
+ - **`replace`**: highest `zIndex` wins (default)
265
+ - **`add`**: numeric values accumulate
266
+ - **`weighted`**: linear interpolation by `weight` (0–1)
267
267
 
268
- Most of the CSS spec. is supported, including:
268
+ Layer configuration per animation: `zIndex`, `weight`, `blendMode`, `enabled`, `properties` (whitelist). Property whitelisting enables effect layering—one layer animates position, another animates opacity.
269
269
 
270
- - `from`, `to`, and percentages
271
- - time units (`s`, `ms`, etc.)
272
- - lengths (`px`, `em`, etc.)
273
- - angles (`deg`, `rad`, etc.)
274
- - colors (`#fff`, `rgb(255, 255, 255)`, `lab(100, 0, 0)`, `lightblue`, etc.)
275
- - transforms (`translateX(100%)`, `rotate(1turn)`, etc.)
276
- - variables (`var(--my-var)`)
277
- - resolved math expressions (`calc(100% - 10px)`)
278
- - Any `key: value` pair that can be parsed by the `CSS` parser, where value can be
279
- - any CSS value
280
- - any CSS function
281
- - any list of CSS values or functions
282
- - a limited subset of `JSON`-like objects, though the implemention of `JSON-CSS` is on the roadmap
270
+ The group manages its own `requestAnimationFrame` loop and marks child animations as `managed`.
283
271
 
284
- The implemented parser currently leverages the [`parsimmon`](https://github.com/jneen/parsimmon) parser combinator library 🙂‍↔
272
+ ## Presets
285
273
 
286
- ### Units: [`units.ts`](src/units.ts) & [`CSS Units`](src/parsing/units.ts)
274
+ 30+ ready-to-use animations in [`animations.ts`](src/animation/animations.ts). All return `CSSKeyframesAnimation` instances and accept optional `InputAnimationOptions`:
287
275
 
288
- A great deal of care has gone into the parsing and resolving of units within the CSS spec. Herein, we cover the following unit types:
276
+ ```ts
277
+ import { fadeIn, bounce, spinner } from "@mkbabb/keyframes.js";
289
278
 
290
- - `length`
291
- - `angle`
292
- - `time`
293
- - `resolution`
294
- - `percentage`
295
- - `color`
279
+ const anim = fadeIn({ duration: 500 });
280
+ anim.setTargets(element);
281
+ anim.play();
282
+ ```
296
283
 
297
- See the parser within the [`CSS Units file`](src/parsing/units.ts) for more information.
284
+ **Fade**: `fadeIn`, `fadeOut` · **Attention**: `pulse`, `heartbeat`, `glow`, `shake`, `bounce` · **Entrance/Exit**: `flip`, `rotateIn`, `slideIn`, `slideInLeft/Right`, `slideOutLeft/Right`, `blurIn/Out/InOut`, `jumpUp/Down`, `warpLeft/Right` · **Effects**: `hover`, `typewriter`, `typingCursor`, `rainbowText`, `progressBar`, `skeletonLoading`, `spinner`, `parallaxScroll`, `gradientBackground`, `rotateScale`, `accordionExpand`, `notificationBounce`, `textFocusBlur`
298
285
 
299
- #### Unit class hierarchy
286
+ ## Web Animations API
300
287
 
301
- A `unit` value comes in three forms, specified in the general [`units.ts`](src/units.ts) file:
288
+ 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.
302
289
 
303
- - `ValueUnit`: a value with a string unit and an array of super types
304
- - `FunctionValue`: a function with a string name and an array of `ValueUnit`s
305
- - `ValueArray`: an array of `ValueUnit`s
290
+ ## Build & Development
306
291
 
307
- Each of these has defined a set of core functions:
292
+ ```sh
293
+ npm run build # library → dist/keyframes.js + keyframes.cjs + keyframes.d.ts
294
+ npm run gh-pages # demo → dist/
295
+ npm run dev # vite dev server on :8080 (cube demo)
296
+ npm test # vitest (jsdom)
297
+ npm run bench # vitest bench
298
+ ```
308
299
 
309
- - `toString()`: returns the string representation of the unit, e.g. `1px` or `translateX(100%)`
310
- - `valueOf()`: if the value is unit-less, returns the value; otherwise, returns the string variant
311
- - `lerp(t: number,
312
- other: FunctionValue<T> | ValueArray<T> | ValueUnit<T>,
313
- target?: HTMLElement,)`: interpolates between two units
300
+ **Dependencies**: [`@mkbabb/value.js`](https://github.com/mkbabb/value.js) (ValueUnit, Color, math, parsing, normalization) and [`@mkbabb/parse-that`](https://github.com/mkbabb/parse-that) (parser combinators).
314
301
 
315
- Note that any `ValueUnit` type variant can be interpolated between another; insofar as, a `ValeUnit` can be interpolated between a `FunctionValue` or `ValueArray`, and vice versa. The values thereof are aligned to the smallest array length of the two: the interpolation is then performed on each element of the array.
302
+ **TypeScript**: `strict: true`, `verbatimModuleSyntax: true`, `target: ES2022`, `moduleResolution: bundler`.
316
303
 
317
- #### Unit interpolation and resolution
304
+ **Node**: >=22. **License**: GPL-3.0.
318
305
 
319
- Units that are of the same supertype can be interpolated between. For example, `px` and `em` are both `length`s, so they can be interpolated between. `px` and `deg` are not, so they cannot.
306
+ ## Sources, acknowledgements, &c.
320
307
 
321
- Supertypes also contain information about the realtive or absolute nature of the unit. For example, `px` is an absolute length, while `em` is a relative length. This information is used to resolve the units to a common supertype, which is then used to interpolate between the two units.
308
+ - [CSS Animations Level 1](https://www.w3.org/TR/css-animations-1/). W3C. — `@keyframes`, `animation-*` properties, timing model.
309
+ - [CSS Easing Functions Level 2](https://www.w3.org/TR/css-easing-2/). W3C. — `linear()` piecewise easing, `steps()` jump terms, `cubic-bezier()`.
310
+ - [CSS Transforms Module Level 2](https://www.w3.org/TR/css-transforms-2/). W3C. — `matrix3d()`, decomposition algorithm, quaternion interpolation.
311
+ - [CSSOM View Module](https://www.w3.org/TR/cssom-view-1/). W3C. — `getComputedStyle`, unit resolution for relative values.
312
+ - [Web Animations API](https://www.w3.org/TR/web-animations-1/). W3C. — `Element.animate()`; the WAAPI delegation path.
313
+ - [`@mkbabb/value.js`](https://github.com/mkbabb/value.js) — CSS value parsing, color spaces, unit conversion, easing functions.
314
+ - [`@mkbabb/parse-that`](https://github.com/mkbabb/parse-that) — Parser combinators for the `@keyframes` grammar.
315
+ - de Casteljau, P. (1959). *Outillages méthodes calcul*. — The recursive subdivision algorithm used for general Bezier curves.