@kodax-ai/botui 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -113
- package/core/identity.js +46 -0
- package/{themes/solids → core}/motion.js +114 -114
- package/core/registry.js +26 -18
- package/docs/usage.md +202 -0
- package/element.js +5 -0
- package/index.d.ts +2 -0
- package/index.js +11 -5
- package/package.json +38 -2
- package/themes/caret/README.md +43 -0
- package/themes/caret/avatar.js +10 -0
- package/themes/caret/engine.js +143 -0
- package/themes/caret/index.d.ts +24 -0
- package/themes/caret/index.js +35 -0
- package/themes/critter/README.md +43 -0
- package/themes/critter/avatar.js +12 -0
- package/themes/critter/engine.js +113 -0
- package/themes/critter/index.d.ts +28 -0
- package/themes/critter/index.js +35 -0
- package/themes/doodle/README.md +43 -0
- package/themes/doodle/avatar.js +10 -0
- package/themes/doodle/engine.js +149 -0
- package/themes/doodle/index.d.ts +24 -0
- package/themes/doodle/index.js +36 -0
- package/themes/dots/README.md +42 -0
- package/themes/dots/avatar.js +15 -0
- package/themes/dots/engine.js +179 -0
- package/themes/dots/index.d.ts +24 -0
- package/themes/dots/index.js +35 -0
- package/themes/kaomoji/README.md +42 -0
- package/themes/kaomoji/avatar.js +10 -0
- package/themes/kaomoji/engine.js +110 -0
- package/themes/kaomoji/index.d.ts +24 -0
- package/themes/kaomoji/index.js +35 -0
- package/themes/liquid/README.md +47 -0
- package/themes/liquid/avatar.js +10 -0
- package/themes/liquid/engine.js +161 -0
- package/themes/liquid/index.d.ts +24 -0
- package/themes/liquid/index.js +35 -0
- package/themes/matrix/README.md +48 -0
- package/themes/matrix/avatar.js +227 -0
- package/themes/matrix/engine.js +479 -0
- package/themes/matrix/index.d.ts +47 -0
- package/themes/matrix/index.js +34 -0
- package/themes/orb/README.md +44 -0
- package/themes/orb/avatar.js +10 -0
- package/themes/orb/engine.js +130 -0
- package/themes/orb/index.d.ts +24 -0
- package/themes/orb/index.js +36 -0
- package/themes/solids/README.md +2 -2
- package/themes/solids/avatar.js +561 -579
- package/themes/solids/face.js +52 -56
- package/themes/solids/index.js +34 -28
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
// The matrix theme's model: a 5x5 dot screen on a coloured tile.
|
|
2
|
+
//
|
|
3
|
+
// It acts out the same states and gestures as the solids. The pose and gesture
|
|
4
|
+
// tables are shared by every theme (core/motion.js); this file draws them flat.
|
|
5
|
+
// Turning slides the face across the screen and, past about a quarter turn,
|
|
6
|
+
// flips the tile like a card. Expressions are dot patterns. Signal states, and
|
|
7
|
+
// the matrix's own thinking, streaming and finished, play on the screen. Marks,
|
|
8
|
+
// particles, circling dots, dizzy stars and the halo sit around the tile.
|
|
9
|
+
//
|
|
10
|
+
// A screen is rigid: it bobs, hops and leans a little, but squashes under a third
|
|
11
|
+
// as much as a solid and never muddies its colour; dimming happens on the dots.
|
|
12
|
+
//
|
|
13
|
+
// Pure: no DOM, no clock, no Math.random. `step(dt, input)` returns plain SVG
|
|
14
|
+
// attributes, so the same Bot and the same inputs always give the same frames.
|
|
15
|
+
// First drawn by RSIBot (ui/bot-avatar/engine.js), moved here on 2026-11-28.
|
|
16
|
+
|
|
17
|
+
import {REST, POSES, GESTURES, FIDGETS} from '../../core/motion.js';
|
|
18
|
+
import {resolveState} from '../../core/states.js';
|
|
19
|
+
import {TAU, ease, clamp, bump, hash, random} from '../../core/core.js';
|
|
20
|
+
|
|
21
|
+
const {sin, cos, abs, max, min, round, floor, sign, sqrt, hypot, atan2} = Math;
|
|
22
|
+
const r1 = v => round(v * 10) / 10;
|
|
23
|
+
const fixed = v => v.toFixed(2);
|
|
24
|
+
const wrap = angle => angle - TAU * floor((angle + Math.PI) / TAU);
|
|
25
|
+
|
|
26
|
+
// ---- geometry ----
|
|
27
|
+
// Frame units: the viewBox is 0..100. The grid is 5x5 dots CELL apart; every outline holds all of it.
|
|
28
|
+
const CELL = 10.5, TILE = 38, U = 34, GROW = 1.3, PARTICLES = 16, KNEE = 1.25, SOFT = 0.35;
|
|
29
|
+
const point = (x, y) => fixed(x) + ',' + fixed(y);
|
|
30
|
+
function squirclePath(radius, p = 4.5) {
|
|
31
|
+
// Superellipse, scaled so the diagonal reaches `radius`.
|
|
32
|
+
const norm = Math.pow(2, 0.5 - 1 / p);
|
|
33
|
+
let d = '';
|
|
34
|
+
for (let i = 0; i < 96; i++) {
|
|
35
|
+
const th = i / 96 * TAU, r = Math.pow(Math.pow(abs(cos(th)), p) + Math.pow(abs(sin(th)), p), -1 / p) / norm;
|
|
36
|
+
d += (i ? 'L' : 'M') + point(50 + cos(th) * r * radius, 50 + sin(th) * r * radius);
|
|
37
|
+
}
|
|
38
|
+
return d + 'Z';
|
|
39
|
+
}
|
|
40
|
+
function polygonPath(sides, radius, corner, turn = 0) {
|
|
41
|
+
const at = Array.from({length: sides}, (_, i) => { const a = turn + i * TAU / sides; return [50 + radius * cos(a), 50 + radius * sin(a)]; });
|
|
42
|
+
const toward = (from, to) => { const k = corner / hypot(to[0] - from[0], to[1] - from[1]); return [from[0] + (to[0] - from[0]) * k, from[1] + (to[1] - from[1]) * k]; };
|
|
43
|
+
let d = '';
|
|
44
|
+
at.forEach((v, i) => {
|
|
45
|
+
const a = toward(v, at[(i + sides - 1) % sides]), b = toward(v, at[(i + 1) % sides]);
|
|
46
|
+
d += (i ? 'L' : 'M') + point(...a) + 'Q' + point(...v) + ' ' + point(...b);
|
|
47
|
+
});
|
|
48
|
+
return d + 'Z';
|
|
49
|
+
}
|
|
50
|
+
const OUTLINES = {
|
|
51
|
+
squircle: () => squirclePath(TILE),
|
|
52
|
+
circle: () => `M${50 - TILE},50a${TILE},${TILE} 0 1,0 ${2 * TILE},0a${TILE},${TILE} 0 1,0 ${-2 * TILE},0Z`,
|
|
53
|
+
// Flat top and bottom, parallel to the rows; large enough that the corner dots stay inside.
|
|
54
|
+
hex: () => polygonPath(6, 41, 7),
|
|
55
|
+
};
|
|
56
|
+
export const matrixShapes = Object.freeze(Object.keys(OUTLINES));
|
|
57
|
+
/** The tile outlines, in the same form as the solids' SHAPES. */
|
|
58
|
+
export const SHAPES = [
|
|
59
|
+
{name: 'squircle', zh: '圆角方', group: 'tile'},
|
|
60
|
+
{name: 'circle', zh: '圆', group: 'tile'},
|
|
61
|
+
{name: 'hex', zh: '六边形', group: 'tile'},
|
|
62
|
+
];
|
|
63
|
+
export const GROUPS = {tile: '屏幕外形'};
|
|
64
|
+
export const matrixShapeForId = identity => matrixShapes[hash(identity + '/shape') % matrixShapes.length];
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Colours for a tone {hue, chroma, light, dots, edge}: the tile, its dots and, for
|
|
68
|
+
* a tile that would fade into the page, an edge. `sat` drains the colour a little.
|
|
69
|
+
*/
|
|
70
|
+
export const paint = ({hue, chroma = 0.15, light = 0.74, dots, edge}, sat = 1) => {
|
|
71
|
+
const c = k => (k * sat).toFixed(3), accent = max(chroma, 0.05);
|
|
72
|
+
return {
|
|
73
|
+
body: `oklch(${light} ${c(chroma)} ${hue})`, edge: edge ?? 'none',
|
|
74
|
+
dark: dots ?? `oklch(0.25 ${c(min(0.06, chroma * 0.4))} ${hue})`,
|
|
75
|
+
accent: `oklch(${min(0.66, light - 0.08).toFixed(2)} ${c(accent * 1.07)} ${hue})`,
|
|
76
|
+
bright: `oklch(0.86 0.14 ${(hue + 50) % 360})`, card: `oklch(0.985 0.012 ${hue})`,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// ---- the face ----
|
|
81
|
+
// Each eye is drawn around its centre (column 1 or 3, row 2) as [x, y, level]. The solids' eye styles:
|
|
82
|
+
// dot (圆点) one dot, bar (竖条) two stacked, tall (长条) three stacked.
|
|
83
|
+
// x points away from the other eye, so one pattern serves both sides.
|
|
84
|
+
const EYE = {dot: [[0, 0]], bar: [[0, -1], [0, 0]], tall: [[0, -1], [0, 0], [0, 1]]};
|
|
85
|
+
export const matrixEyes = Object.freeze(Object.keys(EYE));
|
|
86
|
+
const WIDE = {dot: [[0, -1], [0, 0]], bar: [[0, -1], [0, 0], [0, 1]], tall: [[0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]};
|
|
87
|
+
const DIAMOND = [[0, -1], [1, 0], [0, 1], [-1, 0]], SALTIRE = [[-1, -1], [1, 1], [1, -1], [-1, 1], [0, 0]];
|
|
88
|
+
const AROUND = [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]];
|
|
89
|
+
const HEART = [[1, 0], [3, 0], [0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [1, 3], [2, 3], [3, 3], [2, 4]];
|
|
90
|
+
const ARCH = [[1, 0], [0, -1], [-1, 0]];
|
|
91
|
+
/** The 21 expressions as dot patterns: (side, eye style, t) -> dots, or {screen, level} for the whole screen. */
|
|
92
|
+
export const FACES = {
|
|
93
|
+
neutral: (side, eyes) => EYE[eyes],
|
|
94
|
+
wide: (side, eyes) => WIDE[eyes],
|
|
95
|
+
happy: () => ARCH,
|
|
96
|
+
closed: () => [[1, 0], [0, 0]],
|
|
97
|
+
sleep: () => [[1, 1, 0.8], [0, 1, 0.8]],
|
|
98
|
+
squint: () => [[1, -1], [0, 0], [1, 1]],
|
|
99
|
+
sad: () => [[1, 1], [0, 0]],
|
|
100
|
+
focus: () => [[1, -1], [0, 0]],
|
|
101
|
+
angry: () => [[1, -1], [0, 0], [0, 1]],
|
|
102
|
+
calm: () => [[1, 0], [0, 1], [-1, 0]],
|
|
103
|
+
sleepy: () => [[1, 0], [0, 0], [0, 1, 0.5]],
|
|
104
|
+
shy: () => [[0, 1, 0.75]],
|
|
105
|
+
inspecting: side => side < 0 ? [[0, -1], [0, 0], [0, 1]] : [[0, 0], [1, 0]],
|
|
106
|
+
confused: side => side < 0 ? EYE.bar : DIAMOND,
|
|
107
|
+
wink: (side, eyes) => side > 0 ? ARCH : EYE[eyes],
|
|
108
|
+
skeptic: (side, eyes) => side > 0 ? [[0, 0], [1, -1]] : EYE[eyes],
|
|
109
|
+
cross: () => SALTIRE,
|
|
110
|
+
surprised: () => DIAMOND,
|
|
111
|
+
dizzy: (side, eyes, t) => { const k = floor(t * 12); return [0, 1, 2].map(i => [...AROUND[((k - i) % 8 + 8) % 8], 1 - i * 0.3]); },
|
|
112
|
+
love: (side, eyes, t) => ({screen: HEART, level: 0.8 + 0.2 * abs(sin(t * 4))}),
|
|
113
|
+
sparkle: (side, eyes, t) => floor(t * 3.5) % 2 ? SALTIRE : [...DIAMOND, [0, 0]],
|
|
114
|
+
};
|
|
115
|
+
const SHUT = new Set(['closed', 'sleep']);
|
|
116
|
+
|
|
117
|
+
// ---- the screen ----
|
|
118
|
+
// What the screen shows instead of a face: (t, progress, since) -> [column, row, level], or null for the face.
|
|
119
|
+
const RING = [[1, 1], [2, 1], [3, 1], [3, 2], [3, 3], [2, 3], [1, 3], [1, 2]];
|
|
120
|
+
const BORDER = [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [3, 4], [2, 4], [1, 4], [0, 4], [0, 3], [0, 2], [0, 1]];
|
|
121
|
+
const CHECK = [[0, 2], [1, 3], [2, 2], [3, 1], [4, 0]];
|
|
122
|
+
const DOWN = [[2, 0], [2, 1], [0, 2], [2, 2], [4, 2], [1, 3], [2, 3], [3, 3], [2, 4]];
|
|
123
|
+
const SEND = [[2, 0], [3, 0], [4, 0], [4, 1], [4, 2], [3, 1], [2, 2], [1, 3], [0, 4]];
|
|
124
|
+
const POWER = [[2, 0], [0, 1], [2, 1], [4, 1], [0, 2], [2, 2], [4, 2], [0, 3], [4, 3], [1, 4], [2, 4], [3, 4]];
|
|
125
|
+
const PEN = [[0, 2], [1, 1], [2, 2], [3, 1], [4, 2], [0, 4], [1, 3], [2, 4], [3, 3], [4, 4]];
|
|
126
|
+
const trail = (path, head, length, fade) => Array.from({length}, (_, i) => [...path[((floor(head) - i) % path.length + path.length) % path.length], max(0.15, 1 - i * fade)]);
|
|
127
|
+
const at = (cells, level = 1) => cells.map(([c, r]) => [c, r, level]);
|
|
128
|
+
export const SCREENS = {
|
|
129
|
+
// Slow enough that each dot reaches full brightness before it moves on.
|
|
130
|
+
thinking: t => trail(RING, t * 7, 4, 0.24),
|
|
131
|
+
streaming: t => { const out = []; for (let c = 0; c < 5; c++) { const tall = 1 + floor(4.6 * abs(sin(t * 5.5 + c * 1.9) * cos(t * 2.1 + c * 0.7))); for (let i = 0; i < tall; i++) out.push([c, 4 - i, 1]); } return out; },
|
|
132
|
+
finished: () => at(CHECK),
|
|
133
|
+
orbit: t => [[2, 2, 1], ...[0, 8].flatMap(o => trail(BORDER, t * 8 + o, 2, 0.6))],
|
|
134
|
+
radar: t => {
|
|
135
|
+
const a = t * 2.4, out = [[2, 2, 1], ...at(BORDER, 0.22)];
|
|
136
|
+
for (let r = 0; r < 5; r++) for (let c = 0; c < 5; c++) {
|
|
137
|
+
if ((c === 2 && r === 2) || hypot(c - 2, r - 2) > 2.3) continue;
|
|
138
|
+
const behind = ((a - atan2(r - 2, c - 2)) % TAU + TAU) % TAU;
|
|
139
|
+
if (behind < 1.4) out.push([c, r, 1 - behind / 1.4]);
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
},
|
|
143
|
+
progress: (t, progress) => { const n = round(clamp(progress ?? (t * 0.22) % 1, 0, 1) * 25); return Array.from({length: n}, (_, i) => [i % 5, floor(i / 5), 1]); },
|
|
144
|
+
loading: t => [0, 1, 2].map(i => [i * 2, max(0, sin(t * 6 - i * 0.9)) > 0.55 ? 1 : 2, 1]),
|
|
145
|
+
spawning: (t, progress, since) => { if (since >= 2.4) return null; const u = (t * 0.8) % 1; return [[2, 2, 1], ...at(RING, u), ...at(BORDER, 1 - u)]; },
|
|
146
|
+
dictating: t => { const out = []; for (let c = 0; c < 5; c++) { const h = round(2 * abs(sin(t * 6 + c * 1.3) * cos(t * 2.1 + c))); for (let r = 2 - h; r <= 2 + h; r++) out.push([c, r, 1]); } return out; },
|
|
147
|
+
sending: t => { const o = floor(t * 4) % 2; return [...SEND.map(([c, r]) => [c - 1 + o, r + 1 - o, 1]), [0, 4, 0.35 + 0.4 * o]]; },
|
|
148
|
+
receiving: t => { const o = floor(t * 3) % 3 - 1; return [...DOWN.map(([c, r]) => [c, r + o, 1]), [0, 4, 0.5], [4, 4, 0.5]]; },
|
|
149
|
+
uploading: t => { const o = 1 - floor(t * 3) % 3; return DOWN.map(([c, r]) => [c, 4 - r + o, 1]); },
|
|
150
|
+
writing: t => { const n = 1 + floor(((t * 0.6) % 1) * PEN.length); return PEN.slice(0, n).map((cell, i) => [...cell, i === n - 1 ? 1 : 0.7]); },
|
|
151
|
+
alerting: t => at([[2, 0], [2, 1], [2, 2], [2, 4]], 0.6 + 0.4 * max(0, sin(t * 7))),
|
|
152
|
+
bouncing: t => [[1, 4, 0.5], [2, 4, 0.5], [3, 4, 0.5], [2, 3 - round(3 * abs(sin(t * 4))), 1]],
|
|
153
|
+
'powering-down': t => at(POWER, 0.45 + 0.2 * sin(t * 1.6)),
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// ---- around the tile ----
|
|
157
|
+
// Glyphs beside the head, the same as the solids draw, shown on a small card.
|
|
158
|
+
const MARKS = {
|
|
159
|
+
ask: {d: 'M-4,-6Q-4,-11 0,-11Q4.5,-11 4.5,-6.5Q4.5,-3.5 0,-2L0,1', dot: [0, 6.5, 2]},
|
|
160
|
+
bang: {d: 'M0,-11L0,0', dot: [0, 6.5, 2.1]},
|
|
161
|
+
dots: {d: 'M-7,0L-7,0M0,0L0,0M7,0L7,0', wide: true},
|
|
162
|
+
check: {d: 'M-7,0L-2,5L7,-6'},
|
|
163
|
+
up: {d: 'M0,8L0,-9M-6,-3L0,-9L6,-3'},
|
|
164
|
+
code: {d: 'M-4,-6L-10,0L-4,6M4,-6L10,0L4,6'},
|
|
165
|
+
search: {d: 'M-8,-2a6,6 0 1,0 12,0a6,6 0 1,0 -12,0M3,3L9,9'},
|
|
166
|
+
cursor: {d: 'M-5,-9L-5,7L-1,3L2,9L5,7.5L2,2L7,2Z', fill: true},
|
|
167
|
+
pen: {d: 'M-7,8L-6,3L4,-8L8,-4L-2,7ZM-9,10L8,10'},
|
|
168
|
+
anger: {d: 'M-6,-2Q-2,-2 -2,-6M6,-2Q2,-2 2,-6M-6,2Q-2,2 -2,6M6,2Q2,2 2,6', paint: 'oklch(0.62 0.22 25)'},
|
|
169
|
+
sweat: {d: 'M0,-8Q6,2 0,6Q-6,2 0,-8Z', paint: 'oklch(0.78 0.1 230)'},
|
|
170
|
+
badge: {d: '', dot: [0, 0, 5.5], paint: 'oklch(0.64 0.22 25)'},
|
|
171
|
+
};
|
|
172
|
+
const GLYPHS = {
|
|
173
|
+
dot: 'M-1,0A1,1 0 1,0 1,0A1,1 0 1,0 -1,0Z',
|
|
174
|
+
star: 'M0,-1L.26,-.26L1,0L.26,.26L0,1L-.26,.26L-1,0L-.26,-.26Z',
|
|
175
|
+
heart: 'M0,1C-1.3,0 -1.1,-1 -.5,-1C-.2,-1 0,-.8 0,-.6C0,-.8 .2,-1 .5,-1C1.1,-1 1.3,0 0,1Z',
|
|
176
|
+
z: 'M-1,-1H1L-1,1H1',
|
|
177
|
+
note: 'M-.8,1.4L-.8,-1.6L1.2,-1.1L1.2,.9M-1.3,1.5L-.8,1.4M.7,1L1.2,.9',
|
|
178
|
+
confetti: 'M-1,-.6H1V.6H-1Z',
|
|
179
|
+
};
|
|
180
|
+
const CONFETTI_HUES = [25, 55, 85, 120, 150, 175, 200, 235, 265, 290, 330, 0];
|
|
181
|
+
const ZERO = {yaw: 0, pitch: 0, roll: 0, sy: 0, lift: 0, scale: 0, gx: 0, gy: 0};
|
|
182
|
+
// Small turns barely narrow the tile; real turn-arounds pass through edge-on to the back.
|
|
183
|
+
const turnOf = angle => { const a = abs(angle); return sign(angle) * (a < KNEE ? a * SOFT : KNEE * SOFT + (a - KNEE) * (Math.PI - KNEE * SOFT) / (Math.PI - KNEE)); };
|
|
184
|
+
|
|
185
|
+
export class Matrix {
|
|
186
|
+
/** The shapes the model can draw; a theme with other bodies sets its own. */
|
|
187
|
+
static shapes = matrixShapes;
|
|
188
|
+
constructor({identity = 'bot', hue, chroma, light, dots, edge, shape, eyes = 'tall', state = 'idle', progress = null, size = 36, emphasized = false}) {
|
|
189
|
+
if (!this.constructor.shapes.includes(shape)) throw new TypeError('Unknown shape: ' + shape);
|
|
190
|
+
if (!Number.isFinite(hue)) throw new TypeError('Matrix hue must be a number');
|
|
191
|
+
if (!EYE[eyes]) throw new TypeError('Unknown eye style: ' + eyes);
|
|
192
|
+
Object.assign(this, {identity, tone: {hue, chroma, light, dots, edge}, shape, eyes, size, emphasized: !!emphasized, progress});
|
|
193
|
+
this.state = resolveState(state) || 'idle';
|
|
194
|
+
this.rng = random(hash(identity + '/life')); this.t = this.rng() * 10; this.since = 0; this.dt = 0; this.beat = 0;
|
|
195
|
+
this.p = {yaw: REST.yaw, pitch: REST.pitch, roll: 0, sy: 1, lift: 0, scale: 1, sat: 1, open: 1, blush: 0};
|
|
196
|
+
this.vel = {sy: 0, lift: 0, scale: 0, roll: 0};
|
|
197
|
+
this.omega = 0; this.spun = 0; this.dizzy = 0; this.tug = 0; this.phase = 0; this.dragSpeed = 0;
|
|
198
|
+
this.aim = {x: 0, y: 0}; this.stray = {x: 0, y: 0}; this.strayIn = 0; this.gx = 0; this.gy = 0; this.g = ZERO;
|
|
199
|
+
this.shown = 'neutral'; this.override = null; this.forced = false;
|
|
200
|
+
this.blinkIn = 1 + this.rng() * 3; this.blinkFor = 0; this.fidgetIn = 4 + this.rng() * 6;
|
|
201
|
+
this.gestures = []; this.particles = [];
|
|
202
|
+
this.aura = {orbit: 0, mark: 0, stars: 0, emph: 0}; this.markName = 'ask';
|
|
203
|
+
this.hover = false; this.press = false; this.dragging = false;
|
|
204
|
+
this.level = new Float32Array(25);
|
|
205
|
+
const ink = paint(this.tone), node = (layer, tag, attrs = {}, parent = null) => ({layer, tag, attrs, parent});
|
|
206
|
+
// Nodes in drawing order. `tile` nodes move with the tile; the rest are placed in the frame.
|
|
207
|
+
this.layout = [
|
|
208
|
+
node('back', 'circle', {cx: 50, cy: 50, fill: 'none', display: 'none'}),
|
|
209
|
+
...this.tileLayout(ink, node),
|
|
210
|
+
...this.faceLayout(ink, node),
|
|
211
|
+
...Array.from({length: 3}, () => node('front', 'circle', {display: 'none'})),
|
|
212
|
+
...Array.from({length: 3}, () => node('front', 'path', {d: GLYPHS.star, display: 'none'})),
|
|
213
|
+
node('front', 'circle', {r: 12.5, display: 'none'}),
|
|
214
|
+
node('front', 'path', {fill: 'none', 'stroke-linecap': 'round', 'stroke-linejoin': 'round', display: 'none'}),
|
|
215
|
+
node('front', 'circle', {display: 'none'}),
|
|
216
|
+
...Array.from({length: PARTICLES}, () => node('front', 'path', {display: 'none', 'stroke-linecap': 'round', 'stroke-linejoin': 'round'})),
|
|
217
|
+
];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ---- what the pose and gesture tables call ----
|
|
221
|
+
setState(name) {
|
|
222
|
+
const state = resolveState(name);
|
|
223
|
+
if (!state || state === this.state) return this;
|
|
224
|
+
this.state = state; this.since = 0; this.vel.scale += 1.2; POSES[state]?.enter?.(this);
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
play(name) {
|
|
228
|
+
const def = GESTURES[name];
|
|
229
|
+
if (!def) return this;
|
|
230
|
+
this.gestures = this.gestures.filter(item => item.def !== def); this.gestures.push({def, age: 0}); def.start?.(this);
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
express(name, ms = 1500) { this.override = name && FACES[name] ? {name, left: ms / 1000} : null; return this; }
|
|
234
|
+
blink() { this.blinkFor = 0.13; return this; }
|
|
235
|
+
nudge() { this.vel.sy -= 1.4; return this; }
|
|
236
|
+
poke(action, {turns = 1} = {}) {
|
|
237
|
+
this.pulse();
|
|
238
|
+
if (action === 'spin') { this.omega += 3 * TAU * turns; this.hop(170); return this; }
|
|
239
|
+
if (action) return this.play(action);
|
|
240
|
+
if (['blocked', 'sleeping', 'powering-down'].includes(this.state)) { this.hop(40); return this; }
|
|
241
|
+
const r = this.rng();
|
|
242
|
+
if (r < 0.4) { this.omega += r < 0.2 ? 19 : -19; this.hop(170); this.express('happy', 800); }
|
|
243
|
+
else this.play(r < 0.6 ? 'jump' : r < 0.75 ? 'wobble' : r < 0.9 ? 'flip' : 'roll');
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
hop(strength) { this.vel.lift -= strength; }
|
|
247
|
+
squash(strength) { this.vel.sy -= strength; }
|
|
248
|
+
every(period) { return floor(this.since / period) !== floor((this.since - this.dt) / period); }
|
|
249
|
+
pulse() { this.spawn({type: 'ring', life: 0.6}); }
|
|
250
|
+
emit(type) {
|
|
251
|
+
const rng = this.rng;
|
|
252
|
+
if (type === 'z') this.spawn({glyph: 'z', stroked: true, life: 2.2, x: 0.9, y: -1.0, vx: 0.32, vy: -0.42, size: 2.2, turn: -12, grow: 1});
|
|
253
|
+
else if (type === 'note') this.spawn({glyph: 'note', stroked: true, life: 1.8, x: 0.7 + rng() * 0.5, y: -0.9, vx: 0.3 * (rng() - 0.3), vy: -0.6, size: 4.6, turn: rng() * 24 - 12, grow: 0.3});
|
|
254
|
+
else if (type === 'heart') this.spawn({glyph: 'heart', life: 1.4, x: (rng() - 0.5) * 1.6, y: -0.9, vx: (rng() - 0.5) * 0.5, vy: -0.9, size: 2.6 + rng() * 1.6, paint: 'oklch(0.7 0.19 10)'});
|
|
255
|
+
else if (type === 'tear') this.spawn({glyph: 'dot', life: 0.9, x: rng() < 0.5 ? -0.31 : 0.31, y: 0.1, vy: 0.3, g: 5, size: 1.5, paint: 'oklch(0.72 0.12 230)'});
|
|
256
|
+
else if (type === 'dust') this.spawn({glyph: 'dot', life: 0.5, x: -0.5 - rng() * 0.3, y: 1.2, vx: -1.6 - rng(), vy: -0.5, size: 3 + rng() * 1.6, paint: 'oklch(0.8 0.01 260)'});
|
|
257
|
+
else if (type === 'out') this.spawn({glyph: 'dot', life: 0.7, x: (rng() - 0.5) * 0.6, y: -0.7, vx: 1.6 + rng() * 1.2, vy: -2 - rng() * 1.4, size: 2.4 + rng() * 1.2});
|
|
258
|
+
else if (type === 'up') this.spawn({glyph: 'dot', life: 0.9, x: (rng() - 0.5) * 2.2, y: 0.8, vy: -2.4, size: 2.2 + rng() * 1.2});
|
|
259
|
+
else if (type === 'in') { const a = rng() * TAU, life = 0.6; this.spawn({glyph: 'dot', life, x: 2.3 * cos(a), y: 2.3 * sin(a), vx: -2.3 * cos(a) / life, vy: -2.3 * sin(a) / life, size: 3.2 + rng() * 1.6}); }
|
|
260
|
+
}
|
|
261
|
+
burst(type, count) {
|
|
262
|
+
const rng = this.rng;
|
|
263
|
+
for (let i = 0; i < count; i++) {
|
|
264
|
+
const a = (i + rng()) / count * TAU;
|
|
265
|
+
if (type === 'spark') this.spawn({glyph: 'star', life: 0.55 + rng() * 0.4, x: cos(a), y: sin(a) * 0.9 - 0.1, vx: cos(a) * (1.3 + rng()), vy: sin(a) * (1.3 + rng()), drag: 2, size: 4 + rng() * 4, turn: rng() * 90, spin: 90, bright: true});
|
|
266
|
+
else this.spawn({glyph: 'confetti', life: 1 + rng() * 0.5, x: (rng() - 0.5) * 0.8, y: -0.9, vx: (rng() - 0.5) * 4.6, vy: -2.4 - rng() * 2.4, g: 9, size: 2.4 + rng() * 1.6, turn: rng() * 360, spin: 520, paint: `oklch(0.78 0.17 ${CONFETTI_HUES[floor(rng() * 12)]})`});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
spawn(particle) {
|
|
270
|
+
if (this.particles.length >= PARTICLES) return;
|
|
271
|
+
this.particles.push({age: 0, x: 0, y: 0, vx: 0, vy: 0, size: 1, turn: 0, spin: 0, grow: 0, ...particle});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ---- being handled: the DOM side reports the pointer ----
|
|
275
|
+
/** Dragging across the tile's own width turns it half a revolution. */
|
|
276
|
+
drag(dx, dy, dt) {
|
|
277
|
+
const turn = dx / this.size * Math.PI;
|
|
278
|
+
this.p.yaw += turn; this.spun += abs(turn); this.dragSpeed = this.dragSpeed * 0.5 + turn / max(0.004, dt) * 0.5;
|
|
279
|
+
this.tug = clamp(this.tug - dy / this.size * 1.4, -0.7, 0.7);
|
|
280
|
+
}
|
|
281
|
+
release() { this.omega = clamp(this.dragSpeed, -34, 34); this.dragSpeed = 0; }
|
|
282
|
+
|
|
283
|
+
// ---- time ----
|
|
284
|
+
wanted(look) {
|
|
285
|
+
const want = {...REST, ...(POSES[this.state] || POSES.idle).pose(this, this.t, look)};
|
|
286
|
+
if (this.emphasized) want.scale *= 1.1;
|
|
287
|
+
return want;
|
|
288
|
+
}
|
|
289
|
+
// Where to look. Without anything to look at it glances around, and back at the viewer now and then.
|
|
290
|
+
attend(dt, look) {
|
|
291
|
+
let target = look;
|
|
292
|
+
if (!target) {
|
|
293
|
+
if ((this.strayIn -= dt) < 0) { this.strayIn = 1.2 + this.rng() * 2.6; this.stray = this.rng() < 0.35 ? {x: 0, y: 0} : {x: (this.rng() - 0.5) * 0.7, y: (this.rng() - 0.5) * 0.35}; }
|
|
294
|
+
target = this.stray;
|
|
295
|
+
}
|
|
296
|
+
this.aim.x = ease(this.aim.x, target.x, dt, 7); this.aim.y = ease(this.aim.y, target.y, dt, 7);
|
|
297
|
+
return this.aim;
|
|
298
|
+
}
|
|
299
|
+
spring(key, target, stiff, damp, dt) { this.vel[key] += (stiff * (target - this.p[key]) - damp * this.vel[key]) * dt; this.p[key] += this.vel[key] * dt; }
|
|
300
|
+
advance(dt, look) {
|
|
301
|
+
const p = this.p; this.dt = dt; this.t += dt; this.since += dt;
|
|
302
|
+
const want = this.wanted(this.attend(dt, look));
|
|
303
|
+
if (this.state === 'idle' && !this.hover && !this.press && (this.fidgetIn -= dt) < 0) { this.fidgetIn = 5 + this.rng() * 7; this.play(FIDGETS[floor(this.rng() * FIDGETS.length)]); }
|
|
304
|
+
if (this.hover) want.scale *= 1.08;
|
|
305
|
+
if (this.press && !this.dragging) { want.sy = min(want.sy, 0.55); want.lift += 10; }
|
|
306
|
+
const g = {...ZERO}; let acted = null;
|
|
307
|
+
this.gestures = this.gestures.filter(item => {
|
|
308
|
+
item.age += dt;
|
|
309
|
+
if (item.age >= item.def.dur) { item.def.end?.(this); return false; }
|
|
310
|
+
item.def.move(item.age / item.def.dur, g); acted = item.def.expr || acted;
|
|
311
|
+
return true;
|
|
312
|
+
});
|
|
313
|
+
// Spin too much and it gets dizzy.
|
|
314
|
+
this.spun = max(0, this.spun + abs(this.omega) * dt - 2.5 * dt);
|
|
315
|
+
if (this.spun > 15 && this.dizzy <= 0) { this.dizzy = 2.8; this.spun = 0; }
|
|
316
|
+
this.dizzy = max(0, this.dizzy - dt);
|
|
317
|
+
if (this.dizzy > 0) g.roll += 0.16 * sin(this.t * 9) * min(1, this.dizzy);
|
|
318
|
+
this.omega = ease(this.omega, 0, dt, 3);
|
|
319
|
+
if (!this.dragging) { p.yaw = ease(p.yaw, want.yaw + round((p.yaw - want.yaw) / TAU) * TAU, dt, want.rate) + this.omega * dt; this.tug = ease(this.tug, 0, dt, 4); }
|
|
320
|
+
p.pitch = ease(p.pitch, want.pitch + this.tug, dt, want.rate);
|
|
321
|
+
this.spring('sy', want.sy, want.stiff, sqrt(want.stiff), dt);
|
|
322
|
+
this.spring('lift', want.lift, 180, 12, dt);
|
|
323
|
+
this.spring('scale', want.scale, 220, 18, dt);
|
|
324
|
+
this.spring('roll', want.roll, 120, 12, dt);
|
|
325
|
+
p.sat = ease(p.sat, want.sat, dt, 5); p.blush = ease(p.blush, want.blush, dt, 6);
|
|
326
|
+
this.gx = ease(this.gx, want.gx + g.gx, dt, 10); this.gy = ease(this.gy, want.gy + g.gy, dt, 10);
|
|
327
|
+
// Expressions change behind a blink. Anything that takes over the face also takes over the screen.
|
|
328
|
+
if (this.override && (this.override.left -= dt) <= 0) this.override = null;
|
|
329
|
+
const pressed = this.press && !this.dragging;
|
|
330
|
+
this.forced = !!(this.override || this.dizzy > 0 || acted || pressed);
|
|
331
|
+
const expr = this.override?.name || (this.dizzy > 0 && 'dizzy') || acted || (pressed && 'squint') || (this.hover && want.expr === 'neutral' ? 'wide' : want.expr);
|
|
332
|
+
if (expr !== this.shown && (this.morphs || p.open < 0.2)) this.shown = expr;
|
|
333
|
+
if ((this.blinkIn -= dt) < 0) { this.blinkFor = want.still ? 0 : 0.13; this.blinkIn = this.rng() < 0.2 ? 0.3 : 2 + this.rng() * 3.5; }
|
|
334
|
+
this.blinkFor = max(0, this.blinkFor - dt);
|
|
335
|
+
p.open = ease(p.open, expr !== this.shown || this.blinkFor > 0 ? 0 : 1, dt, 32);
|
|
336
|
+
if (want.mark) this.markName = want.mark;
|
|
337
|
+
this.phase += dt * 2.6 * (want.orbit || 1);
|
|
338
|
+
const goal = {orbit: +(want.orbit > 0), mark: +!!want.mark, stars: +(this.dizzy > 0), emph: +this.emphasized};
|
|
339
|
+
for (const key in goal) this.aura[key] = ease(this.aura[key], goal[key], dt, 7);
|
|
340
|
+
this.particles = this.particles.filter(q => {
|
|
341
|
+
q.age += dt;
|
|
342
|
+
if (q.age >= q.life) return false;
|
|
343
|
+
if (q.g) q.vy += q.g * dt;
|
|
344
|
+
if (q.drag) { q.vx *= 1 - q.drag * dt; q.vy *= 1 - q.drag * dt; }
|
|
345
|
+
q.x += q.vx * dt; q.y += q.vy * dt;
|
|
346
|
+
return true;
|
|
347
|
+
});
|
|
348
|
+
this.g = g;
|
|
349
|
+
}
|
|
350
|
+
/** Advance by dt seconds. input: {look: {x, y} in -1..1 or null, hover, press, dragging}. */
|
|
351
|
+
step(dt, input = {}) {
|
|
352
|
+
this.hover = !!input.hover; this.press = !!input.press; this.dragging = !!input.dragging;
|
|
353
|
+
const slices = max(1, Math.ceil(dt / 0.017 - 1e-6));
|
|
354
|
+
for (let i = 0; i < slices; i++) this.advance(dt / slices, input.look ?? null);
|
|
355
|
+
return this.render(dt);
|
|
356
|
+
}
|
|
357
|
+
/** Jump straight to the resting pose of the current state. */
|
|
358
|
+
settle() {
|
|
359
|
+
this.dt = 0; const want = this.wanted({x: 0, y: 0});
|
|
360
|
+
Object.assign(this.p, {yaw: want.yaw, pitch: want.pitch, roll: want.roll, sy: want.sy, lift: want.lift, scale: want.scale, sat: want.sat, blush: want.blush, open: 1});
|
|
361
|
+
this.vel = {sy: 0, lift: 0, scale: 0, roll: 0}; this.omega = 0; this.dizzy = 0; this.tug = 0; this.gestures = []; this.particles = [];
|
|
362
|
+
this.override = null; this.forced = false; this.g = ZERO; this.gx = want.gx; this.gy = want.gy; this.shown = want.expr;
|
|
363
|
+
if (want.mark) this.markName = want.mark;
|
|
364
|
+
this.aura = {orbit: +(want.orbit > 0), mark: +!!want.mark, stars: 0, emph: +this.emphasized};
|
|
365
|
+
return this.render(null);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ---- the body and the face: a theme built on this one can draw others ----
|
|
369
|
+
/** Nodes for the body, drawn first on the tile layer. `node(layer, tag, attrs, parent)` may nest a node in an earlier one. */
|
|
370
|
+
tileLayout(ink, node) {
|
|
371
|
+
return [node('tile', 'path', {d: OUTLINES[this.shape](), fill: ink.body, stroke: ink.edge, 'stroke-width': 1.6})];
|
|
372
|
+
}
|
|
373
|
+
/** Fill the body nodes from `nodes[n]` on; returns the index after them. */
|
|
374
|
+
drawTile(nodes, n, {ink}) {
|
|
375
|
+
Object.assign(nodes[n++], {fill: ink.body, stroke: ink.edge});
|
|
376
|
+
return n;
|
|
377
|
+
}
|
|
378
|
+
/** The 5x5 screen this frame: a level 0..1 per cell, and which cells are blush. */
|
|
379
|
+
lit(yaw, pitch, facing) {
|
|
380
|
+
const p = this.p;
|
|
381
|
+
const on = new Float32Array(25), blush = new Uint8Array(25);
|
|
382
|
+
const light = (c, r, value) => { if (c >= 0 && c < 5 && r >= 0 && r < 5) on[r * 5 + c] = max(on[r * 5 + c], value); };
|
|
383
|
+
if (facing) {
|
|
384
|
+
const screen = !this.forced && SCREENS[this.state]?.(this.t, this.progress, this.since);
|
|
385
|
+
if (screen) for (const [c, r, value] of screen) light(c, r, value);
|
|
386
|
+
else {
|
|
387
|
+
// Positive yaw turns the face to the viewer's left, negative pitch tilts it up.
|
|
388
|
+
const ox = round(clamp(-sin(yaw) * 1.5 + this.gx * 5, -1, 1)), oy = round(clamp(pitch * 1.6 + this.gy * 5, -1, 1));
|
|
389
|
+
let name = this.shown; if (p.open < 0.35 && !SHUT.has(name)) name = 'closed';
|
|
390
|
+
const face = FACES[name] || FACES.neutral;
|
|
391
|
+
for (const side of [-1, 1]) {
|
|
392
|
+
const dots = face(side, this.eyes, this.t);
|
|
393
|
+
if (dots.screen) { for (const [c, r] of dots.screen) light(c, r, dots.level); break; }
|
|
394
|
+
for (const [x, y, value = 1] of dots) light((side < 0 ? 1 : 3) + side * x + ox, 2 + y + oy, value);
|
|
395
|
+
}
|
|
396
|
+
if (p.blush > 0.05) for (const c of [0, 4]) { const r = 3 + oy, i = r * 5 + c; if (r < 5 && on[i] === 0) { on[i] = 0.85 * p.blush; blush[i] = 1; } }
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return {on, blush};
|
|
400
|
+
}
|
|
401
|
+
/** Nodes for the face, drawn on the tile in this order. */
|
|
402
|
+
faceLayout(ink, node) {
|
|
403
|
+
return Array.from({length: 25}, (_, i) => node('tile', 'circle', {cx: 50 + (i % 5 - 2) * CELL, cy: 50 + (floor(i / 5) - 2) * CELL, fill: ink.dark}));
|
|
404
|
+
}
|
|
405
|
+
/** Fill the face nodes from `nodes[n]` on; returns the index after them. */
|
|
406
|
+
drawFace(nodes, n, {dt, ink, dim, yaw, pitch, facing}) {
|
|
407
|
+
const {on, blush} = this.lit(yaw, pitch, facing);
|
|
408
|
+
// Unlit dots stay visible so it always reads as a screen; lit ones keep a gap between them.
|
|
409
|
+
for (let i = 0; i < 25; i++) {
|
|
410
|
+
this.level[i] = dt === null ? on[i] : ease(this.level[i], on[i], dt, 22);
|
|
411
|
+
Object.assign(nodes[n++], {r: fixed(max(0, 1.9 + 2.6 * this.level[i])), opacity: (0.22 + 0.78 * this.level[i] * dim).toFixed(3), fill: blush[i] ? 'oklch(0.74 0.17 15)' : ink.dark});
|
|
412
|
+
}
|
|
413
|
+
return n;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// ---- drawing ----
|
|
417
|
+
/** The current frame. dt null jumps the dots straight to their levels. */
|
|
418
|
+
render(dt) {
|
|
419
|
+
const p = this.p, g = this.g, ink = paint(this.tone, 0.7 + 0.3 * p.sat), dim = 0.45 + 0.55 * p.sat;
|
|
420
|
+
const yaw = wrap(p.yaw + g.yaw - REST.yaw), pitch = wrap(p.pitch + g.pitch - REST.pitch);
|
|
421
|
+
const fx = cos(turnOf(yaw)), fy = cos(turnOf(pitch)), facing = fx > 0.2 && fy > 0.2;
|
|
422
|
+
// Under a third of the solids' squash and half their lean: a screen is rigid. Slumped poses
|
|
423
|
+
// (blocked, pressed) read from the sinking, the closed eyes and the dimmed dots instead.
|
|
424
|
+
const sy = clamp(1 + (p.sy + g.sy - 1) * 0.3, 0.88, 1.2), sx = 1 + (1 - sy) * 0.3, scale = max(0.1, p.scale * (1 + g.scale)), lift = p.lift + g.lift, roll = (p.roll + g.roll) * 90 / Math.PI;
|
|
425
|
+
// Squash from the bottom edge, turn about the centre.
|
|
426
|
+
const transform = `translate(50 ${fixed(50 + lift)}) rotate(${fixed(roll)}) translate(0 ${TILE}) scale(${sx.toFixed(4)} ${sy.toFixed(4)}) translate(0 ${-TILE}) scale(${(scale * fx).toFixed(4)} ${(scale * fy).toFixed(4)}) translate(-50 -50)`;
|
|
427
|
+
const nodes = this.layout.map(() => ({}));
|
|
428
|
+
let n = 0;
|
|
429
|
+
// Halo on the active Bot.
|
|
430
|
+
const halo = nodes[n++], cy = 50 + lift;
|
|
431
|
+
if (this.aura.emph < 0.02) halo.display = 'none';
|
|
432
|
+
else Object.assign(halo, {display: 'inline', cy: r1(cy), r: r1(TILE * scale + 5 + 1.5 * sin(this.t * 3)), stroke: ink.accent, 'stroke-width': r1(3 * this.aura.emph)});
|
|
433
|
+
const scene = {dt, ink, dim, yaw, pitch, facing, lift, cy, scale};
|
|
434
|
+
n = this.drawTile(nodes, n, scene);
|
|
435
|
+
n = this.drawFace(nodes, n, scene);
|
|
436
|
+
// Dots circling the tile; the far half is behind it.
|
|
437
|
+
for (let i = 0; i < 3; i++) {
|
|
438
|
+
const node = nodes[n++], a = this.phase + i * TAU / 3, z = sin(a);
|
|
439
|
+
if (this.aura.orbit < 0.02) { node.display = 'none'; continue; }
|
|
440
|
+
Object.assign(node, {display: 'inline', fill: ink.accent, cx: r1(50 + 44 * cos(a)), cy: r1(cy - 3 + 14 * z), r: fixed(max(0, (4.4 - 1.1 * z) * this.aura.orbit)), opacity: z < 0 ? 1 : 0.35});
|
|
441
|
+
}
|
|
442
|
+
for (let i = 0; i < 3; i++) {
|
|
443
|
+
const node = nodes[n++], a = this.t * 4 + i * TAU / 3, z = sin(a);
|
|
444
|
+
if (this.aura.stars < 0.02) { node.display = 'none'; continue; }
|
|
445
|
+
Object.assign(node, {display: 'inline', fill: ink.bright, transform: `translate(${r1(50 + 28 * cos(a))} ${r1(cy - TILE - 2 + 5 * z)}) scale(${((4 - 0.8 * z) * this.aura.stars).toFixed(2)})`});
|
|
446
|
+
}
|
|
447
|
+
// The mark: a glyph on a small card at the top right, inside the frame.
|
|
448
|
+
const [card, glyph, dot] = [nodes[n++], nodes[n++], nodes[n++]];
|
|
449
|
+
if (this.aura.mark < 0.02) for (const node of [card, glyph, dot]) node.display = 'none';
|
|
450
|
+
else {
|
|
451
|
+
const def = MARKS[this.markName] || MARKS.ask, colour = def.paint || ink.accent, k = (this.size <= 28 ? 1.25 : 1) * this.aura.mark;
|
|
452
|
+
const place = `translate(80 ${r1(cy - 30 + 1.5 * sin(this.t * 3))}) scale(${k.toFixed(2)})`;
|
|
453
|
+
Object.assign(card, {display: 'inline', fill: ink.card, stroke: ink.accent, 'stroke-width': 1.6, transform: place});
|
|
454
|
+
Object.assign(glyph, {display: def.d ? 'inline' : 'none', d: def.d, stroke: colour, fill: def.fill ? colour : 'none', 'stroke-width': def.wide ? 4.2 : def.fill ? 1.4 : 3.2,
|
|
455
|
+
transform: place + (def.dot && def.d ? ' rotate(12) scale(0.8)' : ' scale(0.8)')});
|
|
456
|
+
Object.assign(dot, def.dot ? {display: 'inline', cx: def.dot[0], cy: def.dot[1], r: def.dot[2], fill: colour, transform: place + (def.d ? ' rotate(12) scale(0.8)' : ' scale(0.8)')} : {display: 'none'});
|
|
457
|
+
}
|
|
458
|
+
for (let i = 0; i < PARTICLES; i++) {
|
|
459
|
+
const node = nodes[n++], q = this.particles[i];
|
|
460
|
+
if (!q) { node.display = 'none'; continue; }
|
|
461
|
+
const u = q.age / q.life, x = r1(50 + q.x * U), y = r1(50 + q.y * U);
|
|
462
|
+
if (q.type === 'ring') {
|
|
463
|
+
const radius = TILE + 2 + 12 * u;
|
|
464
|
+
Object.assign(node, {display: 'inline', d: `M${r1(50 - radius)},${r1(cy)}a${r1(radius)},${r1(radius)} 0 1,0 ${r1(2 * radius)},0a${r1(radius)},${r1(radius)} 0 1,0 ${r1(-2 * radius)},0`,
|
|
465
|
+
transform: '', fill: 'none', stroke: ink.accent, 'stroke-width': fixed(2.2 * (1 - u)), opacity: fixed(1 - u)});
|
|
466
|
+
} else if (q.stroked) {
|
|
467
|
+
Object.assign(node, {display: 'inline', d: GLYPHS[q.glyph], fill: 'none', stroke: ink.accent, 'stroke-width': 0.62, opacity: fixed(bump(u)),
|
|
468
|
+
transform: `translate(${x} ${y}) rotate(${r1(q.turn)}) scale(${(q.size * GROW * (1 - q.grow * 0.4 + q.grow * u)).toFixed(2)})`});
|
|
469
|
+
} else {
|
|
470
|
+
Object.assign(node, {display: 'inline', d: GLYPHS[q.glyph], stroke: 'none', opacity: 1, fill: q.paint || (q.bright ? ink.bright : ink.accent),
|
|
471
|
+
transform: `translate(${x} ${y}) rotate(${r1(q.turn + q.spin * q.age)}) scale(${(q.size * GROW * sqrt(bump(u))).toFixed(2)})`});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return {transform, nodes};
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** A matrix model. Options: identity, hue, chroma, light, dots, edge, shape, eyes, state, progress, size, emphasized. */
|
|
479
|
+
export const createMatrix = options => new Matrix(options);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Types for '@kodax-ai/botui/themes/matrix'.
|
|
2
|
+
|
|
3
|
+
import type {Avatar, AvatarOptions, ColorName, EyeStyle, ExpressionName, IdentityPool, Theme, StateName} from '../../index.js';
|
|
4
|
+
|
|
5
|
+
export type {Avatar, AvatarOptions, ColorName, EyeStyle, ExpressionName, GestureName, IdentityPool, StateName, StateAlias, StateGroup, Theme} from '../../index.js';
|
|
6
|
+
export {setAttention, resolveState, STATES, STATE_GROUPS, ALIASES, COLORS, BIRTH_COLORS, HUES, EYE_STYLES, GESTURES} from '../../index.js';
|
|
7
|
+
|
|
8
|
+
export type MatrixShape = 'squircle' | 'circle' | 'hex';
|
|
9
|
+
/** `shape` is a tile outline; `signals` and `eyeColor` are ignored, the screen shows signals itself. */
|
|
10
|
+
export interface MatrixOptions extends Omit<AvatarOptions, 'shape' | 'signals' | 'eyeColor'> { shape?: MatrixShape; }
|
|
11
|
+
export interface MatrixAvatar extends Avatar { readonly shape: MatrixShape; }
|
|
12
|
+
export const MatrixAvatar: {new (options?: MatrixOptions): MatrixAvatar};
|
|
13
|
+
|
|
14
|
+
export const theme: Theme;
|
|
15
|
+
export function createAvatar(options?: MatrixOptions): MatrixAvatar;
|
|
16
|
+
export function identityFrom(identity: string, pool?: IdentityPool): {shape: MatrixShape; color: ColorName; eyes: EyeStyle};
|
|
17
|
+
export const SHAPES: {name: MatrixShape; zh: string; group: 'tile'}[];
|
|
18
|
+
export const GROUPS: Record<'tile', string>;
|
|
19
|
+
export const EXPRESSIONS: Record<ExpressionName, Function>;
|
|
20
|
+
|
|
21
|
+
// The pure model, for drawing the matrix without the DOM wrapper.
|
|
22
|
+
/** A tile's colours: hue 0..360, OKLCH chroma and lightness, optional dot colour and edge. */
|
|
23
|
+
export interface MatrixTone { hue: number; chroma?: number; light?: number; dots?: string; edge?: string; }
|
|
24
|
+
export interface MatrixFrame { transform: string; nodes: Record<string, string | number>[]; }
|
|
25
|
+
export interface MatrixModel {
|
|
26
|
+
readonly state: StateName;
|
|
27
|
+
/** Nodes to build, in order. `parent` nests a node in an earlier entry (defs, clipPath, gradients); an attr named `text` is the text content. */
|
|
28
|
+
readonly layout: {layer: 'back' | 'tile' | 'front'; tag: string; attrs: Record<string, string | number>; parent?: object | null}[];
|
|
29
|
+
step(dt: number, input?: {look?: {x: number; y: number} | null; hover?: boolean; press?: boolean; dragging?: boolean}): MatrixFrame;
|
|
30
|
+
settle(): MatrixFrame;
|
|
31
|
+
render(dt: number | null): MatrixFrame;
|
|
32
|
+
setState(name: string): this;
|
|
33
|
+
play(name: string): this;
|
|
34
|
+
express(name: string | null, ms?: number): this;
|
|
35
|
+
blink(): this;
|
|
36
|
+
nudge(): this;
|
|
37
|
+
poke(action?: string, options?: {turns?: number}): this;
|
|
38
|
+
drag(dx: number, dy: number, dt: number): void;
|
|
39
|
+
release(): void;
|
|
40
|
+
}
|
|
41
|
+
export function createMatrix(options: MatrixTone & {identity?: string; shape: MatrixShape; eyes?: EyeStyle; state?: string; progress?: number | null; size?: number; emphasized?: boolean}): MatrixModel;
|
|
42
|
+
export const matrixShapes: readonly MatrixShape[];
|
|
43
|
+
export function matrixShapeForId(identity: string): MatrixShape;
|
|
44
|
+
export const matrixEyes: readonly EyeStyle[];
|
|
45
|
+
export function paint(tone: MatrixTone, sat?: number): {body: string; edge: string; dark: string; accent: string; bright: string; card: string};
|
|
46
|
+
export const FACES: Record<ExpressionName, Function>;
|
|
47
|
+
export const SCREENS: Record<string, Function>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// BotUI matrix theme: a 5x5 dot screen on a coloured tile.
|
|
2
|
+
//
|
|
3
|
+
// import {createAvatar} from './themes/matrix/index.js';
|
|
4
|
+
// const bot = createAvatar({identity: 'research-bot', size: 36});
|
|
5
|
+
// sidebar.append(bot.el);
|
|
6
|
+
// bot.setState('thinking');
|
|
7
|
+
|
|
8
|
+
import {MatrixAvatar} from './avatar.js';
|
|
9
|
+
import {COLORS, EYE_STYLES} from '../../core/identity.js';
|
|
10
|
+
import {STATES} from '../../core/states.js';
|
|
11
|
+
import {registerTheme} from '../../core/registry.js';
|
|
12
|
+
import {GESTURES} from '../../core/motion.js';
|
|
13
|
+
import {FACES, SHAPES} from './engine.js';
|
|
14
|
+
|
|
15
|
+
export {MatrixAvatar, identityFrom} from './avatar.js';
|
|
16
|
+
export {COLORS, BIRTH_COLORS, HUES, EYE_STYLES} from '../../core/identity.js';
|
|
17
|
+
export {STATES, STATE_GROUPS, ALIASES, resolveState} from '../../core/states.js';
|
|
18
|
+
export {setAttention} from '../../core/core.js';
|
|
19
|
+
export {GESTURES} from '../../core/motion.js';
|
|
20
|
+
export {createMatrix, matrixShapes, matrixShapeForId, matrixEyes, paint, FACES, SCREENS, SHAPES, GROUPS} from './engine.js';
|
|
21
|
+
|
|
22
|
+
/** The expressions, by name; on this screen each one is a dot pattern (see FACES). */
|
|
23
|
+
export const EXPRESSIONS = FACES;
|
|
24
|
+
|
|
25
|
+
/** Create an avatar. See MatrixAvatar for the options. Append `.el` to the page. */
|
|
26
|
+
export function createAvatar(options) { return new MatrixAvatar(options); }
|
|
27
|
+
|
|
28
|
+
/** This theme, in the form core/registry.js expects. */
|
|
29
|
+
export const theme = {
|
|
30
|
+
name: 'matrix', zh: '点阵', create: createAvatar,
|
|
31
|
+
catalog: () => ({shapes: SHAPES, colors: Object.keys(COLORS), eyes: Object.keys(EYE_STYLES), states: Object.keys(STATES), gestures: Object.keys(GESTURES), expressions: Object.keys(FACES)}),
|
|
32
|
+
};
|
|
33
|
+
// Importing a theme makes it available to createAvatar({theme}) from the package entry.
|
|
34
|
+
registerTheme(theme);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# 光晕 orb
|
|
2
|
+
|
|
3
|
+
方块里三团相邻色相的柔光在缓缓流动,前面是和立体家族一样的眼睛。状态决定光的情绪:工作时转得快,困了慢下来,生气和出错偏暖,难过和害怕偏冷,庆祝时色相散开。说话时有一张嘴,进度时外圈一道环慢慢填满。
|
|
4
|
+
|
|
5
|
+
来自 avatar-lab 的草图方向 AC+(光晕带眼睛),2026-11-28 做成主题。
|
|
6
|
+
|
|
7
|
+
## 用法
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import {createAvatar} from '@kodax-ai/botui/themes/orb'; // 或相对路径 ./themes/orb/index.js
|
|
11
|
+
|
|
12
|
+
const bot = createAvatar({identity: 'research-bot', size: 36});
|
|
13
|
+
sidebar.append(bot.el);
|
|
14
|
+
bot.setState('thinking');
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
从总入口用:先 `import '@kodax-ai/botui/themes/orb'`,再 `createAvatar({theme: 'orb', ...})`。自定义元素:同样先加载主题脚本,再写 `<bot-avatar theme="orb" identity="research-bot"></bot-avatar>`。
|
|
18
|
+
|
|
19
|
+
## 与点阵的关系
|
|
20
|
+
|
|
21
|
+
外形(圆角方、圆、六边形)、颜色、动作、记号、粒子、交互、水印都和 [点阵](../matrix/README.md) 一样,选项和方法也完全相同;同一个 Bot ID 在两个主题里是同一个外形、颜色和眼型。不同的是:
|
|
22
|
+
|
|
23
|
+
| 项 | 光晕 |
|
|
24
|
+
|---|---|
|
|
25
|
+
| 方块 | 底色上三团径向渐变的光(本色、偏亮、偏深的两个邻近色相),沿各自的轨迹绕转,左上一点高光 |
|
|
26
|
+
| 眼睛 | 直接用立体家族的 `eyeShape`:21 种眼神、三种眼型都和立体家族同形 |
|
|
27
|
+
| 情绪(`MOODS`) | 每个状态一组:转速、色相散开程度、亮度、偏向的色相(生气 / 出错偏红,难过 / 害怕偏蓝,喜爱偏粉) |
|
|
28
|
+
| 嘴 | 只在说话的状态出现(输出、听写、哼歌、大笑、打招呼),一张一合 |
|
|
29
|
+
| 信号状态 | 屏幕不放图案,靠光的情绪、眼神和右上角记号表达;进度状态外圈有一道填充环 |
|
|
30
|
+
|
|
31
|
+
渐变和裁剪需要 SVG id,每个光晕实例用一个递增计数生成唯一 id,所以同一页面放多少个都不会串。除此之外模型是纯的:不碰 DOM、不用时钟和 `Math.random`。
|
|
32
|
+
|
|
33
|
+
## 纯模型
|
|
34
|
+
|
|
35
|
+
`createOrb(options)` 与 `createMatrix` 用法相同。节点里有 `defs`、`clipPath`、`radialGradient`、`stop` 和一个裁剪用的 `g`,这些节点带 `parent` 指向它所嵌套的节点。
|
|
36
|
+
|
|
37
|
+
## 文件
|
|
38
|
+
|
|
39
|
+
| 文件 | 内容 |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `index.js` | 入口:`createAvatar`、`theme` 和各种表 |
|
|
42
|
+
| `avatar.js` | `OrbAvatar`:继承点阵的 `MatrixAvatar`,只换模型 |
|
|
43
|
+
| `engine.js` | 光晕:情绪表 `MOODS`、流动的光、眼睛、嘴、进度环 |
|
|
44
|
+
| `playground.html` | 试玩页(共用 `../solids/playground.js`) |
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {MatrixAvatar} from '../matrix/avatar.js';
|
|
2
|
+
import {createOrb} from './engine.js';
|
|
3
|
+
|
|
4
|
+
export {identityFrom} from '../matrix/avatar.js';
|
|
5
|
+
|
|
6
|
+
/** The aura avatar: the matrix avatar with drifting light inside and the solids' eyes. Options and methods as MatrixAvatar's. */
|
|
7
|
+
export class OrbAvatar extends MatrixAvatar {
|
|
8
|
+
static className = 'botui-orb';
|
|
9
|
+
createModel(options) { return createOrb(options); }
|
|
10
|
+
}
|