procedural-bot-animations 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +202 -2
- package/dist/animation.d.ts +90 -0
- package/dist/animation.js +396 -0
- package/dist/brain.d.ts +198 -0
- package/dist/brain.js +581 -0
- 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 +48 -0
- package/dist/gaze.js +133 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -0
- 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 +80 -0
- package/dist/moods.js +51 -0
- 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 -0
- package/dist/reactions.js +1855 -0
- package/dist/rig.d.ts +119 -0
- package/dist/rig.js +59 -0
- package/dist/scroll-traveler.d.ts +41 -0
- package/dist/scroll-traveler.js +43 -0
- package/dist/state-machine.d.ts +49 -0
- package/dist/state-machine.js +68 -0
- 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 +51 -4
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
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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 = {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
/** Gaze velocity (units/s): deforms and tilts the eyes. */
|
|
23
|
+
vx: number;
|
|
24
|
+
vy: number;
|
|
25
|
+
attention: number;
|
|
26
|
+
boredom: number;
|
|
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 {
|
|
33
|
+
private pos;
|
|
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;
|
|
40
|
+
private attention;
|
|
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;
|
|
48
|
+
}
|
package/dist/gaze.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
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;
|
|
17
|
+
attention = 0;
|
|
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);
|
|
29
|
+
}
|
|
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 };
|
|
38
|
+
}
|
|
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);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
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 };
|
|
99
|
+
}
|
|
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
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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";
|
|
9
|
+
export * from "./moods.js";
|
|
10
|
+
export * from "./reactions.js";
|
|
11
|
+
export * from "./transformations.js";
|
|
12
|
+
export * from "./ticker.js";
|
|
13
|
+
export * from "./interactions.js";
|
|
14
|
+
export * from "./library.js";
|
|
15
|
+
export * from "./brain.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";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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";
|
|
9
|
+
export * from "./moods.js";
|
|
10
|
+
export * from "./reactions.js";
|
|
11
|
+
export * from "./transformations.js";
|
|
12
|
+
export * from "./ticker.js";
|
|
13
|
+
export * from "./interactions.js";
|
|
14
|
+
export * from "./library.js";
|
|
15
|
+
export * from "./brain.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";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { PartialDelta } from "./delta.js";
|
|
2
|
+
import type { Rng } from "./motion.js";
|
|
3
|
+
/** A gesture is a reaction name or `"hop"` (the physical jump). */
|
|
4
|
+
export type Gesture = string;
|
|
5
|
+
type Draw = readonly {
|
|
6
|
+
gesture: Gesture;
|
|
7
|
+
weight: number;
|
|
8
|
+
}[];
|
|
9
|
+
export type Interactions = {
|
|
10
|
+
/** Cursor entered. */
|
|
11
|
+
hover: Draw;
|
|
12
|
+
/** A single click. */
|
|
13
|
+
click: Draw;
|
|
14
|
+
/** Quick successive clicks (less than 0.7 s apart): on the n-th one, it fires. */
|
|
15
|
+
sequence: readonly {
|
|
16
|
+
n: number;
|
|
17
|
+
reaction: string;
|
|
18
|
+
}[];
|
|
19
|
+
/** Cursor resting on top for more than 2.5 s. */
|
|
20
|
+
cuddle: (s: number) => PartialDelta;
|
|
21
|
+
/** Cursor resting on top for more than 9 s: falls asleep. */
|
|
22
|
+
nap: (s: number) => PartialDelta;
|
|
23
|
+
};
|
|
24
|
+
export declare const HOVER_CUDDLE_MS = 2500;
|
|
25
|
+
export declare const HOVER_NAP_MS = 9000;
|
|
26
|
+
export declare const CLICK_CHAIN_INTERVAL_MS = 700;
|
|
27
|
+
export declare const INTERACTIONS: Record<string, Interactions>;
|
|
28
|
+
/** Draws a weighted gesture, avoiding a repeat of `last` when there is a choice. */
|
|
29
|
+
export declare function pickGesture(list: Draw, rng: Rng, last: Gesture | null): Gesture;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export const HOVER_CUDDLE_MS = 2500;
|
|
2
|
+
export const HOVER_NAP_MS = 9000;
|
|
3
|
+
export const CLICK_CHAIN_INTERVAL_MS = 700;
|
|
4
|
+
const BERRY = {
|
|
5
|
+
hover: [
|
|
6
|
+
{ gesture: "hop", weight: 3 },
|
|
7
|
+
{ gesture: "peek", weight: 3 },
|
|
8
|
+
{ gesture: "shy", weight: 2 },
|
|
9
|
+
{ gesture: "doubleHop", weight: 2 },
|
|
10
|
+
],
|
|
11
|
+
click: [
|
|
12
|
+
{ gesture: "hop", weight: 3 },
|
|
13
|
+
{ gesture: "laugh", weight: 3 },
|
|
14
|
+
{ gesture: "wink", weight: 2 },
|
|
15
|
+
{ gesture: "somersault", weight: 1 },
|
|
16
|
+
],
|
|
17
|
+
sequence: [
|
|
18
|
+
{ n: 3, reaction: "huff" },
|
|
19
|
+
{ n: 5, reaction: "dizzy" },
|
|
20
|
+
{ n: 8, reaction: "hide" },
|
|
21
|
+
],
|
|
22
|
+
// Petted: arc-shaped eyes, a slow sway, leaves relax.
|
|
23
|
+
cuddle: (s) => ({
|
|
24
|
+
happy: 1,
|
|
25
|
+
bodyRotate: Math.sin(s * 2.1) * 3.5,
|
|
26
|
+
bodySy: 0.015 * Math.sin(s * 4.2),
|
|
27
|
+
partL: -6 + Math.sin(s * 2.1) * 4,
|
|
28
|
+
partR: 6 + Math.sin(s * 2.1) * 4,
|
|
29
|
+
partC: Math.sin(s * 2.1 + 0.6) * 5,
|
|
30
|
+
partAmp: -0.5,
|
|
31
|
+
holdGaze: 0.6,
|
|
32
|
+
}),
|
|
33
|
+
// Dozes off under the cursor.
|
|
34
|
+
nap: (s) => ({
|
|
35
|
+
eyeSy: -0.88,
|
|
36
|
+
lidTop: 0.25,
|
|
37
|
+
eyeY: 10,
|
|
38
|
+
bodySy: 0.03 * Math.sin(s * 1.3),
|
|
39
|
+
bodySx: -0.015 * Math.sin(s * 1.3),
|
|
40
|
+
bodyRotate: -4,
|
|
41
|
+
partL: -10,
|
|
42
|
+
partR: 10,
|
|
43
|
+
zzz: 1,
|
|
44
|
+
holdGaze: 1,
|
|
45
|
+
partAmp: -0.7,
|
|
46
|
+
}),
|
|
47
|
+
};
|
|
48
|
+
const FLAME = {
|
|
49
|
+
hover: [
|
|
50
|
+
{ gesture: "warmUp", weight: 3 },
|
|
51
|
+
{ gesture: "hop", weight: 2 },
|
|
52
|
+
{ gesture: "crackle", weight: 2 },
|
|
53
|
+
{ gesture: "peek", weight: 2 },
|
|
54
|
+
],
|
|
55
|
+
click: [
|
|
56
|
+
{ gesture: "hop", weight: 3 },
|
|
57
|
+
{ gesture: "crackle", weight: 2 },
|
|
58
|
+
{ gesture: "laugh", weight: 2 },
|
|
59
|
+
{ gesture: "wink", weight: 2 },
|
|
60
|
+
{ gesture: "sneeze", weight: 1 },
|
|
61
|
+
],
|
|
62
|
+
sequence: [
|
|
63
|
+
{ n: 3, reaction: "heatUp" },
|
|
64
|
+
{ n: 5, reaction: "dizzy" },
|
|
65
|
+
{ n: 8, reaction: "burst" },
|
|
66
|
+
],
|
|
67
|
+
// Coziness: the core lights up gently, tiny embers, arc-shaped eyes.
|
|
68
|
+
cuddle: (s) => ({
|
|
69
|
+
happy: 1,
|
|
70
|
+
core: 0.45 + 0.1 * Math.sin(s * 3),
|
|
71
|
+
embers: 0.35,
|
|
72
|
+
scale: 0.05,
|
|
73
|
+
bodyRotate: Math.sin(s * 1.8) * 3,
|
|
74
|
+
partAmp: -0.3,
|
|
75
|
+
holdGaze: 0.6,
|
|
76
|
+
}),
|
|
77
|
+
// Nap: crouches, loses a little colour, the tongues almost stop.
|
|
78
|
+
nap: (s) => ({
|
|
79
|
+
eyeSy: -0.88,
|
|
80
|
+
lidTop: 0.25,
|
|
81
|
+
eyeY: 10,
|
|
82
|
+
scale: -0.12,
|
|
83
|
+
bodySy: 0.03 * Math.sin(s * 1.3),
|
|
84
|
+
gray: 0.2,
|
|
85
|
+
zzz: 1,
|
|
86
|
+
holdGaze: 1,
|
|
87
|
+
partAmp: -0.7,
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
export const INTERACTIONS = { berry: BERRY, flame: FLAME };
|
|
91
|
+
/** Draws a weighted gesture, avoiding a repeat of `last` when there is a choice. */
|
|
92
|
+
export function pickGesture(list, rng, last) {
|
|
93
|
+
const options = list.length > 1 ? list.filter((o) => o.gesture !== last) : list;
|
|
94
|
+
const total = options.reduce((a, o) => a + o.weight, 0);
|
|
95
|
+
let r = rng() * total;
|
|
96
|
+
for (const o of options)
|
|
97
|
+
if ((r -= o.weight) < 0)
|
|
98
|
+
return o.gesture;
|
|
99
|
+
return options[options.length - 1].gesture;
|
|
100
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ActivityDef, type ReactionDef } from "./reactions.js";
|
|
2
|
+
import type { PartialDelta } from "./delta.js";
|
|
3
|
+
/**
|
|
4
|
+
* Everything a brain can play, looked up by name. Add your own entries to a
|
|
5
|
+
* library made with `createLibrary()` and pass it to the brain or to
|
|
6
|
+
* `mountCharacter`.
|
|
7
|
+
*/
|
|
8
|
+
export type Library = {
|
|
9
|
+
reactions: Record<string, ReactionDef>;
|
|
10
|
+
activities: Record<string, ActivityDef>;
|
|
11
|
+
moods: Record<string, PartialDelta>;
|
|
12
|
+
/** Reactions that cannot be interrupted (object morphs). */
|
|
13
|
+
exclusive: Set<string>;
|
|
14
|
+
};
|
|
15
|
+
/** A fresh library (shallow copies of the built-in tables) that is safe to extend. */
|
|
16
|
+
export declare function createLibrary(): Library;
|
|
17
|
+
/** The built-in library. Treat it as read-only; use `createLibrary()` to customise. */
|
|
18
|
+
export declare const DEFAULT_LIBRARY: Library;
|
package/dist/library.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EXCLUSIVE } from "./transformations.js";
|
|
2
|
+
import { ACTIVITIES, REACTIONS } from "./reactions.js";
|
|
3
|
+
import { MOODS } from "./moods.js";
|
|
4
|
+
/** A fresh library (shallow copies of the built-in tables) that is safe to extend. */
|
|
5
|
+
export function createLibrary() {
|
|
6
|
+
return {
|
|
7
|
+
reactions: { ...REACTIONS },
|
|
8
|
+
activities: { ...ACTIVITIES },
|
|
9
|
+
moods: { ...MOODS },
|
|
10
|
+
exclusive: new Set(EXCLUSIVE),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/** The built-in library. Treat it as read-only; use `createLibrary()` to customise. */
|
|
14
|
+
export const DEFAULT_LIBRARY = createLibrary();
|