@cesarvenzac/figures 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 +145 -2
- package/dist/figure-acts.d.ts +55 -0
- package/dist/figure-acts.js +616 -0
- package/dist/figure-curves.d.ts +30 -0
- package/dist/figure-curves.js +44 -0
- package/dist/figure-gl.d.ts +33 -0
- package/dist/figure-gl.js +875 -0
- package/dist/figure-loop.d.ts +3 -0
- package/dist/figure-loop.js +21 -0
- package/dist/figure-motion.d.ts +76 -0
- package/dist/figure-motion.js +260 -0
- package/dist/figure-peers.d.ts +36 -0
- package/dist/figure-peers.js +37 -0
- package/dist/figure-pose.d.ts +77 -0
- package/dist/figure-pose.js +108 -0
- package/dist/figure-scene.d.ts +65 -0
- package/dist/figure-scene.js +210 -0
- package/dist/figure-space.d.ts +233 -0
- package/dist/figure-space.js +151 -0
- package/dist/figure.d.ts +204 -0
- package/dist/figure.js +277 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/react.d.ts +69 -0
- package/dist/react.js +775 -0
- package/dist/rubik.d.ts +34 -0
- package/dist/rubik.js +54 -0
- package/package.json +73 -4
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
import { arc, bump, easeIn, easeInOut, easeOut, easeOutBack, hold, keys, noise, phaseOf, span, TURN, wobble } from "./figure-curves.js";
|
|
2
|
+
import { posed, REST } from "./figure-pose.js";
|
|
3
|
+
import { BODY, BOTTOM, FLOOR, HOVER, pictureY, PUFFS, TOP } from "./figure-space.js";
|
|
4
|
+
/**
|
|
5
|
+
* What each shape does its own way, as the cube solves itself like a Rubik's cube while it thinks:
|
|
6
|
+
* pure functions of time, giving a pose (figure-pose.ts) with the shape's own parts.
|
|
7
|
+
*
|
|
8
|
+
* Reactions, once, `p` from 0 to 1 (`actAt`):
|
|
9
|
+
* - Petted: heart eyes and hearts floating up; the cube twirls its top layer, the sphere dents and
|
|
10
|
+
* ripples like jelly, the pyramid leans like jelly, the block's dials rock as it purrs, the
|
|
11
|
+
* cloud's puffs bounce one after another.
|
|
12
|
+
* - A run finished: starry eyes and sparks; the cube bursts into its cubelets, twisting, and snaps
|
|
13
|
+
* back; the sphere splashes into droplets that fly out and run back together; the pyramid
|
|
14
|
+
* launches its capstone, spinning, and catches it; the block spins its dials like a slot machine,
|
|
15
|
+
* the eyes coming round last; the cloud's puffs pop and a rainbow sweeps over it.
|
|
16
|
+
* - A run failed: × × eyes and drained colour; the cube's layers glitch and it slumps; the sphere
|
|
17
|
+
* deflates, wrinkled, and fills again; the pyramid's slabs slip sideways and back; the block's
|
|
18
|
+
* dials jam and jerk; the cloud storms, rain and lightning.
|
|
19
|
+
* - Work starting, switched off, switched on: a determined nod, a yawn as the eyes close, a
|
|
20
|
+
* stretch as they open wide; each shape turning a layer, a capstone or its dials its own way.
|
|
21
|
+
*
|
|
22
|
+
* Loops, at `t` seconds, as strong as `env` (0 to 1: how far it has come in, or gone out)
|
|
23
|
+
* (`actLoopAt`): thinking, the sphere's moons orbit, the pyramid's capstone floats and turns, the
|
|
24
|
+
* block's dials turn like a cryptex's, the cloud boils (the cube's Rubik's turns are
|
|
25
|
+
* rubik.ts); at work, the cube's layers tick like clockwork, the sphere's colour rolls
|
|
26
|
+
* with a moon going round, the capstone stamps, the block's side dials roll like wheels, the cloud
|
|
27
|
+
* rains; asleep, z's rise and the cube's cubelets loosen; worried, goosebumps, a rattle, a grey
|
|
28
|
+
* drizzle. A loop that turns something (`QUANTUM`) comes back to where it started every so many
|
|
29
|
+
* seconds, so it can stop there.
|
|
30
|
+
*/
|
|
31
|
+
/** Where in the picture a shape's top is, as it rests (world units from the picture's middle). */
|
|
32
|
+
export const topOf = (shape) => pictureY(FLOOR + HOVER[shape] - BOTTOM[shape] + TOP[shape]);
|
|
33
|
+
/** Where in the picture its middle is. */
|
|
34
|
+
export const middleOf = (shape) => pictureY(FLOOR + HOVER[shape] - BOTTOM[shape]);
|
|
35
|
+
/** The cloud's puffs from left to right, for a wave running across it. */
|
|
36
|
+
const LEFT_TO_RIGHT = [...PUFFS.keys()].sort((a, b) => PUFFS[a][0] - PUFFS[b][0]);
|
|
37
|
+
const waveOrder = (puff) => LEFT_TO_RIGHT.indexOf(puff);
|
|
38
|
+
/** The cloud's puffs, each moved and swollen by `of(puff, order)`: [dx, dy, dz, scale]. */
|
|
39
|
+
const puffs = (of) => PUFFS.map((_, i) => of(i, waveOrder(i)));
|
|
40
|
+
/** Eyes become a shape for a while, in and out again. */
|
|
41
|
+
const eyesAs = (glyph, p, from, to, fade = 0.08) => ({ glyph, glyphMix: hold(p, from, from + fade, to - fade, to) });
|
|
42
|
+
/** Layers turning: the angles of the three, about an axis; none once they're all home. */
|
|
43
|
+
const layers = (axis, low, middle, high) => (low || middle || high ? { axis, angles: [low, middle, high] } : null);
|
|
44
|
+
const within = (value, most) => Math.max(-most, Math.min(most, value));
|
|
45
|
+
// ---------------------------------------------------------------------------------------------
|
|
46
|
+
// Bits
|
|
47
|
+
/** Hearts floating up from above its head, one after another from `from`, each living `life` of the move. */
|
|
48
|
+
export function hearts(p, shape, from, every, life, count) {
|
|
49
|
+
const top = topOf(shape);
|
|
50
|
+
const bits = [];
|
|
51
|
+
for (let i = 0; i < count; i++) {
|
|
52
|
+
const q = span(p, from + i * every, from + i * every + life);
|
|
53
|
+
if (q <= 0 || q >= 1)
|
|
54
|
+
continue;
|
|
55
|
+
const side = i % 2 ? -1 : 1;
|
|
56
|
+
bits.push({
|
|
57
|
+
kind: "heart",
|
|
58
|
+
x: side * (0.14 + 0.08 * i) + 0.06 * Math.sin(q * Math.PI * 2 + i),
|
|
59
|
+
y: Math.min(1.3, top + 0.12 + 0.48 * easeOut(q)),
|
|
60
|
+
size: 0.24 * easeOutBack(span(q, 0, 0.3), 2) * (1 - 0.25 * q),
|
|
61
|
+
alpha: span(q, 0, 0.12) * (1 - span(q, 0.6, 1)),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return bits;
|
|
65
|
+
}
|
|
66
|
+
/** Sparks round it, popping one after another from `from`, each living `life`. */
|
|
67
|
+
export function sparks(p, shape, from, life) {
|
|
68
|
+
const middle = middleOf(shape);
|
|
69
|
+
const bits = [];
|
|
70
|
+
for (let i = 0; i < 4; i++) {
|
|
71
|
+
const q = span(p, from + i * 0.07, from + i * 0.07 + life);
|
|
72
|
+
if (q <= 0 || q >= 1)
|
|
73
|
+
continue;
|
|
74
|
+
const angle = (0.14 + 0.24 * i) * Math.PI;
|
|
75
|
+
const reach = 0.92 + 0.22 * easeOut(q);
|
|
76
|
+
bits.push({ kind: "spark", x: within(Math.cos(angle) * reach * 1.08, 1.3), y: within(middle + 0.12 + Math.sin(angle) * reach * 0.86, 1.3), size: 0.2 * bump(q), alpha: bump(q) });
|
|
77
|
+
}
|
|
78
|
+
return bits;
|
|
79
|
+
}
|
|
80
|
+
/** A z, `q` (0 to 1) through its rise: drifting up and away from its head, growing and fading. */
|
|
81
|
+
function zAt(q, shape, strength) {
|
|
82
|
+
if (q < 0 || q >= 1)
|
|
83
|
+
return null;
|
|
84
|
+
return {
|
|
85
|
+
kind: "z",
|
|
86
|
+
x: 0.36 + 0.3 * q + 0.04 * Math.sin(q * 6),
|
|
87
|
+
y: Math.min(1.3, topOf(shape) + 0.02 + 0.5 * q),
|
|
88
|
+
size: 0.13 + 0.08 * q,
|
|
89
|
+
alpha: span(q, 0, 0.15) * (1 - span(q, 0.55, 1)) * 0.85 * strength,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** Z's asleep: one rising every 2.2 seconds, each for 3.3, the first once it has had time to doze off. */
|
|
93
|
+
export function zs(t, shape, strength = 1) {
|
|
94
|
+
const asleep = t - 1.8;
|
|
95
|
+
if (asleep < 0)
|
|
96
|
+
return [];
|
|
97
|
+
const latest = Math.floor(asleep / 2.2);
|
|
98
|
+
return [0, 1].map((k) => zAt((asleep - (latest - k) * 2.2) / 3.3, shape, strength)).filter((bit) => bit !== null);
|
|
99
|
+
}
|
|
100
|
+
/** A raindrop under the cloud, `q` (0 to 1) through its life: it forms on the cloud's underside, falls and is gone at the ground. */
|
|
101
|
+
function raindrop(q, x, lift, size = 0.075) {
|
|
102
|
+
if (q <= 0 || q >= 1)
|
|
103
|
+
return null;
|
|
104
|
+
const base = BODY.cloud.base;
|
|
105
|
+
const ground = BOTTOM.cloud - HOVER.cloud - lift + 0.03;
|
|
106
|
+
if (q < 0.25)
|
|
107
|
+
return [x, base - 0.02, 0.12, size * easeOut(q / 0.25)];
|
|
108
|
+
const fall = span(q, 0.25, 0.9);
|
|
109
|
+
const y = base - 0.03 + (ground - base) * fall * fall;
|
|
110
|
+
return [x, y, 0.12, size * (1 - span(q, 0.82, 1)) * (1 + 0.15 * fall)];
|
|
111
|
+
}
|
|
112
|
+
// ---------------------------------------------------------------------------------------------
|
|
113
|
+
// Reactions
|
|
114
|
+
/** Where a reaction has got to `p` of the way through (0 to 1), for a shape, at an energy's amplitude. */
|
|
115
|
+
export function actAt(act, p, shape, amp = 1) {
|
|
116
|
+
switch (act) {
|
|
117
|
+
case "pet":
|
|
118
|
+
return pet(p, shape, amp);
|
|
119
|
+
case "celebrate":
|
|
120
|
+
return celebrate(p, shape, amp);
|
|
121
|
+
case "fail":
|
|
122
|
+
return fail(p, shape, amp);
|
|
123
|
+
case "start":
|
|
124
|
+
return start(p, shape, amp);
|
|
125
|
+
case "doze":
|
|
126
|
+
return doze(p, shape, amp);
|
|
127
|
+
case "wake":
|
|
128
|
+
return wake(p, shape, amp);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function pet(p, shape, amp) {
|
|
132
|
+
const pose = posed({
|
|
133
|
+
...eyesAs("heart", p, 0.06, 0.92),
|
|
134
|
+
squash: keys(p, [
|
|
135
|
+
[0, 1],
|
|
136
|
+
[0.1, 1 - 0.09 * amp],
|
|
137
|
+
[0.24, 1 + 0.05 * amp],
|
|
138
|
+
[0.4, 1 - 0.015 * amp],
|
|
139
|
+
[0.55, 1],
|
|
140
|
+
]),
|
|
141
|
+
pitch: 6 * bump(span(p, 0.1, 0.9)),
|
|
142
|
+
bits: hearts(p, shape, 0.12, 0.16, 0.5, 3),
|
|
143
|
+
});
|
|
144
|
+
if (shape === "cube") {
|
|
145
|
+
// Its top layer twirls right round, as a head scratched.
|
|
146
|
+
pose.split = hold(p, 0.03, 0.13, 0.82, 0.94);
|
|
147
|
+
pose.layers = layers(1, 0, 0, TURN * easeInOut(span(p, 0.13, 0.8)));
|
|
148
|
+
}
|
|
149
|
+
else if (shape === "sphere") {
|
|
150
|
+
// A dent where it's poked, a ripple running out from it, and a jelly's wobble.
|
|
151
|
+
pose.dent = 0.12 * hold(p, 0.03, 0.09, 0.14, 0.28);
|
|
152
|
+
pose.wave = 0.04 * amp * span(p, 0.1, 0.2) * (1 - span(p, 0.25, 0.95));
|
|
153
|
+
pose.wavePhase = p * 30;
|
|
154
|
+
pose.squash *= 1 + 0.07 * amp * wobble(span(p, 0.12, 1), 3);
|
|
155
|
+
}
|
|
156
|
+
else if (shape === "pyramid") {
|
|
157
|
+
pose.shear = 0.2 * amp * wobble(span(p, 0.08, 1), 2.5);
|
|
158
|
+
}
|
|
159
|
+
else if (shape === "block") {
|
|
160
|
+
// Its dials rock one after another, and it purrs.
|
|
161
|
+
const q = span(p, 0.1, 0.92);
|
|
162
|
+
const rock = (dial) => 0.32 * amp * Math.sin(2 * Math.PI * (1.6 * q - dial * 0.12)) * (1 - q) ** 1.2 * span(q, 0, 0.1);
|
|
163
|
+
pose.split = hold(p, 0.03, 0.12, 0.86, 0.96);
|
|
164
|
+
pose.layers = layers(0, rock(0), rock(1), rock(2));
|
|
165
|
+
pose.x = 0.006 * amp * Math.sin(2 * Math.PI * 22 * p) * hold(p, 0.1, 0.2, 0.8, 0.9);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
pose.puffs = puffs((_, order) => {
|
|
169
|
+
const q = span(p, 0.08 + order * 0.07, 0.4 + order * 0.07);
|
|
170
|
+
return [0, 0.11 * amp * bump(q), 0, 1 + 0.1 * bump(q)];
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return pose;
|
|
174
|
+
}
|
|
175
|
+
function celebrate(p, shape, amp) {
|
|
176
|
+
const pose = posed({ ...eyesAs("star", p, 0.08, 0.95), bits: sparks(p, shape, 0.26, 0.3) });
|
|
177
|
+
if (shape === "cube") {
|
|
178
|
+
// It bursts into its cubelets, twisting as it spins, and snaps back together.
|
|
179
|
+
pose.split = hold(p, 0, 0.1, 0.84, 0.95);
|
|
180
|
+
pose.spread = 0.85 * amp * easeOut(span(p, 0.1, 0.32)) * (1 - easeIn(span(p, 0.58, 0.78)));
|
|
181
|
+
const twist = 0.8 * bump(span(p, 0.1, 0.78));
|
|
182
|
+
pose.layers = layers(1, -twist, 0, twist);
|
|
183
|
+
pose.yaw = 360 * easeInOut(span(p, 0.12, 0.7));
|
|
184
|
+
pose.lift = 0.22 * amp * arc(span(p, 0.08, 0.76));
|
|
185
|
+
pose.squash = keys(p, [
|
|
186
|
+
[0, 1],
|
|
187
|
+
[0.06, 1 - 0.1 * amp],
|
|
188
|
+
[0.16, 1 + 0.05 * amp],
|
|
189
|
+
[0.7, 1],
|
|
190
|
+
[0.8, 1 - 0.14 * amp],
|
|
191
|
+
[0.9, 1 + 0.03 * amp],
|
|
192
|
+
[1, 1],
|
|
193
|
+
]);
|
|
194
|
+
}
|
|
195
|
+
else if (shape === "sphere") {
|
|
196
|
+
// It splashes into six droplets that fly out, hang a beat and run back together.
|
|
197
|
+
pose.core = keys(p, [
|
|
198
|
+
[0, 1],
|
|
199
|
+
[0.1, 1.06],
|
|
200
|
+
[0.2, 0.5],
|
|
201
|
+
[0.55, 0.5],
|
|
202
|
+
[0.75, 1.04],
|
|
203
|
+
[0.86, 0.98],
|
|
204
|
+
[1, 1],
|
|
205
|
+
]);
|
|
206
|
+
const out = 0.15 + 0.85 * easeOut(span(p, 0.12, 0.32)) * (1 - easeInOut(span(p, 0.52, 0.74)));
|
|
207
|
+
const size = 0.19 * span(p, 0.1, 0.18) * (1 - span(p, 0.7, 0.8));
|
|
208
|
+
if (size > 0)
|
|
209
|
+
pose.beads = Array.from({ length: 6 }, (_, i) => {
|
|
210
|
+
const angle = ((i * 60 + 15) * Math.PI) / 180 + 0.4 * span(p, 0.2, 0.7);
|
|
211
|
+
return [Math.cos(angle) * out, Math.sin(angle) * out * 0.9, 0.25 * Math.sin(2 * angle) * out, size];
|
|
212
|
+
});
|
|
213
|
+
pose.squash = 1 + 0.06 * amp * wobble(span(p, 0.74, 1), 2);
|
|
214
|
+
}
|
|
215
|
+
else if (shape === "pyramid") {
|
|
216
|
+
// It launches its capstone, spinning twice, and catches it.
|
|
217
|
+
const lift = keys(p, [
|
|
218
|
+
[0, 0],
|
|
219
|
+
[0.1, 0],
|
|
220
|
+
[0.42, 0.46 * amp],
|
|
221
|
+
[0.7, 0],
|
|
222
|
+
[0.77, 0.06 * amp],
|
|
223
|
+
[0.84, 0],
|
|
224
|
+
]);
|
|
225
|
+
pose.pieces = [0, 0, lift, 2 * TURN * easeInOut(span(p, 0.1, 0.72))];
|
|
226
|
+
pose.squash = keys(p, [
|
|
227
|
+
[0, 1],
|
|
228
|
+
[0.08, 1 - 0.08 * amp],
|
|
229
|
+
[0.16, 1],
|
|
230
|
+
[0.68, 1],
|
|
231
|
+
[0.72, 1 - 0.1 * amp],
|
|
232
|
+
[0.82, 1 + 0.03 * amp],
|
|
233
|
+
[1, 1],
|
|
234
|
+
]);
|
|
235
|
+
}
|
|
236
|
+
else if (shape === "block") {
|
|
237
|
+
// A slot machine: every dial spins and stops in turn, the middle one, eyes and all, last.
|
|
238
|
+
const stop = (from, to, turns) => turns * TURN * easeOutBack(span(p, from, to), 0.6);
|
|
239
|
+
pose.split = hold(p, 0, 0.08, 0.86, 0.96);
|
|
240
|
+
pose.layers = layers(0, stop(0.08, 0.48, 2), stop(0.08, 0.72, 4), stop(0.08, 0.6, 3));
|
|
241
|
+
pose.lift = 0.12 * amp * arc(span(p, 0.74, 0.92));
|
|
242
|
+
pose.squash = keys(p, [
|
|
243
|
+
[0, 1],
|
|
244
|
+
[0.72, 1],
|
|
245
|
+
[0.76, 1 - 0.08 * amp],
|
|
246
|
+
[0.84, 1 + 0.04 * amp],
|
|
247
|
+
[0.94, 1 - 0.04 * amp],
|
|
248
|
+
[1, 1],
|
|
249
|
+
]);
|
|
250
|
+
Object.assign(pose, eyesAs("star", p, 0.72, 0.97));
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
// Its puffs pop one after another and a rainbow sweeps over it.
|
|
254
|
+
pose.puffs = puffs((_, order) => {
|
|
255
|
+
const q = span(p, 0.08 + order * 0.05, 0.36 + order * 0.05);
|
|
256
|
+
return [0, 0.04 * amp * bump(q), 0, 1 + 0.2 * amp * bump(q)];
|
|
257
|
+
});
|
|
258
|
+
pose.rainbow = hold(p, 0.12, 0.2, 0.78, 0.96);
|
|
259
|
+
pose.rainbowSweep = 1.1 * easeInOut(span(p, 0.14, 0.48));
|
|
260
|
+
pose.lift = 0.14 * amp * arc(span(p, 0.1, 0.6));
|
|
261
|
+
}
|
|
262
|
+
return pose;
|
|
263
|
+
}
|
|
264
|
+
function fail(p, shape, amp) {
|
|
265
|
+
const slump = keys(p, [
|
|
266
|
+
[0, 1],
|
|
267
|
+
[0.6, 1],
|
|
268
|
+
[0.68, 1 - 0.12 * amp],
|
|
269
|
+
[0.8, 1 - 0.05 * amp],
|
|
270
|
+
[1, 1],
|
|
271
|
+
]);
|
|
272
|
+
const pose = posed({
|
|
273
|
+
...eyesAs("cross", p, 0.1, 0.88),
|
|
274
|
+
dim: 1 - 0.55 * hold(p, 0.08, 0.3, 0.75, 1),
|
|
275
|
+
squash: slump,
|
|
276
|
+
pitch: keys(p, [
|
|
277
|
+
[0, 0],
|
|
278
|
+
[0.62, 0],
|
|
279
|
+
[0.72, -12],
|
|
280
|
+
[1, 0],
|
|
281
|
+
]),
|
|
282
|
+
});
|
|
283
|
+
if (shape === "cube") {
|
|
284
|
+
// Its layers glitch, jumping from one wrong place to the next, then it slumps.
|
|
285
|
+
const step = Math.floor(p / 0.04);
|
|
286
|
+
const glitch = hold(p, 0.06, 0.1, 0.44, 0.55);
|
|
287
|
+
const jump = (layer) => (noise(step, layer) - 0.5) * 0.9 * amp * glitch;
|
|
288
|
+
pose.split = hold(p, 0.03, 0.08, 0.55, 0.65);
|
|
289
|
+
pose.layers = layers(1, jump(1), jump(2), jump(3));
|
|
290
|
+
pose.x = 0.06 * amp * (noise(step, 7) - 0.5) * glitch;
|
|
291
|
+
}
|
|
292
|
+
else if (shape === "sphere") {
|
|
293
|
+
// It goes down like a balloon, wrinkling, flat on the ground, and fills again.
|
|
294
|
+
pose.core = keys(p, [
|
|
295
|
+
[0, 1],
|
|
296
|
+
[0.06, 1.04],
|
|
297
|
+
[0.32, 0.72],
|
|
298
|
+
[0.62, 0.72],
|
|
299
|
+
[0.84, 1.04],
|
|
300
|
+
[0.92, 0.99],
|
|
301
|
+
[1, 1],
|
|
302
|
+
]);
|
|
303
|
+
pose.wrinkle = hold(p, 0.1, 0.3, 0.6, 0.82);
|
|
304
|
+
pose.squash = keys(p, [
|
|
305
|
+
[0, 1],
|
|
306
|
+
[0.3, 0.82],
|
|
307
|
+
[0.42, 0.72],
|
|
308
|
+
[0.6, 0.76],
|
|
309
|
+
[0.84, 1.05],
|
|
310
|
+
[1, 1],
|
|
311
|
+
]);
|
|
312
|
+
pose.x = 0.02 * Math.sin(p * 80) * hold(p, 0.08, 0.12, 0.3, 0.36);
|
|
313
|
+
}
|
|
314
|
+
else if (shape === "pyramid") {
|
|
315
|
+
// Its slabs slip sideways, teeter, and slide back.
|
|
316
|
+
const teeter = 0.03 * Math.sin(2 * Math.PI * 3 * span(p, 0.26, 0.6)) * hold(p, 0.26, 0.3, 0.55, 0.6);
|
|
317
|
+
const middle = 0.22 * amp * (easeOut(span(p, 0.08, 0.26)) - easeInOut(span(p, 0.6, 0.84))) + teeter;
|
|
318
|
+
const top = -0.26 * amp * (easeOut(span(p, 0.14, 0.32)) - easeInOut(span(p, 0.62, 0.86))) - teeter;
|
|
319
|
+
pose.pieces = [middle, top, 0, 0];
|
|
320
|
+
}
|
|
321
|
+
else if (shape === "block") {
|
|
322
|
+
// Its dials jam: they try to turn, jerking back and forth, and give up.
|
|
323
|
+
const jerk = (phase) => {
|
|
324
|
+
const s = Math.sin(2 * Math.PI * 9 * p + phase);
|
|
325
|
+
return Math.sign(s) * Math.abs(s) ** 0.5;
|
|
326
|
+
};
|
|
327
|
+
pose.split = hold(p, 0.03, 0.1, 0.8, 0.92);
|
|
328
|
+
const trying = hold(p, 0.1, 0.16, 0.5, 0.6);
|
|
329
|
+
pose.layers = layers(0, 0.3 * amp * jerk(0) * trying, 0.12 * jerk(1) * hold(p, 0.2, 0.25, 0.4, 0.5), -0.3 * amp * jerk(2) * trying);
|
|
330
|
+
pose.x = 0.012 * amp * Math.sin(2 * Math.PI * 24 * p) * hold(p, 0.1, 0.14, 0.5, 0.6);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
// A storm: it rises dark, rain pours and lightning strikes twice.
|
|
334
|
+
const lift = 0.22 * amp * hold(p, 0.05, 0.2, 0.75, 0.95);
|
|
335
|
+
pose.lift = lift;
|
|
336
|
+
pose.dim = 1 - 0.72 * hold(p, 0.05, 0.25, 0.75, 1);
|
|
337
|
+
const flash = (at, length) => hold(p, at, at + 0.005, at + length, at + length + 0.012);
|
|
338
|
+
pose.bolt = Math.max(flash(0.32, 0.035), 0.4 * flash(0.372, 0.02), flash(0.41, 0.04));
|
|
339
|
+
pose.boltSeed = p < 0.395 ? 1 : 2;
|
|
340
|
+
pose.beads = Array.from({ length: 6 }, (_, i) => raindrop(span(p, 0.12 + i * 0.1, 0.32 + i * 0.1), (noise(i, 3) - 0.5) * 0.9, lift)).filter((drop) => drop !== null);
|
|
341
|
+
pose.x = 0.012 * Math.sin(p * 90) * hold(p, 0.3, 0.32, 0.46, 0.5);
|
|
342
|
+
}
|
|
343
|
+
return pose;
|
|
344
|
+
}
|
|
345
|
+
function start(p, shape, amp) {
|
|
346
|
+
const pose = posed({
|
|
347
|
+
pitch: keys(p, [
|
|
348
|
+
[0, 0],
|
|
349
|
+
[0.15, -9],
|
|
350
|
+
[0.4, 5],
|
|
351
|
+
[0.6, 0],
|
|
352
|
+
]),
|
|
353
|
+
squash: keys(p, [
|
|
354
|
+
[0, 1],
|
|
355
|
+
[0.12, 1 - 0.07 * amp],
|
|
356
|
+
[0.3, 1 + 0.05 * amp],
|
|
357
|
+
[0.45, 1],
|
|
358
|
+
]),
|
|
359
|
+
});
|
|
360
|
+
const quarter = Math.PI / 2;
|
|
361
|
+
if (shape === "cube") {
|
|
362
|
+
// Cracks its knuckles: the top layer and the bottom one turn a quarter and back.
|
|
363
|
+
pose.split = hold(p, 0.03, 0.1, 0.72, 0.84);
|
|
364
|
+
pose.layers = layers(1, -quarter * (easeOutBack(span(p, 0.16, 0.34)) - easeInOut(span(p, 0.44, 0.62))), 0, quarter * (easeOutBack(span(p, 0.1, 0.28)) - easeInOut(span(p, 0.38, 0.56))));
|
|
365
|
+
}
|
|
366
|
+
else if (shape === "sphere") {
|
|
367
|
+
// Winds up: a moon buds off and whips once round it.
|
|
368
|
+
const angle = TURN * easeInOut(span(p, 0.12, 0.72));
|
|
369
|
+
const reach = 0.8 + 0.22 * hold(p, 0.08, 0.2, 0.66, 0.8);
|
|
370
|
+
const size = 0.12 * hold(p, 0.06, 0.16, 0.7, 0.82);
|
|
371
|
+
if (size > 0)
|
|
372
|
+
pose.beads = [[Math.cos(angle) * reach, 0.05, Math.sin(angle) * reach, size]];
|
|
373
|
+
}
|
|
374
|
+
else if (shape === "pyramid") {
|
|
375
|
+
pose.pieces = [0, 0, 0.05 * hold(p, 0.06, 0.15, 0.62, 0.74), TURN * easeInOut(span(p, 0.1, 0.7))];
|
|
376
|
+
}
|
|
377
|
+
else if (shape === "block") {
|
|
378
|
+
pose.split = hold(p, 0.03, 0.1, 0.72, 0.84);
|
|
379
|
+
pose.layers = layers(0, TURN * easeInOut(span(p, 0.1, 0.6)), 0, -TURN * easeInOut(span(p, 0.14, 0.64)));
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
pose.puffs = puffs((_, order) => [0, 0, 0, 1 + 0.1 * amp * bump(span(p, 0.1 + order * 0.05, 0.36 + order * 0.05))]);
|
|
383
|
+
pose.lift = 0.08 * amp * arc(span(p, 0.2, 0.6));
|
|
384
|
+
}
|
|
385
|
+
return pose;
|
|
386
|
+
}
|
|
387
|
+
function doze(p, shape, amp) {
|
|
388
|
+
// A yawn, the lids coming down, and it's asleep: the eyes hand over to the sleeping ones.
|
|
389
|
+
const pose = posed({
|
|
390
|
+
glyph: "rect",
|
|
391
|
+
glyphMix: 1 - span(p, 0.72, 0.86),
|
|
392
|
+
shut: easeInOut(span(p, 0.1, 0.7)) * (1 - span(p, 0.86, 0.96)),
|
|
393
|
+
squash: keys(p, [
|
|
394
|
+
[0, 1],
|
|
395
|
+
[0.12, 1 + 0.07 * amp],
|
|
396
|
+
[0.28, 1 + 0.07 * amp],
|
|
397
|
+
[0.45, 1 - 0.04 * amp],
|
|
398
|
+
[0.7, 1],
|
|
399
|
+
]),
|
|
400
|
+
pitch: keys(p, [
|
|
401
|
+
[0, 0],
|
|
402
|
+
[0.12, 8],
|
|
403
|
+
[0.3, 8],
|
|
404
|
+
[0.55, -6],
|
|
405
|
+
[1, 0],
|
|
406
|
+
]),
|
|
407
|
+
});
|
|
408
|
+
if (shape === "cube") {
|
|
409
|
+
// Powers down: its top layer turns once, slower and slower.
|
|
410
|
+
pose.split = hold(p, 0.05, 0.12, 0.8, 0.92);
|
|
411
|
+
pose.layers = layers(1, 0, 0, TURN * easeOut(span(p, 0.1, 0.78)));
|
|
412
|
+
}
|
|
413
|
+
else if (shape === "sphere") {
|
|
414
|
+
pose.core = 1 - 0.05 * hold(p, 0.3, 0.5, 0.8, 1);
|
|
415
|
+
}
|
|
416
|
+
else if (shape === "pyramid") {
|
|
417
|
+
pose.pieces = [0, 0, 0.06 * hold(p, 0.1, 0.2, 0.3, 0.6), 0];
|
|
418
|
+
}
|
|
419
|
+
else if (shape === "block") {
|
|
420
|
+
pose.split = hold(p, 0.05, 0.12, 0.84, 0.94);
|
|
421
|
+
pose.layers = layers(0, TURN * easeOut(span(p, 0.1, 0.8)), 0, TURN * easeOut(span(p, 0.12, 0.84)));
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
pose.puffs = puffs(() => [0, 0, 0, 1 - 0.06 * hold(p, 0.3, 0.6, 0.8, 1)]);
|
|
425
|
+
}
|
|
426
|
+
return pose;
|
|
427
|
+
}
|
|
428
|
+
function wake(p, shape, amp) {
|
|
429
|
+
// A stretch as the eyes open, wide for a moment, and a little hop.
|
|
430
|
+
const pose = posed({
|
|
431
|
+
glyph: "line",
|
|
432
|
+
glyphMix: 1 - span(p, 0.2, 0.32),
|
|
433
|
+
eyes: 1 + 0.25 * hold(p, 0.3, 0.38, 0.55, 0.8),
|
|
434
|
+
squash: keys(p, [
|
|
435
|
+
[0, 1],
|
|
436
|
+
[0.12, 1 - 0.08 * amp],
|
|
437
|
+
[0.28, 1 + 0.1 * amp],
|
|
438
|
+
[0.42, 1],
|
|
439
|
+
]),
|
|
440
|
+
lift: 0.12 * amp * arc(span(p, 0.3, 0.62)),
|
|
441
|
+
});
|
|
442
|
+
const quarter = Math.PI / 2;
|
|
443
|
+
if (shape === "cube") {
|
|
444
|
+
// Boots up: its bottom layer and its top one turn a quarter, crossing, and back.
|
|
445
|
+
pose.split = hold(p, 0.04, 0.1, 0.7, 0.8);
|
|
446
|
+
pose.layers = layers(1, quarter * (easeOutBack(span(p, 0.1, 0.24)) - easeInOut(span(p, 0.46, 0.62))), 0, -quarter * (easeOutBack(span(p, 0.2, 0.34)) - easeInOut(span(p, 0.5, 0.66))));
|
|
447
|
+
}
|
|
448
|
+
else if (shape === "sphere") {
|
|
449
|
+
pose.wave = 0.03 * span(p, 0.1, 0.2) * (1 - span(p, 0.3, 0.9));
|
|
450
|
+
pose.wavePhase = p * 26;
|
|
451
|
+
}
|
|
452
|
+
else if (shape === "pyramid") {
|
|
453
|
+
const lift = keys(p, [
|
|
454
|
+
[0, 0],
|
|
455
|
+
[0.1, 0],
|
|
456
|
+
[0.28, 0.2 * amp],
|
|
457
|
+
[0.48, 0],
|
|
458
|
+
[0.54, 0.03],
|
|
459
|
+
[0.6, 0],
|
|
460
|
+
]);
|
|
461
|
+
pose.pieces = [0, 0, lift, TURN * easeInOut(span(p, 0.1, 0.5))];
|
|
462
|
+
}
|
|
463
|
+
else if (shape === "block") {
|
|
464
|
+
pose.split = hold(p, 0.04, 0.1, 0.62, 0.72);
|
|
465
|
+
pose.layers = layers(0, TURN * easeInOut(span(p, 0.08, 0.45)), 0, -TURN * easeInOut(span(p, 0.2, 0.56)));
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
pose.puffs = puffs((_, order) => [0, 0, 0, 1 + 0.16 * amp * bump(span(p, 0.1 + order * 0.06, 0.34 + order * 0.06))]);
|
|
469
|
+
}
|
|
470
|
+
return pose;
|
|
471
|
+
}
|
|
472
|
+
// ---------------------------------------------------------------------------------------------
|
|
473
|
+
// Loops
|
|
474
|
+
/**
|
|
475
|
+
* How often each shape's loop comes back to where it started, in seconds at lively energy: a
|
|
476
|
+
* loop that turns its parts stops only then, so nothing jumps. 0 is any time. The thinking cube's
|
|
477
|
+
* Rubik's turns finish their own round (figure-motion.ts).
|
|
478
|
+
*/
|
|
479
|
+
export const QUANTUM = {
|
|
480
|
+
breathe: { cube: 0, sphere: 0, pyramid: 0, block: 0, cloud: 0 },
|
|
481
|
+
float: { cube: 0, sphere: 0, pyramid: 0, block: 0, cloud: 0 },
|
|
482
|
+
think: { cube: 0, sphere: 0, pyramid: 3, block: 5.4, cloud: 0 },
|
|
483
|
+
work: { cube: 3.6, sphere: 2.8, pyramid: 0.8, block: 1.6, cloud: 0 },
|
|
484
|
+
rest: { cube: 0, sphere: 0, pyramid: 0, block: 0, cloud: 0 },
|
|
485
|
+
worry: { cube: 0, sphere: 0, pyramid: 0, block: 0, cloud: 0 },
|
|
486
|
+
};
|
|
487
|
+
/** How long a loop's own act takes to come in and to go out, in seconds. */
|
|
488
|
+
export const ENVELOPE = { in: 0.6, out: 0.5 };
|
|
489
|
+
/** The block's dials as it thinks, like cracking a cryptex: [dial (−1 left, 1 right), quarter turns], one every 0.8 s from half a second in. Each dial ends where it began. */
|
|
490
|
+
const CRYPTEX = [
|
|
491
|
+
[-1, 1],
|
|
492
|
+
[1, -1],
|
|
493
|
+
[-1, 1],
|
|
494
|
+
[1, -1],
|
|
495
|
+
[-1, -2],
|
|
496
|
+
[1, 2],
|
|
497
|
+
];
|
|
498
|
+
/** The cube's layers at work, ticking like clockwork: a quarter turn every 0.9 s, the top one forward twice and back twice, the bottom one the other way, half a tick behind. */
|
|
499
|
+
function clockwork(t) {
|
|
500
|
+
const tick = (at, directions) => {
|
|
501
|
+
const q = phaseOf(at, 3.6) * 3.6;
|
|
502
|
+
let angle = 0;
|
|
503
|
+
directions.forEach((direction, i) => {
|
|
504
|
+
angle += (direction * Math.PI) / 2 * easeOutBack(span(q, i * 0.9, i * 0.9 + 0.32), 1.4);
|
|
505
|
+
});
|
|
506
|
+
return angle;
|
|
507
|
+
};
|
|
508
|
+
return { low: tick(t - 0.45, [-1, -1, 1, 1]), high: tick(t, [1, 1, -1, -1]) };
|
|
509
|
+
}
|
|
510
|
+
/** Where a shape's own loop act is at `t` seconds, as strong as `env`. */
|
|
511
|
+
export function actLoopAt(loop, t, shape, amp = 1, env = 1) {
|
|
512
|
+
if (env <= 0 || loop === "breathe" || loop === "float")
|
|
513
|
+
return REST;
|
|
514
|
+
const pose = posed({});
|
|
515
|
+
if (loop === "think") {
|
|
516
|
+
if (shape === "sphere") {
|
|
517
|
+
// Three moons bud off it and orbit, each on its own tilted path.
|
|
518
|
+
const reach = 0.62 + 0.4 * easeOut(env);
|
|
519
|
+
pose.beads = [0.35, -0.5, 0.95].map((tilt, i) => {
|
|
520
|
+
const angle = i * 2.1 + (0.9 + 0.25 * i) * t;
|
|
521
|
+
return [reach * Math.cos(angle), reach * Math.sin(angle) * Math.sin(tilt), reach * Math.sin(angle) * Math.cos(tilt), 0.14 * env];
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
else if (shape === "pyramid") {
|
|
525
|
+
// Its capstone floats up and turns slowly, bobbing.
|
|
526
|
+
pose.pieces = [0, 0, 0.24 * easeOut(env) + 0.03 * Math.sin(t * 2.2) * env, (TURN * t) / 3];
|
|
527
|
+
}
|
|
528
|
+
else if (shape === "block") {
|
|
529
|
+
// Its side dials turn a quarter at a time, as if cracking a code.
|
|
530
|
+
pose.split = env;
|
|
531
|
+
const angles = [0, 0, 0];
|
|
532
|
+
CRYPTEX.forEach(([dial, quarters], i) => {
|
|
533
|
+
const from = 0.5 + i * 0.8;
|
|
534
|
+
const length = Math.abs(quarters) > 1 ? 0.6 : 0.45;
|
|
535
|
+
angles[dial + 1] += ((quarters * Math.PI) / 2) * easeOutBack(span(phaseOf(t, 5.4) * 5.4, from, from + length), 1.3);
|
|
536
|
+
});
|
|
537
|
+
pose.layers = layers(0, angles[0], 0, angles[2]);
|
|
538
|
+
}
|
|
539
|
+
else if (shape === "cloud") {
|
|
540
|
+
// Its puffs boil, rolling round and swelling.
|
|
541
|
+
pose.puffs = puffs((i) => {
|
|
542
|
+
const calm = i === 0 ? 0.35 : 1;
|
|
543
|
+
return [
|
|
544
|
+
0.07 * calm * env * Math.sin((TURN * t) / 3 + i * 1.7),
|
|
545
|
+
0.06 * calm * env * Math.sin((TURN * t) / 2 + i * 2.3),
|
|
546
|
+
0,
|
|
547
|
+
1 + 0.1 * calm * env * Math.sin((TURN * t) / 1.5 + i * 1.1),
|
|
548
|
+
];
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
else if (loop === "work") {
|
|
553
|
+
if (shape === "cube") {
|
|
554
|
+
pose.split = env;
|
|
555
|
+
const { low, high } = clockwork(t);
|
|
556
|
+
pose.layers = layers(1, low, 0, high);
|
|
557
|
+
}
|
|
558
|
+
else if (shape === "sphere") {
|
|
559
|
+
// Its colour rolls round as a ball rolls, a small moon whizzing round it.
|
|
560
|
+
pose.colourRoll = (TURN * t) / 2.8;
|
|
561
|
+
const angle = (TURN * t) / 1.4;
|
|
562
|
+
pose.beads = [[0.98 * Math.cos(angle), 0.98 * Math.sin(angle) * 0.55, 0.98 * Math.sin(angle) * 0.83, 0.09 * env]];
|
|
563
|
+
}
|
|
564
|
+
else if (shape === "pyramid") {
|
|
565
|
+
// Its capstone stamps, lifted slowly and brought down hard.
|
|
566
|
+
const q = phaseOf(t, 0.8);
|
|
567
|
+
const lift = q < 0.55 ? easeOut(q / 0.55) : 1 - easeIn(span(q, 0.55, 0.75));
|
|
568
|
+
pose.pieces = [0, 0, 0.16 * amp * env * lift, 0];
|
|
569
|
+
pose.squash = 1 - 0.04 * amp * env * bump(span(q, 0.75, 0.9));
|
|
570
|
+
}
|
|
571
|
+
else if (shape === "block") {
|
|
572
|
+
// Its side dials roll like wheels.
|
|
573
|
+
pose.split = env;
|
|
574
|
+
const roll = (TURN * t) / 1.6;
|
|
575
|
+
pose.layers = layers(0, roll, 0, roll);
|
|
576
|
+
}
|
|
577
|
+
else if (shape === "cloud") {
|
|
578
|
+
// It rains: a drop every 0.6 s, forming, falling.
|
|
579
|
+
const drops = [];
|
|
580
|
+
for (let k = 0; k < 2; k++) {
|
|
581
|
+
const born = Math.floor(t / 0.6) - k;
|
|
582
|
+
const drop = raindrop((t - born * 0.6) / 1, [-0.32, 0.12, -0.06, 0.34][((born % 4) + 4) % 4], 0, 0.07 * env);
|
|
583
|
+
if (drop)
|
|
584
|
+
drops.push(drop);
|
|
585
|
+
}
|
|
586
|
+
pose.beads = drops;
|
|
587
|
+
pose.dim = 1 - 0.1 * env;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
else if (loop === "rest") {
|
|
591
|
+
pose.bits = zs(t, shape, env);
|
|
592
|
+
const breath = 0.5 - 0.5 * Math.cos((TURN * t) / 4.5);
|
|
593
|
+
if (shape === "cube")
|
|
594
|
+
pose.split = 0.3 * env * breath;
|
|
595
|
+
else if (shape === "pyramid")
|
|
596
|
+
pose.pieces = [0, 0, 0.03 * env * breath, 0];
|
|
597
|
+
else if (shape === "cloud")
|
|
598
|
+
pose.puffs = puffs(() => [0, 0, 0, 1 - 0.04 * env]);
|
|
599
|
+
}
|
|
600
|
+
else if (loop === "worry") {
|
|
601
|
+
const burst = Math.max(0, Math.sin(t * 2.1)) ** 6;
|
|
602
|
+
if (shape === "sphere")
|
|
603
|
+
pose.wrinkle = 0.3 * env * burst;
|
|
604
|
+
else if (shape === "pyramid")
|
|
605
|
+
pose.pieces = [0, 0, 0.018 * env * burst * Math.abs(Math.sin(t * 60)), 0];
|
|
606
|
+
else if (shape === "cloud") {
|
|
607
|
+
pose.dim = 1 - 0.2 * env;
|
|
608
|
+
const q = phaseOf(t, 1.5);
|
|
609
|
+
const drop = raindrop(q / 0.7, 0.1, 0, 0.06 * env);
|
|
610
|
+
if (drop)
|
|
611
|
+
pose.beads = [drop];
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return pose;
|
|
615
|
+
}
|
|
616
|
+
export { REST };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The curves the figures move by (figure-motion.ts and figure-acts.ts): easing,
|
|
3
|
+
* keyframes, bumps and wobbles, each a pure function of how far through something is.
|
|
4
|
+
*/
|
|
5
|
+
export declare const clamp01: (x: number) => number;
|
|
6
|
+
/** Where `p` is between `from` and `to`, 0 to 1. */
|
|
7
|
+
export declare const span: (p: number, from: number, to: number) => number;
|
|
8
|
+
export declare const easeOut: (x: number) => number;
|
|
9
|
+
export declare const easeIn: (x: number) => number;
|
|
10
|
+
export declare const easeInOut: (x: number) => number;
|
|
11
|
+
/** Past the end and back: an overshoot that settles. */
|
|
12
|
+
export declare const easeOutBack: (x: number, s?: number) => number;
|
|
13
|
+
/** Up and down again, 0 at both ends and 1 in the middle. */
|
|
14
|
+
export declare const bump: (x: number) => number;
|
|
15
|
+
/** A ball's arc, 0 at both ends and 1 in the middle. */
|
|
16
|
+
export declare const arc: (x: number) => number;
|
|
17
|
+
/** A wobble that dies away: `cycles` swings over 0–1. */
|
|
18
|
+
export declare const wobble: (x: number, cycles: number) => number;
|
|
19
|
+
/** A value through keyframes, eased in and out between each: [p, value] pairs, p rising. */
|
|
20
|
+
export declare function keys(p: number, frames: [number, number][]): number;
|
|
21
|
+
/** In and out again, held between: 0 to 1 from `start` to `full`, back to 0 from `leave` to `end`, eased both ways. */
|
|
22
|
+
export declare const hold: (p: number, start: number, full: number, leave: number, end: number) => number;
|
|
23
|
+
/** Into a morph and out again, held at the top: 0 to 1 by `full`, back to 0 from `leave` to `end`, overshooting on the way in. */
|
|
24
|
+
export declare const into: (p: number, start: number, full: number, leave: number, end?: number) => number;
|
|
25
|
+
/** A number from a seed and an index, the same every time, 0 to 1. */
|
|
26
|
+
export declare const noise: (seed: number, index: number) => number;
|
|
27
|
+
/** Where `t` is in a cycle of `period`, 0 to 1. */
|
|
28
|
+
export declare const phaseOf: (t: number, period: number) => number;
|
|
29
|
+
/** A whole turn in radians. */
|
|
30
|
+
export declare const TURN: number;
|