open-bot-avatar 0.2.0 → 0.3.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.
Files changed (69) hide show
  1. package/README.md +25 -6
  2. package/dist/animator.d.ts +9 -4
  3. package/dist/animator.d.ts.map +1 -1
  4. package/dist/animator.js +32 -17
  5. package/dist/animator.js.map +1 -1
  6. package/dist/color.d.ts +4 -0
  7. package/dist/color.d.ts.map +1 -1
  8. package/dist/color.js +45 -0
  9. package/dist/color.js.map +1 -1
  10. package/dist/contour.d.ts +6 -1
  11. package/dist/contour.d.ts.map +1 -1
  12. package/dist/contour.js +83 -7
  13. package/dist/contour.js.map +1 -1
  14. package/dist/dom/mount.d.ts +8 -2
  15. package/dist/dom/mount.d.ts.map +1 -1
  16. package/dist/dom/mount.js +29 -8
  17. package/dist/dom/mount.js.map +1 -1
  18. package/dist/dom/paint.d.ts.map +1 -1
  19. package/dist/dom/paint.js +18 -5
  20. package/dist/dom/paint.js.map +1 -1
  21. package/dist/effects.d.ts +82 -0
  22. package/dist/effects.d.ts.map +1 -0
  23. package/dist/effects.js +352 -0
  24. package/dist/effects.js.map +1 -0
  25. package/dist/eyes.d.ts +29 -0
  26. package/dist/eyes.d.ts.map +1 -0
  27. package/dist/eyes.js +128 -0
  28. package/dist/eyes.js.map +1 -0
  29. package/dist/index.d.ts +3 -3
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/mood.d.ts +10 -5
  34. package/dist/mood.d.ts.map +1 -1
  35. package/dist/mood.js +3 -2
  36. package/dist/mood.js.map +1 -1
  37. package/dist/motion.d.ts +63 -10
  38. package/dist/motion.d.ts.map +1 -1
  39. package/dist/motion.js +239 -41
  40. package/dist/motion.js.map +1 -1
  41. package/dist/path.d.ts +6 -0
  42. package/dist/path.d.ts.map +1 -1
  43. package/dist/path.js +13 -3
  44. package/dist/path.js.map +1 -1
  45. package/dist/render.d.ts +68 -4
  46. package/dist/render.d.ts.map +1 -1
  47. package/dist/render.js +265 -34
  48. package/dist/render.js.map +1 -1
  49. package/dist/types.d.ts +35 -7
  50. package/dist/types.d.ts.map +1 -1
  51. package/dist/volume.d.ts +2 -0
  52. package/dist/volume.d.ts.map +1 -1
  53. package/dist/volume.js +35 -14
  54. package/dist/volume.js.map +1 -1
  55. package/package.json +1 -1
  56. package/src/animator.ts +40 -20
  57. package/src/color.ts +48 -0
  58. package/src/contour.ts +74 -7
  59. package/src/dom/mount.ts +38 -11
  60. package/src/dom/paint.ts +17 -5
  61. package/src/effects.ts +405 -0
  62. package/src/eyes.ts +157 -0
  63. package/src/index.ts +3 -3
  64. package/src/mood.ts +13 -7
  65. package/src/motion.ts +270 -48
  66. package/src/path.ts +14 -3
  67. package/src/render.ts +335 -42
  68. package/src/types.ts +42 -7
  69. package/src/volume.ts +43 -14
package/src/motion.ts CHANGED
@@ -1,9 +1,20 @@
1
1
  /**
2
- * Deterministic pose model. A pose is a pure function of (seed, time, motion,
3
- * gaze, reducedMotion): no wall clock, no Math.random, so any frame can be
4
- * reproduced exactly (tests, SSR, scrubbing).
2
+ * Deterministic pose model. A pose is a pure function of (seed, time, state,
3
+ * mood, gaze, reducedMotion): no wall clock, no Math.random, so any frame can
4
+ * be reproduced exactly (tests, SSR, scrubbing).
5
5
  */
