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/focus.js ADDED
@@ -0,0 +1,284 @@
1
+ /*
2
+ * Where the character looks when it isn't following the mouse. Two modes:
3
+ *
4
+ * - `focus`: looks at what matters. A page event (a field got focus, typing,
5
+ * a click, scrolling) pulls the gaze; with nothing happening, it stays on
6
+ * the site's anchors and, with no anchors, picks something.
7
+ * - `free`: wanders, with the eye gliding across the page. At any moment
8
+ * something can catch its attention (an event, an anchor, an element) and it
9
+ * goes into focus; once the interest passes, it goes back to wandering.
10
+ *
11
+ * Focus anchor: any element with `data-bot-focus` (like the focus of a text
12
+ * box, but declared by the site). The value picks the transition: `fast` (a
13
+ * hop, like noticing something) or `smooth` (the default: the eye glides
14
+ * there). Setting the attribute on an element grabs attention immediately;
15
+ * with several anchors, it alternates between them.
16
+ *
17
+ * Each target change says how the eye arrives: `glide` in seconds (smooth
18
+ * curve) or 0 (a saccade, the fast hop).
19
+ *
20
+ * A single observer (shared, like the ticker) keeps the last event; each
21
+ * character decides with its own state.
22
+ */
23
+ export const ANCHOR_ATTRIBUTE = "data-bot-focus";
24
+ const WEIGHT = {
25
+ focus: 0.55,
26
+ key: 0.12,
27
+ click: 0.4,
28
+ scroll: 0.2,
29
+ anchor: 0.35,
30
+ };
31
+ const DURATION = {
32
+ focus: 3500,
33
+ key: 1600,
34
+ click: 1800,
35
+ scroll: 1200,
36
+ anchor: 4000,
37
+ };
38
+ /** How the eye arrives at each event: a hop (0) or a glide (seconds). */
39
+ const GLIDE = {
40
+ focus: 0,
41
+ click: 0,
42
+ // Reading the text: the eye follows the text cursor, gliding.
43
+ key: 0.18,
44
+ scroll: 0.35,
45
+ };
46
+ /** Glide to a `smooth` anchor (and back to it). */
47
+ const ANCHOR_GLIDE = 0.55;
48
+ let lastEvent = null;
49
+ let subscribers = 0;
50
+ let prevScroll = 0;
51
+ let teardown = null;
52
+ const centerOf = (el) => {
53
+ const r = el.getBoundingClientRect();
54
+ return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
55
+ };
56
+ const isEditable = (el) => !!el &&
57
+ (el instanceof HTMLInputElement ||
58
+ el instanceof HTMLTextAreaElement ||
59
+ el.isContentEditable);
60
+ const isAnchor = (el) => !!el && el.isConnected && el.hasAttribute(ANCHOR_ATTRIBUTE);
61
+ const anchorGlide = (el) => (el.getAttribute(ANCHOR_ATTRIBUTE) === "fast" ? 0 : ANCHOR_GLIDE);
62
+ /** Where the end of the text in a field is (approximate: 8.5 px per character). */
63
+ function textCursorPoint(el) {
64
+ const r = el.getBoundingClientRect();
65
+ const value = el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement
66
+ ? el.value
67
+ : (el.textContent ?? "");
68
+ const width = Math.max(40, r.width - 24);
69
+ const advance = (value.length * 8.5) % width;
70
+ const line = Math.floor((value.length * 8.5) / width);
71
+ const y = el instanceof HTMLTextAreaElement ? r.top + 14 + (line % 4) * 20 : r.top + r.height / 2;
72
+ return { x: r.left + 12 + advance, y: Math.min(y, r.bottom - 8) };
73
+ }
74
+ function attach() {
75
+ const mark = (point, kind, el = null) => (lastEvent = { point, t: performance.now(), kind, el });
76
+ const onFocus = (e) => {
77
+ const el = e.target;
78
+ if (el && el !== document.body)
79
+ mark(centerOf(el), "focus", el);
80
+ };
81
+ const onKey = () => {
82
+ const el = document.activeElement;
83
+ if (isEditable(el))
84
+ requestAnimationFrame(() => mark(textCursorPoint(el), "key"));
85
+ };
86
+ const onClick = (e) => mark({ x: e.clientX, y: e.clientY }, "click");
87
+ const onScroll = () => {
88
+ const y = window.scrollY;
89
+ const down = y > prevScroll;
90
+ prevScroll = y;
91
+ mark({ x: window.innerWidth / 2, y: down ? window.innerHeight : 0 }, "scroll");
92
+ };
93
+ // New anchor (attribute set or element inserted): grabs attention immediately.
94
+ const mo = new MutationObserver((list) => {
95
+ anchorCache.until = 0;
96
+ for (const m of list) {
97
+ if (m.type === "attributes" && isAnchor(m.target)) {
98
+ mark(centerOf(m.target), "anchor", m.target);
99
+ return;
100
+ }
101
+ for (const n of Array.from(m.addedNodes)) {
102
+ if (!(n instanceof Element))
103
+ continue;
104
+ const el = isAnchor(n) ? n : n.querySelector(`[${ANCHOR_ATTRIBUTE}]`);
105
+ if (el) {
106
+ mark(centerOf(el), "anchor", el);
107
+ return;
108
+ }
109
+ }
110
+ }
111
+ });
112
+ mo.observe(document.documentElement, {
113
+ subtree: true,
114
+ childList: true,
115
+ attributes: true,
116
+ attributeFilter: [ANCHOR_ATTRIBUTE],
117
+ });
118
+ prevScroll = window.scrollY;
119
+ document.addEventListener("focusin", onFocus);
120
+ document.addEventListener("keydown", onKey);
121
+ document.addEventListener("pointerdown", onClick, { passive: true });
122
+ window.addEventListener("scroll", onScroll, { passive: true });
123
+ return () => {
124
+ mo.disconnect();
125
+ document.removeEventListener("focusin", onFocus);
126
+ document.removeEventListener("keydown", onKey);
127
+ document.removeEventListener("pointerdown", onClick);
128
+ window.removeEventListener("scroll", onScroll);
129
+ };
130
+ }
131
+ /** Starts observing the page; returns the function that stops it. */
132
+ export function observePage() {
133
+ if (subscribers++ === 0)
134
+ teardown = attach();
135
+ return () => {
136
+ if (--subscribers === 0) {
137
+ teardown?.();
138
+ teardown = null;
139
+ lastEvent = null;
140
+ }
141
+ };
142
+ }
143
+ const INTERESTING = 'h1,h2,h3,button,a[href],input,textarea,select,img,video,[role="button"]';
144
+ const onScreen = (r) => r.width >= 8 &&
145
+ r.height >= 8 &&
146
+ r.bottom >= 0 &&
147
+ r.top <= window.innerHeight &&
148
+ r.right >= 0 &&
149
+ r.left <= window.innerWidth;
150
+ const within = (r, outer) => r.left >= outer.left && r.right <= outer.right && r.top >= outer.top && r.bottom <= outer.bottom;
151
+ function visibleElement(rng, awayFrom) {
152
+ const list = Array.from(document.querySelectorAll(INTERESTING)).slice(0, 120);
153
+ const visible = list.filter((el) => {
154
+ const r = el.getBoundingClientRect();
155
+ // Doesn't look inside itself.
156
+ return onScreen(r) && !within(r, awayFrom);
157
+ });
158
+ if (!visible.length)
159
+ return null;
160
+ return centerOf(visible[Math.floor(rng() * visible.length)]);
161
+ }
162
+ /** Anchors on screen (DOM query at most every 300 ms). */
163
+ const anchorCache = { until: 0, list: [] };
164
+ function visibleAnchors(now, awayFrom) {
165
+ if (now > anchorCache.until) {
166
+ anchorCache.list = Array.from(document.querySelectorAll(`[${ANCHOR_ATTRIBUTE}]`));
167
+ anchorCache.until = now + 300;
168
+ }
169
+ return anchorCache.list.filter((el) => {
170
+ if (!el.isConnected)
171
+ return false;
172
+ const r = el.getBoundingClientRect();
173
+ return onScreen(r) && !within(r, awayFrom);
174
+ });
175
+ }
176
+ export const newFocusState = () => ({
177
+ point: null,
178
+ el: null,
179
+ until: 0,
180
+ seen: 0,
181
+ nextCursor: 0,
182
+ glide: 0,
183
+ mode: "free",
184
+ reason: null,
185
+ anchorIndex: 0,
186
+ });
187
+ function go(f, point, el, until, glide, mode, reason) {
188
+ f.point = point;
189
+ f.el = el;
190
+ f.until = until;
191
+ f.glide = glide;
192
+ f.mode = mode;
193
+ f.reason = reason;
194
+ }
195
+ const decision = (f, interest) => {
196
+ // Following an element: the position is the current one (scrolled, moved, animated).
197
+ if (f.el && f.el.isConnected)
198
+ f.point = centerOf(f.el);
199
+ return { point: f.point, interest, glide: f.glide, mode: f.mode, reason: f.reason };
200
+ };
201
+ /** Picks the next anchor (alternates when there is more than one). */
202
+ function nextAnchor(f, anchors) {
203
+ if (anchors.length > 1 && f.el && anchors.includes(f.el))
204
+ f.anchorIndex++;
205
+ return anchors[f.anchorIndex % anchors.length];
206
+ }
207
+ /**
208
+ * Where to look right now, in the requested mode. Call every frame.
209
+ */
210
+ export function decideGaze(f, now, rng, myRect, cursor, mode) {
211
+ // 1. Something happened on the page: goes into focus, in both modes.
212
+ const ev = lastEvent;
213
+ if (ev && ev.t > f.seen) {
214
+ f.seen = ev.t;
215
+ const glide = ev.kind === "anchor" ? (ev.el ? anchorGlide(ev.el) : 0) : GLIDE[ev.kind];
216
+ go(f, ev.point, ev.el, now + DURATION[ev.kind], glide, "focus", ev.kind);
217
+ return decision(f, WEIGHT[ev.kind]);
218
+ }
219
+ // An anchor that stopped being an anchor (or left the page): let go at once.
220
+ if (f.reason === "anchor" && !isAnchor(f.el))
221
+ f.until = 0;
222
+ // 2. Cursor passing nearby and moving: peeks (at most every 3 s).
223
+ const cx = myRect.left + myRect.width / 2;
224
+ const cy = myRect.top + myRect.height / 2;
225
+ const near = Math.hypot(cursor.x - cx, cursor.y - cy) < Math.max(220, myRect.width * 1.6);
226
+ if (cursor.inside && near && now - cursor.last < 300 && now > f.nextCursor) {
227
+ f.nextCursor = now + 3000;
228
+ go(f, { x: cursor.x, y: cursor.y }, null, now + 1100, 0, "focus", "cursor");
229
+ return decision(f, 0.3);
230
+ }
231
+ // 3. Still on the current target.
232
+ if (now <= f.until)
233
+ return decision(f, 0);
234
+ // 4. Time to pick another one.
235
+ const anchors = visibleAnchors(now, myRect);
236
+ const r = rng();
237
+ if (mode === "focus") {
238
+ if (anchors.length) {
239
+ // Stays on the anchors; now and then, a quick look at whoever is on screen.
240
+ if (f.reason === "anchor" && r < 0.15) {
241
+ go(f, "screen", null, now + 900 + rng() * 600, 0, "focus", "screen");
242
+ return decision(f, 0);
243
+ }
244
+ const el = nextAnchor(f, anchors);
245
+ go(f, null, el, now + 2500 + rng() * 2000, anchorGlide(el), "focus", "anchor");
246
+ return decision(f, 0.1);
247
+ }
248
+ // No anchors: as before (hops between the screen, text and elements).
249
+ const active = document.activeElement;
250
+ if (r < 0.22)
251
+ go(f, "screen", null, now + 1400 + rng() * 3200, 0, "focus", "screen");
252
+ else if (r < 0.45 && isEditable(active))
253
+ go(f, textCursorPoint(active), null, now + 1400 + rng() * 3200, 0, "focus", "text");
254
+ else {
255
+ const p = visibleElement(rng, myRect);
256
+ go(f, p, null, now + 1400 + rng() * 3200, 0, "focus", p ? "element" : null);
257
+ }
258
+ return decision(f, f.reason === "element" || f.reason === "text" ? 0.12 : 0);
259
+ }
260
+ // Free mode: wanders; sometimes something holds the gaze (goes into focus).
261
+ if (anchors.length && r < 0.15) {
262
+ // Noticed the anchor: goes fast, like someone noticing something.
263
+ const el = nextAnchor(f, anchors);
264
+ go(f, null, el, now + 1800 + rng() * 1400, 0, "focus", "anchor");
265
+ return decision(f, 0.25);
266
+ }
267
+ if (r < 0.25) {
268
+ const p = visibleElement(rng, myRect);
269
+ if (p) {
270
+ go(f, p, null, now + 900 + rng() * 700, 0, "focus", "element");
271
+ return decision(f, 0.15);
272
+ }
273
+ }
274
+ if (r < 0.37) {
275
+ go(f, "screen", null, now + 1200 + rng() * 1200, 0.6, "free", "screen");
276
+ return decision(f, 0);
277
+ }
278
+ // Wander: glides to any point (half of the time, something on the page).
279
+ const p = rng() < 0.5
280
+ ? visibleElement(rng, myRect)
281
+ : { x: rng() * window.innerWidth, y: rng() * window.innerHeight };
282
+ go(f, p, null, now + 900 + rng() * 1300, 0.7 + rng() * 0.6, "free", "wander");
283
+ return decision(f, 0);
284
+ }
package/dist/gaze.d.ts CHANGED
@@ -1,60 +1,48 @@
1
- import { type Rng } from "./rng.js";
2
- export interface Point2D {
1
+ import { type Rng } from "./motion.js";
2
+ export type GazeInput = {
3
+ /** Target from −1 to 1. */
4
+ target: {
5
+ x: number;
6
+ y: number;
7
+ };
8
+ reduced: boolean;
9
+ /** Something new caught the attention this frame (0 to 1). */
10
+ interest: number;
11
+ /** How close the cursor is to the character (0 far, 1 right on top). */
12
+ proximity: number;
13
+ /**
14
+ * Seconds to glide to the target (the eye wandering across the page,
15
+ * returning to an anchor). 0 or absent: a saccade, the usual fast hop.
16
+ */
17
+ glide?: number;
18
+ };
19
+ export type GazeOutput = {
3
20
  x: number;
4
21
  y: number;
5
- }
6
- export type ArrivalStyle = "saccade" | "pursuit";
7
- export interface GazeTarget {
8
- point: Point2D;
9
- /** How the eye should get there. Default "pursuit" (smooth, no overshoot) for a moving target, "saccade" (snap + settle) for a newly-appeared one — pass explicitly when you know which. */
10
- arrival?: ArrivalStyle;
11
- }
12
- export interface GazeOutput {
13
- offset: Point2D;
14
- /** 0..1: recently had a big saccade or a fast-moving target. Widens the eyes. */
22
+ /** Gaze velocity (units/s): deforms and tilts the eyes. */
23
+ vx: number;
24
+ vy: number;
15
25
  attention: number;
16
- /** 0..1: no target/motion for a while. Heavies the eyelids. */
17
26
  boredom: number;
18
- }
19
- export interface GazeEngineOptions {
20
- /** Damp rate for smooth pursuit. Default 9. */
21
- pursuitRate?: number;
22
- /** Spring for a saccade — the default slightly overshoots, then settles. */
23
- saccadeSpring?: {
24
- stiffness: number;
25
- damping: number;
26
- };
27
- /** Constant small jitter amplitude so the eye never looks perfectly frozen. Default 0.015. */
28
- microsaccadeAmplitude?: number;
29
- /** Seconds with no target before boredom starts rising. Default 4. */
30
- boredomAfterSeconds?: number;
31
- /** Seconds between autonomous wander targets, used when no explicit target is given. Default [1.2, 3.8]. */
32
- wanderIntervalSeconds?: [number, number];
33
- wanderRange?: number;
34
- recenterChance?: number;
35
- rng?: Rng;
36
- }
37
- /**
38
- * Drives one `(x, y)` gaze offset (-1..1 ish) from either an explicit target
39
- * (pointer position, a focus anchor) or autonomous wandering, with two
40
- * distinct arrival feels — `pursuit` (critically damped, used for a target
41
- * that is itself moving, like a cursor) and `saccade` (slightly
42
- * underdamped, used for a target that just appeared) — plus a constant
43
- * microsaccade jitter and an attention/boredom scalar a caller can map onto
44
- * eye size and eyelid openness.
45
- */
46
- export declare class GazeEngine {
27
+ /** Eyes move closer together (cute cross-eyed look) with the cursor right on top. */
28
+ convergence: number;
29
+ /** Asks for a blink (goes with big saccades, as in the human eye). */
30
+ blink: boolean;
31
+ };
32
+ export declare class Gaze {
47
33
  private pos;
48
- private posY;
49
- private pursuit;
50
- private usingSaccade;
51
- private lastTargetPoint;
34
+ /** Where the eye is heading (the saccade aims here). */
35
+ private goal;
36
+ private prevTarget;
37
+ private targetVel;
38
+ private lastSaccade;
39
+ private nextMicro;
52
40
  private attention;
53
- private idleSeconds;
54
- private wanderTarget;
55
- private nextWanderAt;
56
- private readonly opts;
57
- constructor(options?: GazeEngineOptions);
58
- /** Call every frame. Pass `null` for an explicit target to fall back to autonomous wandering. */
59
- update(dt: number, explicitTarget: GazeTarget | null, t: number): GazeOutput;
41
+ private boredom;
42
+ /** Gliding goal (a spring with no jerk) and the microsaccade added to it. */
43
+ private slide;
44
+ private micro;
45
+ step(input: GazeInput, now: number, dt: number, rng: Rng): GazeOutput;
46
+ private eyeSpring;
47
+ private output;
60
48
  }
package/dist/gaze.js CHANGED
@@ -1,90 +1,133 @@
1
- import { damp, spring } from "./spring.js";
2
- import { neverRepeating } from "./noise.js";
3
- import { chance, mulberry32, randRange } from "./rng.js";
4
- const DEFAULT_SACCADE_SPRING = { stiffness: 260, damping: 16 };
5
- /**
6
- * Drives one `(x, y)` gaze offset (-1..1 ish) from either an explicit target
7
- * (pointer position, a focus anchor) or autonomous wandering, with two
8
- * distinct arrival feels — `pursuit` (critically damped, used for a target
9
- * that is itself moving, like a cursor) and `saccade` (slightly
10
- * underdamped, used for a target that just appeared) — plus a constant
11
- * microsaccade jitter and an attention/boredom scalar a caller can map onto
12
- * eye size and eyelid openness.
13
- */
14
- export class GazeEngine {
15
- pos = { value: 0, velocity: 0 }; // x, driven by whichever arrival style is active
16
- posY = { value: 0, velocity: 0 };
17
- pursuit = { x: 0, y: 0 };
18
- usingSaccade = false;
19
- lastTargetPoint = null;
1
+ import { clamp, damp, newDamped, smoothDamp, substep } from "./motion.js";
2
+ const K = 900;
3
+ const C = 2 * Math.sqrt(K) * 0.62; // slightly underdamped: overshoots ~8% and comes back
4
+ /** Distance from which the eye hops instead of pursuing. */
5
+ const SACCADE_THRESHOLD = 0.12;
6
+ const MIN_SACCADE_INTERVAL = 220;
7
+ /** Fast cursor: the eye pursues instead of hopping, unless it falls far behind. */
8
+ const MAX_PURSUIT_LAG = 0.6;
9
+ export class Gaze {
10
+ pos = { x: 0, y: 0, vx: 0, vy: 0 };
11
+ /** Where the eye is heading (the saccade aims here). */
12
+ goal = { x: 0, y: 0 };
13
+ prevTarget = null;
14
+ targetVel = 0;
15
+ lastSaccade = -Infinity;
16
+ nextMicro = 0;
20
17
  attention = 0;
21
- idleSeconds = 0;
22
- wanderTarget = { x: 0, y: 0 };
23
- nextWanderAt = 0;
24
- opts;
25
- constructor(options = {}) {
26
- this.opts = {
27
- pursuitRate: options.pursuitRate ?? 9,
28
- saccadeSpring: options.saccadeSpring ?? DEFAULT_SACCADE_SPRING,
29
- microsaccadeAmplitude: options.microsaccadeAmplitude ?? 0.015,
30
- boredomAfterSeconds: options.boredomAfterSeconds ?? 4,
31
- wanderIntervalSeconds: options.wanderIntervalSeconds ?? [1.2, 3.8],
32
- wanderRange: options.wanderRange ?? 0.6,
33
- recenterChance: options.recenterChance ?? 0.3,
34
- rng: options.rng ?? mulberry32(),
35
- };
36
- }
37
- /** Call every frame. Pass `null` for an explicit target to fall back to autonomous wandering. */
38
- update(dt, explicitTarget, t) {
39
- const { rng, wanderIntervalSeconds, wanderRange, recenterChance, boredomAfterSeconds } = this.opts;
40
- let target = explicitTarget;
41
- if (!target) {
42
- this.idleSeconds += dt;
43
- this.nextWanderAt -= dt;
44
- if (this.nextWanderAt <= 0) {
45
- this.nextWanderAt = randRange(rng, wanderIntervalSeconds[0], wanderIntervalSeconds[1]);
46
- const newTarget = chance(rng, recenterChance)
47
- ? { x: 0, y: 0 }
48
- : { x: randRange(rng, -wanderRange, wanderRange), y: randRange(rng, -wanderRange, wanderRange) };
49
- this.wanderTarget = newTarget;
50
- target = { point: newTarget, arrival: "saccade" };
51
- }
52
- else {
53
- target = { point: this.wanderTarget, arrival: "pursuit" };
54
- }
18
+ boredom = 0;
19
+ /** Gliding goal (a spring with no jerk) and the microsaccade added to it. */
20
+ slide = { x: newDamped(), y: newDamped() };
21
+ micro = { x: 0, y: 0 };
22
+ step(input, now, dt, rng) {
23
+ const { target } = input;
24
+ let blink = false;
25
+ // Target velocity (smoothed): a fast cursor draws attention.
26
+ if (this.prevTarget && dt > 0) {
27
+ const v = Math.hypot(target.x - this.prevTarget.x, target.y - this.prevTarget.y) / dt;
28
+ this.targetVel = damp(this.targetVel, Math.min(v, 30), 8, dt);
55
29
  }
56
- else {
57
- this.idleSeconds = 0;
30
+ this.prevTarget = { ...target };
31
+ if (input.reduced || dt === 0) {
32
+ // No saccades or springs: the gaze just follows.
33
+ const k = dt === 0 ? 1 : 1 - Math.exp(-6 * dt);
34
+ this.pos.x += (target.x - this.pos.x) * k;
35
+ this.pos.y += (target.y - this.pos.y) * k;
36
+ this.pos.vx = this.pos.vy = 0;
37
+ this.goal = { ...target };
58
38
  }
59
- const isNewPoint = !this.lastTargetPoint || this.lastTargetPoint.x !== target.point.x || this.lastTargetPoint.y !== target.point.y;
60
- this.lastTargetPoint = target.point;
61
- const arrival = target.arrival ?? "pursuit";
62
- this.usingSaccade = arrival === "saccade" && isNewPoint ? true : this.usingSaccade;
63
- let x;
64
- let y;
65
- if (arrival === "saccade") {
66
- spring(this.pos, target.point.x, this.opts.saccadeSpring.stiffness, this.opts.saccadeSpring.damping, dt);
67
- spring(this.posY, target.point.y, this.opts.saccadeSpring.stiffness, this.opts.saccadeSpring.damping, dt);
68
- x = this.pos.value;
69
- y = this.posY.value;
70
- if (isNewPoint)
71
- this.attention = Math.min(1, this.attention + 0.6);
39
+ else if (input.glide && input.glide > 0) {
40
+ // Gliding: the goal moves along a smooth curve (starts and arrives
41
+ // slowly), with no saccades on the way. On arrival, microsaccades return.
42
+ const mx = smoothDamp(this.slide.x, target.x, input.glide, dt);
43
+ const my = smoothDamp(this.slide.y, target.y, input.glide, dt);
44
+ const arrived = Math.hypot(target.x - mx, target.y - my) < 0.03;
45
+ if (now > this.nextMicro) {
46
+ this.nextMicro = now + 500 + rng() * 1100;
47
+ this.micro = arrived
48
+ ? { x: (rng() - 0.5) * 0.04, y: (rng() - 0.5) * 0.034 }
49
+ : { x: 0, y: 0 };
50
+ }
51
+ if (!arrived)
52
+ this.micro = { x: 0, y: 0 };
53
+ this.goal.x = mx + this.micro.x;
54
+ this.goal.y = my + this.micro.y;
55
+ this.eyeSpring(dt);
72
56
  }
73
57
  else {
74
- this.pursuit = { x: damp(this.pursuit.x, target.point.x, this.opts.pursuitRate, dt), y: damp(this.pursuit.y, target.point.y, this.opts.pursuitRate, dt) };
75
- // Keep the saccade spring following too, at rest, so a later saccade doesn't jump from a stale position.
76
- this.pos.value = this.pursuit.x;
77
- this.pos.velocity = 0;
78
- this.posY.value = this.pursuit.y;
79
- this.posY.velocity = 0;
80
- x = this.pursuit.x;
81
- y = this.pursuit.y;
58
+ const dx = target.x - this.goal.x;
59
+ const dy = target.y - this.goal.y;
60
+ const dist = Math.hypot(dx, dy);
61
+ const recent = now - this.lastSaccade <= MIN_SACCADE_INTERVAL;
62
+ if (this.targetVel >= 3 && dist < MAX_PURSUIT_LAG) {
63
+ // Fast cursor: pursues firmly, with no chain of saccades (that was what shook).
64
+ this.goal.x = damp(this.goal.x, target.x, 14, dt);
65
+ this.goal.y = damp(this.goal.y, target.y, 14, dt);
66
+ }
67
+ else if (this.targetVel < 3 && dist < 0.35 && !recent) {
68
+ // Smooth pursuit (the cursor moving slowly).
69
+ this.goal.x = damp(this.goal.x, target.x, 10, dt);
70
+ this.goal.y = damp(this.goal.y, target.y, 10, dt);
71
+ }
72
+ else if (dist > SACCADE_THRESHOLD && now - this.lastSaccade > MIN_SACCADE_INTERVAL) {
73
+ // Saccade: aims a little short (the human eye corrects afterwards).
74
+ this.goal.x = target.x - dx * 0.06;
75
+ this.goal.y = target.y - dy * 0.06;
76
+ this.lastSaccade = now;
77
+ this.attention = Math.min(1, this.attention + Math.min(0.45, dist * 0.35));
78
+ this.boredom = 0;
79
+ if (dist > 0.9 && rng() < 0.35)
80
+ blink = true;
81
+ }
82
+ // Microsaccades: at rest, the eye is never 100% still.
83
+ if (now > this.nextMicro) {
84
+ this.nextMicro = now + 500 + rng() * 1100;
85
+ if (now - this.lastSaccade > 400) {
86
+ this.goal.x += (rng() - 0.5) * 0.04;
87
+ this.goal.y += (rng() - 0.5) * 0.034;
88
+ }
89
+ }
90
+ this.eyeSpring(dt);
91
+ }
92
+ if (!(input.glide && input.glide > 0)) {
93
+ // Outside a glide, the gliding goal follows along (with no velocity): the
94
+ // next glide starts from where the eye is.
95
+ this.slide.x.x = this.goal.x;
96
+ this.slide.y.x = this.goal.y;
97
+ this.slide.x.v = this.slide.y.v = 0;
98
+ this.micro = { x: 0, y: 0 };
82
99
  }
83
- this.attention = damp(this.attention, 0, 1.5, dt);
84
- const boredom = this.idleSeconds > boredomAfterSeconds ? Math.min(1, (this.idleSeconds - boredomAfterSeconds) / 3) : 0;
85
- const jitter = this.opts.microsaccadeAmplitude;
86
- x += neverRepeating(t, 7) * jitter;
87
- y += neverRepeating(t, 13) * jitter * 0.6;
88
- return { offset: { x, y }, attention: this.attention, boredom };
100
+ // Attention: impulses, a fast cursor and a close cursor; decays slowly.
101
+ this.attention = clamp(this.attention + input.interest + (input.glide ? 0 : clamp(this.targetVel / 10, 0, 1) * dt * 1.6), 0, 1);
102
+ this.attention = damp(this.attention, input.proximity * 0.6, 1.3, dt);
103
+ // Boredom: grows when nothing moves; anything new gradually zeroes it.
104
+ const calm = this.targetVel < 0.05 && now - this.lastSaccade > 3000 && this.attention < 0.25;
105
+ this.boredom = calm
106
+ ? Math.min(1, this.boredom + dt / 7)
107
+ : damp(this.boredom, 0, this.attention > 0.3 ? 6 : 1.5, dt);
108
+ const convergence = clamp((input.proximity - 0.75) / 0.25, 0, 1);
109
+ return this.output(convergence, blink);
110
+ }
111
+ eyeSpring(dt) {
112
+ const p = this.pos;
113
+ // K = 900 is the stiffest spring in the engine: explicit Euler diverges above ~38 ms.
114
+ substep(dt, (h) => {
115
+ p.vx += (K * (this.goal.x - p.x) - C * p.vx) * h;
116
+ p.vy += (K * (this.goal.y - p.y) - C * p.vy) * h;
117
+ p.x += p.vx * h;
118
+ p.y += p.vy * h;
119
+ });
120
+ }
121
+ output(convergence, blink) {
122
+ return {
123
+ x: this.pos.x,
124
+ y: this.pos.y,
125
+ vx: this.pos.vx,
126
+ vy: this.pos.vy,
127
+ attention: this.attention,
128
+ boredom: this.boredom,
129
+ convergence: convergence * convergence * (3 - 2 * convergence),
130
+ blink,
131
+ };
89
132
  }
90
133
  }
package/dist/index.d.ts CHANGED
@@ -1,14 +1,21 @@
1
- export * from "./spring.js";
2
- export * from "./rng.js";
3
- export * from "./noise.js";
4
- export * from "./types.js";
5
- export * from "./render.js";
1
+ export * from "./state-machine.js";
2
+ export * from "./scroll-traveler.js";
3
+ export * from "./motion.js";
4
+ export * from "./delta.js";
5
+ export * from "./rig.js";
6
+ export * from "./parts.js";
7
+ export * from "./gaze.js";
8
+ export * from "./focus.js";
6
9
  export * from "./moods.js";
7
10
  export * from "./reactions.js";
8
- export * from "./gaze.js";
9
- export * from "./fake3d.js";
10
- export * from "./blink.js";
11
+ export * from "./transformations.js";
12
+ export * from "./ticker.js";
13
+ export * from "./interactions.js";
14
+ export * from "./library.js";
11
15
  export * from "./brain.js";
12
- export * from "./bind.js";
13
- export * from "./state-machine.js";
14
- export * from "./scroll-traveler.js";
16
+ export * from "./mount.js";
17
+ export * from "./characters/berry.js";
18
+ export * from "./characters/flame.js";
19
+ export * from "./character.js";
20
+ export * from "./animation.js";
21
+ export * from "./examples.js";