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/dist/render.d.ts
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Rim } from './effects.ts';
|
|
2
|
+
import type { Pose } from './motion.ts';
|
|
3
|
+
import type { Bounds, Point } from './path.ts';
|
|
2
4
|
import type { Volume } from './volume.ts';
|
|
3
5
|
import type { AvatarDefinition, RenderOptions } from './types.ts';
|
|
4
6
|
export declare const DEFAULT_COLOR = "#ff6a1a";
|
|
5
7
|
export declare const DEFAULT_EYE_COLOR = "#151515";
|
|
6
8
|
/** `depth` option value that maps to the natural inferred volume. */
|
|
7
9
|
export declare const DEFAULT_DEPTH = 6;
|
|
10
|
+
/**
|
|
11
|
+
* How far the body reaches from the middle of its box, for a working turn's hoop and for a
|
|
12
|
+
* celebration's cage. The hoops' centre hops and stretches with the body (see ribbonsFollow) but
|
|
13
|
+
* does not lean or sway with it, so each is measured over the leans its state takes, the body
|
|
14
|
+
* stretched about the centre: a working bot leans 2.4° to 6.6° and squashes to 98%; a celebrating
|
|
15
|
+
* one sways ±1.7° and stretches up to 5.5%. The hoops stand just outside that, so the body never
|
|
16
|
+
* moves through a point where a ribbon passes behind it.
|
|
17
|
+
*/
|
|
18
|
+
export declare function ribbonRims(polys: Point[][], b: Bounds): {
|
|
19
|
+
work: Rim;
|
|
20
|
+
cage: Rim;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The rim moved to where the hoops' centre is in this pose: it hops and stretches with the body
|
|
24
|
+
* (the root motion's lift and scale about the base) but does not lean or sway with it.
|
|
25
|
+
*/
|
|
26
|
+
export declare function ribbonsFollow(rim: Rim, pose: Pick<Pose, 'lift' | 'sx' | 'sy'>, b: Bounds): Rim;
|
|
8
27
|
/** The validated definition and its cached volume, exactly as the renderers use them. Throws if invalid. */
|
|
9
28
|
export declare function preparedVolume(definition: AvatarDefinition, depth?: number): {
|
|
10
29
|
def: AvatarDefinition;
|
|
@@ -44,7 +63,11 @@ export declare function avatarExtent(definition: AvatarDefinition, depth?: numbe
|
|
|
44
63
|
};
|
|
45
64
|
/** 2D affine matrix in SVG order (a b c d e f). */
|
|
46
65
|
export type Matrix = [number, number, number, number, number, number];
|
|
47
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* How one eye is drawn, in its face plane's coordinates (viewBox units): the plain pill, the
|
|
68
|
+
* plain happy arc (stroked, round caps), or for every other look a filled outline (a waiting
|
|
69
|
+
* bot's slits, and every shape on the way from one look to the next).
|
|
70
|
+
*/
|
|
48
71
|
export type EyeMark = {
|
|
49
72
|
shape: 'pill';
|
|
50
73
|
x: number;
|
|
@@ -56,6 +79,9 @@ export type EyeMark = {
|
|
|
56
79
|
shape: 'arc';
|
|
57
80
|
d: string;
|
|
58
81
|
strokeWidth: number;
|
|
82
|
+
} | {
|
|
83
|
+
shape: 'path';
|
|
84
|
+
d: string;
|
|
59
85
|
};
|
|
60
86
|
export type FrameShape = {
|
|
61
87
|
kind: 'slice';
|
|
@@ -69,6 +95,35 @@ export type FrameShape = {
|
|
|
69
95
|
key: 'eye-0' | 'eye-1';
|
|
70
96
|
matrix: Matrix;
|
|
71
97
|
mark: EyeMark;
|
|
98
|
+
} | {
|
|
99
|
+
/**
|
|
100
|
+
* A piece of a ribbon: fill `d` in `fill`. Each ribbon has a hue of its own, set apart from
|
|
101
|
+
* the body's; a piece `front` of the body is lighter than one behind it.
|
|
102
|
+
*/
|
|
103
|
+
kind: 'ribbon';
|
|
104
|
+
key: string;
|
|
105
|
+
d: string;
|
|
106
|
+
box: {
|
|
107
|
+
x: number;
|
|
108
|
+
y: number;
|
|
109
|
+
w: number;
|
|
110
|
+
h: number;
|
|
111
|
+
};
|
|
112
|
+
matrix: Matrix;
|
|
113
|
+
front: boolean;
|
|
114
|
+
/** The ribbon's colour where it is plainest (the middle of its gradient). */
|
|
115
|
+
fill: string;
|
|
116
|
+
/**
|
|
117
|
+
* A linear gradient along the whole ribbon, in the same coordinates as `d`: from its tail
|
|
118
|
+
* (x1, y1) to its head (x2, y2), the stops evenly spaced. Every piece of a ribbon shares it.
|
|
119
|
+
*/
|
|
120
|
+
gradient: {
|
|
121
|
+
x1: number;
|
|
122
|
+
y1: number;
|
|
123
|
+
x2: number;
|
|
124
|
+
y2: number;
|
|
125
|
+
stops: string[];
|
|
126
|
+
};
|
|
72
127
|
};
|
|
73
128
|
/** One cross-section outline, shared by every shape that draws it. */
|
|
74
129
|
export interface FramePath {
|
|
@@ -84,7 +139,9 @@ export interface FramePath {
|
|
|
84
139
|
}
|
|
85
140
|
/**
|
|
86
141
|
* One frame of the avatar flattened onto the screen: every cross-section the
|
|
87
|
-
* view draws with its projection, and the visible eyes, sorted back to front
|
|
142
|
+
* view draws with its projection, and the visible eyes, sorted back to front,
|
|
143
|
+
* between the ribbons behind the body and those in front of it. The marks of
|
|
144
|
+
* `thinking` and `blocked` are listed as slices too, in place of the body.
|
|
88
145
|
* Painting them in order (body paths in `color`, eyes in `ink`) inside the
|
|
89
146
|
* `root` transform and a 0 0 100 100 viewBox gives the avatar exactly as
|
|
90
147
|
* renderAvatarSvg draws it. Plain numbers and path strings, so any vector
|
|
@@ -125,16 +182,23 @@ export interface SceneFrame {
|
|
|
125
182
|
scene: string;
|
|
126
183
|
/** 2D motion (hop, roll, squash) about the base of the body. */
|
|
127
184
|
motion: string;
|
|
128
|
-
/** 3D view rotation about the body center. */
|
|
185
|
+
/** 3D view rotation about the body center, scaled down while the body gives way to a mark. */
|
|
129
186
|
view: string;
|
|
187
|
+
/** How much of its size the body keeps, 0..1; at 0 the view needs no layers. */
|
|
188
|
+
body: number;
|
|
189
|
+
/** Markup for the scene's two overlays (`data-fx`): ribbons behind the body; ribbons and marks in front. */
|
|
190
|
+
back: string;
|
|
191
|
+
front: string;
|
|
130
192
|
stack: 'z' | 'x';
|
|
131
193
|
/** Per slice of the active stack, in markup order: whether it shows. Hidden ones need no layer. */
|
|
132
194
|
slices: boolean[];
|
|
195
|
+
/** `d` is set while the eye is between the pill and the arc: it then shows the `data-morph` path alone. */
|
|
133
196
|
eyes: {
|
|
134
197
|
visible: boolean;
|
|
135
198
|
happy: boolean;
|
|
136
199
|
transform: string;
|
|
137
200
|
pill: string;
|
|
201
|
+
d: string;
|
|
138
202
|
}[];
|
|
139
203
|
}
|
|
140
204
|
/** Per-frame CSS for a mounted scene. Pure and cheap: no geometry is rebuilt. */
|
package/dist/render.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAKxC,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAI/C,OAAO,KAAK,EAAE,MAAM,EAAe,MAAM,aAAa,CAAC;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAElE,eAAO,MAAM,aAAa,YAAY,CAAC;AACvC,eAAO,MAAM,iBAAiB,YAAY,CAAC;AAC3C,qEAAqE;AACrE,eAAO,MAAM,aAAa,IAAI,CAAC;AA2C/B;;;;;;;GAOG;AACH,wBAAgB,UAAU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@@ -14,9 +14,15 @@
|
|
|
14
14
|
* does projection and occlusion. Each visible slice costs a compositor layer,
|
|
15
15
|
* so mountAvatar paints live avatars to one canvas by default and only uses
|
|
16
16
|
* the scene with `renderer: 'scene'`.
|
|
17
|
+
*
|
|
18
|
+
* A state can draw more than the body (see effects.ts): ribbons behind and in front of it, and
|
|
19
|
+
* the marks a thinking or blocked bot turns into. Those stay where they are while the body hops
|
|
20
|
+
* and rolls.
|
|
17
21
|
*/
|
|
18
|
-
import { parseHex, toHex } from './color.js';
|
|
19
|
-
import {
|
|
22
|
+
import { mix, oklch, parseHex, toHex, toOklch } from './color.js';
|
|
23
|
+
import { bodyScale, markParts, ribbonPieces, ribbonReach, rimOf } from './effects.js';
|
|
24
|
+
import { eyePath, plainLook } from './eyes.js';
|
|
25
|
+
import { computePose, hashString, noise } from './motion.js';
|
|
20
26
|
import { EYE_H, EYE_W, GAZE_X, GAZE_Y, HAPPY_ARC, analyzeBody, eyeCenters, gazeRange, validateAvatar } from './validate.js';
|
|
21
27
|
import { buildVolume } from './volume.js';
|
|
22
28
|
export const DEFAULT_COLOR = '#ff6a1a';
|
|
@@ -41,6 +47,45 @@ function depthOption(options) {
|
|
|
41
47
|
const d = clamp(Number.isFinite(options.depth) ? options.depth : DEFAULT_DEPTH, 0, MAX_DEPTH);
|
|
42
48
|
return Math.round(d * 2) / 2;
|
|
43
49
|
}
|
|
50
|
+
const lazy = (make) => {
|
|
51
|
+
let value;
|
|
52
|
+
return () => (value ??= make());
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* How far the body reaches from the middle of its box, for a working turn's hoop and for a
|
|
56
|
+
* celebration's cage. The hoops' centre hops and stretches with the body (see ribbonsFollow) but
|
|
57
|
+
* does not lean or sway with it, so each is measured over the leans its state takes, the body
|
|
58
|
+
* stretched about the centre: a working bot leans 2.4° to 6.6° and squashes to 98%; a celebrating
|
|
59
|
+
* one sways ±1.7° and stretches up to 5.5%. The hoops stand just outside that, so the body never
|
|
60
|
+
* moves through a point where a ribbon passes behind it.
|
|
61
|
+
*/
|
|
62
|
+
export function ribbonRims(polys, b) {
|
|
63
|
+
const [cx, cy] = pivotOf(b);
|
|
64
|
+
const bx = (b.minX + b.maxX) / 2, by = b.maxY;
|
|
65
|
+
/** The body leaned by each of `rolls` (degrees, about its base) and stretched by each of `sys` about the centre. */
|
|
66
|
+
const posed = (rolls, sys) => {
|
|
67
|
+
const out = [];
|
|
68
|
+
for (const roll of rolls) {
|
|
69
|
+
const r = (roll * Math.PI) / 180, c = Math.cos(r), sn = Math.sin(r);
|
|
70
|
+
for (const k of sys) {
|
|
71
|
+
out.push(polys.map((poly) => poly.map(([x, y]) => {
|
|
72
|
+
const lx = bx + c * (x - bx) - sn * (y - by), ly = by + sn * (x - bx) + c * (y - by);
|
|
73
|
+
return [cx + 1.01 * (lx - cx), cy + k * (ly - cy)];
|
|
74
|
+
})));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
};
|
|
79
|
+
return { work: rimOf(posed([2.4, 4.5, 6.6], [0.98, 1]), cx, cy), cage: rimOf(posed([-1.7, 0, 1.7], [0.99, 1.055]), cx, cy) };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The rim moved to where the hoops' centre is in this pose: it hops and stretches with the body
|
|
83
|
+
* (the root motion's lift and scale about the base) but does not lean or sway with it.
|
|
84
|
+
*/
|
|
85
|
+
export function ribbonsFollow(rim, pose, b) {
|
|
86
|
+
const bx = (b.minX + b.maxX) / 2, by = b.maxY;
|
|
87
|
+
return { ...rim, cx: bx + pose.sx * (rim.cx - bx), cy: by + pose.sy * (rim.cy - by) + pose.lift };
|
|
88
|
+
}
|
|
44
89
|
function prepare(definition, depth) {
|
|
45
90
|
const key = JSON.stringify([definition?.id, definition?.name, definition?.body?.path, definition?.eyes, depth]);
|
|
46
91
|
const hit = cache.get(key);
|
|
@@ -66,6 +111,7 @@ function prepare(definition, depth) {
|
|
|
66
111
|
id,
|
|
67
112
|
ids,
|
|
68
113
|
detail: new Map(),
|
|
114
|
+
rims: lazy(() => ribbonRims(body.polys, vol.bounds)),
|
|
69
115
|
};
|
|
70
116
|
if (cache.size >= 64)
|
|
71
117
|
cache.delete(cache.keys().next().value);
|
|
@@ -78,11 +124,13 @@ export function preparedVolume(definition, depth) {
|
|
|
78
124
|
return { def: p.def, vol: p.vol };
|
|
79
125
|
}
|
|
80
126
|
function poseOf(p, options) {
|
|
81
|
-
const motion = options.motion === 'thinking' || options.motion === 'happy' ? options.motion : 'idle';
|
|
82
127
|
return computePose({
|
|
83
128
|
seed: p.seed,
|
|
84
129
|
time: Number.isFinite(options.time) ? options.time : 0,
|
|
85
|
-
|
|
130
|
+
state: options.state,
|
|
131
|
+
mood: options.mood,
|
|
132
|
+
motion: options.motion,
|
|
133
|
+
stateAge: options.stateAge,
|
|
86
134
|
gaze: options.gaze,
|
|
87
135
|
reducedMotion: options.reducedMotion === true,
|
|
88
136
|
view: options.view,
|
|
@@ -99,6 +147,10 @@ const mul = (m, n) => [
|
|
|
99
147
|
];
|
|
100
148
|
const translate = (x, y) => [1, 0, 0, 1, x, y];
|
|
101
149
|
const about = (x, y, m) => mul(translate(x, y), mul(m, translate(-x, -y)));
|
|
150
|
+
const invert = ([a, b, c, d, e, g]) => {
|
|
151
|
+
const det = a * d - b * c;
|
|
152
|
+
return [d / det, -b / det, -c / det, a / det, (c * g - d * e) / det, (b * e - a * g) / det];
|
|
153
|
+
};
|
|
102
154
|
const mat = (m) => `matrix(${m.map((v, i) => (i < 4 ? (Math.round(v * 10000) / 10000).toString() : f(v))).join(' ')})`;
|
|
103
155
|
export function viewBasis(yaw, pitch) {
|
|
104
156
|
const cY = Math.cos(yaw), cP = Math.cos(pitch);
|
|
@@ -156,11 +208,20 @@ const POSE_SWAY = 0.2;
|
|
|
156
208
|
* canvas renderer uses it only as an upper bound: it sizes each bitmap to what is actually painted.
|
|
157
209
|
*/
|
|
158
210
|
export function avatarExtent(definition, depth) {
|
|
159
|
-
const
|
|
211
|
+
const p = prepare(definition, depthOption({ depth }));
|
|
212
|
+
const { vol } = p;
|
|
160
213
|
const b = vol.bounds;
|
|
161
214
|
const [cx, cy] = pivotOf(b);
|
|
162
215
|
const radius = Math.hypot(b.maxX - b.minX, b.maxY - b.minY, Math.max(...vol.parts.map((part) => part.depth)) + 2 * EYE_LIFT) / 2;
|
|
163
|
-
|
|
216
|
+
// Ribbons reach their hoop plus their band from the middle of the body's box, and keep the
|
|
217
|
+
// canvas's antialiasing margin (FIT_PAD) inside.
|
|
218
|
+
const { work, cage } = p.rims();
|
|
219
|
+
// The hoops' centre also hops and stretches with the body: as far as the body's own pose moves it.
|
|
220
|
+
const reach = (rim) => {
|
|
221
|
+
const r = ribbonReach(rim.max);
|
|
222
|
+
return Math.hypot(rim.cx - cx, rim.cy - cy) + POSE_LIFT + POSE_SWAY * Math.abs(b.maxY - rim.cy) + r.radius + r.band + 1.2;
|
|
223
|
+
};
|
|
224
|
+
const half = Math.max(radius + POSE_LIFT + POSE_SWAY * (radius + b.maxY - cy), reach(work), reach(cage));
|
|
164
225
|
return { x: cx - half, y: cy - half, size: 2 * half };
|
|
165
226
|
}
|
|
166
227
|
/**
|
|
@@ -197,20 +258,27 @@ function eyeStates(p, pose) {
|
|
|
197
258
|
const dx = pose.gazeX * (pose.gazeX >= 0 ? r.right : r.left) * GAZE_X * s;
|
|
198
259
|
const dy = pose.gazeY * (pose.gazeY >= 0 ? r.down : r.up) * GAZE_Y * s;
|
|
199
260
|
const centers = eyeCenters(p.def.eyes);
|
|
261
|
+
const plain = plainLook(pose.eyes);
|
|
200
262
|
return [0, 1].map((i) => {
|
|
201
263
|
const squint = i === 0 ? pose.squintL : pose.squintR;
|
|
202
264
|
const open = Math.max(h * Math.max(0.09, (1 - pose.blink) * (1 - squint * 0.4)), w * 0.55);
|
|
203
|
-
return { x: centers[i][0], y: centers[i][1], w, h, dx, dy, open, shift: (h - open) * 0.18,
|
|
265
|
+
return { x: centers[i][0], y: centers[i][1], w, h, dx, dy, open, shift: (h - open) * 0.18, look: pose.eyes, plain };
|
|
204
266
|
});
|
|
205
267
|
}
|
|
268
|
+
/** Where an eye is drawn: with its gaze offset, or without it where a transform applies the gaze. */
|
|
269
|
+
function eyeSpot(e, gaze) {
|
|
270
|
+
return { x: e.x + (gaze ? e.dx : 0), y: e.y + (gaze ? e.dy : 0), w: e.w, h: e.h, open: e.open, shift: e.shift };
|
|
271
|
+
}
|
|
206
272
|
function happyArc(e) {
|
|
207
273
|
const ex = e.x + e.dx, ey = e.y + e.dy;
|
|
208
274
|
const hw = e.w * HAPPY_ARC.halfWidth, base = ey + e.h * HAPPY_ARC.base;
|
|
209
275
|
return `M${f(ex - hw)} ${f(base)}Q${f(ex)} ${f(ey - e.h * HAPPY_ARC.peak)} ${f(ex + hw)} ${f(base)}`;
|
|
210
276
|
}
|
|
211
277
|
function eyeMark(e) {
|
|
212
|
-
if (e.
|
|
278
|
+
if (e.plain === 'arc')
|
|
213
279
|
return { shape: 'arc', d: happyArc(e), strokeWidth: e.w * HAPPY_ARC.stroke };
|
|
280
|
+
if (e.plain === null)
|
|
281
|
+
return { shape: 'path', d: eyePath(e.look, eyeSpot(e, true)) };
|
|
214
282
|
const cx = e.x + e.dx, cy = e.y + e.dy + e.shift;
|
|
215
283
|
return { shape: 'pill', x: cx - e.w / 2, y: cy - e.open / 2, width: e.w, height: e.open, radius: Math.min(e.w, e.open) / 2 };
|
|
216
284
|
}
|
|
@@ -232,6 +300,124 @@ export function avatarFrame(definition, options = {}) {
|
|
|
232
300
|
export function liveFrame(definition, options = {}) {
|
|
233
301
|
return flatFrame(definition, options, options.size);
|
|
234
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* The ribbons are steps along a ramp of the body's own color, painter's style: each step is
|
|
305
|
+
* RAMP_LIGHT lighter or darker in OKLCH and turns RAMP_HUE degrees with it, toward yellow as it
|
|
306
|
+
* lightens and toward blue as it darkens. Steps run +1, -1, +2, -2 and so on, skipping any that
|
|
307
|
+
* would leave RAMP_RANGE, so a working bot's three are one lighter, one darker and one lighter
|
|
308
|
+
* still on most bodies. A body lighter or darker than the range steps from its edge instead, so
|
|
309
|
+
* a white body's ribbons are all well darker than it, and a black one's well lighter.
|
|
310
|
+
*/
|
|
311
|
+
const RAMP_LIGHT = 0.09;
|
|
312
|
+
const RAMP_HUE = 14;
|
|
313
|
+
// Six steps wide: room for five besides the body's own, wherever it starts. Past it shades wash
|
|
314
|
+
// out toward white or sink toward black.
|
|
315
|
+
const RAMP_RANGE = [0.36, 0.9];
|
|
316
|
+
const WARM = 100;
|
|
317
|
+
const palettes = new Map();
|
|
318
|
+
/**
|
|
319
|
+
* How far a ribbon's colour runs from its tail (its shade) to its head: further from the body's
|
|
320
|
+
* lightness by GRADIENT_LIGHT, turning GRADIENT_HUE degrees with it (warmer as it lightens,
|
|
321
|
+
* cooler as it darkens). GRADIENT_STOPS colours along the way.
|
|
322
|
+
*/
|
|
323
|
+
const GRADIENT_LIGHT = 0.13;
|
|
324
|
+
const GRADIENT_HUE = 25;
|
|
325
|
+
const GRADIENT_STOPS = 5;
|
|
326
|
+
/** Below this chroma a body counts as white, gray or black, and its ribbons are grays with this much tint. */
|
|
327
|
+
const NEUTRAL = 0.04;
|
|
328
|
+
const NEUTRAL_TINT = 0.025;
|
|
329
|
+
/**
|
|
330
|
+
* Which shades (by their order along the ramp: +1, -1, +2, -2, +3 on most bodies) each band's lanes
|
|
331
|
+
* take, a working bot's band first. A celebration's two bands lean to the lighter shades, so the
|
|
332
|
+
* cage stays bright against a dark page.
|
|
333
|
+
*/
|
|
334
|
+
const BAND_SHADES = [[0, 1, 2], [2, 0, 3], [1, 2, 0]];
|
|
335
|
+
/** Turn hue `h` by up to `by` degrees toward hue `to`, never past it. */
|
|
336
|
+
function turnToward(h, to, by) {
|
|
337
|
+
const d = ((to - h + 540) % 360) - 180;
|
|
338
|
+
return (h + Math.sign(d) * Math.min(Math.abs(d), by) + 360) % 360;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The colors of the ribbons on a body this color, one per band and lane (see BAND_SHADES): five
|
|
342
|
+
* steps along its own ramp (see RAMP_LIGHT), at least as vivid as the body. A white, gray or
|
|
343
|
+
* black body gets grays: lighter or darker than it, with just a hint of a hue its seed picks.
|
|
344
|
+
* A piece behind the body is a shade deeper than one in front of it.
|
|
345
|
+
*/
|
|
346
|
+
/** The colour `t` (0..1) of the way along a colour run, in sRGB between its stops. */
|
|
347
|
+
function runAt(run, t) {
|
|
348
|
+
const x = clamp(t, 0, 1) * (run.length - 1), n = Math.min(Math.floor(x), run.length - 2);
|
|
349
|
+
return toHex(mix(run[n], run[n + 1], x - n));
|
|
350
|
+
}
|
|
351
|
+
function ribbonPalette(color, seed) {
|
|
352
|
+
const key = `${color}|${seed}`;
|
|
353
|
+
let palette = palettes.get(key);
|
|
354
|
+
if (palette)
|
|
355
|
+
return palette;
|
|
356
|
+
const [light, chroma, hue] = toOklch(parseHex(color) ?? parseHex(DEFAULT_COLOR));
|
|
357
|
+
// A seed's hue skips the yellows (80 to 130), whose darker steps turn olive.
|
|
358
|
+
const neutral = chroma < NEUTRAL;
|
|
359
|
+
const base = neutral ? (130 + noise(seed, 500) * 310) % 360 : hue;
|
|
360
|
+
const c = neutral ? NEUTRAL_TINT : Math.max(chroma, 0.17);
|
|
361
|
+
const from = Math.min(RAMP_RANGE[1], Math.max(RAMP_RANGE[0], light));
|
|
362
|
+
const steps = [];
|
|
363
|
+
for (let k = 1; steps.length < 5 && k < 10; k++) {
|
|
364
|
+
for (const step of [k, -k]) {
|
|
365
|
+
const l = from + step * RAMP_LIGHT;
|
|
366
|
+
if (steps.length < 5 && l >= RAMP_RANGE[0] - 1e-9 && l <= RAMP_RANGE[1] + 1e-9)
|
|
367
|
+
steps.push(step);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
while (steps.length < 5)
|
|
371
|
+
steps.push(steps[steps.length - 1] ?? 1);
|
|
372
|
+
const shades = steps.map((step) => {
|
|
373
|
+
const l = from + step * RAMP_LIGHT;
|
|
374
|
+
const h = turnToward(base, step > 0 ? WARM : WARM + 180, RAMP_HUE * Math.abs(step));
|
|
375
|
+
// Tail to head, away from the body's lightness: lighter and warmer, or darker and cooler.
|
|
376
|
+
const away = step > 0 ? 1 : -1;
|
|
377
|
+
const run = Array.from({ length: GRADIENT_STOPS }, (_, n) => {
|
|
378
|
+
const t = n / (GRADIENT_STOPS - 1);
|
|
379
|
+
const hue = turnToward(h, away > 0 ? WARM : WARM + 180, GRADIENT_HUE * t);
|
|
380
|
+
return oklch(clamp(l + away * GRADIENT_LIGHT * t, 0.26, 0.9), c, hue);
|
|
381
|
+
});
|
|
382
|
+
return { step, run };
|
|
383
|
+
});
|
|
384
|
+
// Each band takes three of the shades, lightest on the inside lane to darkest on the outside.
|
|
385
|
+
palette = BAND_SHADES.flatMap((pick) => pick.map((n) => shades[n]).sort((x, y) => y.step - x.step).map(({ run }) => run));
|
|
386
|
+
if (palettes.size >= 64)
|
|
387
|
+
palettes.clear();
|
|
388
|
+
palettes.set(key, palette);
|
|
389
|
+
return palette;
|
|
390
|
+
}
|
|
391
|
+
function effectsOf(p, pose, options) {
|
|
392
|
+
const b = p.vol.bounds;
|
|
393
|
+
const time = Number.isFinite(options.time) ? options.time : 0;
|
|
394
|
+
const still = options.reducedMotion === true;
|
|
395
|
+
const [cx, cy] = pivotOf(b);
|
|
396
|
+
return {
|
|
397
|
+
body: bodyScale(pose.bang, pose.dots),
|
|
398
|
+
ribbons: still ? [] : ribbonPieces(b, pose.ribbons, p.seed, time, pose.sweep, pose.cage, ribbonsFollow(pose.cage >= 0.5 ? p.rims().cage : p.rims().work, pose, b)).map((r) => {
|
|
399
|
+
const hue = ribbonPalette(options.color, p.seed)[r.ribbon];
|
|
400
|
+
// The piece's stretch of its ribbon's colour run, front and back alike.
|
|
401
|
+
const [s0, s1] = r.along;
|
|
402
|
+
const stops = [s0, (s0 + s1) / 2, s1].map((t) => runAt(hue, t));
|
|
403
|
+
// A gradient needs some length; a piece too short to have one takes its middle colour.
|
|
404
|
+
const long = Math.hypot(r.to[0] - r.from[0], r.to[1] - r.from[1]) > 0.01;
|
|
405
|
+
return {
|
|
406
|
+
d: r.d, box: r.box, front: r.front, fill: stops[1],
|
|
407
|
+
gradient: long ? { x1: r.from[0], y1: r.from[1], x2: r.to[0], y2: r.to[1], stops } : { x1: r.from[0], y1: r.from[1], x2: r.from[0] + 0.01, y2: r.from[1], stops: [stops[1], stops[1], stops[1]] },
|
|
408
|
+
};
|
|
409
|
+
}),
|
|
410
|
+
marks: markParts(b, pose.bang, pose.dots, p.seed, time, still).map((part) => {
|
|
411
|
+
const turn = [Math.cos(part.turn), Math.sin(part.turn), -Math.sin(part.turn), Math.cos(part.turn), 0, 0];
|
|
412
|
+
return {
|
|
413
|
+
id: `${p.id}-${part.shape}`,
|
|
414
|
+
d: part.d,
|
|
415
|
+
box: part.box,
|
|
416
|
+
matrix: mul(about(cx, cy, turn), mul(translate(part.x, part.y), [part.scale, 0, 0, part.scale, 0, 0])),
|
|
417
|
+
};
|
|
418
|
+
}),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
235
421
|
function flatFrame(definition, options, size) {
|
|
236
422
|
const p = prepare(definition, depthOption(options));
|
|
237
423
|
const { vol } = p;
|
|
@@ -242,47 +428,78 @@ function flatFrame(definition, options, size) {
|
|
|
242
428
|
const v = viewBasis(pose.yaw, pose.pitch);
|
|
243
429
|
const stack = activeStack(v);
|
|
244
430
|
const eyesMid = [p.def.eyes.x, p.def.eyes.y];
|
|
431
|
+
const fx = effectsOf(p, pose, options);
|
|
432
|
+
const root = rootMatrix(pose, b);
|
|
433
|
+
// The body shrinks toward its center as it gives way to a mark.
|
|
434
|
+
const shrink = (m) => (fx.body < 1 ? mul(about(pivot[0], pivot[1], [fx.body, 0, 0, fx.body, 0, 0]), m) : m);
|
|
245
435
|
// Only the cross-sections this view draws are listed; ids are stable across frames.
|
|
246
436
|
const paths = new Map();
|
|
247
437
|
const items = [];
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
438
|
+
if (fx.body > 0) {
|
|
439
|
+
drawnSlices(p, v, size, (s, m, i, j) => {
|
|
440
|
+
let path = paths.get(s.d);
|
|
441
|
+
if (!path) {
|
|
442
|
+
path = { id: p.ids.get(s.d), d: s.d, evenOdd: s.evenOdd, box: { ...s.box } };
|
|
443
|
+
paths.set(s.d, path);
|
|
444
|
+
}
|
|
445
|
+
// Depth keys are taken under the eyes for the eye part, so only depth decides eye-vs-body order there.
|
|
446
|
+
const [rx, ry] = i === vol.eyePart ? eyesMid : vol.parts[i].center;
|
|
447
|
+
const depth = stack === 'z' ? projectPoint(v, [rx, ry, s.at], pivot)[2] : projectPoint(v, [s.at, ry, 0], pivot)[2];
|
|
448
|
+
items.push({ depth, shape: { kind: 'slice', key: `${i}${stack}${j}`, path: path.id, matrix: shrink(m) } });
|
|
449
|
+
});
|
|
450
|
+
// Painted on the surface; each eye goes once the surface under it turns away.
|
|
451
|
+
eyeStates(p, pose).forEach((e, idx) => {
|
|
452
|
+
if (eyeVisible(vol, v, idx)) {
|
|
453
|
+
const z = vol.eyeZ[idx] + EYE_LIFT;
|
|
454
|
+
items.push({
|
|
455
|
+
depth: projectPoint(v, [eyesMid[0], eyesMid[1], z], pivot)[2] + 1e-6,
|
|
456
|
+
shape: { kind: 'eye', key: idx === 0 ? 'eye-0' : 'eye-1', matrix: shrink(planeMatrix(v, z, pivot)), mark: eyeMark(e) },
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
items.sort((a, c) => a.depth - c.depth);
|
|
461
|
+
}
|
|
462
|
+
// Ribbons and marks stay put while the body moves: their matrices undo the root's motion.
|
|
463
|
+
const fixed = fx.ribbons.length || fx.marks.length ? invert(root) : null;
|
|
464
|
+
const ribbon = (front) => fx.ribbons.flatMap((r, i) => r.front === front ? [{ kind: 'ribbon', key: `ribbon-${i}`, d: r.d, box: r.box, matrix: fixed, front, fill: r.fill, gradient: r.gradient }] : []);
|
|
465
|
+
const marks = fx.marks.map((mark, i) => {
|
|
466
|
+
if (!paths.has(mark.id))
|
|
467
|
+
paths.set(mark.id, { id: mark.id, d: mark.d, evenOdd: false, box: mark.box });
|
|
468
|
+
return { kind: 'slice', key: `mark-${i}`, path: mark.id, matrix: mul(fixed, mark.matrix) };
|
|
268
469
|
});
|
|
269
|
-
|
|
270
|
-
|
|
470
|
+
return {
|
|
471
|
+
name: p.def.name, color, ink, paths: [...paths.values()], root,
|
|
472
|
+
shapes: [...ribbon(false), ...items.map((it) => it.shape), ...ribbon(true), ...marks],
|
|
473
|
+
};
|
|
271
474
|
}
|
|
272
475
|
// ---------------------------------------------------------------------------
|
|
273
476
|
// Standalone SVG still
|
|
274
477
|
function eyeSvg(mark, ink) {
|
|
478
|
+
if (mark.shape === 'path')
|
|
479
|
+
return `<path d="${mark.d}" fill="${ink}"/>`;
|
|
275
480
|
if (mark.shape === 'arc') {
|
|
276
481
|
return `<path d="${mark.d}" fill="none" stroke="${ink}" stroke-width="${f(mark.strokeWidth)}" stroke-linecap="round"/>`;
|
|
277
482
|
}
|
|
278
483
|
return `<rect x="${f(mark.x)}" y="${f(mark.y)}" width="${f(mark.width)}" height="${f(mark.height)}" rx="${f(mark.radius)}" fill="${ink}"/>`;
|
|
279
484
|
}
|
|
485
|
+
/**
|
|
486
|
+
* A ribbon piece with its gradient alongside it. The id is taken from the gradient itself, so
|
|
487
|
+
* repeats on a page (the same avatar at the same moment) share one harmlessly.
|
|
488
|
+
*/
|
|
489
|
+
function ribbonSvg(d, g) {
|
|
490
|
+
const id = `oba-g${hashString(`${f(g.x1)},${f(g.y1)},${f(g.x2)},${f(g.y2)},${g.stops.join()}`).toString(36)}`;
|
|
491
|
+
const stops = g.stops.map((c, i) => `<stop offset="${f(i / (g.stops.length - 1))}" stop-color="${c}"/>`).join('');
|
|
492
|
+
return (`<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${f(g.x1)}" y1="${f(g.y1)}" x2="${f(g.x2)}" y2="${f(g.y2)}">${stops}</linearGradient>` +
|
|
493
|
+
`<path d="${d}" fill="url(#${id})"/>`);
|
|
494
|
+
}
|
|
280
495
|
/** Render a definition to a standalone SVG string. Throws if the definition is invalid. */
|
|
281
496
|
export function renderAvatarSvg(definition, options = {}) {
|
|
282
497
|
const frame = avatarFrame(definition, options);
|
|
283
498
|
const defsMarkup = frame.paths.map((d) => `<path id="${d.id}" d="${d.d}"${d.evenOdd ? ' fill-rule="evenodd"' : ''}/>`).join('');
|
|
284
499
|
const body = frame.shapes
|
|
285
|
-
.map((s) =>
|
|
500
|
+
.map((s) => s.kind === 'slice' ? `<use href="#${s.path}" transform="${mat(s.matrix)}"/>`
|
|
501
|
+
: s.kind === 'eye' ? `<g transform="${mat(s.matrix)}">${eyeSvg(s.mark, frame.ink)}</g>`
|
|
502
|
+
: `<g transform="${mat(s.matrix)}">${ribbonSvg(s.d, s.gradient)}</g>`)
|
|
286
503
|
.join('');
|
|
287
504
|
const size = Number.isFinite(options.size) && options.size > 0 ? f(clamp(options.size, 8, 4096)) : null;
|
|
288
505
|
const dims = size ? ` width="${size}" height="${size}"` : '';
|
|
@@ -310,6 +527,7 @@ function sliceMarkup(s) {
|
|
|
310
527
|
return (`<svg viewBox="${f(x)} ${f(y)} ${f(w)} ${f(h)}" style="position:absolute;width:${u(w)};height:${u(h)};overflow:visible;${place}" aria-hidden="true">` +
|
|
311
528
|
`<path d="${s.d}"${rule} fill="currentColor"/></svg>`);
|
|
312
529
|
}
|
|
530
|
+
const overlay = (side) => `<svg data-fx="${side}" viewBox="0 0 100 100" style="${FILL}overflow:visible;pointer-events:none" fill="currentColor" aria-hidden="true"></svg>`;
|
|
313
531
|
/**
|
|
314
532
|
* Static markup of the live scene for a definition and depth. Mount it once,
|
|
315
533
|
* then apply `sceneFrame` every frame. Body color comes from CSS `color` on
|
|
@@ -330,14 +548,21 @@ export function renderAvatarScene(definition, options = {}) {
|
|
|
330
548
|
`<i style="position:absolute;inset:0;border-radius:999px;background:${ink}"></i>` +
|
|
331
549
|
`<svg viewBox="${f(e.x - e.w / 2)} ${f(e.y - e.h / 2)} ${f(e.w)} ${f(e.h)}" style="${FILL}overflow:visible;display:none" aria-hidden="true">` +
|
|
332
550
|
`<path d="${happyArc(e)}" fill="none" stroke="${ink}" stroke-width="${f(e.w * HAPPY_ARC.stroke)}" stroke-linecap="round"/></svg>` +
|
|
551
|
+
// Every look between the pill and the arc: its outline is written each frame it changes.
|
|
552
|
+
`<svg data-morph viewBox="${f(e.x - e.w / 2)} ${f(e.y - e.h / 2)} ${f(e.w)} ${f(e.h)}" style="${FILL}overflow:visible;display:none" aria-hidden="true">` +
|
|
553
|
+
`<path fill="${ink}"/></svg>` +
|
|
333
554
|
`</div>`);
|
|
334
555
|
});
|
|
335
556
|
return (`<div data-avatar-scene="${p.id}" style="position:relative;width:100%;height:100%;container-type:size">` +
|
|
557
|
+
// Ribbons behind the body; they and the marks are written each frame they show.
|
|
558
|
+
overlay('back') +
|
|
336
559
|
// will-change pins the slices' raster scale: without it every hop and squash re-rasterizes each slice.
|
|
337
560
|
`<div data-motion style="${FILL}will-change:transform;transform-origin:${u((b.minX + b.maxX) / 2)} ${u(b.maxY)}">` +
|
|
338
561
|
`<div data-view style="${FILL}transform-style:preserve-3d;transform-origin:${u(cx)} ${u(cy)}">` +
|
|
339
562
|
stack('z') + stack('x') + eyes.join('') +
|
|
340
|
-
`</div></div
|
|
563
|
+
`</div></div>` +
|
|
564
|
+
overlay('front') +
|
|
565
|
+
`</div>`);
|
|
341
566
|
}
|
|
342
567
|
const r4 = (n) => Math.round(n * 10000) / 10000;
|
|
343
568
|
/** Closest on-screen spacing, CSS px, between cross-sections a small live avatar keeps. */
|
|
@@ -379,18 +604,24 @@ export function sceneFrame(definition, options = {}) {
|
|
|
379
604
|
const stack = activeStack(v);
|
|
380
605
|
const drawn = sliceCull(v);
|
|
381
606
|
const kept = sliceDetail(p, stack, options.size);
|
|
607
|
+
const fx = effectsOf(p, pose, options);
|
|
608
|
+
const piece = (front) => fx.ribbons.filter((r) => r.front === front).map((r) => ribbonSvg(r.d, r.gradient)).join('');
|
|
382
609
|
let k = 0;
|
|
383
610
|
return {
|
|
384
611
|
scene: p.id,
|
|
385
612
|
motion: `translate(0,${u(pose.lift)}) rotate(${f(pose.roll)}deg) scale(${r4(pose.sx)},${r4(pose.sy)})`,
|
|
386
|
-
view: `rotateX(${r4(pose.pitch)}rad) rotateY(${r4(pose.yaw)}rad)`,
|
|
613
|
+
view: (fx.body < 1 ? `scale(${r4(fx.body)}) ` : '') + `rotateX(${r4(pose.pitch)}rad) rotateY(${r4(pose.yaw)}rad)`,
|
|
614
|
+
body: fx.body,
|
|
615
|
+
back: piece(false),
|
|
616
|
+
front: piece(true) + fx.marks.map((mark) => `<path d="${mark.d}" transform="${mat(mark.matrix)}"/>`).join(''),
|
|
387
617
|
stack,
|
|
388
618
|
slices: p.vol.parts.flatMap((part) => (stack === 'z' ? part.zSlices : part.xSlices).map((s) => kept[k++] && drawn(s))),
|
|
389
619
|
eyes: states.map((e, i) => ({
|
|
390
620
|
visible: eyeVisible(p.vol, v, i),
|
|
391
|
-
happy: e.
|
|
621
|
+
happy: e.plain === 'arc',
|
|
392
622
|
transform: `translateZ(${u(p.vol.eyeZ[i] + EYE_LIFT)}) translate(${u(e.dx)},${u(e.dy)})`,
|
|
393
623
|
pill: `translateY(${u(e.shift)}) scaleY(${r4(e.open / e.h)})`,
|
|
624
|
+
d: e.plain === null ? eyePath(e.look, eyeSpot(e, false)) : '',
|
|
394
625
|
})),
|
|
395
626
|
};
|
|
396
627
|
}
|