6
- import type { AvatarMotion, AvatarMotionBlend } from './types.ts';
6
+ import type { AvatarMood, AvatarMotion, AvatarMotionBlend, AvatarState } from './types.ts';
7
+
8
+ /**
9
+ * How the eyes are drawn, as numbers that blend: any two looks mix into a shape in between, so
10
+ * the eyes morph from one to the other (see eyes.ts).
11
+ */
12
+ export interface EyeLook {
13
+ /** 0 = the plain pill, 1 = the happy arc. */
14
+ arc: number;
15
+ /** 0 = the plain pill, 1 = the flat slit of a waiting bot. Never more than `1 - arc`. */
16
+ slit: number;
17
+ }
7
18
 
8
19
  export interface Pose {
9
20
  /** View rotation in radians: yaw turns about the vertical axis, pitch about the horizontal. */
@@ -24,7 +35,42 @@ export interface Pose {
24
35
  /** Extra lid narrowing per eye, 0..1. */
25
36
  squintL: number;
26
37
  squintR: number;
27
- eyeShape: 'pill' | 'happy';
38
+ eyes: EyeLook;
39
+ /** Presence of the ribbons that circle the body: 0..1 draws them out and back in. */
40
+ ribbons: number;
41
+ /** Which layout the ribbons take: 0 a working turn's one hoop, 1 a celebration's cage of three. */
42
+ cage: number;
43
+ /**
44
+ * How far the body's turn has carried the ribbons round, radians, signed and not wrapped: each
45
+ * ribbon goes its own share of it, so a whole turn more does not put them back where they were.
46
+ */
47
+ sweep: number;
48
+ /** How far the body has given way to a mark, 0..1 each: the exclamation mark and the three dots. */
49
+ bang: number;
50
+ dots: number;
51
+ }
52
+
53
+ /** Every state and mood, in the order hosts list them. */
54
+ export const STATES: readonly AvatarState[] = ['idle', 'thinking', 'working', 'waiting', 'blocked', 'done'];
55
+ export const MOODS: readonly AvatarMood[] = ['neutral', 'happy'];
56
+
57
+ export function isState(value: unknown): value is AvatarState {
58
+ return STATES.includes(value as AvatarState);
59
+ }
60
+
61
+ export function isMood(value: unknown): value is AvatarMood {
62
+ return MOODS.includes(value as AvatarMood);
63
+ }
64
+
65
+ /**
66
+ * The state and mood to show. `motion` is the single option that came before them: `thinking`
67
+ * is a state, `happy` a mood. An explicit `state` or `mood` wins over it.
68
+ */
69
+ export function resolveLook(given: { state?: unknown; mood?: unknown; motion?: unknown }): { state: AvatarState; mood: AvatarMood } {
70
+ return {
71
+ state: isState(given.state) ? given.state : given.motion === 'thinking' ? 'thinking' : 'idle',
72
+ mood: isMood(given.mood) ? given.mood : given.motion === 'happy' ? 'happy' : 'neutral',
73
+ };
28
74
  }
29
75
 
30
76
  /** Resting view: turned a little and seen a little from above, so the volume reads without dominating. */
@@ -61,6 +107,7 @@ export function noise(seed: number, n: number): number {
61
107
 
62
108
  const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
63
109
  const smooth = (u: number) => { const x = clamp(u, 0, 1); return x * x * (3 - 2 * x); };
110
+ const mix = (a: number, b: number, k: number) => a + (b - a) * k;
64
111
 
65
112
  /** Blink closure 0..1: one blink per ~4.2 s slot at a seeded offset, sometimes a double blink. */
66
113
  export function blinkAt(seed: number, t: number): number {
@@ -91,51 +138,141 @@ export function wanderAt(seed: number, t: number): [number, number] {
91
138
  return [a[0] + (b[0] - a[0]) * e, a[1] + (b[1] - a[1]) * e];
92
139
  }
93
140
 
141
+ /** Working: the eyes dart from one thing to the next, mostly down at the work. */
142
+ function dartAt(seed: number, t: number): [number, number] {
143
+ const SLOT = 0.62;
144
+ const s = Math.floor(t / SLOT);
145
+ const target = (k: number): [number, number] => [(noise(seed, 2001 + k * 3) * 2 - 1) * 0.8, 0.1 + noise(seed, 2002 + k * 3) * 0.5];
146
+ const a = target(s - 1), b = target(s);
147
+ const e = smooth((t - s * SLOT) / 0.12);
148
+ return [mix(a[0], b[0], e), mix(a[1], b[1], e)];
149
+ }
150
+
94
151
  export interface PoseInput {
95
152
  seed: number;
96
153
  time: number;
97
- motion: AvatarMotion;
154
+ state?: AvatarState;
155
+ mood?: AvatarMood;
156
+ /** @deprecated Use `state` and `mood`; they win when given. */
157
+ motion?: AvatarMotion;
158
+ /** Seconds since `state` began: `done` celebrates from then on. Without it the state is at rest. */
159
+ stateAge?: number;
98
160
  /** Look target, -1..1 per axis (x right, y down); `weight` 0..1 fades it in over the idle pose (default 1). */
99
161
  gaze?: { x: number; y: number; weight?: number };
100
162
  reducedMotion: boolean;
101
163
  /** Extra view rotation in radians (e.g. from drag-to-turn), added to the resting view. */
102
164
  view?: { yaw: number; pitch: number };
103
- /** Easing in `motion` from the mood shown before (see blendPoses). */
165
+ /** Easing in from what was shown before (see blendPoses). */
104
166
  blend?: AvatarMotionBlend;
105
167
  }
106
168
 
107
169
  /**
108
- * Pose part way from `a` to `b`, both taken at the same moment so either
109
- * side's motion keeps running. A change of eye shape swaps halfway, inside a
110
- * blink: pills close toward the swap and open after it.
170
+ * Pose part way from `a` to `b`, both taken at the same moment so either side's motion keeps
171
+ * running. Everything is a number, the eyes' look included, so eyes morph from one shape to the
172
+ * next and a body gives way to a mark (and back) part by part.
111
173
  */
112
174
  export function blendPoses(a: Pose, b: Pose, t: number): Pose {
113
175
  const k = clamp(t, 0, 1);
114
- const mix = (x: number, y: number) => x + (y - x) * k;
115
- const pose: Pose = {
116
- yaw: mix(a.yaw, b.yaw), pitch: mix(a.pitch, b.pitch), roll: mix(a.roll, b.roll), lift: mix(a.lift, b.lift),
117
- sx: mix(a.sx, b.sx), sy: mix(a.sy, b.sy), gazeX: mix(a.gazeX, b.gazeX), gazeY: mix(a.gazeY, b.gazeY),
118
- blink: mix(a.blink, b.blink), squintL: mix(a.squintL, b.squintL), squintR: mix(a.squintR, b.squintR),
119
- eyeShape: b.eyeShape,
176
+ const m = (x: number, y: number) => mix(x, y, k);
177
+ // The ribbons stay where the side that shows them has carried them: mixing a celebration's many
178
+ // turns with a pose that has none would wind them back round while they fade.
179
+ const sweep = a.ribbons > 0 && b.ribbons > 0 ? m(a.sweep, b.sweep) : a.ribbons > 0 ? a.sweep : b.sweep;
180
+ // Likewise their layout: a celebration fading out keeps its cage.
181
+ const cage = a.ribbons > 0 && b.ribbons > 0 ? m(a.cage, b.cage) : a.ribbons > 0 ? a.cage : b.cage;
182
+ return {
183
+ yaw: m(a.yaw, b.yaw), pitch: m(a.pitch, b.pitch), roll: m(a.roll, b.roll), lift: m(a.lift, b.lift),
184
+ sx: m(a.sx, b.sx), sy: m(a.sy, b.sy), gazeX: m(a.gazeX, b.gazeX), gazeY: m(a.gazeY, b.gazeY),
185
+ blink: m(a.blink, b.blink), squintL: m(a.squintL, b.squintL), squintR: m(a.squintR, b.squintR),
186
+ eyes: { arc: m(a.eyes.arc, b.eyes.arc), slit: m(a.eyes.slit, b.eyes.slit) },
187
+ ribbons: m(a.ribbons, b.ribbons), cage, sweep, bang: m(a.bang, b.bang), dots: m(a.dots, b.dots),
120
188
  };
121
- if (a.eyeShape !== b.eyeShape) {
122
- pose.eyeShape = k < 0.5 ? a.eyeShape : b.eyeShape;
123
- pose.blink = Math.max(pose.blink, 1 - Math.abs(2 * k - 1));
124
- }
125
- return pose;
126
189
  }
127
190
 
128
191
  export function computePose(input: PoseInput): Pose {
129
- const pose = moodPose(input);
192
+ const pose = statePose(input);
130
193
  const blend = input.blend;
131
194
  if (!blend || !(blend.t < 1)) return pose;
132
195
  const from = typeof blend.from === 'string' ? { motion: blend.from } : blend.from;
133
- return blendPoses(computePose({ ...input, motion: from.motion, blend: from.blend }), pose, Number.isFinite(blend.t) ? blend.t : 1);
196
+ // A change of mood alone keeps the state's clock running on both sides, so a turn under way
197
+ // stays one turn; a state that was left is held at the age it had.
198
+ const stateAge = resolveLook(from).state === resolveLook(input).state ? input.stateAge : from.age;
199
+ const before = computePose({ ...input, state: from.state, mood: from.mood, motion: from.motion, stateAge, blend: from.blend });
200
+ return blendPoses(before, pose, Number.isFinite(blend.t) ? blend.t : 1);
201
+ }
202
+
203
+ /**
204
+ * Done celebrates in cycles, timed as PR #14 measured the Grok Bot demo: the ribbons go round
205
+ * DONE_SWEEPS times while the body turns DONE_SWEEPS / 3 times (see celebration), then they draw
206
+ * back in and the bot rests. DONE_TURN_S covers the turn and the drawing in. A cycle lasts
207
+ * DONE_CYCLE_S or up to a seventh longer, differently for each avatar, so several done bots on a
208
+ * page drift apart instead of turning together.
209
+ */
210
+ export const DONE_TURN_S = 3.87;
211
+ export const DONE_CYCLE_S = 6.2;
212
+ const DONE_SWEEPS = 9;
213
+ /**
214
+ * How long the ribbons take to draw back in, seconds: they start once the turn has slowed below
215
+ * about 0.9 rad/s, 3.37 s in, and are gone at DONE_TURN_S.
216
+ */
217
+ const DONE_DRAW_IN_S = 0.5;
218
+
219
+ /**
220
+ * A working bot gets on with it and every so often throws itself into the work: a band of
221
+ * ribbons sweeps once round it, easing in like a spring settling, then draws back in. The first comes WORK_FIRST_S
222
+ * (plus up to WORK_FIRST_JITTER_S) after the state begins, then one every WORK_GAP_S, each up to
223
+ * WORK_JITTER_S late, so several working bots do not turn together. A turn takes WORK_TURN_S.
224
+ */
225
+ const WORK_FIRST_S = 1.2;
226
+ const WORK_FIRST_JITTER_S = 1.2;
227
+ const WORK_GAP_S = 7.5;
228
+ const WORK_JITTER_S = 1.5;
229
+ const WORK_TURN_S = 1.5;
230
+ /** How quickly a working turn eases in to its full circle, per second: a critically damped spring's rate. */
231
+ const WORK_EASE = 6.2;
232
+
233
+ /**
234
+ * Where a working bot is in its turns `age` seconds into the state: how far into the current
235
+ * turn (seconds), or null between turns.
236
+ */
237
+ function workTurn(seed: number, age: number): number | null {
238
+ const first = WORK_FIRST_S + WORK_FIRST_JITTER_S * noise(seed, 41);
239
+ if (age < first) return null;
240
+ const n = Math.floor((age - first) / WORK_GAP_S);
241
+ // A turn may start up to WORK_JITTER_S late, so the one before may still be running.
242
+ for (const k of [n, n - 1]) {
243
+ if (k < 0) continue;
244
+ const start = first + k * WORK_GAP_S + (k ? WORK_JITTER_S * noise(seed, 60 + k) : 0);
245
+ if (age >= start && age < start + WORK_TURN_S) return age - start;
246
+ }
247
+ return null;
248
+ }
249
+
250
+ /** An angle as the nearest way round to it, -π..π, so a blend never unwinds whole turns. */
251
+ const near = (angle: number) => angle - TAU * Math.round(angle / TAU);
252
+
253
+ /**
254
+ * How far a celebration's ribbons have gone round `t` seconds in, radians, on PR #14's curve: half
255
+ * a radian of wind-up the other way over 0.24 s, a speed-up to a steady run, then from 2.54 s a
256
+ * slow-down that lands, at 3.79 s, on DONE_SWEEPS whole turns.
257
+ */
258
+ const CELEBRATION_END_S = 3.79;
259
+ const CELEBRATION_SPEED = (DONE_SWEEPS * TAU + 0.5) / 2.4625;
260
+ function celebration(t: number): number {
261
+ const v = CELEBRATION_SPEED;
262
+ if (t <= 0) return 0;
263
+ if (t < 0.24) return -0.25 * (1 - Math.cos((Math.PI * t) / 0.24));
264
+ if (t < 0.54) return -0.5 + (v * (t - 0.24) ** 2) / 0.6;
265
+ if (t < 2.54) return -0.5 + v * (t - 0.39);
266
+ if (t < CELEBRATION_END_S) return -0.5 + v * 2.15 + (1.25 * v * (1 - (1 - (t - 2.54) / 1.25) ** 4)) / 4;
267
+ return DONE_SWEEPS * TAU;
134
268
  }
135
269
 
136
- function moodPose({ seed, time, motion, gaze, reducedMotion, view }: PoseInput): Pose {
270
+ function statePose(input: PoseInput): Pose {
271
+ const { seed, time, gaze, reducedMotion, view } = input;
272
+ const { state, mood } = resolveLook(input);
137
273
  // Per-avatar phase offset so several avatars on a page do not move in lockstep.
138
274
  const t = reducedMotion ? 0 : time + noise(seed, 7) * 10;
275
+ const live = reducedMotion ? 0 : 1;
139
276
  const wave = (period: number, phase = 0) => (reducedMotion ? 0 : Math.sin((TAU * t) / period + phase));
140
277
  const given = gaze && Number.isFinite(gaze.x) && Number.isFinite(gaze.y)
141
278
  ? [clamp(gaze.x, -1, 1), clamp(gaze.y, -1, 1)] as [number, number]
@@ -146,55 +283,140 @@ function moodPose({ seed, time, motion, gaze, reducedMotion, view }: PoseInput):
146
283
 
147
284
  const pose: Pose = {
148
285
  yaw: BASE_YAW, pitch: BASE_PITCH, roll: 0, lift: 0, sx: 1, sy: 1,
149
- gazeX: 0, gazeY: 0, blink: 0, squintL: 0, squintR: 0, eyeShape: 'pill',
286
+ gazeX: 0, gazeY: 0, blink: 0, squintL: 0, squintR: 0,
287
+ eyes: { arc: mood === 'happy' ? 1 : 0, slit: 0 }, ribbons: 0, cage: 0, sweep: 0, bang: 0, dots: 0,
288
+ };
289
+ /** The calm pose of a bot with nothing to do: it breathes, sways, blinks and looks around. */
290
+ const rest = (pace: number) => {
291
+ pose.yaw += 0.07 * sway * wave(6.1 * pace);
292
+ pose.pitch += 0.04 * sway * wave(4.7 * pace, 1);
293
+ pose.roll = 1.6 * wave(5.3 * pace, 2);
294
+ pose.lift = reducedMotion ? 0 : -0.45 * (1 - Math.cos((TAU * t) / (3.4 * pace)));
295
+ const breath = wave(3.4 * pace, -Math.PI / 2);
296
+ pose.sy = 1 + 0.012 * breath;
297
+ pose.sx = 1 - 0.008 * breath;
298
+ if (!reducedMotion) {
299
+ pose.blink = blinkAt(seed, t);
300
+ [pose.gazeX, pose.gazeY] = wanderAt(seed, t);
301
+ }
150
302
  };
151
303
 
152
- if (motion === 'happy') {
304
+ if (state === 'idle' && mood === 'happy') {
305
+ // With nothing to do, a happy bot hops.
153
306
  const period = 0.92;
154
307
  const p = reducedMotion ? 0.5 : ((t % period) + period) % period / period;
155
308
  const air = 4 * p * (1 - p);
156
309
  const land = reducedMotion ? 0 : clamp(1 - Math.min(p, 1 - p) / 0.12, 0, 1);
157
310
  pose.lift = reducedMotion ? 0 : -3.2 * air;
158
- pose.sy = 1 - 0.09 * land + 0.03 * air * (reducedMotion ? 0 : 1);
159
- pose.sx = 1 + 0.07 * land - 0.015 * air * (reducedMotion ? 0 : 1);
311
+ pose.sy = 1 - 0.09 * land + 0.03 * air * live;
312
+ pose.sx = 1 + 0.07 * land - 0.015 * air * live;
160
313
  pose.roll = 4.5 * wave(period * 2);
161
314
  pose.yaw += 0.1 * wave(period * 2, 0.6);
162
- pose.eyeShape = 'happy';
163
- } else if (motion === 'thinking') {
315
+ } else if (state === 'thinking') {
316
+ // Looks up and away while it gives way to the three dots.
164
317
  pose.yaw += 0.12 + 0.05 * sway * wave(7.3);
165
318
  pose.pitch += 0.08;
166
319
  pose.roll = -5 + 1.4 * wave(4.1);
167
- pose.lift = -0.5 * (1 - Math.cos((TAU * t) / 5.2)) * (reducedMotion ? 0 : 1);
320
+ pose.lift = -0.5 * (1 - Math.cos((TAU * t) / 5.2)) * live;
168
321
  pose.gazeX = 0.55 + 0.3 * wave(5.6);
169
322
  pose.gazeY = -0.8;
170
323
  pose.squintL = 0.22;
171
324
  pose.squintR = 0.32 + 0.1 * wave(3.1);
172
325
  if (!reducedMotion) pose.blink = blinkAt(seed, t * 0.7);
173
- } else {
174
- pose.yaw += 0.07 * sway * wave(6.1);
175
- pose.pitch += 0.04 * sway * wave(4.7, 1);
176
- pose.roll = 1.6 * wave(5.3, 2);
177
- pose.lift = reducedMotion ? 0 : -0.45 * (1 - Math.cos((TAU * t) / 3.4));
178
- const breath = wave(3.4, -Math.PI / 2);
179
- pose.sy = 1 + 0.012 * breath;
180
- pose.sx = 1 - 0.008 * breath;
181
- if (!reducedMotion) {
182
- pose.blink = blinkAt(seed, t);
183
- [pose.gazeX, pose.gazeY] = wanderAt(seed, t);
326
+ pose.dots = 1;
327
+ } else if (state === 'working') {
328
+ // Bent over the work and busy: a quick steady bob and eyes darting across it, and every so
329
+ // often a turn with a band of ribbons sweeping round it.
330
+ const beat = wave(0.7);
331
+ pose.yaw += 0.08 * sway * wave(2.3, 0.4);
332
+ pose.pitch -= 0.05;
333
+ pose.roll = 3 + 0.6 * beat;
334
+ pose.lift = -0.3 * (0.5 + 0.5 * beat) * live;
335
+ pose.sy = 0.99 - 0.008 * beat;
336
+ pose.sx = 1.005 + 0.005 * beat;
337
+ pose.squintL = 0.25;
338
+ pose.squintR = 0.25;
339
+ if (reducedMotion) {
340
+ pose.gazeX = 0.15;
341
+ pose.gazeY = 0.3;
342
+ } else {
343
+ [pose.gazeX, pose.gazeY] = dartAt(seed, t);
344
+ pose.blink = blinkAt(seed, t * 1.3);
345
+ }
346
+ // Counted from when the state began; without an age, from the clock.
347
+ const age = Number.isFinite(input.stateAge) ? Math.max(0, input.stateAge as number) : Math.max(0, time);
348
+ const into = reducedMotion ? null : workTurn(seed, age);
349
+ if (into !== null) {
350
+ // A full circle that eases in like a spring settling. The band rides it round, out at the
351
+ // start and back in at the end; the body keeps facing the work, leaning into the sweep, and
352
+ // the eyes follow the band across.
353
+ const e = WORK_EASE * into;
354
+ const turn = TAU * (1 - (1 + e) * Math.exp(-e));
355
+ const swell = Math.sin(turn / 2);
356
+ pose.ribbons = clamp(into / 0.16, 0, 1) * clamp((WORK_TURN_S - into) / 0.5, 0, 1);
357
+ pose.sweep = turn;
358
+ pose.yaw += 0.22 * swell;
359
+ pose.roll += 3 * swell;
360
+ pose.lift -= 1.2 * swell;
361
+ pose.gazeX = clamp(pose.gazeX * (1 - swell) - 0.85 * Math.sin(turn), -1, 1);
362
+ pose.gazeY = pose.gazeY * (1 - swell) - 0.3 * swell;
184
363
  }
364
+ } else if (state === 'waiting') {
365
+ // Leans to one side, eyes flattened to slits, glancing off at whatever it is waiting on.
366
+ // Now and then it sighs. A happy bot keeps its arcs.
367
+ const p = reducedMotion ? 0 : ((t % 6.5) + 6.5) % 6.5 / 6.5;
368
+ // One breath in and out over the first third of the cycle.
369
+ const sigh = Math.sin(TAU * clamp(p / 0.33, 0, 1));
370
+ pose.yaw += 0.04 * sway * wave(7.4);
371
+ pose.pitch += 0.02 + 0.03 * sigh;
372
+ pose.roll = 7 + 1.2 * wave(6.4, 2);
373
+ pose.lift = -0.3 * (1 - Math.cos((TAU * t) / 6.5)) * live;
374
+ pose.sy = 1 + 0.025 * sigh;
375
+ pose.sx = 1 - 0.012 * sigh;
376
+ pose.gazeX = 0.5 + 0.15 * wave(8.2);
377
+ pose.gazeY = 0.1;
378
+ pose.eyes.slit = 1 - pose.eyes.arc;
379
+ if (!reducedMotion) pose.blink = blinkAt(seed, t * 0.6);
380
+ } else if (state === 'blocked') {
381
+ // Stops still while it gives way to the exclamation mark.
382
+ pose.sy = 1.02;
383
+ pose.sx = 0.99;
384
+ pose.bang = 1;
385
+ } else if (state === 'done') {
386
+ // Celebrates: a few turns with ribbons flying round it, then a rest, content and a little
387
+ // slower than idle, and after a while again, the other way round.
388
+ rest(1.25);
389
+ // Counted from when the state began. Without an age it is at rest.
390
+ const age = !reducedMotion && Number.isFinite(input.stateAge) ? Math.max(0, input.stateAge as number) : DONE_TURN_S;
391
+ const length = DONE_CYCLE_S * (1 + 0.15 * noise(seed, 31));
392
+ const cycle = Math.floor(age / length), since = age - cycle * length;
393
+ if (since < DONE_TURN_S) {
394
+ const swell = Math.sin((Math.PI * since) / DONE_TURN_S);
395
+ const way = cycle % 2 ? -1 : 1;
396
+ const sweep = celebration(since) * way;
397
+ // The body turns a third as often as the ribbons go round.
398
+ pose.yaw += near(sweep / 3);
399
+ pose.lift -= 2.2 * swell;
400
+ pose.sy += 0.04 * swell;
401
+ // All the ribbons out at full strength while the turn is quick, drawn back in as it settles.
402
+ pose.ribbons = clamp(since / 0.1, 0, 1) * clamp((DONE_TURN_S - since) / DONE_DRAW_IN_S, 0, 1);
403
+ pose.cage = 1;
404
+ // The ribbons only set off once the turn goes forward: the wind-up does not carry them.
405
+ pose.sweep = way * Math.max(0, celebration(since));
406
+ }
407
+ } else {
408
+ rest(1);
185
409
  }
186
410
 
187
411
  if (given && weight > 0) {
188
412
  // A look target wins over idle wander. The body turns to face it: the resting
189
413
  // three-quarter turn gives way (so a target on either side turns the front that
190
414
  // way) and the eyes add a modest share on top. Reduced motion keeps the body
191
- // still, so the eyes alone use their full (still small) range.
192
- const mix = (a: number, b: number) => a + (b - a) * weight;
415
+ // still, so the eyes alone use their full (still small) range. Happy arcs stay put.
193
416
  const share = reducedMotion ? 1 : LOOK_EYES;
194
- if (pose.eyeShape === 'pill') {
195
- pose.gazeX = mix(pose.gazeX, given[0] * share);
196
- pose.gazeY = mix(pose.gazeY, given[1] * share);
197
- }
417
+ const follow = weight * (1 - pose.eyes.arc);
418
+ pose.gazeX = mix(pose.gazeX, given[0] * share, follow);
419
+ pose.gazeY = mix(pose.gazeY, given[1] * share, follow);
198
420
  if (!reducedMotion) {
199
421
  // Positive yaw turns the front to the right, positive pitch tips it up.
200
422
  pose.yaw += weight * (given[0] * LOOK_YAW - BASE_YAW);
package/src/path.ts CHANGED
@@ -194,7 +194,9 @@ function sampleArc(x0: number, y0: number, s: Extract<Segment, { c: 'A' }>, out:
194
194
  let dt = ang((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
195
195
  if (!s.sweep && dt > 0) dt -= 2 * Math.PI;
196
196
  if (s.sweep && dt < 0) dt += 2 * Math.PI;
197
- const steps = Math.max(4, Math.ceil((Math.abs(dt) / (Math.PI / 2)) * 6));
197
+ // Chords within FLATNESS of the arc: a chord over angle θ sags r(1 - cos(θ/2)).
198
+ const step = 2 * Math.acos(Math.max(-1, 1 - FLATNESS / Math.max(rx, ry)));
199
+ const steps = Math.min(256, Math.max(4, Math.ceil(Math.abs(dt) / step)));
198
200
  for (let k = 1; k <= steps; k++) {
199
201
  const t = t1 + (dt * k) / steps;
200
202
  const ex = rx * Math.cos(t), ey = ry * Math.sin(t);
@@ -202,6 +204,13 @@ function sampleArc(x0: number, y0: number, s: Extract<Segment, { c: 'A' }>, out:
202
204
  }
203
205
  }
204
206
 
207
+ /**
208
+ * Largest gap, viewBox units, between a curve and the polygon that stands for it. Volume slices
209
+ * follow that polygon while the flat core draws the real curve, so coarser chords show as facets
210
+ * and teeth along the rim of large avatars.
211
+ */
212
+ export const FLATNESS = 0.02;
213
+
205
214
  /** Flatten into closed polygons, one per subpath. */
206
215
  export function flattenPath(segs: Segment[]): Point[][] {
207
216
  const polys: Point[][] = [];
@@ -214,7 +223,9 @@ export function flattenPath(segs: Segment[]): Point[][] {
214
223
  cur = [[s.x, s.y]]; px = s.x; py = s.y; break;
215
224
  case 'L': cur.push([s.x, s.y]); px = s.x; py = s.y; break;
216
225
  case 'C': {
217
- const n = 12;
226
+ // Wang's bound: this many chords keep within FLATNESS of the curve.
227
+ const m = Math.max(Math.hypot(px - 2 * s.x1 + s.x2, py - 2 * s.y1 + s.y2), Math.hypot(s.x1 - 2 * s.x2 + s.x, s.y1 - 2 * s.y2 + s.y));
228
+ const n = Math.min(128, Math.max(2, Math.ceil(Math.sqrt((0.75 * m) / FLATNESS))));
218
229
  for (let k = 1; k <= n; k++) {
219
230
  const t = k / n, u = 1 - t;
220
231
  cur.push([
@@ -225,7 +236,7 @@ export function flattenPath(segs: Segment[]): Point[][] {
225
236
  px = s.x; py = s.y; break;
226
237
  }
227
238
  case 'Q': {
228
- const n = 8;
239
+ const n = Math.min(128, Math.max(2, Math.ceil(Math.sqrt((0.25 * Math.hypot(px - 2 * s.x1 + s.x, py - 2 * s.y1 + s.y)) / FLATNESS))));
229
240
  for (let k = 1; k <= n; k++) {
230
241
  const t = k / n, u = 1 - t;
231
242
  cur.push([u * u * px + 2 * u * t * s.x1 + t * t * s.x, u * u * py + 2 * u * t * s.y1 + t * t * s.y]);