procedural-bot-animations 0.1.0 → 0.2.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 +167 -47
- package/dist/animation.d.ts +90 -0
- package/dist/animation.js +396 -0
- package/dist/brain.d.ts +194 -35
- package/dist/brain.js +578 -54
- package/dist/character.d.ts +123 -0
- package/dist/character.js +916 -0
- package/dist/characters/berry.d.ts +2 -0
- package/dist/characters/berry.js +38 -0
- package/dist/characters/flame.d.ts +2 -0
- package/dist/characters/flame.js +46 -0
- package/dist/delta.d.ts +16 -0
- package/dist/delta.js +146 -0
- package/dist/examples.d.ts +4 -0
- package/dist/examples.js +208 -0
- package/dist/focus.d.ts +46 -0
- package/dist/focus.js +284 -0
- package/dist/gaze.d.ts +41 -53
- package/dist/gaze.js +125 -82
- package/dist/index.d.ts +18 -11
- package/dist/index.js +18 -11
- package/dist/interactions.d.ts +30 -0
- package/dist/interactions.js +100 -0
- package/dist/library.d.ts +18 -0
- package/dist/library.js +14 -0
- package/dist/moods.d.ts +78 -46
- package/dist/moods.js +48 -73
- package/dist/motion.d.ts +93 -0
- package/dist/motion.js +167 -0
- package/dist/mount.d.ts +90 -0
- package/dist/mount.js +707 -0
- package/dist/parts.d.ts +36 -0
- package/dist/parts.js +131 -0
- package/dist/reactions.d.ts +184 -44
- package/dist/reactions.js +1851 -116
- package/dist/rig.d.ts +119 -0
- package/dist/rig.js +59 -0
- package/dist/scroll-traveler.js +1 -1
- package/dist/ticker.d.ts +10 -0
- package/dist/ticker.js +42 -0
- package/dist/transformations.d.ts +21 -0
- package/dist/transformations.js +306 -0
- package/package.json +7 -11
- package/dist/bind.d.ts +0 -27
- package/dist/bind.js +0 -42
- package/dist/blink.d.ts +0 -25
- package/dist/blink.js +0 -48
- package/dist/fake3d.d.ts +0 -20
- package/dist/fake3d.js +0 -24
- package/dist/noise.d.ts +0 -16
- package/dist/noise.js +0 -17
- package/dist/render.d.ts +0 -29
- package/dist/render.js +0 -97
- package/dist/rng.d.ts +0 -10
- package/dist/rng.js +0 -18
- package/dist/spring.d.ts +0 -21
- package/dist/spring.js +0 -33
- package/dist/types.d.ts +0 -63
- package/dist/types.js +0 -46
package/dist/mount.js
ADDED
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
import { Brain } from "./brain.js";
|
|
2
|
+
import { decideGaze, newFocusState, observePage, } from "./focus.js";
|
|
3
|
+
import { fmtNum } from "./motion.js";
|
|
4
|
+
import { axisOf, bendPath, circleLike, mixPath, parsePath, } from "./parts.js";
|
|
5
|
+
import { ASPECT, CHECK, CODE, DOT_R, HAPPY_EYE_PATH, ORBIT, ROUND_RADIUS, SPARKS, SPARK_PATH, VIEWBOX_ATTR, } from "./rig.js";
|
|
6
|
+
import { pointer, subscribe } from "./ticker.js";
|
|
7
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
8
|
+
const BRAND_COLOR = "#e11d2e";
|
|
9
|
+
const DEFAULTS = {
|
|
10
|
+
color: BRAND_COLOR,
|
|
11
|
+
eyeColor: "#0a0a0a",
|
|
12
|
+
accentColor: "#157a3c",
|
|
13
|
+
inkColor: "#0a0a0a",
|
|
14
|
+
lampColor: "#facc15",
|
|
15
|
+
raysColor: "#f5b301",
|
|
16
|
+
grayColor: "#8a8a8a",
|
|
17
|
+
};
|
|
18
|
+
/** Embers rising from the tongues: lateral shift and colour of each one. */
|
|
19
|
+
const EMBERS = Array.from({ length: 7 }, (_, i) => ({
|
|
20
|
+
dx: Math.sin(i * 2.4) * 150,
|
|
21
|
+
color: i % 2 ? "#fbbf24" : "#fb923c",
|
|
22
|
+
}));
|
|
23
|
+
const SMOKE_PUFFS = 4;
|
|
24
|
+
let instances = 0;
|
|
25
|
+
function node(tag, attrs = {}, children = []) {
|
|
26
|
+
const el = document.createElementNS(NS, tag);
|
|
27
|
+
for (const k in attrs)
|
|
28
|
+
el.setAttribute(k, String(attrs[k]));
|
|
29
|
+
for (const c of children)
|
|
30
|
+
el.appendChild(c);
|
|
31
|
+
return el;
|
|
32
|
+
}
|
|
33
|
+
/** `translate(p) inside translate(-p)`: transforms around a point. */
|
|
34
|
+
const around = (p, inside) => `translate(${p.x} ${p.y}) ${inside} translate(${-p.x} ${-p.y})`;
|
|
35
|
+
const n = fmtNum;
|
|
36
|
+
function normalizeMood(m) {
|
|
37
|
+
if (!m)
|
|
38
|
+
return { neutral: 1 };
|
|
39
|
+
return typeof m === "string" ? { [m]: 1 } : m;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Mounts an animated character inside `host`. The returned handle updates the
|
|
43
|
+
* options live (`update`), fires reactions by hand and cleans everything up
|
|
44
|
+
* (`destroy`).
|
|
45
|
+
*/
|
|
46
|
+
export function mountCharacter(host, options) {
|
|
47
|
+
const uid = `pba${++instances}`;
|
|
48
|
+
let raw = { ...options };
|
|
49
|
+
let cur = resolve(raw);
|
|
50
|
+
const brain = new Brain(cur.rig, {
|
|
51
|
+
library: raw.library,
|
|
52
|
+
interactions: raw.interactions,
|
|
53
|
+
now: performance.now(),
|
|
54
|
+
});
|
|
55
|
+
let svg;
|
|
56
|
+
let parts;
|
|
57
|
+
let roundGeo;
|
|
58
|
+
let lastColor = "";
|
|
59
|
+
let lastRound = -1;
|
|
60
|
+
let lastPose = null;
|
|
61
|
+
let hovering = false;
|
|
62
|
+
let touchTimer = null;
|
|
63
|
+
let destroyed = false;
|
|
64
|
+
const gazeState = {
|
|
65
|
+
attention: 0,
|
|
66
|
+
boredom: 0,
|
|
67
|
+
mode: null,
|
|
68
|
+
reason: null,
|
|
69
|
+
gliding: false,
|
|
70
|
+
};
|
|
71
|
+
function resolve(o) {
|
|
72
|
+
return {
|
|
73
|
+
rig: o.rig,
|
|
74
|
+
size: o.size,
|
|
75
|
+
color: o.color ?? DEFAULTS.color,
|
|
76
|
+
eyeStyle: o.eyeStyle ?? "classic",
|
|
77
|
+
gaze: o.gaze ?? (o.gaze === null ? "none" : "pointer"),
|
|
78
|
+
interactive: o.interactive ?? true,
|
|
79
|
+
bend: o.bend ?? true,
|
|
80
|
+
reducedMotion: o.reducedMotion ?? "auto",
|
|
81
|
+
glow: o.glow ?? false,
|
|
82
|
+
fixedRays: o.fixedRays ?? false,
|
|
83
|
+
grayColor: o.grayColor ?? DEFAULTS.grayColor,
|
|
84
|
+
mood: normalizeMood(o.mood),
|
|
85
|
+
activity: o.activity ?? null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/* ---------- building the SVG ---------- */
|
|
89
|
+
function build() {
|
|
90
|
+
const rig = cur.rig;
|
|
91
|
+
const ink = raw.inkColor ?? DEFAULTS.inkColor;
|
|
92
|
+
const eyeColor = raw.eyeColor ?? DEFAULTS.eyeColor;
|
|
93
|
+
const accent = raw.accentColor ?? DEFAULTS.accentColor;
|
|
94
|
+
const gray = cur.grayColor;
|
|
95
|
+
const visible = rig.appendages.filter((a) => !raw.appendages || raw.appendages.includes(a.role));
|
|
96
|
+
const bodyCmds = parsePath(rig.body);
|
|
97
|
+
roundGeo = { body: bodyCmds, circle: circleLike(bodyCmds, rig.anchor, ROUND_RADIUS) };
|
|
98
|
+
const clipId = (side) => `${uid}-lid-${side}`;
|
|
99
|
+
const sides = ["l", "r"];
|
|
100
|
+
const defs = node("defs", {}, [
|
|
101
|
+
...sides.map((l) => node("clipPath", { id: clipId(l), clipPathUnits: "userSpaceOnUse" }, [
|
|
102
|
+
node("polygon", { points: "-200,-200 200,-200 200,200 -200,200" }),
|
|
103
|
+
])),
|
|
104
|
+
...(rig.core
|
|
105
|
+
? [
|
|
106
|
+
node("radialGradient", { id: `${uid}-glow`, cx: 0.5, cy: 0.75, r: 0.6 }, [
|
|
107
|
+
node("stop", { offset: 0, "stop-color": "#fde047" }),
|
|
108
|
+
node("stop", { offset: 0.55, "stop-color": "#fb923c", "stop-opacity": 0.9 }),
|
|
109
|
+
node("stop", { offset: 1, "stop-color": "#fb923c", "stop-opacity": 0 }),
|
|
110
|
+
]),
|
|
111
|
+
]
|
|
112
|
+
: []),
|
|
113
|
+
]);
|
|
114
|
+
const shadow = node("ellipse", {
|
|
115
|
+
cx: rig.shadow.x,
|
|
116
|
+
cy: rig.shadow.y,
|
|
117
|
+
rx: rig.shadow.rx,
|
|
118
|
+
ry: rig.shadow.ry,
|
|
119
|
+
fill: "currentColor",
|
|
120
|
+
opacity: 0.14,
|
|
121
|
+
});
|
|
122
|
+
const bodyPath = node("path", { d: rig.body });
|
|
123
|
+
const partsGroup = node("g");
|
|
124
|
+
const appendages = visible.map((a) => {
|
|
125
|
+
const path = node("path", { d: a.path });
|
|
126
|
+
const g = node("g", {}, [path]);
|
|
127
|
+
partsGroup.appendChild(g);
|
|
128
|
+
return {
|
|
129
|
+
role: a.role,
|
|
130
|
+
g,
|
|
131
|
+
path,
|
|
132
|
+
cmds: parsePath(a.path),
|
|
133
|
+
axis: axisOf(a),
|
|
134
|
+
pivot: a.pivot,
|
|
135
|
+
lastBend: 0,
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
const core = rig.core
|
|
139
|
+
? node("path", {
|
|
140
|
+
d: rig.core,
|
|
141
|
+
fill: `url(#${uid}-glow)`,
|
|
142
|
+
opacity: cur.glow ? 1 : 0,
|
|
143
|
+
})
|
|
144
|
+
: null;
|
|
145
|
+
const eyes = sides.map((l) => {
|
|
146
|
+
const open = node("rect", { "clip-path": `url(#${clipId(l)})` });
|
|
147
|
+
const arc = node("path", { d: HAPPY_EYE_PATH, opacity: 0 });
|
|
148
|
+
const g = node("g", { fill: eyeColor }, [open, arc]);
|
|
149
|
+
const clip = defs.querySelector(`#${clipId(l)} polygon`);
|
|
150
|
+
return { g, open, arc, clip };
|
|
151
|
+
});
|
|
152
|
+
const smokeEls = Array.from({ length: SMOKE_PUFFS }, () => node("circle", { r: 46, opacity: 0 }));
|
|
153
|
+
const smoke = node("g", { opacity: 0, fill: gray }, smokeEls);
|
|
154
|
+
const emberEls = EMBERS.map((b) => node("circle", { r: 16, fill: b.color, opacity: 0 }));
|
|
155
|
+
const embers = node("g", { opacity: 0 }, emberEls);
|
|
156
|
+
const scale = node("g", {}, [
|
|
157
|
+
bodyPath,
|
|
158
|
+
partsGroup,
|
|
159
|
+
...(core ? [core] : []),
|
|
160
|
+
eyes[0].g,
|
|
161
|
+
eyes[1].g,
|
|
162
|
+
smoke,
|
|
163
|
+
embers,
|
|
164
|
+
]);
|
|
165
|
+
// Transformation object, in the body's system (it squashes and turns with it).
|
|
166
|
+
// At the swap, the bubble covers the round body exactly.
|
|
167
|
+
const orbitEls = ORBIT.radii.map((r) => node("circle", { r }));
|
|
168
|
+
const orbit = node("g", { visibility: "hidden", fill: "currentColor", stroke: "none" }, orbitEls);
|
|
169
|
+
const check = node("path", {
|
|
170
|
+
d: CHECK.path,
|
|
171
|
+
"stroke-width": CHECK.width,
|
|
172
|
+
"stroke-linecap": "round",
|
|
173
|
+
"stroke-linejoin": "round",
|
|
174
|
+
"stroke-dasharray": `${CHECK.length} ${CHECK.length + 1}`,
|
|
175
|
+
visibility: "hidden",
|
|
176
|
+
});
|
|
177
|
+
const codeEls = CODE.strokes.map((d, i) => node("path", { d, "stroke-dasharray": `${CODE.lengths[i]} ${CODE.lengths[i] + 1}` }));
|
|
178
|
+
const code = node("g", { visibility: "hidden", "stroke-width": CODE.radius * 2, "stroke-linecap": "round" }, codeEls);
|
|
179
|
+
const sideDots = [0, 1].map(() => node("circle", { r: DOT_R, fill: "currentColor", stroke: "none" }));
|
|
180
|
+
const bubble = node("circle", {
|
|
181
|
+
r: DOT_R / 2,
|
|
182
|
+
"stroke-width": DOT_R,
|
|
183
|
+
"stroke-linecap": "round",
|
|
184
|
+
});
|
|
185
|
+
const object = node("g", { visibility: "hidden", stroke: "currentColor", fill: "none" }, [orbit, check, code, ...sideDots, bubble]);
|
|
186
|
+
const body = node("g", { fill: "currentColor" }, [scale, object]);
|
|
187
|
+
const center = node("g", {}, [body]);
|
|
188
|
+
// Accessories follow the jump; ink colour.
|
|
189
|
+
const lamp = node("g", { opacity: 0 }, [
|
|
190
|
+
node("circle", { cx: 0, cy: -20, r: 42, fill: raw.lampColor ?? DEFAULTS.lampColor }),
|
|
191
|
+
node("rect", { x: -20, y: 18, width: 40, height: 30, rx: 8 }),
|
|
192
|
+
node("path", {
|
|
193
|
+
d: "M0,-90 V-110 M-58,-66 L-72,-80 M58,-66 L72,-80",
|
|
194
|
+
stroke: ink,
|
|
195
|
+
"stroke-width": 10,
|
|
196
|
+
"stroke-linecap": "round",
|
|
197
|
+
}),
|
|
198
|
+
]);
|
|
199
|
+
const exclaim = node("g", { opacity: 0 }, [
|
|
200
|
+
node("rect", { x: -14, y: -110, width: 28, height: 84, rx: 14 }),
|
|
201
|
+
node("circle", { cx: 0, cy: 4, r: 16 }),
|
|
202
|
+
]);
|
|
203
|
+
const question = node("g", { opacity: 0 }, [
|
|
204
|
+
node("path", {
|
|
205
|
+
d: "M-40,-70 C-40,-110 40,-110 40,-70 C40,-40 6,-36 6,-6",
|
|
206
|
+
fill: "none",
|
|
207
|
+
stroke: ink,
|
|
208
|
+
"stroke-width": 26,
|
|
209
|
+
"stroke-linecap": "round",
|
|
210
|
+
}),
|
|
211
|
+
node("circle", { cx: 6, cy: 34, r: 15 }),
|
|
212
|
+
]);
|
|
213
|
+
const rays = node("g", { opacity: 0 }, [
|
|
214
|
+
node("path", {
|
|
215
|
+
d: "M-30,-40 L-10,-110 M20,-10 L90,-60 M30,40 L105,32",
|
|
216
|
+
stroke: raw.raysColor ?? DEFAULTS.raysColor,
|
|
217
|
+
"stroke-width": 26,
|
|
218
|
+
"stroke-linecap": "round",
|
|
219
|
+
fill: "none",
|
|
220
|
+
}),
|
|
221
|
+
]);
|
|
222
|
+
const ellipsisEls = [
|
|
223
|
+
node("circle", { cx: 0, cy: 0, r: 16 }),
|
|
224
|
+
node("circle", { cx: 48, cy: -22, r: 20 }),
|
|
225
|
+
node("circle", { cx: 104, cy: -52, r: 25 }),
|
|
226
|
+
];
|
|
227
|
+
const ellipsis = node("g", { opacity: 0, transform: "translate(840 340)" }, ellipsisEls);
|
|
228
|
+
const zzzEls = [0, 1, 2].map(() => node("g", { opacity: 0 }, [
|
|
229
|
+
node("path", {
|
|
230
|
+
transform: "translate(800 380)",
|
|
231
|
+
d: "M-22,-22 H22 L-22,22 H22",
|
|
232
|
+
fill: "none",
|
|
233
|
+
stroke: ink,
|
|
234
|
+
"stroke-width": 10,
|
|
235
|
+
"stroke-linecap": "round",
|
|
236
|
+
"stroke-linejoin": "round",
|
|
237
|
+
}),
|
|
238
|
+
]));
|
|
239
|
+
const zzz = node("g", { opacity: 0 }, zzzEls);
|
|
240
|
+
const inkGroup = node("g", { fill: ink }, [lamp, exclaim, question, rays, ellipsis, zzz]);
|
|
241
|
+
const root = node("g", {}, [center, inkGroup]);
|
|
242
|
+
const sparkEls = SPARKS.map((sp) => node("path", { d: SPARK_PATH, fill: sp.alt ? accent : "currentColor" }));
|
|
243
|
+
const sparks = node("g", { visibility: "hidden" }, sparkEls);
|
|
244
|
+
const el = node("svg", { viewBox: VIEWBOX_ATTR }, [defs, shadow, root, sparks]);
|
|
245
|
+
el.style.display = "block";
|
|
246
|
+
el.style.flexShrink = "0";
|
|
247
|
+
el.style.overflow = "visible";
|
|
248
|
+
el.style.userSelect = "none";
|
|
249
|
+
if (svg) {
|
|
250
|
+
io?.unobserve(svg);
|
|
251
|
+
svg.removeEventListener("pointerenter", onEnter);
|
|
252
|
+
svg.removeEventListener("pointerleave", onLeave);
|
|
253
|
+
svg.removeEventListener("pointerdown", onDown);
|
|
254
|
+
svg.replaceWith(el);
|
|
255
|
+
}
|
|
256
|
+
else
|
|
257
|
+
host.appendChild(el);
|
|
258
|
+
svg = el;
|
|
259
|
+
parts = {
|
|
260
|
+
shadow,
|
|
261
|
+
root,
|
|
262
|
+
center,
|
|
263
|
+
body,
|
|
264
|
+
scale,
|
|
265
|
+
bodyPath,
|
|
266
|
+
partsGroup,
|
|
267
|
+
appendages,
|
|
268
|
+
core,
|
|
269
|
+
eyes,
|
|
270
|
+
smoke,
|
|
271
|
+
smokeEls,
|
|
272
|
+
embers,
|
|
273
|
+
emberEls,
|
|
274
|
+
object,
|
|
275
|
+
orbit,
|
|
276
|
+
orbitEls,
|
|
277
|
+
check,
|
|
278
|
+
code,
|
|
279
|
+
codeEls,
|
|
280
|
+
sides: sideDots,
|
|
281
|
+
bubble,
|
|
282
|
+
lamp,
|
|
283
|
+
exclaim,
|
|
284
|
+
question,
|
|
285
|
+
rays,
|
|
286
|
+
ellipsis,
|
|
287
|
+
ellipsisEls,
|
|
288
|
+
zzz,
|
|
289
|
+
zzzEls,
|
|
290
|
+
sparks,
|
|
291
|
+
sparkEls,
|
|
292
|
+
};
|
|
293
|
+
svg.addEventListener("pointerenter", onEnter);
|
|
294
|
+
svg.addEventListener("pointerleave", onLeave);
|
|
295
|
+
svg.addEventListener("pointerdown", onDown);
|
|
296
|
+
lastColor = "";
|
|
297
|
+
lastRound = -1;
|
|
298
|
+
applyAttrs();
|
|
299
|
+
applyEyeShape();
|
|
300
|
+
io?.observe(svg);
|
|
301
|
+
// A complete first pose, synchronously: colours, eyes and appendages are
|
|
302
|
+
// never left unposed, even off-screen, paused or in a hidden tab.
|
|
303
|
+
if (lastPose)
|
|
304
|
+
render(lastPose);
|
|
305
|
+
else
|
|
306
|
+
paintNow();
|
|
307
|
+
}
|
|
308
|
+
/** Attributes that can change without rebuilding. */
|
|
309
|
+
function applyAttrs() {
|
|
310
|
+
const size = cur.size;
|
|
311
|
+
if (size !== undefined) {
|
|
312
|
+
svg.setAttribute("width", String(size));
|
|
313
|
+
svg.setAttribute("height", String(Math.round(size * ASPECT)));
|
|
314
|
+
svg.style.width = "";
|
|
315
|
+
svg.style.height = "";
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
svg.removeAttribute("width");
|
|
319
|
+
svg.removeAttribute("height");
|
|
320
|
+
svg.style.width = "100%";
|
|
321
|
+
svg.style.height = "auto";
|
|
322
|
+
}
|
|
323
|
+
if (raw.label) {
|
|
324
|
+
svg.setAttribute("role", "img");
|
|
325
|
+
svg.setAttribute("aria-label", raw.label);
|
|
326
|
+
svg.removeAttribute("aria-hidden");
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
svg.setAttribute("aria-hidden", "true");
|
|
330
|
+
svg.removeAttribute("role");
|
|
331
|
+
svg.removeAttribute("aria-label");
|
|
332
|
+
}
|
|
333
|
+
if (raw.className !== undefined)
|
|
334
|
+
svg.setAttribute("class", raw.className);
|
|
335
|
+
else
|
|
336
|
+
svg.removeAttribute("class");
|
|
337
|
+
svg.style.cursor = cur.interactive ? "pointer" : "";
|
|
338
|
+
svg.style.touchAction = cur.interactive ? "manipulation" : "";
|
|
339
|
+
if (parts.core)
|
|
340
|
+
parts.core.setAttribute("opacity", cur.glow ? "1" : "0");
|
|
341
|
+
}
|
|
342
|
+
/** The eye's resting shape: written once, not every frame. */
|
|
343
|
+
function applyEyeShape() {
|
|
344
|
+
const shape = cur.rig.eyeStyles[cur.eyeStyle];
|
|
345
|
+
for (const { open } of parts.eyes) {
|
|
346
|
+
open.setAttribute("x", String(-shape.w / 2));
|
|
347
|
+
open.setAttribute("y", String(-shape.h / 2));
|
|
348
|
+
open.setAttribute("width", String(shape.w));
|
|
349
|
+
open.setAttribute("height", String(shape.h));
|
|
350
|
+
open.setAttribute("rx", String(shape.w / 2));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/* ---------- writing the pose ---------- */
|
|
354
|
+
/** Writes one eye's pose: position, scale, eyelids (clip) and happy arc. */
|
|
355
|
+
function applyEye(eye, base, shape, o) {
|
|
356
|
+
eye.g.setAttribute("transform", `translate(${n(base.x + o.x)} ${n(base.y + o.y)}) rotate(${n(shape.tilt + o.rot)}) scale(${n(o.sx)} ${n(o.sy)})`);
|
|
357
|
+
const { w, h } = shape;
|
|
358
|
+
// Eyelids: a line on top and another below; the clip is what is left between them.
|
|
359
|
+
const k = Math.tan((o.lidAngle * Math.PI) / 180) * Math.min(1, o.lidTop * 4);
|
|
360
|
+
const yTop = -h / 2 - 2 + o.lidTop * (h + 2);
|
|
361
|
+
const yBot = Math.max(yTop + 1, h / 2 + 2 - o.lidBottom * (h + 2));
|
|
362
|
+
eye.clip.setAttribute("points", `${-w},${n(yTop - k * w)} ${w},${n(yTop + k * w)} ${w},${n(yBot)} ${-w},${n(yBot)}`);
|
|
363
|
+
eye.open.setAttribute("opacity", n(1 - Math.min(1, o.happy * 1.6)));
|
|
364
|
+
eye.arc.setAttribute("opacity", n(Math.max(0, o.happy * 1.6 - 0.6)));
|
|
365
|
+
// The arc does not follow the eye's `sy` (otherwise the blink would erase it).
|
|
366
|
+
eye.arc.setAttribute("transform", `scale(${n(w / 80)} ${n(((0.6 + 0.4 * o.happy) * (w / 80)) / Math.max(o.sy, 0.2))})`);
|
|
367
|
+
}
|
|
368
|
+
function render(pose) {
|
|
369
|
+
const p = parts;
|
|
370
|
+
const rig = cur.rig;
|
|
371
|
+
const { ground, center, accessory, anchor } = rig;
|
|
372
|
+
p.root.setAttribute("transform", `translate(0 ${n(pose.rootY)})`);
|
|
373
|
+
p.root.setAttribute("opacity", n(pose.opacity));
|
|
374
|
+
p.center.setAttribute("transform", around(center, `rotate(${n(pose.centerRotate)})`));
|
|
375
|
+
p.body.setAttribute("transform", around(ground, `rotate(${n(pose.bodyRotate)}) scale(${n(pose.bodySx)} ${n(pose.bodySy)})`));
|
|
376
|
+
// Scale is around the ground; during the transformation, around the anchor.
|
|
377
|
+
const pivot = {
|
|
378
|
+
x: ground.x + (anchor.x - ground.x) * pose.morph,
|
|
379
|
+
y: ground.y + (anchor.y - ground.y) * pose.morph,
|
|
380
|
+
};
|
|
381
|
+
p.scale.setAttribute("transform", around(pivot, `scale(${n(pose.scale)})`));
|
|
382
|
+
const asObject = pose.swap >= 0.5;
|
|
383
|
+
p.scale.setAttribute("visibility", asObject ? "hidden" : "visible");
|
|
384
|
+
p.object.setAttribute("visibility", asObject ? "visible" : "hidden");
|
|
385
|
+
const round = Math.min(1, Math.max(0, pose.round));
|
|
386
|
+
if (Math.abs(round - lastRound) > 0.0005 || (round === 0) !== (lastRound === 0)) {
|
|
387
|
+
p.bodyPath.setAttribute("d", round === 0 ? rig.body : mixPath(roundGeo.body, roundGeo.circle, round));
|
|
388
|
+
lastRound = round;
|
|
389
|
+
}
|
|
390
|
+
p.partsGroup.setAttribute("transform", `translate(${n(pose.partsX)} 0)`);
|
|
391
|
+
for (const a of p.appendages) {
|
|
392
|
+
const angle = a.role === "C" ? pose.partC : a.role === "L" ? pose.partL : pose.partR;
|
|
393
|
+
const bend = a.role === "C" ? pose.bendC : a.role === "L" ? pose.bendL : pose.bendR;
|
|
394
|
+
a.g.setAttribute("transform", around(a.pivot, `rotate(${n(angle)}) scale(${n(Math.max(0, 1 - pose.retract))})`));
|
|
395
|
+
if (Math.abs(bend - a.lastBend) >= 0.08) {
|
|
396
|
+
a.lastBend = bend;
|
|
397
|
+
a.path.setAttribute("d", bendPath(a.cmds, a.axis, bend));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const shape = rig.eyeStyles[cur.eyeStyle];
|
|
401
|
+
applyEye(p.eyes[0], { x: rig.eyes.left.x + shape.dx, y: rig.eyes.left.y + shape.dy }, shape, pose.leftEye);
|
|
402
|
+
applyEye(p.eyes[1], { x: rig.eyes.right.x - shape.dx, y: rig.eyes.right.y + shape.dy }, shape, pose.rightEye);
|
|
403
|
+
// Loses its colour (put out, at risk): mixes with gray.
|
|
404
|
+
const nowColor = pose.gray > 0.005
|
|
405
|
+
? `color-mix(in oklab, ${cur.color}, ${cur.grayColor} ${Math.round(pose.gray * 100)}%)`
|
|
406
|
+
: cur.color;
|
|
407
|
+
if (nowColor !== lastColor) {
|
|
408
|
+
svg.style.color = nowColor;
|
|
409
|
+
lastColor = nowColor;
|
|
410
|
+
}
|
|
411
|
+
p.core?.setAttribute("opacity", n(Math.max(cur.glow ? 1 : 0, pose.core) * (1 - pose.gray)));
|
|
412
|
+
// Embers and smoke come off the tip of the middle tongue, in a loop.
|
|
413
|
+
const top = rig.appendages.find((a) => a.role === "C")?.tip ?? accessory;
|
|
414
|
+
const tb = pose.time;
|
|
415
|
+
p.embers.setAttribute("opacity", n(pose.embers));
|
|
416
|
+
if (pose.embers > 0)
|
|
417
|
+
p.emberEls.forEach((el, i) => {
|
|
418
|
+
const u = (tb * 0.8 + i / EMBERS.length) % 1;
|
|
419
|
+
const x = top.x + EMBERS[i].dx * u + Math.sin(tb * 3 + i) * 16;
|
|
420
|
+
const y = top.y + 220 - u * 420;
|
|
421
|
+
el.setAttribute("transform", `translate(${n(x)} ${n(y)}) scale(${n(1 - 0.7 * u)})`);
|
|
422
|
+
el.setAttribute("opacity", n(Math.sin(u * Math.PI)));
|
|
423
|
+
});
|
|
424
|
+
p.smoke.setAttribute("opacity", n(pose.smoke));
|
|
425
|
+
if (pose.smoke > 0)
|
|
426
|
+
p.smokeEls.forEach((el, i) => {
|
|
427
|
+
const u = (tb * 0.45 + i / SMOKE_PUFFS) % 1;
|
|
428
|
+
const x = top.x + Math.sin(u * 5 + i) * 40 + u * 60;
|
|
429
|
+
const y = top.y + 60 - u * 320;
|
|
430
|
+
el.setAttribute("transform", `translate(${n(x)} ${n(y)}) scale(${n(0.5 + u)})`);
|
|
431
|
+
el.setAttribute("opacity", n(0.5 * Math.sin(u * Math.PI)));
|
|
432
|
+
});
|
|
433
|
+
p.shadow.setAttribute("transform", around(rig.shadow, `scale(${n(pose.shadowScale)})`));
|
|
434
|
+
p.shadow.setAttribute("opacity", n(pose.shadowOpacity));
|
|
435
|
+
// Sparks: leave the center, spin and fade.
|
|
436
|
+
const f = pose.sparks;
|
|
437
|
+
p.sparks.setAttribute("visibility", f > 0 ? "visible" : "hidden");
|
|
438
|
+
if (f > 0) {
|
|
439
|
+
const e = 1 - (1 - f) ** 3;
|
|
440
|
+
const op = f < 0.25 ? f / 0.25 : 1 - (f - 0.25) / 0.75;
|
|
441
|
+
p.sparkEls.forEach((el, i) => {
|
|
442
|
+
const { dx, dy } = SPARKS[i];
|
|
443
|
+
el.setAttribute("transform", `translate(${n(center.x + dx * e)} ${n(center.y + dy * e)}) rotate(${n(f * 90)}) scale(${n(1 - 0.4 * f)})`);
|
|
444
|
+
el.setAttribute("opacity", n(op));
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
const pop = (g, v, extra = "") => {
|
|
448
|
+
g.setAttribute("opacity", n(v));
|
|
449
|
+
g.setAttribute("transform", `translate(${accessory.x} ${accessory.y}) scale(${n(0.6 + 0.4 * v)}) ${extra}`);
|
|
450
|
+
};
|
|
451
|
+
const t = pose.time;
|
|
452
|
+
pop(p.lamp, pose.lamp, `rotate(${n(Math.sin(t * 3) * 6)})`);
|
|
453
|
+
pop(p.exclaim, pose.exclaim, `rotate(${n(Math.sin(t * 30) * 4 * pose.exclaim)})`);
|
|
454
|
+
pop(p.question, pose.question, `rotate(${n(Math.sin(t * 2) * 10)})`);
|
|
455
|
+
const rays = cur.fixedRays ? Math.max(pose.rays, 1) : pose.rays;
|
|
456
|
+
pop(p.rays, rays, `scale(${n(1 + Math.sin(t * 5) * 0.06)})`);
|
|
457
|
+
p.ellipsis.setAttribute("opacity", n(pose.ellipsis));
|
|
458
|
+
p.ellipsisEls.forEach((el, i) => {
|
|
459
|
+
const u = (t * 0.9 - i * 0.18) % 1;
|
|
460
|
+
const v = u < 0 ? 0 : Math.sin(Math.min(1, u / 0.6) * Math.PI);
|
|
461
|
+
el.setAttribute("transform", `translate(0 ${n(-v * 16)})`);
|
|
462
|
+
el.setAttribute("opacity", n(0.35 + 0.65 * v));
|
|
463
|
+
});
|
|
464
|
+
p.zzz.setAttribute("opacity", n(pose.zzz));
|
|
465
|
+
p.zzzEls.forEach((el, i) => {
|
|
466
|
+
const u = (t * 0.35 + i / 3) % 1;
|
|
467
|
+
el.setAttribute("transform", `translate(${n(u * 70 + Math.sin(u * 6 + i) * 10)} ${n(-u * 150)}) scale(${n(0.6 + u * 0.6)})`);
|
|
468
|
+
el.setAttribute("opacity", n(Math.sin(u * Math.PI)));
|
|
469
|
+
});
|
|
470
|
+
/* ---------- object (transformation) ---------- */
|
|
471
|
+
if (asObject) {
|
|
472
|
+
const R = Math.max(0, pose.bubbleR);
|
|
473
|
+
const hole = Math.min(R, Math.max(0, pose.bubbleHole));
|
|
474
|
+
p.object.setAttribute("transform", `translate(${anchor.x} ${anchor.y}) scale(${n(1 + pose.objScale)})`);
|
|
475
|
+
// Bouncing dots: the middle one is the bubble itself.
|
|
476
|
+
const bounce = (i) => -Math.max(0, Math.sin(t * 9 - i * 0.9)) * 26 * Math.max(0, pose.dotsBounce);
|
|
477
|
+
// Bubble: a stroked circle. With no hole, the stroke covers center to edge (a disc).
|
|
478
|
+
const r = (R + hole) / 2;
|
|
479
|
+
const C = 2 * Math.PI * r;
|
|
480
|
+
const b = p.bubble;
|
|
481
|
+
b.setAttribute("r", n(r));
|
|
482
|
+
b.setAttribute("stroke-width", n(R - hole));
|
|
483
|
+
b.setAttribute("stroke-dasharray", `${n(C * (1 - Math.max(0, pose.bubbleArc)))} ${n(C + 1)}`);
|
|
484
|
+
b.setAttribute("transform", `translate(${n(pose.bubbleX)} ${n(pose.bubbleY + bounce(1))}) rotate(${n(pose.objRotate - 90)})`);
|
|
485
|
+
p.sides.forEach((el, i) => {
|
|
486
|
+
const side = i === 0 ? -1 : 1;
|
|
487
|
+
el.setAttribute("r", n(R));
|
|
488
|
+
el.setAttribute("transform", `translate(${n(pose.bubbleX + side * pose.dotsDist)} ${n(pose.bubbleY + bounce(i === 0 ? 0 : 2))})`);
|
|
489
|
+
});
|
|
490
|
+
const ck = pose.checkDraw;
|
|
491
|
+
p.check.setAttribute("visibility", ck > 0 ? "visible" : "hidden");
|
|
492
|
+
p.check.setAttribute("stroke-dashoffset", n(CHECK.length * (1 - ck)));
|
|
493
|
+
const cd = pose.codeDraw;
|
|
494
|
+
p.code.setAttribute("visibility", cd > 0 ? "visible" : "hidden");
|
|
495
|
+
p.codeEls.forEach((el, i) => el.setAttribute("stroke-dashoffset", n(CODE.lengths[i] * (1 - cd))));
|
|
496
|
+
const open = Math.max(0, pose.orbitOpen);
|
|
497
|
+
p.orbit.setAttribute("visibility", open > 0 ? "visible" : "hidden");
|
|
498
|
+
if (open > 0)
|
|
499
|
+
p.orbitEls.forEach((el, i) => {
|
|
500
|
+
const ang = (i / ORBIT.radii.length) * Math.PI * 2 + (pose.objRotate * Math.PI) / 180;
|
|
501
|
+
const dist = (ORBIT.R + Math.sin(t * 6 + i) * 24) * open;
|
|
502
|
+
el.setAttribute("r", n(R + (ORBIT.radii[i] - R) * Math.min(1, open)));
|
|
503
|
+
el.setAttribute("transform", `translate(${n(pose.bubbleX + Math.cos(ang) * dist)} ${n(pose.bubbleY + Math.sin(ang) * dist)})`);
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/* ---------- frame ---------- */
|
|
508
|
+
const media = typeof window !== "undefined" && window.matchMedia
|
|
509
|
+
? window.matchMedia("(prefers-reduced-motion: reduce)")
|
|
510
|
+
: null;
|
|
511
|
+
let systemReduced = media?.matches ?? false;
|
|
512
|
+
const onMedia = () => (systemReduced = media?.matches ?? false);
|
|
513
|
+
media?.addEventListener("change", onMedia);
|
|
514
|
+
const focus = newFocusState();
|
|
515
|
+
let stopPage = null;
|
|
516
|
+
/** The page observer only runs while a `free`/`focus` gaze needs it. */
|
|
517
|
+
function syncPageObserver() {
|
|
518
|
+
const need = cur.gaze === "free" || cur.gaze === "focus";
|
|
519
|
+
if (need && !stopPage)
|
|
520
|
+
stopPage = observePage();
|
|
521
|
+
else if (!need && stopPage) {
|
|
522
|
+
stopPage();
|
|
523
|
+
stopPage = null;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function frame(now) {
|
|
527
|
+
const r = svg.getBoundingClientRect();
|
|
528
|
+
const cx = r.left + r.width / 2;
|
|
529
|
+
const cy = r.top + r.height * 0.6;
|
|
530
|
+
const recent = pointer.inside && now - pointer.last < 2500;
|
|
531
|
+
// Cursor near the character: draws attention (and, right on top, crosses the eyes).
|
|
532
|
+
const proximity = recent
|
|
533
|
+
? Math.min(1, Math.max(0, 1 - Math.hypot(pointer.x - cx, pointer.y - cy) / (r.width * 1.8)))
|
|
534
|
+
: 0;
|
|
535
|
+
let target = null;
|
|
536
|
+
let point = null;
|
|
537
|
+
let interest = 0;
|
|
538
|
+
let glide;
|
|
539
|
+
const gs = gazeState;
|
|
540
|
+
gs.mode = gs.reason = null;
|
|
541
|
+
const g = cur.gaze;
|
|
542
|
+
if (g === "pointer") {
|
|
543
|
+
// Recent pointer, or outside the window: looks at the edge it left through.
|
|
544
|
+
if (!pointer.inside || recent)
|
|
545
|
+
point = pointer;
|
|
546
|
+
}
|
|
547
|
+
else if (g === "free" || g === "focus") {
|
|
548
|
+
const l = decideGaze(focus, now, Math.random, r, pointer, g);
|
|
549
|
+
interest = l.interest;
|
|
550
|
+
glide = l.glide || undefined;
|
|
551
|
+
gs.mode = l.mode;
|
|
552
|
+
gs.reason = l.reason;
|
|
553
|
+
if (l.point === "screen")
|
|
554
|
+
target = { x: 0, y: 0.05 };
|
|
555
|
+
else
|
|
556
|
+
point = l.point;
|
|
557
|
+
}
|
|
558
|
+
else if (typeof g === "function")
|
|
559
|
+
point = g();
|
|
560
|
+
else if (typeof g === "object")
|
|
561
|
+
point = g;
|
|
562
|
+
if (point) {
|
|
563
|
+
target = {
|
|
564
|
+
x: (point.x - cx) / (window.innerWidth * 0.35),
|
|
565
|
+
y: (point.y - cy) / (window.innerHeight * 0.35),
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
gs.gliding = !!glide;
|
|
569
|
+
// An unmeasured host (off-screen, hidden, not laid out yet) reads as width 0: assume full detail.
|
|
570
|
+
const width = cur.size ?? r.width;
|
|
571
|
+
const detail = width > 0 && width < 48 ? "simple" : "full";
|
|
572
|
+
const reduced = cur.reducedMotion === "auto" ? systemReduced : cur.reducedMotion;
|
|
573
|
+
const pose = brain.step(now, {
|
|
574
|
+
gaze: target,
|
|
575
|
+
hover: hovering,
|
|
576
|
+
reduced,
|
|
577
|
+
mood: cur.mood,
|
|
578
|
+
activity: cur.activity,
|
|
579
|
+
detail,
|
|
580
|
+
bend: cur.bend,
|
|
581
|
+
interest,
|
|
582
|
+
glide,
|
|
583
|
+
proximity: g === "none" ? 0 : proximity,
|
|
584
|
+
});
|
|
585
|
+
gs.attention = pose.attention;
|
|
586
|
+
gs.boredom = pose.boredom;
|
|
587
|
+
lastPose = pose;
|
|
588
|
+
render(pose);
|
|
589
|
+
}
|
|
590
|
+
/** Steps the brain once and paints, outside the ticker (the first frame). */
|
|
591
|
+
function paintNow() {
|
|
592
|
+
frame(performance.now());
|
|
593
|
+
}
|
|
594
|
+
/* ---------- running only when on screen and the tab is visible ---------- */
|
|
595
|
+
let onScreen = typeof IntersectionObserver === "undefined";
|
|
596
|
+
let tabVisible = typeof document === "undefined" || document.visibilityState !== "hidden";
|
|
597
|
+
let unsubscribe = null;
|
|
598
|
+
function syncRunning() {
|
|
599
|
+
const should = !destroyed && onScreen && tabVisible;
|
|
600
|
+
if (should && !unsubscribe) {
|
|
601
|
+
// Frames were not running: do not integrate across the gap.
|
|
602
|
+
brain.resume();
|
|
603
|
+
unsubscribe = subscribe(frame);
|
|
604
|
+
}
|
|
605
|
+
else if (!should && unsubscribe) {
|
|
606
|
+
unsubscribe();
|
|
607
|
+
unsubscribe = null;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const io = typeof IntersectionObserver === "undefined"
|
|
611
|
+
? null
|
|
612
|
+
: new IntersectionObserver((entries) => {
|
|
613
|
+
onScreen = entries[entries.length - 1].isIntersecting;
|
|
614
|
+
syncRunning();
|
|
615
|
+
});
|
|
616
|
+
const onVisibility = () => {
|
|
617
|
+
tabVisible = document.visibilityState !== "hidden";
|
|
618
|
+
syncRunning();
|
|
619
|
+
};
|
|
620
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
621
|
+
/* ---------- interaction ---------- */
|
|
622
|
+
function onEnter(e) {
|
|
623
|
+
if (!cur.interactive || e.pointerType !== "mouse")
|
|
624
|
+
return;
|
|
625
|
+
hovering = true;
|
|
626
|
+
const gesture = brain.hoverEnter(performance.now());
|
|
627
|
+
if (gesture)
|
|
628
|
+
raw.onEvent?.({ type: "hover", name: gesture });
|
|
629
|
+
}
|
|
630
|
+
function onLeave() {
|
|
631
|
+
hovering = false;
|
|
632
|
+
}
|
|
633
|
+
function onDown(e) {
|
|
634
|
+
if (!cur.interactive)
|
|
635
|
+
return;
|
|
636
|
+
raw.onEvent?.({ type: "click", name: brain.tap(performance.now()) });
|
|
637
|
+
if (e.pointerType !== "mouse") {
|
|
638
|
+
hovering = true;
|
|
639
|
+
if (touchTimer)
|
|
640
|
+
clearTimeout(touchTimer);
|
|
641
|
+
touchTimer = setTimeout(() => (hovering = false), 900);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
/* ---------- start ---------- */
|
|
645
|
+
build();
|
|
646
|
+
syncPageObserver();
|
|
647
|
+
syncRunning();
|
|
648
|
+
return {
|
|
649
|
+
brain,
|
|
650
|
+
get svg() {
|
|
651
|
+
return svg;
|
|
652
|
+
},
|
|
653
|
+
update(patch) {
|
|
654
|
+
if (destroyed)
|
|
655
|
+
return;
|
|
656
|
+
const before = cur;
|
|
657
|
+
const beforeRaw = raw;
|
|
658
|
+
raw = { ...raw, ...patch };
|
|
659
|
+
cur = resolve(raw);
|
|
660
|
+
if (cur.rig !== before.rig)
|
|
661
|
+
brain.setConfig(cur.rig);
|
|
662
|
+
if (raw.library !== beforeRaw.library)
|
|
663
|
+
brain.setLibrary(raw.library);
|
|
664
|
+
if (raw.interactions !== beforeRaw.interactions)
|
|
665
|
+
brain.setInteractions(raw.interactions);
|
|
666
|
+
const rebuild = cur.rig !== before.rig ||
|
|
667
|
+
raw.appendages !== beforeRaw.appendages ||
|
|
668
|
+
raw.eyeColor !== beforeRaw.eyeColor ||
|
|
669
|
+
raw.inkColor !== beforeRaw.inkColor ||
|
|
670
|
+
raw.accentColor !== beforeRaw.accentColor ||
|
|
671
|
+
raw.lampColor !== beforeRaw.lampColor ||
|
|
672
|
+
raw.raysColor !== beforeRaw.raysColor ||
|
|
673
|
+
cur.grayColor !== before.grayColor;
|
|
674
|
+
if (rebuild)
|
|
675
|
+
build();
|
|
676
|
+
else {
|
|
677
|
+
applyAttrs();
|
|
678
|
+
if (cur.eyeStyle !== before.eyeStyle)
|
|
679
|
+
applyEyeShape();
|
|
680
|
+
if (cur.color !== before.color)
|
|
681
|
+
lastColor = "";
|
|
682
|
+
}
|
|
683
|
+
syncPageObserver();
|
|
684
|
+
},
|
|
685
|
+
react: (name) => brain.react(name, performance.now()),
|
|
686
|
+
jump: () => brain.jump(),
|
|
687
|
+
currentReaction: () => brain.currentReaction,
|
|
688
|
+
getGazeState: () => ({ ...gazeState }),
|
|
689
|
+
destroy() {
|
|
690
|
+
if (destroyed)
|
|
691
|
+
return;
|
|
692
|
+
destroyed = true;
|
|
693
|
+
syncRunning();
|
|
694
|
+
io?.disconnect();
|
|
695
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
696
|
+
media?.removeEventListener("change", onMedia);
|
|
697
|
+
stopPage?.();
|
|
698
|
+
stopPage = null;
|
|
699
|
+
if (touchTimer)
|
|
700
|
+
clearTimeout(touchTimer);
|
|
701
|
+
svg.removeEventListener("pointerenter", onEnter);
|
|
702
|
+
svg.removeEventListener("pointerleave", onLeave);
|
|
703
|
+
svg.removeEventListener("pointerdown", onDown);
|
|
704
|
+
svg.remove();
|
|
705
|
+
},
|
|
706
|
+
};
|
|
707
|
+
}
|