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.
Files changed (59) hide show
  1. package/README.md +167 -47
  2. package/dist/animation.d.ts +90 -0
  3. package/dist/animation.js +396 -0
  4. package/dist/brain.d.ts +194 -35
  5. package/dist/brain.js +578 -54
  6. package/dist/character.d.ts +123 -0
  7. package/dist/character.js +916 -0
  8. package/dist/characters/berry.d.ts +2 -0
  9. package/dist/characters/berry.js +38 -0
  10. package/dist/characters/flame.d.ts +2 -0
  11. package/dist/characters/flame.js +46 -0
  12. package/dist/delta.d.ts +16 -0
  13. package/dist/delta.js +146 -0
  14. package/dist/examples.d.ts +4 -0
  15. package/dist/examples.js +208 -0
  16. package/dist/focus.d.ts +46 -0
  17. package/dist/focus.js +284 -0
  18. package/dist/gaze.d.ts +41 -53
  19. package/dist/gaze.js +125 -82
  20. package/dist/index.d.ts +18 -11
  21. package/dist/index.js +18 -11
  22. package/dist/interactions.d.ts +30 -0
  23. package/dist/interactions.js +100 -0
  24. package/dist/library.d.ts +18 -0
  25. package/dist/library.js +14 -0
  26. package/dist/moods.d.ts +78 -46
  27. package/dist/moods.js +48 -73
  28. package/dist/motion.d.ts +93 -0
  29. package/dist/motion.js +167 -0
  30. package/dist/mount.d.ts +90 -0
  31. package/dist/mount.js +707 -0
  32. package/dist/parts.d.ts +36 -0
  33. package/dist/parts.js +131 -0
  34. package/dist/reactions.d.ts +184 -44
  35. package/dist/reactions.js +1851 -116
  36. package/dist/rig.d.ts +119 -0
  37. package/dist/rig.js +59 -0
  38. package/dist/scroll-traveler.js +1 -1
  39. package/dist/ticker.d.ts +10 -0
  40. package/dist/ticker.js +42 -0
  41. package/dist/transformations.d.ts +21 -0
  42. package/dist/transformations.js +306 -0
  43. package/package.json +7 -11
  44. package/dist/bind.d.ts +0 -27
  45. package/dist/bind.js +0 -42
  46. package/dist/blink.d.ts +0 -25
  47. package/dist/blink.js +0 -48
  48. package/dist/fake3d.d.ts +0 -20
  49. package/dist/fake3d.js +0 -24
  50. package/dist/noise.d.ts +0 -16
  51. package/dist/noise.js +0 -17
  52. package/dist/render.d.ts +0 -29
  53. package/dist/render.js +0 -97
  54. package/dist/rng.d.ts +0 -10
  55. package/dist/rng.js +0 -18
  56. package/dist/spring.d.ts +0 -21
  57. package/dist/spring.js +0 -33
  58. package/dist/types.d.ts +0 -63
  59. package/dist/types.js +0 -46
