@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,204 @@
1
+ /**
2
+ * The figures: a solid (a cube, a sphere, a pyramid, a block, a cloud) in an iridescent material,
3
+ * its colours its colourway's three, and two eyes painted on its face. Nothing else: no mouth, no hats, arms or legs. They say everything with their eyes and the
4
+ * way they hold and move themselves, as Anki's Cozmo does: lids cut into the eyes for each
5
+ * expression (an upper lid that slopes, a lower one that rises in an arc), or the eyes become a
6
+ * shape (^ ^, − −, stars, hearts, × ×, > <), the head turns and tips with them, and the body
7
+ * moves: it hops, spins, turns to a new face, flips, nods, shakes, wobbles, turns into a star, a
8
+ * blob or a heart for a moment. Each shape also has acts of its own for what happens to it
9
+ * (figure-acts.ts): the cube solves itself like a Rubik's cube while it thinks, the
10
+ * block turns its dials like a cryptex, the pyramid's capstone floats, the sphere has moons, the
11
+ * cloud boils; and so on at work, when a run finishes or fails, when it's petted, put to sleep or
12
+ * woken.
13
+ *
14
+ * Pure. A figure is its shape, colours, energy (calm, lively, bouncy: how big and quick it moves)
15
+ * and habit (what it does when nothing's happening). `FACES` says how each expression sets the
16
+ * eyes and the head, `LOOKS` how its Aura glows and flows, `MOOD` which expression and motion each
17
+ * of its states shows; figure-motion.ts moves it, figure-scene.ts turns all of it into what the
18
+ * shader draws. `svgOf` writes a flat still for anywhere outside the page. `figureFor` makes a
19
+ * figure from any word, the same every time.
20
+ */
21
+ export declare const SHAPES: readonly ["cube", "sphere", "pyramid", "block", "cloud"];
22
+ export declare const ENERGIES: readonly ["calm", "lively", "bouncy"];
23
+ export declare const HABITS: readonly ["look", "hum", "nap", "spin", "wave", "morph"];
24
+ /** Eight colourways, by number (`COLOURWAYS`). */
25
+ export declare const PALETTES: readonly [1, 2, 3, 4, 5, 6, 7, 8];
26
+ export type Shape = (typeof SHAPES)[number];
27
+ export type Energy = (typeof ENERGIES)[number];
28
+ export type Habit = (typeof HABITS)[number];
29
+ export type Palette = (typeof PALETTES)[number];
30
+ export type Figure = {
31
+ shape: Shape;
32
+ energy: Energy;
33
+ habit: Habit;
34
+ palette: Palette;
35
+ };
36
+ /** What its eyes say. */
37
+ export declare const EXPRESSIONS: readonly ["neutral", "happy", "sad", "tired", "thinking", "working", "listening", "speaking", "sleeping", "surprised", "trouble", "love", "excited", "failed"];
38
+ export type Expression = (typeof EXPRESSIONS)[number];
39
+ /** What its eyes can be, in the order the shader numbers them: a rectangle (lids cut into it), ^ ^, − −, stars, hearts, × ×, > <. */
40
+ export declare const GLYPHS: readonly ["rect", "arc", "line", "star", "heart", "cross", "squeeze"];
41
+ export type Glyph = (typeof GLYPHS)[number];
42
+ /** How it moves while it stays in a state: still but breathing, at work, thinking, floating, asleep, worried. */
43
+ export declare const LOOPS: readonly ["breathe", "work", "think", "float", "rest", "worry"];
44
+ export type Loop = (typeof LOOPS)[number];
45
+ /** The moves anything can make once, then back to its loop. */
46
+ export declare const MOVES: readonly ["hop", "bounce", "spin", "turn", "flip", "nod", "shake", "wave", "pop", "melt", "love"];
47
+ /** What it does when something happens to it, each shape its own way (figure-acts.ts): petted, a run finished or failed, work starting, switched off or on. */
48
+ export declare const REACTIONS: readonly ["pet", "celebrate", "fail", "start", "doze", "wake"];
49
+ export declare const ONCE: readonly ["hop", "bounce", "spin", "turn", "flip", "nod", "shake", "wave", "pop", "melt", "love", "pet", "celebrate", "fail", "start", "doze", "wake"];
50
+ export type Once = (typeof ONCE)[number];
51
+ export type Reaction = (typeof REACTIONS)[number];
52
+ export type Motion = Loop | Once;
53
+ export declare const MOTIONS: readonly Motion[];
54
+ /** How long each move takes, in ms at lively energy. */
55
+ export declare const ONCE_FOR: Record<Once, number>;
56
+ /** What the agent it stands for is doing: waiting, at work, thinking (setting up), done (something to look at), in trouble, or switched off. */
57
+ export type Mood = "idle" | "working" | "thinking" | "happy" | "trouble" | "sleep";
58
+ export declare const MOODS: readonly Mood[];
59
+ /** What each state shows: its expression, the loop it moves in, and a move it makes now and then (every so many ms, at random between two). */
60
+ export declare const MOOD: Record<Mood, {
61
+ expression: Expression;
62
+ loop: Loop;
63
+ now?: Once;
64
+ every?: [number, number];
65
+ }>;
66
+ /**
67
+ * What it does as its state changes, the moment it changes: a run finished, it celebrates; one
68
+ * failed, it fails; work starting, it gets going; switched off, it dozes off; switched on, it
69
+ * wakes; its finished run seen to, a nod. Nothing for a change it was already making (setting up,
70
+ * then working).
71
+ */
72
+ export declare function reactionTo(from: Mood, to: Mood): Once | null;
73
+ /**
74
+ * The eyes' settings for an expression: the upper lid's edge at each eye's inner and outer side, as
75
+ * a share of its height (`li`, `lo`: 0 open, 1 shut), how far a lower lid rises in an arc
76
+ * (`smile`, 0–1), their size, and where they look on the face (`dx`, `dy`, in body units: the
77
+ * cube's face is 1.2 wide).
78
+ */
79
+ export type Lids = {
80
+ li: number;
81
+ lo: number;
82
+ smile: number;
83
+ size: number;
84
+ dx: number;
85
+ dy: number;
86
+ };
87
+ export declare const LID_KEYS: readonly ["li", "lo", "smile", "size", "dx", "dy"];
88
+ /** How it holds its head, in degrees: turned toward the picture's right (`yaw`), tipped up (`pitch`), leaning to the picture's right (`roll`). */
89
+ export type Turn = {
90
+ yaw: number;
91
+ pitch: number;
92
+ roll: number;
93
+ };
94
+ export declare const TURN_KEYS: readonly ["yaw", "pitch", "roll"];
95
+ /** An expression whole: its eyes and its head, what the live figure tweens. */
96
+ export type Face = Lids & Turn;
97
+ export declare const FACE_KEYS: readonly ["li", "lo", "smile", "size", "dx", "dy", "yaw", "pitch", "roll"];
98
+ /**
99
+ * Each expression: what its eyes are (`glyph`), their lids and gaze, how it holds its head (it goes
100
+ * where the eyes look: down when sad, up and aside when thinking, aslant when listening), and how
101
+ * the eyes move on their own: scanning side to side, bobbing as it talks, swelling as it listens;
102
+ * how it blinks.
103
+ */
104
+ export declare const FACES: Record<Expression, Face & {
105
+ glyph: Glyph;
106
+ scan?: boolean;
107
+ bob?: boolean;
108
+ pulse?: boolean;
109
+ blink: "quick" | "slow" | "none";
110
+ }>;
111
+ /** How long a blink takes, by kind, in ms. */
112
+ export declare const BLINK_FOR: {
113
+ readonly quick: 150;
114
+ readonly slow: 320;
115
+ };
116
+ /**
117
+ * How its Aura glows and flows, as the Aura experiment's states did: brightness and halo strength
118
+ * (`energy`), how fast its colour flows (`speed`) and churns (`swirl`), how far its halo reaches,
119
+ * rings travelling out through it (`ripple`), a light running round its rim (`arc`), colour (0
120
+ * grey to 1 full), and how much a breath, speech or a voice heard swells it.
121
+ */
122
+ export type Look = {
123
+ energy: number;
124
+ speed: number;
125
+ swirl: number;
126
+ halo: number;
127
+ ripple: number;
128
+ arc: number;
129
+ saturation: number;
130
+ breath: number;
131
+ talk: number;
132
+ listen: number;
133
+ };
134
+ export declare const LOOK_KEYS: readonly ["energy", "speed", "swirl", "halo", "ripple", "arc", "saturation", "breath", "talk", "listen"];
135
+ export declare const LOOKS: Record<Expression, Look>;
136
+ /** A switched-off figure's look, whatever its face: grey and dim, barely breathing. */
137
+ export declare const OFF_LOOK: Look;
138
+ export declare const DEFAULT_FIGURE: Figure;
139
+ /** How much bigger, and how much quicker, each energy moves. */
140
+ export declare const ENERGY: Record<Energy, {
141
+ amp: number;
142
+ speed: number;
143
+ }>;
144
+ /** A colourway: its colour, a lighter neighbour, a deeper one, any CSS colours. Its Aura passes through all three. */
145
+ export type Colourway = {
146
+ main: string;
147
+ light: string;
148
+ deep: string;
149
+ };
150
+ /** The eight colourways: blue, green, sun, pink, violet, teal, orange and red. */
151
+ export declare const COLOURWAYS: Record<Palette, Colourway>;
152
+ /** The eyes' ink: a near black. */
153
+ export declare const INK = "oklch(20% 0.004 265)";
154
+ /** A spring per value: stiff enough to change state in a few tenths of a second, damped just under critical. */
155
+ export declare const SPRING: {
156
+ stiffness: number;
157
+ damping: number;
158
+ };
159
+ export type Sprung = {
160
+ value: number;
161
+ velocity: number;
162
+ };
163
+ export declare function springStep(current: Sprung, target: number, dt: number): Sprung;
164
+ /** A slow breath, −1 to 1, about once every four and a half seconds. */
165
+ export declare const breathAt: (t: number) => number;
166
+ /** Speech, 0 to 1: syllables at four and a half a second, each its own loudness, in phrases with a pause between some. */
167
+ export declare function speechAt(t: number): number;
168
+ /** A voice heard, 0 to 1: louder and softer, never quite still. */
169
+ export declare const voiceAt: (t: number) => number;
170
+ /** How much each signal swells the body, at full strength. */
171
+ export declare const SWELL: {
172
+ breath: number;
173
+ talk: number;
174
+ listen: number;
175
+ };
176
+ /** The body's size, as a share of its resting size, from the sprung amounts and the clock. */
177
+ export declare const swellAt: (look: Pick<Look, "breath" | "talk" | "listen">, t: number) => number;
178
+ /** A 32-bit hash of a word (FNV-1a), the seed of its figure. */
179
+ export declare function hashOf(text: string): number;
180
+ /** A figure made from a word (an agent's name, a shuffle's count): the same word, the same creature. */
181
+ export declare function figureFor(seed: string): Figure;
182
+ /** A figure as it's stored, read back: a flat shape from before as the solid it became, anything missing or unknown the default's part. */
183
+ export declare function readFigure(value: unknown): Figure;
184
+ /** Whether a value is a whole figure, every part known: what a form may save. */
185
+ export declare const isFigure: (value: unknown) => value is Figure;
186
+ /** Each shape seen from the front, on a 100-unit square: its outline, and where its eyes sit (their middle, each eye's width and height, how far each is from the middle). */
187
+ export declare const FLAT: Record<Shape, {
188
+ d: string;
189
+ face: [number, number];
190
+ eye: {
191
+ w: number;
192
+ h: number;
193
+ gap: number;
194
+ };
195
+ }>;
196
+ /**
197
+ * A figure as an SVG file, `size` pixels square, seen from the front: its shape in its colours (a
198
+ * light wash at the top left, the deep colour at the bottom right), its eyes as `expression` has
199
+ * them (a rectangle cut by its upper lid, or a bar for closed and happy eyes). Colours are any CSS
200
+ * colours, its colourway's and `INK` unless given; `id` names its gradient, unique on a page.
201
+ */
202
+ export declare function svgOf(figure: Figure, expression?: Expression, given?: Partial<Colourway & {
203
+ ink: string;
204
+ }>, size?: number, id?: string): string;
package/dist/figure.js ADDED
@@ -0,0 +1,277 @@
1
+ /**
2
+ * The figures: a solid (a cube, a sphere, a pyramid, a block, a cloud) in an iridescent material,
3
+ * its colours its colourway's three, and two eyes painted on its face. Nothing else: no mouth, no hats, arms or legs. They say everything with their eyes and the
4
+ * way they hold and move themselves, as Anki's Cozmo does: lids cut into the eyes for each
5
+ * expression (an upper lid that slopes, a lower one that rises in an arc), or the eyes become a
6
+ * shape (^ ^, − −, stars, hearts, × ×, > <), the head turns and tips with them, and the body
7
+ * moves: it hops, spins, turns to a new face, flips, nods, shakes, wobbles, turns into a star, a
8
+ * blob or a heart for a moment. Each shape also has acts of its own for what happens to it
9
+ * (figure-acts.ts): the cube solves itself like a Rubik's cube while it thinks, the
10
+ * block turns its dials like a cryptex, the pyramid's capstone floats, the sphere has moons, the
11
+ * cloud boils; and so on at work, when a run finishes or fails, when it's petted, put to sleep or
12
+ * woken.
13
+ *
14
+ * Pure. A figure is its shape, colours, energy (calm, lively, bouncy: how big and quick it moves)
15
+ * and habit (what it does when nothing's happening). `FACES` says how each expression sets the
16
+ * eyes and the head, `LOOKS` how its Aura glows and flows, `MOOD` which expression and motion each
17
+ * of its states shows; figure-motion.ts moves it, figure-scene.ts turns all of it into what the
18
+ * shader draws. `svgOf` writes a flat still for anywhere outside the page. `figureFor` makes a
19
+ * figure from any word, the same every time.
20
+ */
21
+ export const SHAPES = ["cube", "sphere", "pyramid", "block", "cloud"];
22
+ export const ENERGIES = ["calm", "lively", "bouncy"];
23
+ export const HABITS = ["look", "hum", "nap", "spin", "wave", "morph"];
24
+ /** Eight colourways, by number (`COLOURWAYS`). */
25
+ export const PALETTES = [1, 2, 3, 4, 5, 6, 7, 8];
26
+ /** The flat shapes the figures had before they were solids, read back as the solid each became. */
27
+ const SOLID_OF = { square: "cube", circle: "sphere", triangle: "pyramid", rectangle: "block" };
28
+ /** What its eyes say. */
29
+ export const EXPRESSIONS = ["neutral", "happy", "sad", "tired", "thinking", "working", "listening", "speaking", "sleeping", "surprised", "trouble", "love", "excited", "failed"];
30
+ /** What its eyes can be, in the order the shader numbers them: a rectangle (lids cut into it), ^ ^, − −, stars, hearts, × ×, > <. */
31
+ export const GLYPHS = ["rect", "arc", "line", "star", "heart", "cross", "squeeze"];
32
+ /** How it moves while it stays in a state: still but breathing, at work, thinking, floating, asleep, worried. */
33
+ export const LOOPS = ["breathe", "work", "think", "float", "rest", "worry"];
34
+ /** The moves anything can make once, then back to its loop. */
35
+ export const MOVES = ["hop", "bounce", "spin", "turn", "flip", "nod", "shake", "wave", "pop", "melt", "love"];
36
+ /** What it does when something happens to it, each shape its own way (figure-acts.ts): petted, a run finished or failed, work starting, switched off or on. */
37
+ export const REACTIONS = ["pet", "celebrate", "fail", "start", "doze", "wake"];
38
+ export const ONCE = [...MOVES, ...REACTIONS];
39
+ export const MOTIONS = [...LOOPS, ...ONCE];
40
+ /** How long each move takes, in ms at lively energy. */
41
+ export const ONCE_FOR = {
42
+ hop: 900,
43
+ bounce: 480,
44
+ spin: 1050,
45
+ turn: 850,
46
+ flip: 1100,
47
+ nod: 800,
48
+ shake: 700,
49
+ wave: 1200,
50
+ pop: 1500,
51
+ melt: 1700,
52
+ love: 1500,
53
+ pet: 1400,
54
+ celebrate: 2400,
55
+ fail: 2200,
56
+ start: 1100,
57
+ doze: 2000,
58
+ wake: 1600,
59
+ };
60
+ export const MOODS = ["idle", "working", "thinking", "happy", "trouble", "sleep"];
61
+ /** What each state shows: its expression, the loop it moves in, and a move it makes now and then (every so many ms, at random between two). */
62
+ export const MOOD = {
63
+ idle: { expression: "neutral", loop: "breathe" },
64
+ working: { expression: "working", loop: "work" },
65
+ thinking: { expression: "thinking", loop: "think" },
66
+ happy: { expression: "happy", loop: "breathe", now: "pop", every: [11000, 19000] },
67
+ trouble: { expression: "trouble", loop: "worry", now: "shake", every: [8000, 14000] },
68
+ sleep: { expression: "sleeping", loop: "rest" },
69
+ };
70
+ /**
71
+ * What it does as its state changes, the moment it changes: a run finished, it celebrates; one
72
+ * failed, it fails; work starting, it gets going; switched off, it dozes off; switched on, it
73
+ * wakes; its finished run seen to, a nod. Nothing for a change it was already making (setting up,
74
+ * then working).
75
+ */
76
+ export function reactionTo(from, to) {
77
+ if (from === to)
78
+ return null;
79
+ if (to === "sleep")
80
+ return "doze";
81
+ if (from === "sleep")
82
+ return "wake";
83
+ if (to === "happy")
84
+ return "celebrate";
85
+ if (to === "trouble")
86
+ return "fail";
87
+ if ((to === "working" || to === "thinking") && from !== "working" && from !== "thinking")
88
+ return "start";
89
+ if (from === "happy" && to === "idle")
90
+ return "nod";
91
+ return null;
92
+ }
93
+ export const LID_KEYS = ["li", "lo", "smile", "size", "dx", "dy"];
94
+ export const TURN_KEYS = ["yaw", "pitch", "roll"];
95
+ export const FACE_KEYS = [...LID_KEYS, ...TURN_KEYS];
96
+ /**
97
+ * Each expression: what its eyes are (`glyph`), their lids and gaze, how it holds its head (it goes
98
+ * where the eyes look: down when sad, up and aside when thinking, aslant when listening), and how
99
+ * the eyes move on their own: scanning side to side, bobbing as it talks, swelling as it listens;
100
+ * how it blinks.
101
+ */
102
+ export const FACES = {
103
+ neutral: { glyph: "rect", li: 0, lo: 0, smile: 0, size: 1, dx: 0, dy: 0, yaw: 0, pitch: 0, roll: 0, blink: "quick" },
104
+ happy: { glyph: "arc", li: 0, lo: 0, smile: 0, size: 1.05, dx: 0, dy: 0.01, yaw: 0, pitch: 8, roll: 0, blink: "none" },
105
+ sad: { glyph: "rect", li: 0.05, lo: 0.5, smile: 0, size: 0.96, dx: 0, dy: -0.03, yaw: 0, pitch: -14, roll: -5, blink: "slow" },
106
+ tired: { glyph: "rect", li: 0.5, lo: 0.5, smile: 0, size: 1, dx: 0, dy: -0.015, yaw: 0, pitch: -8, roll: 3, blink: "slow" },
107
+ thinking: { glyph: "rect", li: 0.2, lo: 0.04, smile: 0, size: 1, dx: 0.045, dy: 0.045, yaw: 14, pitch: 12, roll: 5, blink: "quick" },
108
+ working: { glyph: "rect", li: 0.3, lo: 0.3, smile: 0.12, size: 0.98, dx: 0, dy: -0.01, yaw: 0, pitch: -7, roll: 0, scan: true, blink: "quick" },
109
+ listening: { glyph: "rect", li: 0, lo: 0, smile: 0, size: 1.12, dx: 0, dy: 0, yaw: -6, pitch: 0, roll: -9, pulse: true, blink: "quick" },
110
+ speaking: { glyph: "rect", li: 0.03, lo: 0.03, smile: 0.22, size: 1, dx: 0, dy: 0, yaw: 0, pitch: 3, roll: 0, bob: true, blink: "quick" },
111
+ sleeping: { glyph: "line", li: 0, lo: 0, smile: 0, size: 1, dx: 0, dy: -0.025, yaw: 0, pitch: -16, roll: 7, blink: "none" },
112
+ surprised: { glyph: "rect", li: 0, lo: 0, smile: 0, size: 1.3, dx: 0, dy: 0.015, yaw: 0, pitch: 10, roll: 0, blink: "none" },
113
+ trouble: { glyph: "squeeze", li: 0, lo: 0, smile: 0, size: 1, dx: 0, dy: 0, yaw: 0, pitch: -4, roll: 6, blink: "none" },
114
+ love: { glyph: "heart", li: 0, lo: 0, smile: 0, size: 1.15, dx: 0, dy: 0.01, yaw: 0, pitch: 6, roll: -4, blink: "none" },
115
+ excited: { glyph: "star", li: 0, lo: 0, smile: 0, size: 1.15, dx: 0, dy: 0.015, yaw: 0, pitch: 10, roll: 0, blink: "none" },
116
+ failed: { glyph: "cross", li: 0, lo: 0, smile: 0, size: 1, dx: 0, dy: -0.01, yaw: 0, pitch: -10, roll: -6, blink: "none" },
117
+ };
118
+ /** How long a blink takes, by kind, in ms. */
119
+ export const BLINK_FOR = { quick: 150, slow: 320 };
120
+ export const LOOK_KEYS = ["energy", "speed", "swirl", "halo", "ripple", "arc", "saturation", "breath", "talk", "listen"];
121
+ const IDLE = { energy: 0.6, speed: 0.22, swirl: 0.35, halo: 0.85, ripple: 0, arc: 0, saturation: 0.95, breath: 1, talk: 0, listen: 0 };
122
+ const DONE = { energy: 1.15, speed: 0.5, swirl: 0.3, halo: 1.5, ripple: 0, arc: 0, saturation: 1, breath: 0.5, talk: 0, listen: 0 };
123
+ const ASLEEP = { energy: 0.3, speed: 0.06, swirl: 0.2, halo: 0.55, ripple: 0, arc: 0, saturation: 0.15, breath: 1.4, talk: 0, listen: 0 };
124
+ export const LOOKS = {
125
+ neutral: IDLE,
126
+ happy: DONE,
127
+ sad: { ...IDLE, energy: 0.45, speed: 0.12, saturation: 0.75 },
128
+ tired: { ...IDLE, energy: 0.4, speed: 0.1, saturation: 0.8, breath: 1.3 },
129
+ thinking: { energy: 0.75, speed: 1.5, swirl: 1, halo: 0.95, ripple: 0, arc: 1, saturation: 1, breath: 0, talk: 0, listen: 0 },
130
+ working: { energy: 0.8, speed: 1.1, swirl: 0.8, halo: 1, ripple: 0, arc: 0.55, saturation: 1, breath: 0, talk: 0, listen: 0 },
131
+ listening: { energy: 0.85, speed: 0.4, swirl: 0.45, halo: 1.35, ripple: 1, arc: 0, saturation: 1, breath: 0.3, talk: 0, listen: 1 },
132
+ speaking: { energy: 0.9, speed: 0.6, swirl: 0.5, halo: 1.15, ripple: 0.25, arc: 0, saturation: 1, breath: 0, talk: 1, listen: 0 },
133
+ sleeping: ASLEEP,
134
+ surprised: { ...DONE, energy: 1, speed: 0.8, ripple: 0.4 },
135
+ trouble: { ...IDLE, energy: 0.7, speed: 0.9, swirl: 0.9, saturation: 1, ripple: 0.3, halo: 1 },
136
+ love: { ...DONE, energy: 1.2, halo: 1.6 },
137
+ excited: { ...DONE, speed: 0.9, swirl: 0.6 },
138
+ failed: { ...IDLE, energy: 0.35, speed: 0.08, saturation: 0.45, breath: 0.6 },
139
+ };
140
+ /** A switched-off figure's look, whatever its face: grey and dim, barely breathing. */
141
+ export const OFF_LOOK = ASLEEP;
142
+ export const DEFAULT_FIGURE = { shape: "sphere", energy: "lively", habit: "look", palette: 1 };
143
+ /** How much bigger, and how much quicker, each energy moves. */
144
+ export const ENERGY = { calm: { amp: 0.65, speed: 1.3 }, lively: { amp: 1, speed: 1 }, bouncy: { amp: 1.35, speed: 0.82 } };
145
+ /** The eight colourways: blue, green, sun, pink, violet, teal, orange and red. */
146
+ export const COLOURWAYS = {
147
+ 1: { main: "oklch(65% 0.19 254)", light: "oklch(78% 0.13 220)", deep: "oklch(60% 0.2 285)" },
148
+ 2: { main: "oklch(77% 0.18 150)", light: "oklch(88% 0.14 120)", deep: "oklch(68% 0.14 180)" },
149
+ 3: { main: "oklch(88% 0.16 92)", light: "oklch(93% 0.1 110)", deep: "oklch(78% 0.17 65)" },
150
+ 4: { main: "oklch(72% 0.2 356)", light: "oklch(84% 0.11 330)", deep: "oklch(70% 0.19 20)" },
151
+ 5: { main: "oklch(64% 0.2 300)", light: "oklch(78% 0.13 320)", deep: "oklch(58% 0.2 270)" },
152
+ 6: { main: "oklch(76% 0.13 192)", light: "oklch(88% 0.09 170)", deep: "oklch(66% 0.13 225)" },
153
+ 7: { main: "oklch(76% 0.17 58)", light: "oklch(87% 0.12 80)", deep: "oklch(68% 0.2 35)" },
154
+ 8: { main: "oklch(66% 0.21 27)", light: "oklch(80% 0.12 40)", deep: "oklch(60% 0.22 5)" },
155
+ };
156
+ /** The eyes' ink: a near black. */
157
+ export const INK = "oklch(20% 0.004 265)";
158
+ // ---------------------------------------------------------------------------------------------
159
+ // The Aura experiment's signals, kept as it had them
160
+ /** A spring per value: stiff enough to change state in a few tenths of a second, damped just under critical. */
161
+ export const SPRING = { stiffness: 90, damping: 16 };
162
+ export function springStep(current, target, dt) {
163
+ const force = SPRING.stiffness * (target - current.value) - SPRING.damping * current.velocity;
164
+ const velocity = current.velocity + force * dt;
165
+ return { value: current.value + velocity * dt, velocity };
166
+ }
167
+ const noise = (n) => {
168
+ const x = Math.sin(n * 127.1 + 311.7) * 43758.5453;
169
+ return x - Math.floor(x);
170
+ };
171
+ /** A slow breath, −1 to 1, about once every four and a half seconds. */
172
+ export const breathAt = (t) => Math.sin(t * 2 * Math.PI * 0.22);
173
+ /** Speech, 0 to 1: syllables at four and a half a second, each its own loudness, in phrases with a pause between some. */
174
+ export function speechAt(t) {
175
+ const syllables = t * 4.5;
176
+ const index = Math.floor(syllables);
177
+ const phrase = Math.floor(index / 6);
178
+ if (noise(phrase * 7.3) < 0.3 && index % 6 >= 3)
179
+ return 0;
180
+ const amount = 0.35 + 0.65 * noise(index);
181
+ return amount * Math.sin(Math.PI * (syllables - index)) ** 2;
182
+ }
183
+ /** A voice heard, 0 to 1: louder and softer, never quite still. */
184
+ export const voiceAt = (t) => Math.max(0, Math.min(1, 0.5 + 0.28 * Math.sin(t * 5.1) + 0.18 * Math.sin(t * 8.7 + 1.3) + 0.12 * Math.sin(t * 13.3 + 2.1)));
185
+ /** How much each signal swells the body, at full strength. */
186
+ export const SWELL = { breath: 0.014, talk: 0.05, listen: 0.03 };
187
+ /** The body's size, as a share of its resting size, from the sprung amounts and the clock. */
188
+ export const swellAt = (look, t) => 1 + look.breath * SWELL.breath * breathAt(t) + look.talk * SWELL.talk * speechAt(t) + look.listen * SWELL.listen * voiceAt(t);
189
+ // ---------------------------------------------------------------------------------------------
190
+ // Making and reading figures
191
+ /** A 32-bit hash of a word (FNV-1a), the seed of its figure. */
192
+ export function hashOf(text) {
193
+ let h = 0x811c9dc5;
194
+ for (let i = 0; i < text.length; i++) {
195
+ h ^= text.charCodeAt(i);
196
+ h = Math.imul(h, 0x01000193);
197
+ }
198
+ return h >>> 0;
199
+ }
200
+ /** Numbers from a seed, the same every time (mulberry32). */
201
+ function numbers(seed) {
202
+ let state = seed;
203
+ return () => {
204
+ state = (state + 0x6d2b79f5) | 0;
205
+ let t = Math.imul(state ^ (state >>> 15), 1 | state);
206
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
207
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
208
+ };
209
+ }
210
+ const pick = (list, next) => list[Math.floor(next() * list.length) % list.length];
211
+ /** A figure made from a word (an agent's name, a shuffle's count): the same word, the same creature. */
212
+ export function figureFor(seed) {
213
+ const next = numbers(hashOf(seed));
214
+ return { shape: pick(SHAPES, next), energy: pick(ENERGIES, next), habit: pick(HABITS, next), palette: pick(PALETTES, next) };
215
+ }
216
+ const oneOf = (list, value, fallback) => (list.includes(value) ? value : fallback);
217
+ /** A figure as it's stored, read back: a flat shape from before as the solid it became, anything missing or unknown the default's part. */
218
+ export function readFigure(value) {
219
+ const v = value && typeof value === "object" ? value : {};
220
+ const shape = typeof v.shape === "string" && v.shape in SOLID_OF ? SOLID_OF[v.shape] : v.shape;
221
+ return {
222
+ shape: oneOf(SHAPES, shape, DEFAULT_FIGURE.shape),
223
+ energy: oneOf(ENERGIES, v.energy, DEFAULT_FIGURE.energy),
224
+ habit: oneOf(HABITS, v.habit, DEFAULT_FIGURE.habit),
225
+ palette: oneOf(PALETTES, v.palette, DEFAULT_FIGURE.palette),
226
+ };
227
+ }
228
+ /** Whether a value is a whole figure, every part known: what a form may save. */
229
+ export const isFigure = (value) => {
230
+ if (!value || typeof value !== "object")
231
+ return false;
232
+ const v = value;
233
+ return SHAPES.includes(v.shape) && ENERGIES.includes(v.energy) && HABITS.includes(v.habit) && PALETTES.includes(v.palette);
234
+ };
235
+ // ---------------------------------------------------------------------------------------------
236
+ // A flat still, for anywhere the shader can't draw (a social card, a browser without WebGL 2)
237
+ const r2 = (n) => Math.round(n * 100) / 100;
238
+ /** Each shape seen from the front, on a 100-unit square: its outline, and where its eyes sit (their middle, each eye's width and height, how far each is from the middle). */
239
+ export const FLAT = {
240
+ cube: { d: "M22 22H78Q84 22 84 28V82Q84 88 78 88H22Q16 88 16 82V28Q16 22 22 22Z", face: [50, 54], eye: { w: 9, h: 13, gap: 12 } },
241
+ sphere: { d: "M14 54A36 36 0 1 0 86 54A36 36 0 1 0 14 54Z", face: [50, 50], eye: { w: 9, h: 13, gap: 12 } },
242
+ pyramid: { d: "M44 14Q50 6 56 14L92 82Q95 88 88 88H12Q5 88 8 82Z", face: [50, 66], eye: { w: 8, h: 11, gap: 10 } },
243
+ block: { d: "M14 36H86Q92 36 92 42V82Q92 88 86 88H14Q8 88 8 82V42Q8 36 14 36Z", face: [50, 62], eye: { w: 9, h: 13, gap: 14 } },
244
+ cloud: {
245
+ d: "M18 64H82Q88 64 88 70V76Q88 82 82 82H18Q12 82 12 76V70Q12 64 18 64ZM12 66A17 17 0 1 1 46 66A17 17 0 1 1 12 66ZM27 52A24 24 0 1 1 75 52A24 24 0 1 1 27 52ZM56 66A16 16 0 1 1 88 66A16 16 0 1 1 56 66Z",
246
+ face: [51, 58],
247
+ eye: { w: 8, h: 12, gap: 11 },
248
+ },
249
+ };
250
+ /**
251
+ * A figure as an SVG file, `size` pixels square, seen from the front: its shape in its colours (a
252
+ * light wash at the top left, the deep colour at the bottom right), its eyes as `expression` has
253
+ * them (a rectangle cut by its upper lid, or a bar for closed and happy eyes). Colours are any CSS
254
+ * colours, its colourway's and `INK` unless given; `id` names its gradient, unique on a page.
255
+ */
256
+ export function svgOf(figure, expression = "neutral", given = {}, size = 100, id = "figure") {
257
+ const colours = { ...COLOURWAYS[figure.palette], ink: INK, ...given };
258
+ const art = FLAT[figure.shape];
259
+ const face = FACES[expression];
260
+ const [fx, fy] = art.face;
261
+ const { w, h, gap } = art.eye;
262
+ const eyes = [-1, 1]
263
+ .map((side) => {
264
+ const cx = fx + side * gap + face.dx * 60;
265
+ const cy = fy - face.dy * 60;
266
+ const ew = w * face.size;
267
+ const eh = h * face.size;
268
+ const top = cy - eh / 2;
269
+ if (face.glyph !== "rect")
270
+ return `<rect x="${r2(cx - ew * 0.62)}" y="${r2(cy - eh * 0.09)}" width="${r2(ew * 1.24)}" height="${r2(eh * 0.18)}" fill="${colours.ink}"/>`;
271
+ // The upper lid, as the shader cuts it: lower at the outer side for a sad face, at the inner for a troubled one.
272
+ const [atLeft, atRight] = side < 0 ? [top + face.lo * eh, top + face.li * eh] : [top + face.li * eh, top + face.lo * eh];
273
+ return `<polygon points="${r2(cx - ew / 2)},${r2(atLeft)} ${r2(cx + ew / 2)},${r2(atRight)} ${r2(cx + ew / 2)},${r2(cy + eh / 2)} ${r2(cx - ew / 2)},${r2(cy + eh / 2)}" fill="${colours.ink}"/>`;
274
+ })
275
+ .join("");
276
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="${size}" height="${size}"><defs><linearGradient id="${id}-body" x1="0.15" y1="0.1" x2="0.85" y2="0.95"><stop offset="0" stop-color="${colours.light}"/><stop offset="0.55" stop-color="${colours.main}"/><stop offset="1" stop-color="${colours.deep}"/></linearGradient></defs><path d="${art.d}" fill="url(#${id}-body)"/>${eyes}</svg>`;
277
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Small figures for the agents in your interface: a cube, a sphere, a pyramid, a block or a cloud,
3
+ * really in 3D, in an iridescent material, with two eyes painted on its face. They say everything
4
+ * with their eyes and the way they move: waiting, at work, thinking, done, in trouble or asleep,
5
+ * reacting as that changes, looking at the pointer and at each other, petted when clicked.
6
+ *
7
+ * import { figureFor } from "@cesarvenzac/figures";
8
+ * import { AgentFigure } from "@cesarvenzac/figures/react";
9
+ *
10
+ * <AgentFigure figure={figureFor("Ada")} mood="thinking" size={48} />
11
+ *
12
+ * A figure is four choices (`Figure`): its shape, its energy, its habit and its colourway.
13
+ * `figureFor` makes one from any word, the same every time; `readFigure` reads a stored one back.
14
+ * `svgOf` writes a flat still, for anywhere a live one can't go (an email, a social card).
15
+ *
16
+ * Plain TypeScript, no dependencies; this entry runs anywhere. The live figure is
17
+ * `@cesarvenzac/figures/react`, drawn with WebGL 2, one context for the whole page.
18
+ */
19
+ export { type Colourway, COLOURWAYS, DEFAULT_FIGURE, ENERGIES, type Energy, type Expression, EXPRESSIONS, type Figure, figureFor, HABITS, type Habit, INK, isFigure, LOOPS, type Loop, type Mood, MOODS, type Motion, MOTIONS, MOVES, ONCE, type Once, type Palette, PALETTES, type Reaction, REACTIONS, reactionTo, readFigure, type Shape, SHAPES, svgOf, } from "./figure.ts";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Small figures for the agents in your interface: a cube, a sphere, a pyramid, a block or a cloud,
3
+ * really in 3D, in an iridescent material, with two eyes painted on its face. They say everything
4
+ * with their eyes and the way they move: waiting, at work, thinking, done, in trouble or asleep,
5
+ * reacting as that changes, looking at the pointer and at each other, petted when clicked.
6
+ *
7
+ * import { figureFor } from "@cesarvenzac/figures";
8
+ * import { AgentFigure } from "@cesarvenzac/figures/react";
9
+ *
10
+ * <AgentFigure figure={figureFor("Ada")} mood="thinking" size={48} />
11
+ *
12
+ * A figure is four choices (`Figure`): its shape, its energy, its habit and its colourway.
13
+ * `figureFor` makes one from any word, the same every time; `readFigure` reads a stored one back.
14
+ * `svgOf` writes a flat still, for anywhere a live one can't go (an email, a social card).
15
+ *
16
+ * Plain TypeScript, no dependencies; this entry runs anywhere. The live figure is
17
+ * `@cesarvenzac/figures/react`, drawn with WebGL 2, one context for the whole page.
18
+ */
19
+ export { COLOURWAYS, DEFAULT_FIGURE, ENERGIES, EXPRESSIONS, figureFor, HABITS, INK, isFigure, LOOPS, MOODS, MOTIONS, MOVES, ONCE, PALETTES, REACTIONS, reactionTo, readFigure, SHAPES, svgOf, } from "./figure.js";
@@ -0,0 +1,69 @@
1
+ import { type CSSProperties } from "react";
2
+ import { type Colourway, type Expression, type Figure, type Mood, type Motion } from "./figure.ts";
3
+ /**
4
+ * An agent's figure (figure.ts), `size` pixels square: a cube, a sphere, a pyramid, a
5
+ * block or a cloud, really in 3D, in the original Aura's iridescent material in its colourway,
6
+ * with two eyes painted on its face, drawn by one shader for the whole page (figure-gl.ts).
7
+ *
8
+ * Its state (`mood`) gives its expression and how it moves, the shape's own act included (the
9
+ * cube's Rubik's turns as it thinks, the block's dials as wheels at work, z's asleep: figure-acts
10
+ * ts); and as its state changes it reacts, the shape its own way: a run finished, it celebrates;
11
+ * failed, it fails; work starting, it gets going; switched off, it dozes off; on again, it wakes.
12
+ * Clicked (`pet`), it's petted, and doesn't open the card it's on. `expression` and `motion` set
13
+ * its face and loop outright (the editor's preview), `play` a move to make now.
14
+ *
15
+ * It holds its head as its expression does, blinks, glances, sways, turns toward the pointer on a
16
+ * desktop (`look`; every figure but a switched-off one), looks at the others on the page when one
17
+ * of them has a moment and now and then at whichever is busy, does its habit while it waits, and
18
+ * smiles with a small bounce when the card it's on is hovered (`react`). `grab` lets it be turned
19
+ * by hand, springing back when let go. `aim` turns its eyes somewhere of its own in place of the
20
+ * pointer and the others' glances (the Lab's playground: at whoever it's talking to), and `lift`
21
+ * hops it off the ground as it walks, its shadow staying there. Its energy scales every move;
22
+ * `drain` (0–1: an agent's budget running out, say) greys it. Its colours are its colourway's
23
+ * (`COLOURWAYS`) unless `colours` says otherwise, and its eyes `INK` unless `ink` does: any CSS
24
+ * colours, a custom property (`var(--name)`) read from the page's root once. All of it is worked
25
+ * out by figure-motion.ts, figure-acts.ts and figure-scene.ts on one frame loop for the page
26
+ * (figure-loop.ts); a still figure (`still`, reduced motion, off screen) is drawn once. Where
27
+ * there's no WebGL 2 it's a flat drawing instead. A drawing, hidden from assistive tech: the words beside it say who it is and
28
+ * what it's doing.
29
+ */
30
+ export declare function AgentFigure({ figure, mood, expression: asked, motion: askedMotion, play, size, look, react, pet, still, grab, drain, aim, lift, colours: given, ink, className, style, }: {
31
+ figure: Figure;
32
+ mood?: Mood;
33
+ expression?: Expression;
34
+ /** A loop to move in, in place of its state's. */
35
+ motion?: Motion;
36
+ /** A move to make now; a new `play.at` makes it again. */
37
+ play?: {
38
+ motion: Motion;
39
+ at: number;
40
+ };
41
+ size?: number;
42
+ look?: boolean;
43
+ react?: boolean;
44
+ pet?: boolean;
45
+ still?: boolean;
46
+ grab?: boolean;
47
+ drain?: number;
48
+ /** Where it looks, from −1 to 1 each way (right and down), in place of the pointer and the others; only another's moment turns it away. */
49
+ aim?: {
50
+ x: number;
51
+ y: number;
52
+ };
53
+ /** How far off the ground it is at a moment (frame time, as `performance.now()`), from 0 to 1: a hop's height. Read every frame. */
54
+ lift?: (now: number) => number;
55
+ /** Its colours, in place of its colourway's: any CSS colours, `var(--name)` included. */
56
+ colours?: Colourway;
57
+ /** Its eyes' colour, a CSS colour. */
58
+ ink?: string;
59
+ className?: string;
60
+ style?: CSSProperties;
61
+ }): import("react").JSX.Element;
62
+ /** An agent's colours alone, as a dot `size` pixels across: where a whole figure wouldn't read (a table's row). */
63
+ export declare function AgentDot({ figure, colours, size, className, style }: {
64
+ figure: Figure;
65
+ colours?: Colourway;
66
+ size?: number;
67
+ className?: string;
68
+ style?: CSSProperties;
69
+ }): import("react").JSX.Element;