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.
- package/README.md +25 -6
- package/dist/animator.d.ts +9 -4
- package/dist/animator.d.ts.map +1 -1
- package/dist/animator.js +32 -17
- package/dist/animator.js.map +1 -1
- package/dist/color.d.ts +4 -0
- package/dist/color.d.ts.map +1 -1
- package/dist/color.js +45 -0
- package/dist/color.js.map +1 -1
- package/dist/contour.d.ts +6 -1
- package/dist/contour.d.ts.map +1 -1
- package/dist/contour.js +83 -7
- package/dist/contour.js.map +1 -1
- package/dist/dom/mount.d.ts +8 -2
- package/dist/dom/mount.d.ts.map +1 -1
- package/dist/dom/mount.js +29 -8
- package/dist/dom/mount.js.map +1 -1
- package/dist/dom/paint.d.ts.map +1 -1
- package/dist/dom/paint.js +18 -5
- package/dist/dom/paint.js.map +1 -1
- package/dist/effects.d.ts +82 -0
- package/dist/effects.d.ts.map +1 -0
- package/dist/effects.js +352 -0
- package/dist/effects.js.map +1 -0
- package/dist/eyes.d.ts +29 -0
- package/dist/eyes.d.ts.map +1 -0
- package/dist/eyes.js +128 -0
- package/dist/eyes.js.map +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mood.d.ts +10 -5
- package/dist/mood.d.ts.map +1 -1
- package/dist/mood.js +3 -2
- package/dist/mood.js.map +1 -1
- package/dist/motion.d.ts +63 -10
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +239 -41
- package/dist/motion.js.map +1 -1
- package/dist/path.d.ts +6 -0
- package/dist/path.d.ts.map +1 -1
- package/dist/path.js +13 -3
- package/dist/path.js.map +1 -1
- package/dist/render.d.ts +68 -4
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +265 -34
- package/dist/render.js.map +1 -1
- package/dist/types.d.ts +35 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/volume.d.ts +2 -0
- package/dist/volume.d.ts.map +1 -1
- package/dist/volume.js +35 -14
- package/dist/volume.js.map +1 -1
- package/package.json +1 -1
- package/src/animator.ts +40 -20
- package/src/color.ts +48 -0
- package/src/contour.ts +74 -7
- package/src/dom/mount.ts +38 -11
- package/src/dom/paint.ts +17 -5
- package/src/effects.ts +405 -0
- package/src/eyes.ts +157 -0
- package/src/index.ts +3 -3
- package/src/mood.ts +13 -7
- package/src/motion.ts +270 -48
- package/src/path.ts +14 -3
- package/src/render.ts +335 -42
- package/src/types.ts +42 -7
- package/src/volume.ts +43 -14
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
|
|
5
|
-
* `gaze` and `
|
|
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
|
-
|
|
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
|
-
/**
|
|
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
|
|
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
|
-
//
|
|
41
|
-
let mood:
|
|
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,
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
56
|
-
|
|
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 (
|
|
59
|
-
else
|
|
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
|
-
|
|
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/contour.ts
CHANGED
|
@@ -113,14 +113,81 @@ export function simplifyLoop(loop: Point[], tolerance: number): Point[] {
|
|
|
113
113
|
return loop.filter((_, k) => keep[k]);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
/** Path numbers are hundredths; vertices snap to even ones so edge midpoints land exactly on the grid. */
|
|
117
|
+
const UNIT = 100;
|
|
118
|
+
|
|
119
|
+
/** A grid value as a path number, as short as SVG allows: no leading zero, no `-0`. */
|
|
120
|
+
const num = (k: number) => {
|
|
121
|
+
const a = Math.abs(k), whole = Math.floor(a / UNIT), frac = a % UNIT;
|
|
122
|
+
const f = frac === 0 ? '' : frac % 10 === 0 ? `.${frac / 10}` : frac < 10 ? `.0${frac}` : `.${frac}`;
|
|
123
|
+
return (k < 0 ? '-' : '') + (whole || !f ? whole : '') + f;
|
|
119
124
|
};
|
|
120
125
|
|
|
121
|
-
/**
|
|
126
|
+
/** Turns sharper than this (radians) stay corners; gentler ones are drawn as curves. */
|
|
127
|
+
const CORNER = 0.6;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A gentle turn between long edges is a corner of the outline too, not a facet of a traced curve:
|
|
131
|
+
* it stays sharp once its curve would leave the traced loop by more than this (viewBox units).
|
|
132
|
+
*/
|
|
133
|
+
const SAG = 0.1;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Compact closed path from loops (draw with fill-rule evenodd). Traced loops are polylines, so
|
|
137
|
+
* gentle turns are drawn as quadratics between edge midpoints, which hides their facets at large
|
|
138
|
+
* sizes; real corners stay sharp. Relative commands, smooth t segments and implicit repeats keep
|
|
139
|
+
* the string short.
|
|
140
|
+
*/
|
|
122
141
|
export function loopsToPath(loops: Point[][]): string {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
142
|
+
let d = '', cmd = '', last = '';
|
|
143
|
+
const emit = (c: string, ...ks: number[]) => {
|
|
144
|
+
if (c !== cmd) { d += c; cmd = c; last = ''; }
|
|
145
|
+
for (const k of ks) {
|
|
146
|
+
const t = num(k);
|
|
147
|
+
// A separator is only needed where the next number could be read as part of the last one.
|
|
148
|
+
if (last && t[0] !== '-' && !(t[0] === '.' && last.includes('.'))) d += ' ';
|
|
149
|
+
d += t;
|
|
150
|
+
last = t;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
for (const loop of loops) {
|
|
154
|
+
const n = loop.length;
|
|
155
|
+
const v = loop.map(([x, y]) => [2 * Math.round(x * UNIT / 2), 2 * Math.round(y * UNIT / 2)]);
|
|
156
|
+
const corner = loop.map((p, k) => {
|
|
157
|
+
const a = loop[(k + n - 1) % n], b = loop[(k + 1) % n];
|
|
158
|
+
const cross = (p[0] - a[0]) * (b[1] - p[1]) - (p[1] - a[1]) * (b[0] - p[0]);
|
|
159
|
+
// The curve passes a quarter of p's height over the chord a-b away from p.
|
|
160
|
+
return Math.abs(Math.atan2(cross, (p[0] - a[0]) * (b[0] - p[0]) + (p[1] - a[1]) * (b[1] - p[1]))) > CORNER
|
|
161
|
+
|| Math.abs(cross) > 4 * SAG * Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
162
|
+
});
|
|
163
|
+
const mid = (k: number) => [(v[k % n][0] + v[(k + 1) % n][0]) / 2, (v[k % n][1] + v[(k + 1) % n][1]) / 2];
|
|
164
|
+
let [cx, cy] = mid(0);
|
|
165
|
+
emit('M', cx, cy);
|
|
166
|
+
let smooth = false;
|
|
167
|
+
for (let k = 1; k <= n; k++) {
|
|
168
|
+
const [px, py] = v[k % n];
|
|
169
|
+
if (corner[k % n]) {
|
|
170
|
+
// A midpoint between two corners lies on their straight edge, so it is only kept to start a curve.
|
|
171
|
+
emit('l', px - cx, py - cy);
|
|
172
|
+
cx = px; cy = py;
|
|
173
|
+
if (k < n && !corner[(k + 1) % n]) {
|
|
174
|
+
const [mx, my] = mid(k);
|
|
175
|
+
emit('l', mx - cx, my - cy);
|
|
176
|
+
cx = mx; cy = my;
|
|
177
|
+
}
|
|
178
|
+
smooth = false;
|
|
179
|
+
} else {
|
|
180
|
+
const [mx, my] = mid(k);
|
|
181
|
+
// After a curve, the reflected control point is exactly this vertex (it mirrors the last one
|
|
182
|
+
// across their shared midpoint), so t needs only the end point.
|
|
183
|
+
if (smooth) emit('t', mx - cx, my - cy);
|
|
184
|
+
else emit('q', px - cx, py - cy, mx - cx, my - cy);
|
|
185
|
+
cx = mx; cy = my;
|
|
186
|
+
smooth = true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
d += 'Z';
|
|
190
|
+
cmd = 'Z'; last = '';
|
|
191
|
+
}
|
|
192
|
+
return d;
|
|
126
193
|
}
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
-
//
|
|
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) -
|
|
121
|
-
return { x, y, w: Math.max(...xs) +
|
|
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
|
}
|