@link-foundation/ui-animation 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 +24 -0
- package/README.md +454 -2
- package/package.json +77 -4
- package/src/backgrounds/README.md +131 -0
- package/src/backgrounds/beams.d.ts +21 -0
- package/src/backgrounds/beams.js +140 -0
- package/src/backgrounds/common.d.ts +23 -0
- package/src/backgrounds/common.js +322 -0
- package/src/backgrounds/grid.d.ts +16 -0
- package/src/backgrounds/grid.js +122 -0
- package/src/backgrounds/nebula.d.ts +9 -0
- package/src/backgrounds/nebula.js +57 -0
- package/src/backgrounds/rain-cleanup.js +77 -0
- package/src/backgrounds/rain.d.ts +33 -0
- package/src/backgrounds/rain.js +238 -0
- package/src/backgrounds/shader.d.ts +14 -0
- package/src/backgrounds/shader.js +151 -0
- package/src/backgrounds/sprites.js +72 -0
- package/src/backgrounds/starfield.d.ts +38 -0
- package/src/backgrounds/starfield.js +168 -0
- package/src/clock.js +98 -0
- package/src/compile-waapi.js +147 -0
- package/src/diagnostics.js +51 -0
- package/src/easing.js +327 -0
- package/src/effects/decipher.js +83 -0
- package/src/effects.js +44 -0
- package/src/index.d.ts +581 -0
- package/src/index.js +74 -0
- package/src/layers.js +88 -0
- package/src/model.js +636 -0
- package/src/player.js +267 -0
- package/src/random.js +62 -0
- package/src/recorder/buffer.js +40 -0
- package/src/recorder/clock.js +2 -0
- package/src/recorder/format.js +125 -0
- package/src/recorder/input.js +53 -0
- package/src/recorder/recorder.js +183 -0
- package/src/recorder/redact.js +43 -0
- package/src/recorder/replay.js +115 -0
- package/src/recorder/state-at.js +62 -0
- package/src/recorder/store.js +92 -0
- package/src/recorder/to-playwright.js +113 -0
- package/src/recorder/verify.js +49 -0
- package/src/run.js +283 -0
- package/src/sample.js +160 -0
- package/src/timeline.js +115 -0
- package/src/timing.js +172 -0
- package/src/tokens.js +32 -0
- package/src/types.js +130 -0
- package/src/validate.js +218 -0
- package/src/values.js +331 -0
package/src/layers.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sampleLayers(layers, tMs)`: several animations on one element, combined
|
|
3
|
+
* bottom to top by their `composite`:
|
|
4
|
+
* - `replace` (the default): the layer's value replaces the value below;
|
|
5
|
+
* - `add` and `accumulate`: the layer's value is added to the value below
|
|
6
|
+
* (number by number for texts with the same skeleton); `scale` combines
|
|
7
|
+
* by multiplication, so `(composite add)` of scale 1.1 over 0.9 is 0.99.
|
|
8
|
+
* A layer with an `(envelope ...)` is weighted by it (see `sample`), so a
|
|
9
|
+
* bell-shaped glow fades in and out over whatever is below it.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { sampleTyped } from './sample.js';
|
|
13
|
+
import { outputValue } from './values.js';
|
|
14
|
+
|
|
15
|
+
const isQuantity = (part) => typeof part === 'object';
|
|
16
|
+
|
|
17
|
+
/** Combine an additive value `top` onto `under`, or `null` when they can't. */
|
|
18
|
+
function combine(property, under, top) {
|
|
19
|
+
const op = property === 'scale' ? (a, b) => a * b : (a, b) => a + b;
|
|
20
|
+
if (under.type === 'number' && top.type === 'number') {
|
|
21
|
+
return { type: 'number', value: op(under.value, top.value) };
|
|
22
|
+
}
|
|
23
|
+
if (
|
|
24
|
+
under.type === 'text' &&
|
|
25
|
+
top.type === 'text' &&
|
|
26
|
+
under.parts.length === top.parts.length
|
|
27
|
+
) {
|
|
28
|
+
const parts = [];
|
|
29
|
+
for (let i = 0; i < under.parts.length; i++) {
|
|
30
|
+
const a = under.parts[i];
|
|
31
|
+
const b = top.parts[i];
|
|
32
|
+
if (isQuantity(a) && isQuantity(b)) {
|
|
33
|
+
if (a.unit !== b.unit && a.unit !== '' && b.unit !== '') {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
parts.push({ value: op(a.value, b.value), unit: a.unit || b.unit });
|
|
37
|
+
} else if (a === b) {
|
|
38
|
+
parts.push(a);
|
|
39
|
+
} else {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { type: 'text', parts };
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A layer or a bare animation as `{ animation, start, seed, input }`. */
|
|
49
|
+
function asLayer(layer) {
|
|
50
|
+
if (layer && Object.hasOwn(layer, 'animation')) {
|
|
51
|
+
return { ...layer, start: layer.start ?? 0 };
|
|
52
|
+
}
|
|
53
|
+
return { animation: layer, start: 0 };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The combined values of animation layers at `tMs`. A layer is an animation
|
|
58
|
+
* or `{ animation, start, seed, input }` (`start`: when it starts, ms).
|
|
59
|
+
* @param {(import('./types.js').Animation | import('./types.js').Layer)[]} layers
|
|
60
|
+
* @param {number} tMs
|
|
61
|
+
* @param {import('./types.js').SampleOptions} [options]
|
|
62
|
+
* @returns {Record<string, number|string|unknown>}
|
|
63
|
+
*/
|
|
64
|
+
export function sampleLayers(layers, tMs, options = {}) {
|
|
65
|
+
const values = new Map();
|
|
66
|
+
const effects = {};
|
|
67
|
+
for (const layer of layers.map(asLayer)) {
|
|
68
|
+
const { animation } = layer;
|
|
69
|
+
const result = sampleTyped(animation, tMs - layer.start, {
|
|
70
|
+
...options,
|
|
71
|
+
seed: layer.seed ?? options.seed,
|
|
72
|
+
input: layer.input ?? options.input,
|
|
73
|
+
});
|
|
74
|
+
const additive = animation.composite && animation.composite !== 'replace';
|
|
75
|
+
for (const [property, value] of result.values) {
|
|
76
|
+
const under = values.get(property);
|
|
77
|
+
const combined =
|
|
78
|
+
additive && under ? combine(property, under, value) : null;
|
|
79
|
+
values.set(property, combined ?? value);
|
|
80
|
+
}
|
|
81
|
+
Object.assign(effects, result.effects);
|
|
82
|
+
}
|
|
83
|
+
const out = {};
|
|
84
|
+
for (const [property, value] of values) {
|
|
85
|
+
out[property] = outputValue(value);
|
|
86
|
+
}
|
|
87
|
+
return Object.assign(out, effects);
|
|
88
|
+
}
|