package/dist/brain.js CHANGED
@@ -1,57 +1,581 @@
1
- import { BlinkScheduler } from "./blink.js";
2
- import { fakeTurn } from "./fake3d.js";
3
- import { GazeEngine } from "./gaze.js";
4
- import { MoodBlender } from "./moods.js";
5
- import { breathing } from "./noise.js";
6
- import { ReactionPlayer } from "./reactions.js";
7
- import { addPose, neutralPose, resolveDelta } from "./types.js";
8
- /**
9
- * The pure, DOM-free core: combines mood blending, gaze (with the fake-3D
10
- * turn), blinking and one-shot reactions into a single `Pose` every frame.
11
- * No rendering, no `requestAnimationFrame` — a DOM binder (see
12
- * `examples/states-demo.html` or write your own) reads `update()`'s output
13
- * and calls `MountedRig.applyPose`.
14
- */
1
+ import { MOTION_FIELDS, addDelta, zeroDelta } from "./delta.js";
2
+ import { Gaze } from "./gaze.js";
3
+ import { CLICK_CHAIN_INTERVAL_MS, HOVER_CUDDLE_MS, HOVER_NAP_MS, INTERACTIONS, pickGesture, } from "./interactions.js";
4
+ import { DEFAULT_LIBRARY } from "./library.js";
5
+ import { atRest, clamp, launch, newBlink, newDamped, newHold, smooth, smoothDamp, spring, stepBlink, stepHold, stepJump, } from "./motion.js";
6
+ import { stepTip } from "./parts.js";
7
+ /** Transformation fields, passed straight through to the renderer. */
8
+ const OBJECT_FIELDS = [
9
+ "morph",
10
+ "round",
11
+ "retract",
12
+ "swap",
13
+ "bubbleR",
14
+ "bubbleHole",
15
+ "bubbleArc",
16
+ "bubbleX",
17
+ "bubbleY",
18
+ "dotsDist",
19
+ "dotsBounce",
20
+ "checkDraw",
21
+ "codeDraw",
22
+ "orbitOpen",
23
+ "objScale",
24
+ "objRotate",
25
+ ];
26
+ export const DEFAULT_BRAIN_CONFIG = {
27
+ id: "berry",
28
+ baseHeight: 535,
29
+ idleAppendage: { amp: 1, freq: 1 },
30
+ flicker: 0,
31
+ };
32
+ const REACTION_FADE_MS = 350;
33
+ /** After a hover gesture, waits this long before another. */
34
+ const HOVER_PAUSE_MS = 1200;
35
+ /** A gesture in the queue is valid for this long; after that, it lost its charm. */
36
+ const PENDING_VALIDITY_MS = 1600;
37
+ const ROLES = ["C", "L", "R"];
38
+ const SIDE = { C: 0, L: -1, R: 1 };
39
+ const interactionsFor = (c) => INTERACTIONS[c.id] ?? INTERACTIONS.berry;
40
+ const rolesOf = (c) => c.appendages ? ROLES.filter((r) => c.appendages.some((a) => a.role === r)) : ROLES;
41
+ const own = (rec, name) => Object.hasOwn(rec, name) ? rec[name] : undefined;
15
42
  export class Brain {
16
- gazeEngine;
17
- moodBlender;
18
- reactionPlayer;
19
- blinkScheduler;
20
- clock = 0;
21
- opts;
22
- constructor(options = {}) {
23
- this.opts = options;
24
- this.gazeEngine = new GazeEngine(options.gaze);
25
- this.moodBlender = new MoodBlender(options.moods);
26
- this.reactionPlayer = new ReactionPlayer(options.reactions);
27
- this.blinkScheduler = new BlinkScheduler(options.blink);
28
- }
29
- setMood(weights) {
30
- this.moodBlender.set(weights);
31
- }
32
- react(name) {
33
- return this.reactionPlayer.play(name);
34
- }
35
- update(dt, gazeTarget) {
36
- this.clock += dt;
37
- const t = this.clock;
38
- let pose = neutralPose();
39
- const moodDelta = this.moodBlender.update(dt);
40
- pose = addPose(pose, resolveDelta(moodDelta));
41
- const gaze = this.gazeEngine.update(dt, gazeTarget, t);
42
- const turnDelta = fakeTurn(gaze.offset.x, this.opts.fakeTurn);
43
- pose = addPose(pose, resolveDelta(turnDelta));
44
- pose = addPose(pose, resolveDelta({ eyes: { offsetY: gaze.offset.y * (this.opts.gazeRangeY ?? 2.5) } }));
45
- const attentionScale = 1 + gaze.attention * 0.15;
46
- const boredomScale = 1 - gaze.boredom * 0.3;
47
- pose = addPose(pose, resolveDelta({ eyes: { scaleX: attentionScale * boredomScale, scaleY: attentionScale * boredomScale, shut: gaze.boredom * 0.25 } }));
48
- const blinkShut = this.blinkScheduler.update(t);
49
- pose = addPose(pose, resolveDelta({ eyes: { shut: blinkShut } }));
50
- const breathe = breathing(t, this.opts.breathing);
51
- pose = addPose(pose, resolveDelta({ body: { scaleY: breathe, scaleX: 1 + (1 - breathe) * 0.5 } }));
52
- const reactionDelta = this.reactionPlayer.update(dt);
53
- if (reactionDelta)
54
- pose = addPose(pose, resolveDelta(reactionDelta));
55
- return pose;
43
+ rng;
44
+ cfg;
45
+ library;
46
+ interactions;
47
+ /** True when the caller passed their own interactions (they then ignore `config.id`). */
48
+ customInteractions;
49
+ /** Roles simulated (those the rig has). */
50
+ roles;
51
+ last = null;
52
+ blink;
53
+ gaze = new Gaze();
54
+ wander = { x: 0, y: 0, next: 0 };
55
+ parts = {
56
+ C: { a: 0, v: 0 },
57
+ L: { a: 0, v: 0 },
58
+ R: { a: 0, v: 0 },
59
+ };
60
+ /** Tip link of each appendage (a two-link chain). */
61
+ tips = {
62
+ C: { a: 0, v: 0 },
63
+ L: { a: 0, v: 0 },
64
+ R: { a: 0, v: 0 },
65
+ };
66
+ targets = { C: 0, L: 0, R: 0 };
67
+ /** Velocities of the previous frame, to measure acceleration. */
68
+ previous = { baseY: 0, vy: 0, rot: 0, vPart: { C: 0, L: 0, R: 0 } };
69
+ jumpState = { y: 0, vy: 0 };
70
+ squash = { s: 0, v: 0 };
71
+ hover = 0;
72
+ moodSprings = {};
73
+ moodWeights = {};
74
+ activitySprings = {};
75
+ activityWeights = {};
76
+ reaction = null;
77
+ leaving = null;
78
+ /** Requested during a transformation: starts when it ends. */
79
+ queue = null;
80
+ /** Last value of `morph`: during the transformation, it does not jump. */
81
+ morph = 0;
82
+ chain = 0;
83
+ lastTap = -Infinity;
84
+ lastGesture = null;
85
+ lastHover = -Infinity;
86
+ hoverSince = null;
87
+ pending = null;
88
+ cuddle = newDamped();
89
+ nap = newDamped();
90
+ /*
91
+ * Expression without jerks: every input that changes the shape of the eyes
92
+ * goes through a critically damped spring, and the states (hover, near,
93
+ * attention) hold their peak for a while before releasing. Putting the mouse
94
+ * in and out quickly does not make the eye flicker between two shapes: it
95
+ * stays in the "alert" shape and only relaxes afterwards.
96
+ */
97
+ smoothed = {};
98
+ hoverHold = newHold();
99
+ nearHold = newHold();
100
+ attentionHold = newHold();
101
+ sv(name, start = 0) {
102
+ return (this.smoothed[name] ??= newDamped(start));
103
+ }
104
+ constructor(config = DEFAULT_BRAIN_CONFIG, options = {}) {
105
+ this.cfg = config;
106
+ this.rng = options.rng ?? Math.random;
107
+ this.library = options.library ?? DEFAULT_LIBRARY;
108
+ this.customInteractions = !!options.interactions;
109
+ this.interactions = options.interactions ?? interactionsFor(config);
110
+ this.roles = rolesOf(config);
111
+ this.blink = newBlink(options.now ?? performance.now());
112
+ }
113
+ /**
114
+ * Swaps the physical configuration (another rig) keeping the state: reaction
115
+ * in progress, mood weights, gaze. Appendages that no longer exist are zeroed.
116
+ */
117
+ setConfig(config) {
118
+ this.cfg = config;
119
+ this.roles = rolesOf(config);
120
+ if (!this.customInteractions)
121
+ this.interactions = interactionsFor(config);
122
+ for (const k of ROLES)
123
+ if (!this.roles.includes(k)) {
124
+ this.parts[k].a = this.parts[k].v = 0;
125
+ this.tips[k].a = this.tips[k].v = 0;
126
+ }
127
+ }
128
+ /** Swaps what can be played (reactions, activities, moods). Default: `DEFAULT_LIBRARY`. */
129
+ setLibrary(library = DEFAULT_LIBRARY) {
130
+ this.library = library;
131
+ }
132
+ /** Swaps the interactions; `undefined` goes back to the one for `config.id`. */
133
+ setInteractions(interactions) {
134
+ this.customInteractions = !!interactions;
135
+ this.interactions = interactions ?? interactionsFor(this.cfg);
136
+ }
137
+ /**
138
+ * Forget the last frame time. Call it when frames stop for a while (the
139
+ * character left the screen, the tab was hidden): the next `step` then
140
+ * treats the gap as a fresh start instead of integrating across it.
141
+ */
142
+ resume() {
143
+ this.last = null;
144
+ }
145
+ /** Name of the reaction in progress, or `null`. */
146
+ get currentReaction() {
147
+ return this.reaction?.name ?? null;
148
+ }
149
+ /** Progress (0 to 1+) of a reaction; 1 if its definition no longer exists. */
150
+ progress(r, now) {
151
+ const def = own(this.library.reactions, r.name);
152
+ return def ? (now - r.t0) / 1000 / def.duration : 1;
153
+ }
154
+ /**
155
+ * Fires a reaction. A new one replaces the previous one, which fades out in
156
+ * 350 ms instead of cutting off. During a transformation (the body does not
157
+ * even exist), the new one waits in the queue. An unknown name is ignored
158
+ * and returns `false`.
159
+ */
160
+ react(name, now) {
161
+ if (!own(this.library.reactions, name))
162
+ return false;
163
+ const current = this.reaction;
164
+ if (current && this.library.exclusive.has(current.name) && this.progress(current, now) < 1) {
165
+ this.queue = name;
166
+ return true;
167
+ }
168
+ if (current)
169
+ this.leaving = { ...current, cut: now };
170
+ this.reaction = { name, t0: now };
171
+ return true;
172
+ }
173
+ jump(vy) {
174
+ if (this.morph > 0.01)
175
+ return;
176
+ launch(this.jumpState, vy);
177
+ }
178
+ /**
179
+ * Busy: a reaction playing or the body in the air. Hover and click gestures
180
+ * do not cut what is happening (that is what made the character shake with
181
+ * fast clicks): they wait their turn. App reactions (`react`) still enter at once.
182
+ */
183
+ busy(now) {
184
+ if (this.reaction && this.progress(this.reaction, now) < 1)
185
+ return true;
186
+ return !atRest(this.jumpState);
187
+ }
188
+ /** Gesture waiting, for debugging. */
189
+ get pendingGesture() {
190
+ return this.pending?.gesture ?? null;
191
+ }
192
+ perform(g, now, weight = 1) {
193
+ if (this.busy(now)) {
194
+ // The most important stays (sequence > gesture > hop); a tie: the newest.
195
+ if (!this.pending || weight >= this.pending.weight)
196
+ this.pending = { gesture: g, t: now, weight };
197
+ return g;
198
+ }
199
+ this.lastGesture = g;
200
+ if (g === "hop")
201
+ this.jump();
202
+ else
203
+ this.react(g, now);
204
+ return g;
205
+ }
206
+ /** Cursor entered: a drawn gesture, if not in the middle of another. */
207
+ hoverEnter(now) {
208
+ if (this.busy(now) || now - this.lastHover < HOVER_PAUSE_MS)
209
+ return null;
210
+ this.lastHover = now;
211
+ return this.perform(pickGesture(this.interactions.hover, this.rng, this.lastGesture), now);
212
+ }
213
+ /**
214
+ * Click or tap. A single click draws a gesture; successive clicks (less than
215
+ * 0.7 s apart) hop until the sequence asks for something else: the character
216
+ * table's reactions come on the 3rd, 5th and 8th. It also wakes whoever was
217
+ * dozing under the cursor.
218
+ */
219
+ tap(now) {
220
+ if (this.hoverSince !== null)
221
+ this.hoverSince = now;
222
+ if (this.nap.x > 0.5) {
223
+ this.chain = 0;
224
+ this.react("wake", now);
225
+ return "wake";
226
+ }
227
+ this.chain = now - this.lastTap < CLICK_CHAIN_INTERVAL_MS ? this.chain + 1 : 1;
228
+ this.lastTap = now;
229
+ const { sequence, click } = this.interactions;
230
+ const step = sequence.find((q) => q.n === this.chain);
231
+ if (step) {
232
+ if (this.chain >= sequence[sequence.length - 1].n)
233
+ this.chain = 0;
234
+ return this.perform(step.reaction, now, 3);
235
+ }
236
+ if (this.chain > 1)
237
+ return this.perform("hop", now, 1);
238
+ return this.perform(pickGesture(click, this.rng, this.lastGesture), now, 2);
239
+ }
240
+ step(now, e) {
241
+ // The single place where time enters the brain. 50 ms caps what one frame
242
+ // can advance; the integrators further split it into steps of at most
243
+ // `MAX_STEP` (20 ms), because the gaze spring (K = 900) diverges above ~38 ms.
244
+ const dt = this.last === null ? 0 : Math.min(0.05, Math.max(0, now - this.last) / 1000);
245
+ this.last = now;
246
+ const s = now / 1000;
247
+ const amp = e.reduced ? 0 : 1;
248
+ const first = dt === 0;
249
+ const lib = this.library;
250
+ /* ---------- queued gesture: enters when the previous one ends ---------- */
251
+ if (this.pending && !this.busy(now)) {
252
+ const p = this.pending;
253
+ this.pending = null;
254
+ if (now - p.t < PENDING_VALIDITY_MS)
255
+ this.perform(p.gesture, now, p.weight);
256
+ }
257
+ /* ---------- mood and activity weights ---------- */
258
+ for (const h in lib.moods) {
259
+ const target = e.mood[h] ?? 0;
260
+ const m = (this.moodSprings[h] ??= newDamped(target));
261
+ this.moodWeights[h] = first ? (m.x = target) : smoothDamp(m, target, 0.45, dt);
262
+ }
263
+ for (const a in lib.activities) {
264
+ const target = e.activity === a ? 1 : 0;
265
+ const m = (this.activitySprings[a] ??= newDamped(target));
266
+ this.activityWeights[a] = first ? (m.x = target) : smoothDamp(m, target, 0.4, dt);
267
+ }
268
+ /* ---------- sum of the layers ---------- */
269
+ const d = zeroDelta();
270
+ for (const h in lib.moods) {
271
+ const w = this.moodWeights[h];
272
+ if (w > 0.001)
273
+ addDelta(d, lib.moods[h], w);
274
+ }
275
+ for (const a in lib.activities) {
276
+ const w = this.activityWeights[a];
277
+ if (w > 0.001)
278
+ addDelta(d, lib.activities[a](s), w);
279
+ }
280
+ if (this.leaving) {
281
+ const { name, t0, cut } = this.leaving;
282
+ const def = own(lib.reactions, name);
283
+ const t = def ? (now - t0) / 1000 / def.duration : 1;
284
+ const weight = 1 - smooth(0, 1, (now - cut) / REACTION_FADE_MS);
285
+ if (!def || t >= 1 || weight <= 0)
286
+ this.leaving = null;
287
+ else
288
+ addDelta(d, def.sample(Math.max(0, t)), weight);
289
+ }
290
+ if (this.reaction) {
291
+ const { name, t0 } = this.reaction;
292
+ const def = own(lib.reactions, name);
293
+ const t = def ? (now - t0) / 1000 / def.duration : 1;
294
+ if (t >= 1) {
295
+ this.reaction = null;
296
+ if (this.queue) {
297
+ const next = this.queue;
298
+ this.queue = null;
299
+ if (this.react(next, now))
300
+ addDelta(d, lib.reactions[next].sample(0));
301
+ }
302
+ }
303
+ else
304
+ addDelta(d, def.sample(Math.max(0, t)));
305
+ }
306
+ /* ---------- cursor resting on top: cuddle and, later, nap ---------- */
307
+ if (e.hover && this.hoverSince === null)
308
+ this.hoverSince = now;
309
+ if (!e.hover)
310
+ this.hoverSince = null;
311
+ const over = this.hoverSince === null ? 0 : now - this.hoverSince;
312
+ // Starts only with the body free but, once on, does not switch off because
313
+ // of a passing reaction (it was another source of on-off flicker).
314
+ const free = this.reaction === null;
315
+ const napTarget = (free || this.nap.x > 0.5) && over > HOVER_NAP_MS ? 1 : 0;
316
+ const cuddleTarget = (free || this.cuddle.x > 0.5) && over > HOVER_CUDDLE_MS && !napTarget ? 1 : 0;
317
+ const cuddle = first
318
+ ? (this.cuddle.x = cuddleTarget)
319
+ : clamp(smoothDamp(this.cuddle, cuddleTarget, 0.6, dt), 0, 1);
320
+ const nap = first
321
+ ? (this.nap.x = napTarget)
322
+ : clamp(smoothDamp(this.nap, napTarget, 0.9, dt), 0, 1);
323
+ if (cuddle > 0.001)
324
+ addDelta(d, this.interactions.cuddle(s), cuddle);
325
+ if (nap > 0.001)
326
+ addDelta(d, this.interactions.nap(s), nap);
327
+ if (e.reduced)
328
+ for (const k of MOTION_FIELDS)
329
+ d[k] = 0;
330
+ const hold = clamp(d.holdGaze, 0, 1);
331
+ // During the transformation, the idle goes quiet: at the swap, the circle
332
+ // has to be exactly where the bubble will appear.
333
+ const m = clamp(d.morph, 0, 1);
334
+ this.morph = m;
335
+ const alive = 1 - m;
336
+ /* ---------- gaze (saccades, pursuit, attention, boredom) ---------- */
337
+ let tx;
338
+ let ty;
339
+ if (e.gaze) {
340
+ tx = clamp(e.gaze.x, -1, 1);
341
+ ty = clamp(e.gaze.y, -1, 1);
342
+ }
343
+ else {
344
+ if (now > this.wander.next) {
345
+ if (this.rng() < 0.3)
346
+ this.wander.x = this.wander.y = 0;
347
+ else {
348
+ this.wander.x = (this.rng() * 2 - 1) * 0.8;
349
+ this.wander.y = this.rng() * 0.8 - 0.5;
350
+ }
351
+ this.wander.next = now + 1200 + this.rng() * 2600;
352
+ }
353
+ tx = this.wander.x;
354
+ ty = this.wander.y;
355
+ }
356
+ tx *= (1 - hold) * alive;
357
+ ty *= (1 - hold) * alive;
358
+ // Proximity holds ~0.9 s and releases slowly: the cursor leaving and
359
+ // coming back does not make the attention and the cross-eyed look oscillate.
360
+ const near = smoothDamp(this.sv("near"), stepHold(this.nearHold, (e.proximity ?? 0) * alive, now, 900, 1.2, dt), 0.3, dt);
361
+ const look = this.gaze.step({
362
+ target: { x: tx, y: ty },
363
+ reduced: e.reduced,
364
+ interest: (e.interest ?? 0) * alive,
365
+ proximity: near,
366
+ glide: e.glide,
367
+ }, now, dt, this.rng);
368
+ if (look.blink && !this.blink.active)
369
+ this.blink.next = now;
370
+ // How much the gaze rules the expression (reactions can take over).
371
+ const freeGaze = (1 - hold * 0.7) * alive;
372
+ const attention = clamp(smoothDamp(this.sv("attention"), stepHold(this.attentionHold, look.attention * freeGaze, now, 1200, 0.8, dt), 0.35, dt), 0, 1);
373
+ const boredom = clamp(smoothDamp(this.sv("boredom"), look.boredom * freeGaze * (1 - nap), 0.8, dt), 0, 1);
374
+ /* ---------- blink ---------- */
375
+ const sleeping = Math.max(this.activityWeights.sleeping ?? 0, nap);
376
+ const blinkAmount = amp && sleeping < 0.5 ? stepBlink(this.blink, now, this.rng) : 0;
377
+ /* ---------- jump, squash, hover ---------- */
378
+ if (e.reduced) {
379
+ this.jumpState.y = this.jumpState.vy = 0;
380
+ this.squash.s = this.squash.v = 0;
381
+ }
382
+ else
383
+ stepJump(this.jumpState, this.squash, dt);
384
+ // Hover holds 0.6 s after the cursor leaves: going in and out quickly does not shake.
385
+ this.hover = clamp(smoothDamp(this.sv("hover"), stepHold(this.hoverHold, e.hover ? 1 : 0, now, 600, 2, dt), 0.25, dt), 0, 1);
386
+ const hover = this.hover * alive;
387
+ const stretch = clamp(-this.jumpState.vy / 12000, -0.05, 0.1) * amp * alive;
388
+ /* ---------- appendages on springs ---------- */
389
+ const { idleAppendage: idle, flicker } = this.cfg;
390
+ const partAmp = Math.max(0, 1 + d.partAmp) * amp * idle.amp * (e.detail === "simple" ? 0 : 1);
391
+ const f = idle.freq;
392
+ const lagY = clamp(this.jumpState.vy * 0.003, -18, 22) * amp;
393
+ const targets = this.targets;
394
+ targets.C =
395
+ (3 * Math.sin(s * 1.1 * f) + 1.5 * Math.sin(s * 2.3 * f + 1)) * partAmp -
396
+ look.x * 4 +
397
+ d.partC;
398
+ targets.L =
399
+ 2.5 * Math.sin(s * 0.9 * f + 2) * partAmp +
400
+ lagY +
401
+ 20 * hover +
402
+ 10 * attention -
403
+ 7 * boredom +
404
+ d.partL;
405
+ targets.R =
406
+ 2.5 * Math.sin(s * 1.0 * f + 4) * partAmp -
407
+ lagY -
408
+ 20 * hover -
409
+ 10 * attention +
410
+ 7 * boredom +
411
+ d.partR;
412
+ for (const k of this.roles) {
413
+ if (e.reduced || first) {
414
+ this.parts[k].a = targets[k];
415
+ this.parts[k].v = 0;
416
+ }
417
+ else
418
+ spring(this.parts[k], targets[k], 120, 11, dt);
419
+ }
420
+ /* ---------- body ---------- */
421
+ const turn = clamp(look.x * 0.35 * alive + d.turn, -1.6, 1.6);
422
+ const absTurn = Math.abs(turn);
423
+ const profile = clamp((absTurn - 1) / 0.6, 0, 1);
424
+ const breath = Math.sin(s * 1.65) * amp * (1 - sleeping) * alive;
425
+ const flickX = flicker * 0.012 * Math.sin(s * 9.1) * amp * alive;
426
+ const flickY = flicker * 0.022 * Math.sin(s * 7.3) * amp * alive;
427
+ const squashed = this.squash.s * alive;
428
+ const bodySx = (1 - 0.012 * breath + squashed - stretch * 0.5 + d.bodySx - flickX) *
429
+ (1 - Math.min(absTurn, 1) * 0.03 - profile * 0.14);
430
+ const bodySy = 1 + 0.024 * breath - squashed + stretch + d.bodySy + flickY + 0.02 * attention - 0.015 * boredom;
431
+ const bodyRotate = (look.x * 2.2 + flicker * 1.4 * Math.sin(s * 5.3) * amp) * alive + d.bodyRotate;
432
+ const rootY = this.jumpState.y * alive + d.rootY;
433
+ const scale = Math.max(0, 1 + d.scale);
434
+ /* ---------- eyes: 3D turn, eyelids and reactive shape ---------- */
435
+ const ex = look.x * 32 + d.eyeX;
436
+ const ey = look.y * 24 + d.eyeY;
437
+ // Gaze velocity: in a saccade the eye stretches along the movement and
438
+ // tilts toward where it is going (like a cartoon motion blur).
439
+ // Microsaccades (low velocity) do not deform; and the shape does not
440
+ // follow the raw velocity: stretch and tilt go through springs, so the
441
+ // saccade leaves the eye tilted for a moment and it comes back without a jerk.
442
+ const vel = Math.hypot(look.vx, look.vy);
443
+ const fast = smooth(2, 9, vel);
444
+ const stretchAmount = 0.3 * fast * amp * alive;
445
+ const ux = vel > 1e-6 ? Math.abs(look.vx) / vel : 0;
446
+ const uy = vel > 1e-6 ? Math.abs(look.vy) / vel : 0;
447
+ // Fast attack, slow release: the deformation arrives with the saccade and
448
+ // stays a little after it, slowly coming back.
449
+ const follow = (name, target, attack, release) => {
450
+ const sm = this.sv(name);
451
+ return smoothDamp(sm, target, Math.abs(target) > Math.abs(sm.x) ? attack : release, dt);
452
+ };
453
+ const stretchX = follow("stretchX", stretchAmount * (ux - 0.6 * uy), 0.05, 0.22);
454
+ const stretchY = follow("stretchY", stretchAmount * (uy - 0.6 * ux), 0.05, 0.22);
455
+ const tilt = follow("tilt", clamp(look.vx * 1.6, -16, 16) * fast * amp * alive, 0.06, 0.45) +
456
+ smoothDamp(this.sv("tiltX"), look.x * 6 * alive, 0.3, dt);
457
+ // Attention: bigger, more open eyes. Boredom: heavy eyelids.
458
+ // Looking up, the eye grows; looking down, the eyelid comes down with it.
459
+ const lookY = first
460
+ ? (this.sv("lookY").x = look.y)
461
+ : smoothDamp(this.sv("lookY"), look.y, 0.25, dt);
462
+ const up = Math.max(0, -lookY);
463
+ const down = Math.max(0, lookY);
464
+ const size = 1 + 0.16 * attention;
465
+ const sx = (1 + d.eyeSx + hover * 0.04) * size * (1 + stretchX);
466
+ const syBase = (1 + d.eyeSy + hover * 0.08) *
467
+ (size + 0.06 * attention) *
468
+ (1 + stretchY) *
469
+ (1 + 0.07 * up * alive - 0.04 * down * alive - 0.06 * boredom);
470
+ const vanish = 1 - clamp(d.eyesVanish, 0, 1);
471
+ const lidTop = clamp(d.lidTop + 0.3 * boredom + 0.16 * down * alive - 0.15 * attention, 0, 1);
472
+ const lidBottom = clamp(d.lidBottom, 0, 1);
473
+ const converge = clamp(smoothDamp(this.sv("converge"), look.convergence * freeGaze, 0.35, dt), 0, 1);
474
+ // Up to |turn| = 1, the eye on the side it turns toward shrinks 30%; in profile, it vanishes.
475
+ const shrink = (side) => {
476
+ const g = Math.max(side * turn, 0);
477
+ return g <= 1 ? 1 - 0.3 * g : 0.7 * (1 - profile);
478
+ };
479
+ const shift = (nearSide) => turn * (nearSide ? 34 : 48) + Math.sign(turn) * profile * 40;
480
+ const eye = (side) => {
481
+ const close = clamp(side < 0 ? d.closeLeft : d.closeRight, 0, 1);
482
+ const happy = clamp(d.happy + (side < 0 ? d.happyLeft : d.happyRight), 0, 1);
483
+ return {
484
+ x: ex + shift(side * turn > 0) - side * converge * 16,
485
+ y: ey,
486
+ sx: sx * shrink(side) * vanish,
487
+ sy: Math.max(0.04, syBase * (1 - blinkAmount * 0.93 * alive) * (1 - close * 0.94)) * vanish,
488
+ lidTop,
489
+ lidBottom,
490
+ lidAngle: side < 0 ? d.lidAngle : -d.lidAngle,
491
+ happy,
492
+ rot: tilt - side * converge * 9,
493
+ };
494
+ };
495
+ const leftEye = eye(-1);
496
+ const rightEye = eye(1);
497
+ /* ---------- appendage tips (a chain with inertia) ---------- */
498
+ const baseY = rootY - this.cfg.baseHeight * bodySy * scale;
499
+ const rotTotal = bodyRotate + d.centerRotate;
500
+ let dRot = rotTotal - this.previous.rot;
501
+ dRot = ((((dRot + 180) % 360) + 360) % 360) - 180; // a 360° turn is not a jerk
502
+ const prev = this.previous;
503
+ if (first || e.reduced || e.detail === "simple" || e.bend === false) {
504
+ for (const k of ROLES) {
505
+ this.tips[k].a = 0;
506
+ this.tips[k].v = 0;
507
+ }
508
+ prev.vy = 0;
509
+ prev.vPart.C = prev.vPart.L = prev.vPart.R = 0;
510
+ }
511
+ else {
512
+ const vy = (baseY - prev.baseY) / dt;
513
+ const vRot = dRot / dt;
514
+ const dvy = clamp(vy - prev.vy, -3000, 3000);
515
+ for (const k of this.roles) {
516
+ const vPart = this.parts[k].v + vRot;
517
+ const dv = clamp(vPart - prev.vPart[k], -2000, 2000);
518
+ stepTip(this.tips[k], dv, dvy, SIDE[k], dt);
519
+ prev.vPart[k] = vPart;
520
+ }
521
+ prev.vy = vy;
522
+ }
523
+ prev.baseY = baseY;
524
+ prev.rot = rotTotal;
525
+ /* ---------- the rest ---------- */
526
+ const opacity = clamp(1 + d.opacity, 0, 1);
527
+ const shadowScale = clamp(1 + Math.min(0, rootY) / 450, 0.4, 1) * Math.min(1, 0.3 + scale);
528
+ const simple = e.detail === "simple";
529
+ const accessory = (v) => (simple ? 0 : clamp(v, 0, 1));
530
+ return {
531
+ rootY,
532
+ opacity,
533
+ bodyRotate,
534
+ centerRotate: d.centerRotate,
535
+ bodySx,
536
+ bodySy,
537
+ scale,
538
+ partsX: clamp(turn, -1, 1) * 14 + Math.sign(turn) * profile * 20,
539
+ partC: this.parts.C.a,
540
+ partL: this.parts.L.a,
541
+ partR: this.parts.R.a,
542
+ bendC: this.tips.C.a,
543
+ bendL: this.tips.L.a,
544
+ bendR: this.tips.R.a,
545
+ leftEye,
546
+ rightEye,
547
+ shadowScale,
548
+ shadowOpacity: 0.14 * shadowScale * opacity * clamp(1 + d.shadow, 0, 1),
549
+ sparks: simple ? 0 : clamp(d.sparks, 0, 1),
550
+ lamp: accessory(d.lamp),
551
+ exclaim: accessory(d.exclaim),
552
+ question: accessory(d.question),
553
+ ellipsis: accessory(d.ellipsis),
554
+ zzz: accessory(d.zzz),
555
+ rays: accessory(d.rays),
556
+ attention,
557
+ boredom,
558
+ gray: clamp(d.gray, 0, 1),
559
+ core: clamp(d.core, 0, 1),
560
+ embers: accessory(d.embers),
561
+ smoke: accessory(d.smoke),
562
+ morph: m,
563
+ round: d.round,
564
+ retract: d.retract,
565
+ swap: d.swap,
566
+ bubbleR: d.bubbleR,
567
+ bubbleHole: d.bubbleHole,
568
+ bubbleArc: d.bubbleArc,
569
+ bubbleX: d.bubbleX,
570
+ bubbleY: d.bubbleY,
571
+ dotsDist: d.dotsDist,
572
+ dotsBounce: d.dotsBounce,
573
+ checkDraw: d.checkDraw,
574
+ codeDraw: d.codeDraw,
575
+ orbitOpen: d.orbitOpen,
576
+ objScale: d.objScale,
577
+ objRotate: d.objRotate,
578
+ time: s,
579
+ };
56
580
  }
57
581
  }