open-bot-avatar 0.2.1 → 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 (54) 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/dom/mount.d.ts +8 -2
  11. package/dist/dom/mount.d.ts.map +1 -1
  12. package/dist/dom/mount.js +29 -8
  13. package/dist/dom/mount.js.map +1 -1
  14. package/dist/dom/paint.d.ts.map +1 -1
  15. package/dist/dom/paint.js +18 -5
  16. package/dist/dom/paint.js.map +1 -1
  17. package/dist/effects.d.ts +82 -0
  18. package/dist/effects.d.ts.map +1 -0
  19. package/dist/effects.js +352 -0
  20. package/dist/effects.js.map +1 -0
  21. package/dist/eyes.d.ts +29 -0
  22. package/dist/eyes.d.ts.map +1 -0
  23. package/dist/eyes.js +128 -0
  24. package/dist/eyes.js.map +1 -0
  25. package/dist/index.d.ts +3 -3
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +1 -1
  28. package/dist/index.js.map +1 -1
  29. package/dist/mood.d.ts +10 -5
  30. package/dist/mood.d.ts.map +1 -1
  31. package/dist/mood.js +3 -2
  32. package/dist/mood.js.map +1 -1
  33. package/dist/motion.d.ts +63 -10
  34. package/dist/motion.d.ts.map +1 -1
  35. package/dist/motion.js +239 -41
  36. package/dist/motion.js.map +1 -1
  37. package/dist/render.d.ts +68 -4
  38. package/dist/render.d.ts.map +1 -1
  39. package/dist/render.js +265 -34
  40. package/dist/render.js.map +1 -1
  41. package/dist/types.d.ts +35 -7
  42. package/dist/types.d.ts.map +1 -1
  43. package/package.json +1 -1
  44. package/src/animator.ts +40 -20
  45. package/src/color.ts +48 -0
  46. package/src/dom/mount.ts +38 -11
  47. package/src/dom/paint.ts +17 -5
  48. package/src/effects.ts +405 -0
  49. package/src/eyes.ts +157 -0
  50. package/src/index.ts +3 -3
  51. package/src/mood.ts +13 -7
  52. package/src/motion.ts +270 -48
  53. package/src/render.ts +335 -42
  54. package/src/types.ts +42 -7
package/dist/types.d.ts CHANGED
@@ -27,14 +27,32 @@ export interface AvatarDefinition {
27
27
  };
28
28
  eyes: AvatarEyes;
29
29
  }
30
+ /**
31
+ * What the bot is doing. `thinking` and `blocked` replace the body with a mark (three dots, an
32
+ * exclamation mark); `working` sweeps a band of ribbons round the body every few seconds; `done`
33
+ * celebrates: it turns a few times with ribbons flying, rests, and after a while does it again.
34
+ */
35
+ export type AvatarState = 'idle' | 'thinking' | 'working' | 'waiting' | 'blocked' | 'done';
36
+ /** How the bot feels. It shapes the eyes on top of any state; in `idle` a happy bot also hops. */
37
+ export type AvatarMood = 'neutral' | 'happy';
38
+ /**
39
+ * The single option that came before `state` and `mood`, still accepted: `idle` and `thinking`
40
+ * are states, `happy` is the idle state in a happy mood.
41
+ * @deprecated Use `state` and `mood`.
42
+ */
30
43
  export type AvatarMotion = 'idle' | 'thinking' | 'happy';
31
44
  /**
32
- * A mood easing in over what was shown before it: a plain mood, or a mood that
33
- * was itself still easing in (its progress frozen where it was). `t` 0..1.
45
+ * A change easing in over what was shown before it: a plain legacy motion, or a state and mood
46
+ * that were themselves still easing in (their progress frozen where it was). `t` 0..1.
34
47
  */
