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/motion.js ADDED
@@ -0,0 +1,167 @@
1
+ /*
2
+ * Basic building blocks of character motion. Pure functions (or ones that
3
+ * only mutate the object they are given), no DOM, so they can be tested in
4
+ * Node and reused by any character.
5
+ */
6
+ export const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
7
+ export const lerp = (a, b, t) => a + (b - a) * t;
8
+ /** Number formatter for SVG attributes (which reject exponential notation such as `1e-7`). */
9
+ export const fmtNum = (v) => (Number.isFinite(v) ? v.toFixed(4) : "0");
10
+ /** Smoothstep between `a` and `b`: 0 before `a`, 1 after `b`, a smooth curve in between. */
11
+ export function smooth(a, b, x) {
12
+ const t = clamp((x - a) / (b - a), 0, 1);
13
+ return t * t * (3 - 2 * t);
14
+ }
15
+ /** Moves `current` toward `target` independent of frame rate (exponential smoothing). */
16
+ export const damp = (current, target, rate, dt) => lerp(current, target, 1 - Math.exp(-rate * dt));
17
+ /**
18
+ * Largest step any explicit-Euler integrator takes. The stiffest spring in the
19
+ * engine (the eye, K = 900, C ≈ 37) diverges above ~38 ms; the others tolerate
20
+ * 75 to 110 ms. At 60 fps (16.7 ms) a frame is a single step, so nothing
21
+ * changes there; a longer frame is split into equal steps of at most 20 ms.
22
+ */
23
+ export const MAX_STEP = 0.02;
24
+ /** Calls `fn` with equal sub-steps that add up to `dt`, none above `MAX_STEP`. */
25
+ export function substep(dt, fn) {
26
+ if (!(dt > 0))
27
+ return;
28
+ const n = Math.ceil(dt / MAX_STEP - 1e-9);
29
+ const h = dt / n;
30
+ for (let i = 0; i < n; i++)
31
+ fn(h);
32
+ }
33
+ /** Damped spring: `k` is the stiffness, `c` the damping. Mutates `m` in place. */
34
+ export function spring(m, target, k, c, dt) {
35
+ substep(dt, (h) => {
36
+ m.v += (k * (target - m.a) - c * m.v) * h;
37
+ m.a += m.v * h;
38
+ });
39
+ }
40
+ export const CURVES = {
41
+ linear: (t) => t,
42
+ easeIn: (t) => t * t * t,
43
+ easeOut: (t) => 1 - (1 - t) ** 3,
44
+ smooth: (t) => t * t * (3 - 2 * t),
45
+ };
46
+ /**
47
+ * Piecewise interpolation between keyframes. Before the first keyframe the
48
+ * first value holds; after the last, the last. Each keyframe's curve applies
49
+ * up to the next one (default: `smooth`).
50
+ */
51
+ export function track(keyframes) {
52
+ return (t) => {
53
+ if (t <= keyframes[0][0])
54
+ return keyframes[0][1];
55
+ for (let i = 0; i < keyframes.length - 1; i++) {
56
+ const [t0, v0, name = "smooth"] = keyframes[i];
57
+ const [t1, v1] = keyframes[i + 1];
58
+ if (t <= t1)
59
+ return lerp(v0, v1, CURVES[name]((t - t0) / (t1 - t0 || 1)));
60
+ }
61
+ return keyframes[keyframes.length - 1][1];
62
+ };
63
+ }
64
+ /** Combines several per-field tracks into one function of progress `t` returning a partial delta. */
65
+ export function fromTracks(tracks) {
66
+ const fns = Object.entries(tracks).map(([k, c]) => [k, track(c)]);
67
+ return (t) => {
68
+ const d = {};
69
+ for (const [k, f] of fns)
70
+ d[k] = f(t);
71
+ return d;
72
+ };
73
+ }
74
+ const BLINK_DURATION = 150;
75
+ export const newBlink = (now) => ({
76
+ active: false,
77
+ t0: 0,
78
+ remaining: 0,
79
+ next: now + 1500,
80
+ });
81
+ /**
82
+ * Advances the blink and returns how closed the eye is (0 open, 1 closed).
83
+ * Random interval of 1.8 to 6 s; 20% of the time it blinks twice in a row.
84
+ */
85
+ export function stepBlink(p, now, rng) {
86
+ if (!p.active && now >= p.next) {
87
+ p.active = true;
88
+ p.t0 = now;
89
+ p.remaining = rng() < 0.2 ? 2 : 1;
90
+ }
91
+ if (!p.active)
92
+ return 0;
93
+ const phase = (now - p.t0) / BLINK_DURATION;
94
+ if (phase >= 1) {
95
+ p.remaining--;
96
+ if (p.remaining > 0)
97
+ p.t0 = now + 90;
98
+ else {
99
+ p.active = false;
100
+ p.next = now + 1800 + rng() * 4200;
101
+ }
102
+ return 0;
103
+ }
104
+ return phase > 0 ? Math.sin(phase * Math.PI) : 0;
105
+ }
106
+ const GRAVITY = 5200;
107
+ export const atRest = (p) => p.y === 0 && p.vy === 0;
108
+ /** Starts a jump if on the ground. Negative `vy` is up (drawing px per second). */
109
+ export function launch(p, vy = -1500) {
110
+ if (atRest(p))
111
+ p.vy = vy;
112
+ }
113
+ /**
114
+ * Gravity, a short bounce (32% of the velocity) and a squash on landing.
115
+ * Mutates `p` and `squash` in place.
116
+ */
117
+ export function stepJump(p, squash, dt) {
118
+ substep(dt, (h) => stepJumpOnce(p, squash, h));
119
+ }
120
+ function stepJumpOnce(p, squash, dt) {
121
+ if (!atRest(p)) {
122
+ p.vy += GRAVITY * dt;
123
+ p.y += p.vy * dt;
124
+ if (p.y >= 0) {
125
+ const impact = Math.abs(p.vy);
126
+ p.y = 0;
127
+ if (impact > 500) {
128
+ p.vy = -impact * 0.32;
129
+ squash.s = Math.min(0.16, impact / 9000);
130
+ squash.v = 0;
131
+ }
132
+ else
133
+ p.vy = 0;
134
+ }
135
+ }
136
+ squash.v += (-320 * squash.s - 15 * squash.v) * dt;
137
+ squash.s += squash.v * dt;
138
+ }
139
+ export const newDamped = (x = 0) => ({ x, v: 0 });
140
+ /**
141
+ * Takes `m` to `target` in about `time` seconds, without overshooting and
142
+ * without a jerk: it starts slowly, speeds up and arrives slowly (the
143
+ * velocity is continuous, unlike `damp`). Stable for any `dt` (the SmoothDamp
144
+ * formula).
145
+ */
146
+ export function smoothDamp(m, target, time, dt) {
147
+ if (dt <= 0)
148
+ return m.x;
149
+ const w = 2 / Math.max(time, 1e-3);
150
+ const x = w * dt;
151
+ const e = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x);
152
+ const delta = m.x - target;
153
+ const tmp = (m.v + w * delta) * dt;
154
+ m.v = (m.v - w * tmp) * e;
155
+ m.x = target + (delta + tmp) * e;
156
+ return m.x;
157
+ }
158
+ export const newHold = () => ({ v: 0, until: 0 });
159
+ export function stepHold(s, input, now, holdMs, releasePerSecond, dt) {
160
+ if (input >= s.v) {
161
+ s.v = input;
162
+ s.until = now + holdMs;
163
+ }
164
+ else if (now > s.until)
165
+ s.v = Math.max(input, s.v - releasePerSecond * dt);
166
+ return s.v;
167
+ }
@@ -0,0 +1,90 @@
1
+ import { Brain } from "./brain.js";
2
+ import { type GazeMode, type Reason } from "./focus.js";
3
+ import type { Interactions } from "./interactions.js";
4
+ import type { Library } from "./library.js";
5
+ import { type EyeStyle, type Rig, type Role } from "./rig.js";
6
+ /** What the character looks at. A point or a function gives window px. */
7
+ export type GazeTarget = "pointer" | "free" | "focus" | "none" | {
8
+ x: number;
9
+ y: number;
10
+ } | (() => {
11
+ x: number;
12
+ y: number;
13
+ } | null);
14
+ export type MountEvent = {
15
+ type: "hover" | "click";
16
+ name: string;
17
+ };
18
+ export interface MountOptions {
19
+ rig: Rig;
20
+ /** Width in px; default: fills the host width. Below 48 px, it loses appendage sway and accessories. */
21
+ size?: number;
22
+ /** Body colour (any CSS colour). Default `#e11d2e`. */
23
+ color?: string;
24
+ /** Eye colour. Default `#0a0a0a`. */
25
+ eyeColor?: string;
26
+ /** Colour of the spark bursts' alternate sparks (the "success" green). Default `#157a3c`. */
27
+ accentColor?: string;
28
+ /** Colour of the accessories (`!`, `?`, `…`, zzz, lamp base). Default `#0a0a0a`. */
29
+ inkColor?: string;
30
+ /** Lamp bulb. Default `#facc15`. */
31
+ lampColor?: string;
32
+ /** Alert rays. Default `#f5b301`. */
33
+ raysColor?: string;
34
+ /** What the body mixes into when it loses its colour (`gray`), and the smoke. Default `#8a8a8a`. */
35
+ grayColor?: string;
36
+ eyeStyle?: EyeStyle;
37
+ /** A mood name or a blend of weights. Default: neutral. */
38
+ mood?: string | Record<string, number>;
39
+ activity?: string | null;
40
+ /** Where to look (default `'pointer'`). `null` and `'none'` only wander. */
41
+ gaze?: GazeTarget | null;
42
+ /** Hover hops and click/touch reacts. Off, the character is decorative. Default `true`. */
43
+ interactive?: boolean;
44
+ /** Appendage tips bend with physics. Default `true`. */
45
+ bend?: boolean;
46
+ /** `true` forces reduced motion; `'auto'` (default) follows `prefers-reduced-motion`; `false` ignores it. */
47
+ reducedMotion?: boolean | "auto";
48
+ /** Which appendages are drawn (default: all of the rig's). */
49
+ appendages?: Role[];
50
+ /** Light core always on (only rigs with `core`). */
51
+ glow?: boolean;
52
+ /** Alert rays always visible (streak milestones). */
53
+ fixedRays?: boolean;
54
+ /** Accessible name. Without it, the SVG is `aria-hidden`. */
55
+ label?: string;
56
+ className?: string;
57
+ library?: Library;
58
+ interactions?: Interactions;
59
+ /** Called with the gesture drawn when the cursor enters or someone clicks. */
60
+ onEvent?: (e: MountEvent) => void;
61
+ }
62
+ export type GazeState = {
63
+ attention: number;
64
+ boredom: number;
65
+ /** In `free` and `focus` modes: wandering or with something holding the gaze. */
66
+ mode: GazeMode | null;
67
+ reason: Reason | null;
68
+ /** How the eye is reaching the target: gliding or in a saccade. */
69
+ gliding: boolean;
70
+ };
71
+ export interface MountedCharacter {
72
+ readonly brain: Brain;
73
+ /** The current root `<svg>` (it is rebuilt when the rig changes). */
74
+ readonly svg: SVGSVGElement;
75
+ update(patch: Partial<MountOptions>): void;
76
+ /** Fires a reaction by name; `false` if the library does not know it. */
77
+ react(name: string): boolean;
78
+ jump(): void;
79
+ /** Reaction in progress right now, for debugging. */
80
+ currentReaction(): string | null;
81
+ /** Gaze state in the last frame, for the debug meter. */
82
+ getGazeState(): GazeState;
83
+ destroy(): void;
84
+ }
85
+ /**
86
+ * Mounts an animated character inside `host`. The returned handle updates the
87
+ * options live (`update`), fires reactions by hand and cleans everything up
88
+ * (`destroy`).
89
+ */
90
+ export declare function mountCharacter(host: HTMLElement, options: MountOptions): MountedCharacter;