@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.
@@ -0,0 +1,3 @@
1
+ /** One frame loop for every moving figure on the page (AgentFigure.tsx): their eyes tween, blink and glance on it. */
2
+ /** Runs `step` every frame, with the seconds since the last and the time now, until the returned function is called. */
3
+ export declare function animate(step: (dt: number, now: number) => void): () => void;
@@ -0,0 +1,21 @@
1
+ /** One frame loop for every moving figure on the page (AgentFigure.tsx): their eyes tween, blink and glance on it. */
2
+ const steps = new Set();
3
+ let frame = 0;
4
+ let last = 0;
5
+ function tick(now) {
6
+ const dt = Math.min(0.05, last ? (now - last) / 1000 : 1 / 60);
7
+ last = now;
8
+ for (const step of steps)
9
+ step(dt, now);
10
+ frame = steps.size ? requestAnimationFrame(tick) : 0;
11
+ if (!frame)
12
+ last = 0;
13
+ }
14
+ /** Runs `step` every frame, with the seconds since the last and the time now, until the returned function is called. */
15
+ export function animate(step) {
16
+ steps.add(step);
17
+ frame ||= requestAnimationFrame(tick);
18
+ return () => {
19
+ steps.delete(step);
20
+ };
21
+ }
@@ -0,0 +1,76 @@
1
+ import { type Loop, type Once, type Shape } from "./figure.ts";
2
+ import { combine, type Pose, REST } from "./figure-pose.ts";
3
+ import { type Move } from "./rubik.ts";
4
+ /**
5
+ * How the figures move: each move and loop as a pure function of time, so the live figure,
6
+ * a still one and the tests all agree on where it is. Moves and loops give a pose
7
+ * (figure-pose.ts): what's added to how the figure holds itself at rest. The moves
8
+ * anything can make are here; what each shape does its own way (petted, a run finished, asleep…)
9
+ * is figure-acts.ts.
10
+ *
11
+ * The moves are character animation's old rules: a crouch before a jump and a squash when it
12
+ * lands, a stretch as it rises, an overshoot that settles, a beat held at the top.
13
+ */
14
+ export { combine, type Pose, REST };
15
+ /** How each shape stands at rest, turned a little so its sides show (the sphere has none to show). */
16
+ export declare const REST_YAW: Record<Shape, number>;
17
+ /** How far a turn takes each shape round: a cube or a pyramid shows its next face, a block its other side (its ends would be another creature); a sphere rolls away and back, and a cloud drifts. */
18
+ export declare const TURN_BY: Record<Shape, number>;
19
+ /** When, through a turn, the eyes move to the face now in front: while they're shut. */
20
+ export declare const SWITCH_AT = 0.45;
21
+ /** Where a move has got to `p` of the way through (0 to 1), for a shape, at an energy's amplitude. */
22
+ export declare function moveAt(move: Once, p: number, shape: Shape, amp?: number): Pose;
23
+ /**
24
+ * How the body moves in a loop at `t` seconds, any shape: breathing is the Aura's own swell; at
25
+ * work it bobs; thinking (but for the cube, which solves itself instead) and floating it drifts and
26
+ * sways; asleep it sinks a little; worried it trembles now and then.
27
+ */
28
+ export declare function bodyLoopAt(loop: Loop, t: number, shape: Shape, amp?: number): Pose;
29
+ /** Where a loop is at `t` seconds: the body's part, and the shape's own (`actLoopAt`) at full strength. */
30
+ export declare function loopAt(loop: Loop, t: number, shape: Shape, amp?: number): Pose;
31
+ /**
32
+ * A shape's own loop act as it plays (the cube's Rubik's turns, the block's cryptex…): which loop,
33
+ * when it began (ms, on the page's clock), and when it was told to stop turning, if it was.
34
+ */
35
+ export type LoopAct = {
36
+ loop: Loop;
37
+ from: number;
38
+ stop: number | null;
39
+ };
40
+ /** How often a loop act comes back to where it started, in seconds at lively energy: the thinking cube's round, or the shape's own (`QUANTUM`). */
41
+ export declare const quantumOf: (loop: Loop, shape: Shape) => number;
42
+ /** When a loop act told to stop at `now` stops turning: the next time it's back where it started (now, if nothing of it turns). */
43
+ export declare function stopFor(act: LoopAct, now: number, shape: Shape, speed: number): number;
44
+ /**
45
+ * A loop act at `now`: its clock (seconds, at lively energy) and how strong it is, coming in over
46
+ * `ENVELOPE.in` and going out over `ENVELOPE.out` once it has stopped turning; null once it's gone.
47
+ * Its clock stands still from its stop, so whatever it turns stays home while it fades.
48
+ */
49
+ export declare function loopActAt(act: LoopAct, now: number, shape: Shape, speed: number): {
50
+ t: number;
51
+ env: number;
52
+ } | null;
53
+ /** The thinking cube's timing, in seconds: it splits, makes its moves (a scramble, then its solution), holds, joins and rests. */
54
+ export declare const THINKING: {
55
+ readonly split: 0.4;
56
+ readonly move: 0.46;
57
+ readonly pause: 0.12;
58
+ readonly moves: 4;
59
+ readonly hold: 0.35;
60
+ readonly join: 0.4;
61
+ readonly rest: 1;
62
+ };
63
+ export declare const THINKING_CYCLE: number;
64
+ /**
65
+ * Where the thinking cube is at `t` seconds: how far it has split into cubelets, the moves done
66
+ * so far this round (to be applied to a solved cube), and the move under way with how far it has
67
+ * turned (radians). Each round scrambles by its own moves and solves itself by the end.
68
+ */
69
+ export declare function thinkingAt(t: number, seed: number): {
70
+ split: number;
71
+ done: Move[];
72
+ turning: {
73
+ move: Move;
74
+ angle: number;
75
+ } | null;
76
+ };
@@ -0,0 +1,260 @@
1
+ import { REACTIONS } from "./figure.js";
2
+ import { actAt, actLoopAt, ENVELOPE, QUANTUM } from "./figure-acts.js";
3
+ import { arc, bump, clamp01, easeInOut, easeOut, easeOutBack, into, keys, span, wobble } from "./figure-curves.js";
4
+ import { combine, REST } from "./figure-pose.js";
5
+ import { thinkingMoves } from "./rubik.js";
6
+ /**
7
+ * How the figures move: each move and loop as a pure function of time, so the live figure,
8
+ * a still one and the tests all agree on where it is. Moves and loops give a pose
9
+ * (figure-pose.ts): what's added to how the figure holds itself at rest. The moves
10
+ * anything can make are here; what each shape does its own way (petted, a run finished, asleep…)
11
+ * is figure-acts.ts.
12
+ *
13
+ * The moves are character animation's old rules: a crouch before a jump and a squash when it
14
+ * lands, a stretch as it rises, an overshoot that settles, a beat held at the top.
15
+ */
16
+ export { combine, REST };
17
+ /** How each shape stands at rest, turned a little so its sides show (the sphere has none to show). */
18
+ export const REST_YAW = { cube: -18, sphere: 0, pyramid: -20, block: -12, cloud: -10 };
19
+ /** How far a turn takes each shape round: a cube or a pyramid shows its next face, a block its other side (its ends would be another creature); a sphere rolls away and back, and a cloud drifts. */
20
+ export const TURN_BY = { cube: 90, sphere: 0, pyramid: 90, block: 180, cloud: 0 };
21
+ /** When, through a turn, the eyes move to the face now in front: while they're shut. */
22
+ export const SWITCH_AT = 0.45;
23
+ const isReaction = (move) => REACTIONS.includes(move);
24
+ // ---------------------------------------------------------------------------------------------
25
+ // Moves
26
+ /** Where a move has got to `p` of the way through (0 to 1), for a shape, at an energy's amplitude. */
27
+ export function moveAt(move, p, shape, amp = 1) {
28
+ if (isReaction(move))
29
+ return actAt(move, p, shape, amp);
30
+ const pose = { ...REST };
31
+ const sphere = shape === "sphere";
32
+ switch (move) {
33
+ case "hop": {
34
+ pose.squash = keys(p, [
35
+ [0, 1],
36
+ [0.16, 1 - 0.14 * amp],
37
+ [0.28, 1 + 0.1 * amp],
38
+ [0.5, 1],
39
+ [0.7, 1 + 0.05 * amp],
40
+ [0.78, 1 - 0.12 * amp],
41
+ [0.9, 1 + 0.03 * amp],
42
+ [1, 1],
43
+ ]);
44
+ pose.lift = 0.34 * amp * arc(span(p, 0.2, 0.76));
45
+ pose.pitch = keys(p, [
46
+ [0, 0],
47
+ [0.16, -5],
48
+ [0.4, 7 * amp],
49
+ [0.78, -4],
50
+ [1, 0],
51
+ ]);
52
+ break;
53
+ }
54
+ case "bounce": {
55
+ pose.squash = keys(p, [
56
+ [0, 1],
57
+ [0.22, 1 - 0.07 * amp],
58
+ [0.5, 1 + 0.04 * amp],
59
+ [0.75, 1 - 0.015 * amp],
60
+ [1, 1],
61
+ ]);
62
+ pose.lift = 0.06 * amp * arc(span(p, 0.25, 0.72));
63
+ break;
64
+ }
65
+ case "spin": {
66
+ pose.yaw = 360 * easeInOut(span(p, 0.08, 0.9)) - 14 * amp * bump(span(p, 0, 0.16));
67
+ pose.lift = 0.14 * amp * arc(span(p, 0.12, 0.88));
68
+ pose.squash = keys(p, [
69
+ [0, 1],
70
+ [0.1, 1 - 0.06 * amp],
71
+ [0.3, 1 + 0.04 * amp],
72
+ [0.85, 1],
73
+ [0.92, 1 - 0.05 * amp],
74
+ [1, 1],
75
+ ]);
76
+ break;
77
+ }
78
+ case "turn": {
79
+ if (sphere) {
80
+ // A sphere rolls a little way and back, its eyes carried round and catching up.
81
+ pose.x = 0.26 * amp * bump(p);
82
+ pose.roll = ((pose.x / 0.8) * 180) / Math.PI;
83
+ break;
84
+ }
85
+ if (shape === "cloud") {
86
+ // A cloud drifts off a little way and back, leaning into it.
87
+ pose.x = 0.24 * amp * bump(p);
88
+ pose.roll = -6 * amp * Math.sin(2 * Math.PI * clamp01(p));
89
+ pose.lift = 0.04 * amp * bump(p);
90
+ break;
91
+ }
92
+ pose.yaw = TURN_BY[shape] * easeOutBack(span(p, 0.06, 0.82), 1.4);
93
+ pose.lift = 0.05 * amp * bump(span(p, 0.06, 0.6));
94
+ pose.shut = span(p, 0.1, 0.3) - span(p, 0.55, 0.75);
95
+ break;
96
+ }
97
+ case "flip": {
98
+ pose.pitch = -360 * easeInOut(span(p, 0.14, 0.82));
99
+ pose.lift = 0.42 * amp * arc(span(p, 0.12, 0.86));
100
+ pose.squash = keys(p, [
101
+ [0, 1],
102
+ [0.1, 1 - 0.14 * amp],
103
+ [0.2, 1 + 0.08 * amp],
104
+ [0.8, 1],
105
+ [0.88, 1 - 0.1 * amp],
106
+ [1, 1],
107
+ ]);
108
+ break;
109
+ }
110
+ case "nod": {
111
+ pose.pitch = -17 * amp * Math.sin(4 * Math.PI * p) * (1 - p) ** 0.5;
112
+ pose.lift = -0.012 * amp * bump(p);
113
+ break;
114
+ }
115
+ case "shake": {
116
+ pose.yaw = 24 * amp * wobble(p, 2.5);
117
+ break;
118
+ }
119
+ case "wave": {
120
+ pose.roll = 12 * amp * wobble(p, 2);
121
+ pose.yaw = -9 * amp * wobble(span(p, 0.06, 1), 2);
122
+ pose.squash = 1 + 0.04 * amp * wobble(p, 4);
123
+ break;
124
+ }
125
+ case "pop": {
126
+ // A star for a beat, twirling into it, eyes starry.
127
+ const star = into(p, 0.1, 0.32, 0.72, 0.95);
128
+ pose.morphTo = "star";
129
+ pose.morph = clamp01(star);
130
+ pose.yaw = 360 * easeInOut(span(p, 0.08, 0.5));
131
+ pose.lift = 0.16 * amp * arc(span(p, 0.06, 0.55));
132
+ pose.squash = keys(p, [
133
+ [0, 1],
134
+ [0.08, 1 - 0.1 * amp],
135
+ [0.2, 1 + 0.06 * amp],
136
+ [0.5, 1],
137
+ ]);
138
+ pose.glyph = "star";
139
+ pose.glyphMix = clamp01(span(p, 0.14, 0.3) - span(p, 0.7, 0.88));
140
+ break;
141
+ }
142
+ case "melt": {
143
+ // Into a blob that sags, then pulled back together with a bounce.
144
+ const blob = into(p, 0, 0.32, 0.68, 0.92);
145
+ pose.morphTo = "blob";
146
+ pose.morph = clamp01(blob);
147
+ pose.squash = 1 - 0.16 * clamp01(blob) + 0.05 * amp * wobble(span(p, 0.86, 1), 1.5);
148
+ pose.roll = 4 * amp * Math.sin(p * Math.PI * 3) * clamp01(blob);
149
+ break;
150
+ }
151
+ case "love": {
152
+ // A heart that beats twice, the eyes happy.
153
+ const heart = into(p, 0.08, 0.3, 0.74, 0.95);
154
+ pose.morphTo = "heart";
155
+ pose.morph = clamp01(heart);
156
+ pose.pulse = 1 + 0.08 * clamp01(heart) * Math.max(0, Math.sin(2 * Math.PI * 2.2 * span(p, 0.3, 0.74))) ** 3;
157
+ pose.lift = 0.05 * amp * bump(span(p, 0.05, 0.4));
158
+ pose.glyph = "arc";
159
+ pose.glyphMix = clamp01(span(p, 0.1, 0.24) - span(p, 0.74, 0.9));
160
+ break;
161
+ }
162
+ }
163
+ return pose;
164
+ }
165
+ // ---------------------------------------------------------------------------------------------
166
+ // Loops
167
+ /**
168
+ * How the body moves in a loop at `t` seconds, any shape: breathing is the Aura's own swell; at
169
+ * work it bobs; thinking (but for the cube, which solves itself instead) and floating it drifts and
170
+ * sways; asleep it sinks a little; worried it trembles now and then.
171
+ */
172
+ export function bodyLoopAt(loop, t, shape, amp = 1) {
173
+ const pose = { ...REST };
174
+ if (loop === "work") {
175
+ pose.lift = 0.025 * amp * Math.abs(Math.sin(t * 5.2));
176
+ pose.squash = 1 - 0.015 * amp * Math.cos(t * 10.4);
177
+ }
178
+ else if (loop === "float" || (loop === "think" && shape !== "cube")) {
179
+ pose.lift = 0.06 * amp * (0.5 + 0.5 * Math.sin(t * 1.7));
180
+ pose.yaw = 9 * amp * Math.sin(t * 0.8);
181
+ pose.roll = 3 * amp * Math.sin(t * 1.1 + 1);
182
+ }
183
+ else if (loop === "rest") {
184
+ pose.lift = shape === "cloud" ? -0.06 : 0;
185
+ pose.squash = 1 - 0.012 * (0.5 + 0.5 * Math.sin(t * 1.4));
186
+ pose.x = shape === "cloud" ? 0.03 * Math.sin(t * 0.5) : 0;
187
+ }
188
+ else if (loop === "worry") {
189
+ // A tremble, in bursts.
190
+ const burst = Math.max(0, Math.sin(t * 2.1)) ** 6;
191
+ pose.x = 0.012 * amp * burst * Math.sin(t * 71);
192
+ pose.roll = 1.6 * amp * burst * Math.sin(t * 53 + 1);
193
+ }
194
+ return pose;
195
+ }
196
+ /** Where a loop is at `t` seconds: the body's part, and the shape's own (`actLoopAt`) at full strength. */
197
+ export function loopAt(loop, t, shape, amp = 1) {
198
+ return combine(bodyLoopAt(loop, t, shape, amp), actLoopAt(loop, t, shape, amp, 1));
199
+ }
200
+ /** How often a loop act comes back to where it started, in seconds at lively energy: the thinking cube's round, or the shape's own (`QUANTUM`). */
201
+ export const quantumOf = (loop, shape) => (loop === "think" && shape === "cube" ? THINKING_CYCLE : QUANTUM[loop][shape]);
202
+ /** When a loop act told to stop at `now` stops turning: the next time it's back where it started (now, if nothing of it turns). */
203
+ export function stopFor(act, now, shape, speed) {
204
+ const quantum = quantumOf(act.loop, shape) * speed * 1000;
205
+ if (!quantum || now <= act.from)
206
+ return Math.max(now, act.from);
207
+ return act.from + Math.ceil((now - act.from) / quantum) * quantum;
208
+ }
209
+ /**
210
+ * A loop act at `now`: its clock (seconds, at lively energy) and how strong it is, coming in over
211
+ * `ENVELOPE.in` and going out over `ENVELOPE.out` once it has stopped turning; null once it's gone.
212
+ * Its clock stands still from its stop, so whatever it turns stays home while it fades.
213
+ */
214
+ export function loopActAt(act, now, shape, speed) {
215
+ if (now < act.from)
216
+ return { t: 0, env: 0 };
217
+ const turns = quantumOf(act.loop, shape) > 0;
218
+ const clock = turns && act.stop !== null ? Math.min(now, act.stop) : now;
219
+ const t = (clock - act.from) / 1000 / speed;
220
+ const rise = span((now - act.from) / 1000, 0, ENVELOPE.in * speed);
221
+ if (act.stop === null)
222
+ return { t, env: rise };
223
+ const fall = 1 - span((now - act.stop) / 1000, 0, ENVELOPE.out * speed);
224
+ if (now >= act.stop && fall <= 0)
225
+ return null;
226
+ return { t, env: Math.min(rise, fall) };
227
+ }
228
+ // ---------------------------------------------------------------------------------------------
229
+ // The cube's Rubik's turns, while it thinks
230
+ /** The thinking cube's timing, in seconds: it splits, makes its moves (a scramble, then its solution), holds, joins and rests. */
231
+ export const THINKING = { split: 0.4, move: 0.46, pause: 0.12, moves: 4, hold: 0.35, join: 0.4, rest: 1 };
232
+ export const THINKING_CYCLE = THINKING.split + THINKING.moves * 2 * (THINKING.move + THINKING.pause) + THINKING.hold + THINKING.join + THINKING.rest;
233
+ /**
234
+ * Where the thinking cube is at `t` seconds: how far it has split into cubelets, the moves done
235
+ * so far this round (to be applied to a solved cube), and the move under way with how far it has
236
+ * turned (radians). Each round scrambles by its own moves and solves itself by the end.
237
+ */
238
+ export function thinkingAt(t, seed) {
239
+ const round = Math.floor(t / THINKING_CYCLE);
240
+ let at = t - round * THINKING_CYCLE;
241
+ const moves = thinkingMoves(seed + round * 7919, THINKING.moves);
242
+ const total = moves.length * (THINKING.move + THINKING.pause);
243
+ let split = 1;
244
+ if (at < THINKING.split)
245
+ split = easeOut(at / THINKING.split);
246
+ const turningFrom = THINKING.split;
247
+ const joinFrom = turningFrom + total + THINKING.hold;
248
+ if (at >= joinFrom)
249
+ split = 1 - easeInOut(span(at, joinFrom, joinFrom + THINKING.join));
250
+ at -= turningFrom;
251
+ if (at < 0 || at >= total)
252
+ return { split, done: at < 0 ? [] : moves, turning: null };
253
+ const step = THINKING.move + THINKING.pause;
254
+ const index = Math.floor(at / step);
255
+ const into = (at - index * step) / THINKING.move;
256
+ if (into >= 1)
257
+ return { split, done: moves.slice(0, index + 1), turning: null };
258
+ const move = moves[index];
259
+ return { split, done: moves.slice(0, index), turning: { move, angle: (easeInOut(into) * move.turns * Math.PI) / 2 } };
260
+ }
@@ -0,0 +1,36 @@
1
+ import type { Mood } from "./figure.ts";
2
+ /**
3
+ * The figures on a page notice each other: when one is petted, finishes or fails, the
4
+ * others turn to look; and now and then one with nothing to do glances at one that's busy, which
5
+ * sometimes looks back. Pure: who to look at, and which way.
6
+ */
7
+ /** Where a figure is on the page, in pixels. */
8
+ export type Box = {
9
+ x: number;
10
+ y: number;
11
+ width: number;
12
+ height: number;
13
+ };
14
+ /** A figure as the others see it: its state, where it is, its moment (an event still going, ms), and who it's looking at. */
15
+ export type Peer = {
16
+ id: number;
17
+ mood: Mood;
18
+ box: Box | null;
19
+ event: number;
20
+ watching: number | null;
21
+ };
22
+ /** How long the others watch a figure's moment, in ms. */
23
+ export declare const WATCH_FOR = 1600;
24
+ /** Busy: at work or setting up. */
25
+ export declare const isBusy: (mood: Mood) => mood is "thinking" | "working";
26
+ /** The figure whose moment the others should turn to: the latest still going, never itself, and nobody asleep looks. */
27
+ export declare function eventToWatch(self: Peer, peers: Iterable<Peer>, now: number): Peer | null;
28
+ /** Where to look, −1 to 1 each way, to see one box from another: straight at it, as far as the eyes and head go. */
29
+ export declare function gazeAt(from: Box, to: Box, most?: number): {
30
+ x: number;
31
+ y: number;
32
+ };
33
+ /** A busy figure to glance at, at random, for one that isn't busy itself (or asleep); null if there's none. */
34
+ export declare function glanceTarget(self: Peer, peers: readonly Peer[], random: () => number): Peer | null;
35
+ /** Who's looking at this figure, if it might look back: a busy figure caught being watched. */
36
+ export declare function watchedBy(self: Peer, peers: Iterable<Peer>): Peer | null;
@@ -0,0 +1,37 @@
1
+ /** How long the others watch a figure's moment, in ms. */
2
+ export const WATCH_FOR = 1600;
3
+ /** Busy: at work or setting up. */
4
+ export const isBusy = (mood) => mood === "working" || mood === "thinking";
5
+ /** The figure whose moment the others should turn to: the latest still going, never itself, and nobody asleep looks. */
6
+ export function eventToWatch(self, peers, now) {
7
+ if (self.mood === "sleep")
8
+ return null;
9
+ let latest = null;
10
+ for (const peer of peers)
11
+ if (peer.id !== self.id && peer.event > now && peer.box && (!latest || peer.event > latest.event))
12
+ latest = peer;
13
+ return latest;
14
+ }
15
+ /** Where to look, −1 to 1 each way, to see one box from another: straight at it, as far as the eyes and head go. */
16
+ export function gazeAt(from, to, most = 0.95) {
17
+ const dx = to.x + to.width / 2 - (from.x + from.width / 2);
18
+ const dy = to.y + to.height / 2 - (from.y + from.height / 2);
19
+ const distance = Math.hypot(dx, dy) || 1;
20
+ return { x: (dx / distance) * most, y: (dy / distance) * most * 0.7 };
21
+ }
22
+ /** A busy figure to glance at, at random, for one that isn't busy itself (or asleep); null if there's none. */
23
+ export function glanceTarget(self, peers, random) {
24
+ if (self.mood === "sleep" || isBusy(self.mood))
25
+ return null;
26
+ const busy = peers.filter((peer) => peer.id !== self.id && peer.box && isBusy(peer.mood));
27
+ return busy.length ? busy[Math.floor(random() * busy.length) % busy.length] : null;
28
+ }
29
+ /** Who's looking at this figure, if it might look back: a busy figure caught being watched. */
30
+ export function watchedBy(self, peers) {
31
+ if (!isBusy(self.mood))
32
+ return null;
33
+ for (const peer of peers)
34
+ if (peer.id !== self.id && peer.watching === self.id && peer.box)
35
+ return peer;
36
+ return null;
37
+ }
@@ -0,0 +1,77 @@
1
+ import type { Glyph } from "./figure.ts";
2
+ import { type Ball, type BitKind, type Morph } from "./figure-space.ts";
3
+ /**
4
+ * A figure's pose: what's added to how it holds itself at rest, at one moment of a move or a loop
5
+ * (figure-motion.ts, figure-acts.ts). The body's: turns in degrees (toward the picture's
6
+ * right, tipped up, leaning right), a lift off the ground and a slide sideways in world units (the
7
+ * figure's box is 2 wide), a squash (its height's share; its width keeps the volume), a pulse (all
8
+ * of it), a morph into another shape, the eyes become another shape for a moment, a blink of its
9
+ * own, the eyes' size and the colour's strength. Then each shape's own parts: the cube's cubelets
10
+ * and the block's dials parting and turning, the cube's cubelets flying apart, the pyramid's
11
+ * slabs sliding and its capstone lifting and spinning, the pyramid leaning like jelly, the sphere
12
+ * swelling and shrinking, with moons and droplets beside it (or a cloud's raindrops), the cloud's
13
+ * puffs moving, a ripple, a dent or wrinkles on the surface, the colour rolling round, bits in front
14
+ * (hearts, stars, z's, sparks), the cloud's rainbow and its lightning. Pure data.
15
+ */
16
+ /** Layers turning about one axis: how far each of the three (−1, 0, 1) has turned, in radians. */
17
+ export type Layers = {
18
+ axis: 0 | 1 | 2;
19
+ angles: [number, number, number];
20
+ };
21
+ /** A bit drawn in front of the figure: where in the picture (world units from its middle), how big, how opaque. */
22
+ export type Bit = {
23
+ kind: Exclude<BitKind, "none">;
24
+ x: number;
25
+ y: number;
26
+ size: number;
27
+ alpha: number;
28
+ };
29
+ export type Pose = {
30
+ yaw: number;
31
+ pitch: number;
32
+ roll: number;
33
+ lift: number;
34
+ x: number;
35
+ squash: number;
36
+ pulse: number;
37
+ morph: number;
38
+ morphTo: Morph;
39
+ glyph?: Glyph;
40
+ glyphMix: number;
41
+ shut: number;
42
+ /** The eyes' size, as a share. */
43
+ eyes: number;
44
+ /** How much colour and light it has, as a share: a failure drains it for a moment. */
45
+ dim: number;
46
+ split: number;
47
+ spread: number;
48
+ layers: Layers | null;
49
+ /** The pyramid apart: the middle slab's slide, the capstone's slide, lift and spin (radians). */
50
+ pieces: readonly [number, number, number, number];
51
+ shear: number;
52
+ core: number;
53
+ beads: readonly Ball[];
54
+ /** The cloud's puffs, moved (x, y, z) and swollen (w, a share) from where they rest; null where they rest. */
55
+ puffs: readonly Ball[] | null;
56
+ /** A ripple over the surface: its height, and how far it has run (radians). */
57
+ wave: number;
58
+ wavePhase: number;
59
+ dent: number;
60
+ wrinkle: number;
61
+ /** How far its colour has rolled round, in radians. */
62
+ colourRoll: number;
63
+ bits: readonly Bit[];
64
+ /** The cloud's rainbow: how strongly it shows, and how far it has swept across. */
65
+ rainbow: number;
66
+ rainbowSweep: number;
67
+ /** Its lightning: how bright, and which zigzag. */
68
+ bolt: number;
69
+ boltSeed: number;
70
+ };
71
+ export declare const REST: Readonly<Pose>;
72
+ /** A pose from its parts, the rest at rest. */
73
+ export declare const posed: (parts: Partial<Pose>) => Pose;
74
+ /** A move's pose added to another's: turns, lifts and slides add, sizes multiply, the stronger morph, glyph, ripple and flash win. */
75
+ export declare function combine(a: Pose, b: Pose): Pose;
76
+ /** The body's part of a pose, part way from one to another (a loop changing into the next): its turns, lift, slide and squash. */
77
+ export declare function blendBody(a: Pose, b: Pose, t: number): Pose;
@@ -0,0 +1,108 @@
1
+ import { MOST } from "./figure-space.js";
2
+ const NONE = Object.freeze([]);
3
+ export const REST = Object.freeze({
4
+ yaw: 0,
5
+ pitch: 0,
6
+ roll: 0,
7
+ lift: 0,
8
+ x: 0,
9
+ squash: 1,
10
+ pulse: 1,
11
+ morph: 0,
12
+ morphTo: "none",
13
+ glyphMix: 0,
14
+ shut: 0,
15
+ eyes: 1,
16
+ dim: 1,
17
+ split: 0,
18
+ spread: 0,
19
+ layers: null,
20
+ pieces: Object.freeze([0, 0, 0, 0]),
21
+ shear: 0,
22
+ core: 1,
23
+ beads: NONE,
24
+ puffs: null,
25
+ wave: 0,
26
+ wavePhase: 0,
27
+ dent: 0,
28
+ wrinkle: 0,
29
+ colourRoll: 0,
30
+ bits: NONE,
31
+ rainbow: 0,
32
+ rainbowSweep: 0,
33
+ bolt: 0,
34
+ boltSeed: 0,
35
+ });
36
+ /** A pose from its parts, the rest at rest. */
37
+ export const posed = (parts) => ({ ...REST, ...parts });
38
+ const turned = (layers) => (layers ? Math.abs(layers.angles[0]) + Math.abs(layers.angles[1]) + Math.abs(layers.angles[2]) : 0);
39
+ /** Two sets of layers at once: about one axis they add; about two, the one turned further wins. */
40
+ function bothLayers(a, b) {
41
+ if (!a || !b)
42
+ return a ?? b;
43
+ if (a.axis === b.axis)
44
+ return { axis: a.axis, angles: [a.angles[0] + b.angles[0], a.angles[1] + b.angles[1], a.angles[2] + b.angles[2]] };
45
+ return turned(b) > turned(a) ? b : a;
46
+ }
47
+ function bothPuffs(a, b) {
48
+ if (!a || !b)
49
+ return a ?? b;
50
+ return a.map((puff, i) => [puff[0] + b[i][0], puff[1] + b[i][1], puff[2] + b[i][2], puff[3] * b[i][3]]);
51
+ }
52
+ /** The most of a list there's room for, the strongest kept. */
53
+ const most = (list, room, strength) => (list.length <= room ? list : [...list].sort((x, y) => strength(y) - strength(x)).slice(0, room));
54
+ /** A move's pose added to another's: turns, lifts and slides add, sizes multiply, the stronger morph, glyph, ripple and flash win. */
55
+ export function combine(a, b) {
56
+ const morph = b.morph > a.morph ? b : a;
57
+ const glyph = b.glyphMix > a.glyphMix ? b : a;
58
+ const wave = b.wave > a.wave ? b : a;
59
+ const bolt = b.bolt > a.bolt ? b : a;
60
+ return {
61
+ yaw: a.yaw + b.yaw,
62
+ pitch: a.pitch + b.pitch,
63
+ roll: a.roll + b.roll,
64
+ lift: a.lift + b.lift,
65
+ x: a.x + b.x,
66
+ squash: a.squash * b.squash,
67
+ pulse: a.pulse * b.pulse,
68
+ morph: morph.morph,
69
+ morphTo: morph.morphTo,
70
+ glyph: glyph.glyph,
71
+ glyphMix: glyph.glyphMix,
72
+ shut: Math.max(a.shut, b.shut),
73
+ eyes: a.eyes * b.eyes,
74
+ dim: a.dim * b.dim,
75
+ split: Math.max(a.split, b.split),
76
+ spread: Math.max(a.spread, b.spread),
77
+ layers: bothLayers(a.layers, b.layers),
78
+ pieces: [a.pieces[0] + b.pieces[0], a.pieces[1] + b.pieces[1], a.pieces[2] + b.pieces[2], a.pieces[3] + b.pieces[3]],
79
+ shear: a.shear + b.shear,
80
+ core: a.core * b.core,
81
+ beads: most([...a.beads, ...b.beads], MOST.beads, (bead) => bead[3]),
82
+ puffs: bothPuffs(a.puffs, b.puffs),
83
+ wave: wave.wave,
84
+ wavePhase: wave.wavePhase,
85
+ dent: Math.max(a.dent, b.dent),
86
+ wrinkle: Math.max(a.wrinkle, b.wrinkle),
87
+ colourRoll: a.colourRoll + b.colourRoll,
88
+ bits: most([...a.bits, ...b.bits], MOST.bits, (bit) => bit.alpha),
89
+ rainbow: Math.max(a.rainbow, b.rainbow),
90
+ rainbowSweep: Math.max(a.rainbowSweep, b.rainbowSweep),
91
+ bolt: bolt.bolt,
92
+ boltSeed: bolt.boltSeed,
93
+ };
94
+ }
95
+ const lerp = (a, b, t) => a + (b - a) * t;
96
+ /** The body's part of a pose, part way from one to another (a loop changing into the next): its turns, lift, slide and squash. */
97
+ export function blendBody(a, b, t) {
98
+ return {
99
+ ...b,
100
+ yaw: lerp(a.yaw, b.yaw, t),
101
+ pitch: lerp(a.pitch, b.pitch, t),
102
+ roll: lerp(a.roll, b.roll, t),
103
+ lift: lerp(a.lift, b.lift, t),
104
+ x: lerp(a.x, b.x, t),
105
+ squash: lerp(a.squash, b.squash, t),
106
+ pulse: lerp(a.pulse, b.pulse, t),
107
+ };
108
+ }