35
48
  export interface AvatarMotionBlend {
36
49
  from: AvatarMotion | {
37
- motion: AvatarMotion;
50
+ state?: AvatarState;
51
+ mood?: AvatarMood;
52
+ /** @deprecated Use `state` and `mood`. */
53
+ motion?: AvatarMotion;
54
+ /** Seconds that state had been showing when the change began (see RenderOptions.stateAge). */
55
+ age?: number;
38
56
  blend?: AvatarMotionBlend;
39
57
  };
40
58
  t: number;
@@ -68,14 +86,24 @@ export interface RenderOptions {
68
86
  y: number;
69
87
  weight?: number;
70
88
  };
89
+ /** What the bot is doing (default `idle`). */
90
+ state?: AvatarState;
91
+ /** How the bot feels (default `neutral`). */
92
+ mood?: AvatarMood;
93
+ /** @deprecated Use `state` and `mood`; they win when given. */
71
94
  motion?: AvatarMotion;
72
95
  /**
73
- * Ease into `motion` from the mood shown before: the pose blends between the
74
- * two, and eyes that change shape close and reopen. Hosts leave it out once
75
- * `t` reaches 1 (and under reduced motion).
96
+ * Seconds since `state` began. `done` celebrates from the moment it begins, so hosts that
97
+ * animate pass this (createAnimator returns it); without it the state is drawn at rest.
98
+ */
99
+ stateAge?: number;
100
+ /**
101
+ * Ease into `state` and `mood` from what was shown before: the pose blends between the two
102
+ * and the eyes morph from one shape to the other. Hosts leave it out once `t` reaches 1 (and
103
+ * under reduced motion).
76
104
  */
77
105
  motionBlend?: AvatarMotionBlend;
78
- /** Freeze body motion and blinking at a calm pose; gaze still applies. */
106
+ /** Freeze body motion, blinking, ribbons and marks at a calm pose; gaze still applies. */
79
107
  reducedMotion?: boolean;
80
108
  }
81
109
  export type ValidationResult = {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,UAAU;IACzB,6CAA6C;IAC7C,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,qEAAqE;IACrE,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,CAAC,CAAC;IACX,yCAAyC;IACzC,EAAE,EAAE,MAAM,CAAC;IACX,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,gFAAgF;IAChF,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACvB,IAAI,EAAE,UAAU,CAAC;CAClB;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,UAAU,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AG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IACtC,2EAA2E;IAC3E,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,IAAI,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACjD,8CAA8C;IAC9C,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,6CAA6C;IAC7C,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,+DAA+D;IAC/D,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,0FAA0F;IAC1F,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,MAAM,gBAAgB,GACxB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,gBAAgB,CAAA;CAAE,GACrC;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "open-bot-avatar",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Small object characters with eyes: validate a silhouette + eyes definition and render it with implied 3D volume, idle motion, moods and gaze. No dependencies.",
5
5
  "keywords": ["avatar", "bot", "svg", "character", "animation", "css-3d", "mascot"],
6
6
  "license": "MIT",
package/src/animator.ts CHANGED
@@ -1,18 +1,22 @@
1
1
  /**
2
2
  * The per-avatar state a live renderer keeps between frames, independent of
3
3
  * any platform: the eased look toward a target (follow.ts) and the eased
4
- * change between moods (mood.ts). Feed it once per frame; it returns the
5
- * `gaze` and `motionBlend` render options for that frame and whether it is
6
- * still easing.
4
+ * change between states and moods (mood.ts). Feed it once per frame; it
5
+ * returns the `gaze`, `motionBlend` and `stateAge` render options for that
6
+ * frame and whether it is still easing.
7
7
  */
8
8
  import { REST, followGaze, followStep, type FollowState } from './follow.ts';
9
+ import { DONE_TURN_S, resolveLook } from './motion.ts';
9
10
  import { moodProgress, settleMood, type MoodState } from './mood.ts';
10
- import type { AvatarMotion, AvatarMotionBlend } from './types.ts';
11
+ import type { AvatarMood, AvatarMotion, AvatarMotionBlend, AvatarState } from './types.ts';
11
12
 
12
13
  export interface AnimatorInput {
13
14
  /** Wall clock, ms (e.g. performance.now()). Easing runs on it even when the animation clock stops. */
14
15
  now: number;
15
- motion: AvatarMotion;
16
+ state?: AvatarState;
17
+ mood?: AvatarMood;
18
+ /** @deprecated Use `state` and `mood`; they win when given. */
19
+ motion?: AvatarMotion;
16
20
  /** Look target, -1..1 per axis (x right, y down), e.g. from pointerGaze; null eases back to rest. */
17
21
  target: { x: number; y: number } | null;
18
22
  /** Keep the current look as it is (e.g. while the user drags the view). */
@@ -24,46 +28,62 @@ export interface AnimatorInput {
24
28
  export interface AnimatorStep {
25
29
  gaze?: { x: number; y: number; weight: number };
26
30
  motionBlend?: AvatarMotionBlend;
27
- /** True while the look or a mood change is still easing. */
31
+ /** Seconds since the state began (a change of mood alone does not restart it). */
32
+ stateAge: number;
33
+ /** True while the look or a change of state or mood is still easing. */
28
34
  moving: boolean;
29
35
  }
30
36
 
31
37
  export interface Animator {
32
38
  step(input: AnimatorInput): AnimatorStep;
33
- /** Back to rest with no mood history, e.g. when the avatar is remounted. */
39
+ /** Back to rest with no history, e.g. when the avatar is remounted. */
34
40
  reset(): void;
35
41
  }
36
42
 
37
43
  export function createAnimator(): Animator {
38
44
  let follow: FollowState = REST;
39
45
  let last: number | null = null;
40
- // The mood on screen and when its change began.
41
- let mood: (MoodState & { since: number }) | null = null;
46
+ // What is on screen, when its change began and when its state began.
47
+ let shown: { state: AvatarState; mood: AvatarMood; blend?: AvatarMotionBlend; since: number; began: number } | null = null;
42
48
 
43
49
  return {
44
- step({ now, motion, target, hold = false, reducedMotion }) {
50
+ step({ now, target, hold = false, reducedMotion, ...given }) {
45
51
  const dt = last === null ? 0 : (now - last) / 1000;
46
52
  last = now;
47
53
  let moving = false;
48
54
  if (!hold) ({ state: follow, moving } = followStep(follow, target, dt, reducedMotion));
49
55
 
50
- // A new mood eases in from the pose on screen (a change under way included); reduced motion switches at once.
51
- if (mood && motion !== mood.motion) {
52
- const shown: MoodState = { motion: mood.motion, blend: mood.blend && { from: mood.blend.from, t: moodProgress(now - mood.since) } };
53
- mood = { motion, blend: reducedMotion ? undefined : { from: settleMood(shown), t: 0 }, since: now };
56
+ // A new state or mood eases in from the pose on screen (a change under way included); reduced motion switches at once.
57
+ const { state, mood } = resolveLook(given);
58
+ if (shown && (state !== shown.state || mood !== shown.mood)) {
59
+ const before: MoodState = {
60
+ state: shown.state,
61
+ mood: shown.mood,
62
+ age: (now - shown.began) / 1000,
63
+ blend: shown.blend && { from: shown.blend.from, t: moodProgress(now - shown.since) },
64
+ };
65
+ shown = {
66
+ state,
67
+ mood,
68
+ blend: reducedMotion ? undefined : { from: settleMood(before), t: 0 },
69
+ since: now,
70
+ began: state === shown.state ? shown.began : now,
71
+ };
54
72
  }
55
- mood ??= { motion, since: now };
56
- const t = reducedMotion ? 1 : moodProgress(now - mood.since);
73
+ // An avatar that mounts done starts it at rest, as if it had just finished celebrating; any
74
+ // other state starts afresh (a working bot's first sweep comes a second or two in).
75
+ shown ??= { state, mood, since: now, began: now - (state === 'done' ? DONE_TURN_S * 1000 : 0) };
76
+ const t = reducedMotion ? 1 : moodProgress(now - shown.since);
57
77
  let motionBlend: AvatarMotionBlend | undefined;
58
- if (mood.blend && t < 1) motionBlend = { from: mood.blend.from, t };
59
- else mood.blend = undefined;
78
+ if (shown.blend && t < 1) motionBlend = { from: shown.blend.from, t };
79
+ else shown.blend = undefined;
60
80
 
61
- return { gaze: followGaze(follow), motionBlend, moving: moving || motionBlend !== undefined };
81
+ return { gaze: followGaze(follow), motionBlend, stateAge: (now - shown.began) / 1000, moving: moving || motionBlend !== undefined };
62
82
  },
63
83
  reset() {
64
84
  follow = REST;
65
85
  last = null;
66
- mood = null;
86
+ shown = null;
67
87
  },
68
88
  };
69
89
  }
package/src/color.ts CHANGED
@@ -55,3 +55,51 @@ export const LIGHT_INK: Rgb = [250, 250, 255];
55
55
  export function autoEyeColor(body: Rgb): Rgb {
56
56
  return contrast(body, DARK_INK) >= contrast(body, LIGHT_INK) ? DARK_INK : LIGHT_INK;
57
57
  }
58
+
59
+ // OKLCH, for colors that look equally bright whatever their hue.
60
+
61
+ const toLinear = (v: number) => {
62
+ const s = v / 255;
63
+ return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
64
+ };
65
+ const fromLinear = (v: number) => 255 * (v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055);
66
+
67
+ /** A color as OKLCH: lightness 0..1, chroma, hue in degrees. */
68
+ export function toOklch(c: Rgb): [number, number, number] {
69
+ const [r, g, b] = c.map(toLinear);
70
+ const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
71
+ const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
72
+ const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
73
+ const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s;
74
+ const A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s;
75
+ const B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s;
76
+ const h = (Math.atan2(B, A) * 180) / Math.PI;
77
+ return [L, Math.hypot(A, B), h < 0 ? h + 360 : h];
78
+ }
79
+
80
+ function oklchLinear(L: number, C: number, h: number): [number, number, number] {
81
+ const a = C * Math.cos((h * Math.PI) / 180), b = C * Math.sin((h * Math.PI) / 180);
82
+ const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
83
+ const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
84
+ const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
85
+ return [
86
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
87
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
88
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
89
+ ];
90
+ }
91
+
92
+ /** An OKLCH color in sRGB, its chroma lowered as far as it takes to fit (lightness and hue are kept). */
93
+ export function oklch(L: number, C: number, h: number): Rgb {
94
+ const fits = (c: number) => oklchLinear(L, c, h).every((v) => v >= -1e-4 && v <= 1 + 1e-4);
95
+ let lo = 0, hi = C;
96
+ if (!fits(hi)) {
97
+ for (let i = 0; i < 20; i++) {
98
+ const mid = (lo + hi) / 2;
99
+ if (fits(mid)) lo = mid;
100
+ else hi = mid;
101
+ }
102
+ hi = lo;
103
+ }
104
+ return oklchLinear(L, hi, h).map(fromLinear) as Rgb;
105
+ }
package/src/dom/mount.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * A live avatar in any DOM element, without a framework.
3
3
  *
4
4
  * const avatar = mountAvatar(el, { definition, color: '#1f7cf2', followPointer: true });
5
- * avatar.update({ mood: 'happy' });
5
+ * avatar.update({ state: 'working', mood: 'happy' });
6
6
  * avatar.destroy();
7
7
  *
8
8
  * Live avatars paint each frame to one canvas (see paint.ts): one compositor
@@ -15,8 +15,9 @@
15
15
  */
16
16
  import { createAnimator } from '../animator.ts';
17
17
  import { pointerGaze } from '../follow.ts';
18
+ import { resolveLook } from '../motion.ts';
18
19
  import { DEFAULT_COLOR, DEFAULT_EYE_COLOR, liveFrame, renderAvatarScene, renderAvatarSvg, sceneFrame, type SceneFrame } from '../render.ts';
19
- import type { AvatarDefinition, AvatarMotion, RenderOptions } from '../types.ts';
20
+ import type { AvatarDefinition, AvatarMood, AvatarState, RenderOptions } from '../types.ts';
20
21
  import { requestFrame, subscribeFrames } from './clock.ts';
21
22
  import { canvasSupported, mountCanvas, paintCanvas, type CanvasSurface } from './paint.ts';
22
23
  import { trackPointer } from './pointer.ts';
@@ -30,7 +31,13 @@ export interface AvatarOptions {
30
31
  eyeColor?: string;
31
32
  /** Volume amount 0..14 (6 = natural depth, 0 = flat). */
32
33
  depth?: number;
33
- mood?: AvatarMotion;
34
+ /** What the bot is doing (default `idle`). Changes ease from what is on screen. */
35
+ state?: AvatarState;
36
+ /**
37
+ * How the bot feels (default `neutral`): it shapes the eyes on top of any state. `idle` and
38
+ * `thinking` are still accepted here from before `state` existed and mean those states.
39
+ */
40
+ mood?: AvatarMood | 'idle' | 'thinking';
34
41
  /** Eyes look and the body turns toward the mouse, pen or finger anywhere on the page. */
35
42
  followPointer?: boolean;
36
43
  /** Fixed look direction (-1..1 per axis, x right, y down). Wins over the pointer. */
@@ -67,7 +74,8 @@ interface Scene {
67
74
  view: HTMLElement;
68
75
  stacks: Record<'z' | 'x', HTMLElement>;
69
76
  slices: Record<'z' | 'x', HTMLElement[]>;
70
- eyes: { root: HTMLElement; pill: HTMLElement; arc: HTMLElement }[];
77
+ eyes: { root: HTMLElement; pill: HTMLElement; arc: HTMLElement; morph: HTMLElement; path: SVGPathElement }[];
78
+ fx: Record<'back' | 'front', HTMLElement>;
71
79
  last: Record<string, string>;
72
80
  }
73
81
 
@@ -84,8 +92,10 @@ function mountScene(el: HTMLElement, definition: AvatarDefinition, depth: number
84
92
  slices: { z: [...stacks.z.children] as HTMLElement[], x: [...stacks.x.children] as HTMLElement[] },
85
93
  eyes: [0, 1].map((i) => {
86
94
  const root = q(`[data-eye="${i}"]`);
87
- return { root, pill: root.querySelector('i') as HTMLElement, arc: root.querySelector('svg') as unknown as HTMLElement };
95
+ const morph = root.querySelector('[data-morph]') as HTMLElement;
96
+ return { root, pill: root.querySelector('i') as HTMLElement, arc: root.querySelector('svg') as unknown as HTMLElement, morph, path: morph.querySelector('path') as SVGPathElement };
88
97
  }),
98
+ fx: { back: q('[data-fx="back"]'), front: q('[data-fx="front"]') },
89
99
  last: {},
90
100
  };
91
101
  }
@@ -100,6 +110,13 @@ function put(m: Scene, key: string, el: HTMLElement, prop: 'transform' | 'visibi
100
110
  function apply(m: Scene, frame: SceneFrame) {
101
111
  put(m, 'motion', m.motion, 'transform', frame.motion);
102
112
  put(m, 'view', m.view, 'transform', frame.view);
113
+ // A body that has given way to a mark holds no layers.
114
+ put(m, 'body', m.view, 'display', frame.body > 0 ? 'block' : 'none');
115
+ for (const side of ['back', 'front'] as const) {
116
+ if (m.last[side] === frame[side]) continue;
117
+ m.last[side] = frame[side];
118
+ m.fx[side].innerHTML = frame[side];
119
+ }
103
120
  // display:none, not visibility: hidden slices would still each hold a 3D layer.
104
121
  put(m, 'z', m.stacks.z, 'display', frame.stack === 'z' ? 'block' : 'none');
105
122
  put(m, 'x', m.stacks.x, 'display', frame.stack === 'x' ? 'block' : 'none');
@@ -110,8 +127,13 @@ function apply(m: Scene, frame: SceneFrame) {
110
127
  put(m, `e${i}`, e.root, 'transform', eye.transform);
111
128
  put(m, `v${i}`, e.root, 'visibility', eye.visible ? 'visible' : 'hidden');
112
129
  put(m, `p${i}`, e.pill, 'transform', eye.pill);
113
- put(m, `ph${i}`, e.pill, 'display', eye.happy ? 'none' : 'block');
114
- put(m, `a${i}`, e.arc, 'display', eye.happy ? 'block' : 'none');
130
+ put(m, `ph${i}`, e.pill, 'display', eye.happy || eye.d ? 'none' : 'block');
131
+ put(m, `a${i}`, e.arc, 'display', eye.happy && !eye.d ? 'block' : 'none');
132
+ put(m, `m${i}`, e.morph, 'display', eye.d ? 'block' : 'none');
133
+ if (eye.d && m.last[`d${i}`] !== eye.d) {
134
+ m.last[`d${i}`] = eye.d;
135
+ e.path.setAttribute('d', eye.d);
136
+ }
115
137
  });
116
138
  }
117
139
 
@@ -214,6 +236,8 @@ export function mountAvatar(host: HTMLElement, initial: AvatarOptions): MountedA
214
236
  const p = opts;
215
237
  const reducedMotion = p.reducedMotion === 'system' || p.reducedMotion === undefined ? prefersReducedMotion() : p.reducedMotion;
216
238
  const now = performance.now();
239
+ // `mood: 'idle' | 'thinking'` are the states they were before `state` existed.
240
+ const look = resolveLook({ state: p.state, mood: p.mood, motion: p.mood });
217
241
  try {
218
242
  if (p.still) {
219
243
  if (scene || surface) clear();
@@ -221,7 +245,8 @@ export function mountAvatar(host: HTMLElement, initial: AvatarOptions): MountedA
221
245
  color: p.color,
222
246
  eyeColor: p.eyeColor,
223
247
  depth: p.depth,
224
- motion: p.mood,
248
+ state: look.state,
249
+ mood: look.mood,
225
250
  view: p.view,
226
251
  gaze: p.lookAt ?? undefined,
227
252
  reducedMotion: true,
@@ -241,10 +266,12 @@ export function mountAvatar(host: HTMLElement, initial: AvatarOptions): MountedA
241
266
  : p.followPointer && pointer && rect && rect.width > 0
242
267
  ? pointerGaze(pointer, rect, eyesOrigin(p.definition))
243
268
  : null;
244
- const step = animator.step({ now, motion: p.mood ?? 'idle', target, hold: p.holdGaze, reducedMotion });
269
+ const step = animator.step({ now, state: look.state, mood: look.mood, target, hold: p.holdGaze, reducedMotion });
245
270
  const options: RenderOptions = {
246
271
  depth: p.depth,
247
- motion: p.mood ?? 'idle',
272
+ state: look.state,
273
+ mood: look.mood,
274
+ stateAge: step.stateAge,
248
275
  motionBlend: step.motionBlend,
249
276
  size: size || undefined,
250
277
  time,
@@ -267,7 +294,7 @@ export function mountAvatar(host: HTMLElement, initial: AvatarOptions): MountedA
267
294
  return !reducedMotion || step.moving;
268
295
  }
269
296
  if (surface) clear();
270
- const frame = sceneFrame(p.definition, options);
297
+ const frame = sceneFrame(p.definition, { ...options, color: p.color });
271
298
  if (!scene || scene.key !== frame.scene || scene.geometry !== geometryKey(p.definition) || !host.contains(scene.view)) {
272
299
  scene = mountScene(host, p.definition, p.depth);
273
300
  }
package/src/dom/paint.ts CHANGED
@@ -100,7 +100,7 @@ export function framePlacement(frame: AvatarFrame): { matrices: Matrix[]; bounds
100
100
  const matrices = frame.shapes.map((shape) => {
101
101
  const [a, b, c, d, e, f] = shape.matrix;
102
102
  const m: Matrix = [ra * a + rc * b, rb * a + rd * b, ra * c + rc * d, rb * c + rd * d, ra * e + rc * f + re, rb * e + rd * f + rf];
103
- const box = shape.kind === 'slice' ? boxes.get(shape.path) : eyeBox(shape.mark);
103
+ const box = shape.kind === 'slice' ? boxes.get(shape.path) : shape.kind === 'eye' ? eyeBox(shape.mark) : shape.box;
104
104
  if (box) {
105
105
  add(m, box.x, box.y);
106
106
  add(m, box.x + box.w, box.y);
@@ -114,14 +114,19 @@ export function framePlacement(frame: AvatarFrame): { matrices: Matrix[]; bounds
114
114
 
115
115
  function eyeBox(mark: EyeMark): FramePath['box'] {
116
116
  if (mark.shape === 'pill') return { x: mark.x, y: mark.y, w: mark.width, h: mark.height };
117
- // A quadratic arc lies within its points' hull; the stroke reaches half its width past it.
117
+ // Quadratics lie within their points' hull; an arc's stroke reaches half its width past it.
118
+ const reach = mark.shape === 'arc' ? mark.strokeWidth : 0;
118
119
  const n = (mark.d.match(/-?\d+(?:\.\d+)?/g) ?? []).map(Number);
119
120
  const xs = n.filter((_, i) => i % 2 === 0), ys = n.filter((_, i) => i % 2 === 1);
120
- const x = Math.min(...xs) - mark.strokeWidth, y = Math.min(...ys) - mark.strokeWidth;
121
- return { x, y, w: Math.max(...xs) + mark.strokeWidth - x, h: Math.max(...ys) + mark.strokeWidth - y };
121
+ const x = Math.min(...xs) - reach, y = Math.min(...ys) - reach;
122
+ return { x, y, w: Math.max(...xs) + reach - x, h: Math.max(...ys) + reach - y };
122
123
  }
123
124
 
124
125
  function eye(ctx: CanvasRenderingContext2D, mark: EyeMark) {
126
+ if (mark.shape === 'path') {
127
+ ctx.fill(new Path2D(mark.d));
128
+ return;
129
+ }
125
130
  if (mark.shape === 'arc') {
126
131
  ctx.lineWidth = mark.strokeWidth;
127
132
  ctx.stroke(new Path2D(mark.d));
@@ -206,10 +211,17 @@ export function paintCanvas(surface: CanvasSurface, frame: AvatarFrame, size: nu
206
211
  if (shape.kind === 'slice') {
207
212
  const section = sections.get(shape.path) as Section;
208
213
  ctx.fill(section.path, section.rule);
209
- } else {
214
+ } else if (shape.kind === 'eye') {
210
215
  ctx.fillStyle = ink;
211
216
  eye(ctx, shape.mark);
212
217
  ctx.fillStyle = fill;
218
+ } else {
219
+ const g = shape.gradient;
220
+ const paint = ctx.createLinearGradient(g.x1, g.y1, g.x2, g.y2);
221
+ g.stops.forEach((c, i) => paint.addColorStop(i / (g.stops.length - 1), c));
222
+ ctx.fillStyle = paint;
223
+ ctx.fill(new Path2D(shape.d));
224
+ ctx.fillStyle = fill;
213
225
  }
214
226
  }
215
227
  }