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/moods.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moods are continuous and blend by weight (0 to 1). Each one describes the
|
|
3
|
+
* pose at weight 1; the brain smooths the weights, so switching moods is never
|
|
4
|
+
* a hard cut. The expression comes mostly from the eyelids, not the eye size.
|
|
5
|
+
*/
|
|
6
|
+
export declare const MOODS: {
|
|
7
|
+
neutral: {};
|
|
8
|
+
curious: {
|
|
9
|
+
eyeSy: number;
|
|
10
|
+
eyeSx: number;
|
|
11
|
+
partC: number;
|
|
12
|
+
bodyRotate: number;
|
|
13
|
+
partL: number;
|
|
14
|
+
partR: number;
|
|
15
|
+
turn: number;
|
|
16
|
+
};
|
|
17
|
+
focused: {
|
|
18
|
+
lidTop: number;
|
|
19
|
+
lidAngle: number;
|
|
20
|
+
partAmp: number;
|
|
21
|
+
bodySy: number;
|
|
22
|
+
};
|
|
23
|
+
suspicious: {
|
|
24
|
+
lidTop: number;
|
|
25
|
+
lidBottom: number;
|
|
26
|
+
lidAngle: number;
|
|
27
|
+
partL: number;
|
|
28
|
+
partR: number;
|
|
29
|
+
bodyRotate: number;
|
|
30
|
+
turn: number;
|
|
31
|
+
};
|
|
32
|
+
sleepy: {
|
|
33
|
+
lidTop: number;
|
|
34
|
+
lidAngle: number;
|
|
35
|
+
eyeY: number;
|
|
36
|
+
partL: number;
|
|
37
|
+
partR: number;
|
|
38
|
+
partC: number;
|
|
39
|
+
rootY: number;
|
|
40
|
+
partAmp: number;
|
|
41
|
+
};
|
|
42
|
+
proud: {
|
|
43
|
+
happy: number;
|
|
44
|
+
partL: number;
|
|
45
|
+
partR: number;
|
|
46
|
+
bodySy: number;
|
|
47
|
+
eyeY: number;
|
|
48
|
+
};
|
|
49
|
+
downcast: {
|
|
50
|
+
lidTop: number;
|
|
51
|
+
lidAngle: number;
|
|
52
|
+
eyeY: number;
|
|
53
|
+
eyeSx: number;
|
|
54
|
+
partL: number;
|
|
55
|
+
partR: number;
|
|
56
|
+
partC: number;
|
|
57
|
+
rootY: number;
|
|
58
|
+
bodySy: number;
|
|
59
|
+
partAmp: number;
|
|
60
|
+
};
|
|
61
|
+
angry: {
|
|
62
|
+
lidTop: number;
|
|
63
|
+
lidAngle: number;
|
|
64
|
+
eyeSy: number;
|
|
65
|
+
partL: number;
|
|
66
|
+
partR: number;
|
|
67
|
+
bodySy: number;
|
|
68
|
+
};
|
|
69
|
+
smiling: {
|
|
70
|
+
lidBottom: number;
|
|
71
|
+
eyeSy: number;
|
|
72
|
+
partL: number;
|
|
73
|
+
partR: number;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
export type Mood = keyof typeof MOODS;
|
|
77
|
+
export declare const MOOD_NAMES: Mood[];
|
|
78
|
+
export type MoodWeights = Partial<Record<Mood, number>>;
|
|
79
|
+
/** Accepts a name (`'curious'`) or a blend (`{ curious: 0.5, focused: 0.5 }`). */
|
|
80
|
+
export declare function normalizeMood(h: Mood | MoodWeights | undefined): MoodWeights;
|
package/dist/moods.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moods are continuous and blend by weight (0 to 1). Each one describes the
|
|
3
|
+
* pose at weight 1; the brain smooths the weights, so switching moods is never
|
|
4
|
+
* a hard cut. The expression comes mostly from the eyelids, not the eye size.
|
|
5
|
+
*/
|
|
6
|
+
export const MOODS = {
|
|
7
|
+
neutral: {},
|
|
8
|
+
curious: { eyeSy: 0.1, eyeSx: 0.08, partC: 9, bodyRotate: 4, partL: 4, partR: -4, turn: 0.25 },
|
|
9
|
+
focused: { lidTop: 0.22, lidAngle: 6, partAmp: -0.6, bodySy: -0.01 },
|
|
10
|
+
suspicious: {
|
|
11
|
+
lidTop: 0.42,
|
|
12
|
+
lidBottom: 0.12,
|
|
13
|
+
lidAngle: 18,
|
|
14
|
+
partL: -12,
|
|
15
|
+
partR: 12,
|
|
16
|
+
bodyRotate: -3,
|
|
17
|
+
turn: -0.3,
|
|
18
|
+
},
|
|
19
|
+
sleepy: {
|
|
20
|
+
lidTop: 0.58,
|
|
21
|
+
lidAngle: -6,
|
|
22
|
+
eyeY: 10,
|
|
23
|
+
partL: -10,
|
|
24
|
+
partR: 10,
|
|
25
|
+
partC: -6,
|
|
26
|
+
rootY: 6,
|
|
27
|
+
partAmp: -0.5,
|
|
28
|
+
},
|
|
29
|
+
proud: { happy: 1, partL: 12, partR: -12, bodySy: 0.03, eyeY: -6 },
|
|
30
|
+
downcast: {
|
|
31
|
+
lidTop: 0.28,
|
|
32
|
+
lidAngle: -16,
|
|
33
|
+
eyeY: 16,
|
|
34
|
+
eyeSx: -0.06,
|
|
35
|
+
partL: -18,
|
|
36
|
+
partR: 18,
|
|
37
|
+
partC: 10,
|
|
38
|
+
rootY: 10,
|
|
39
|
+
bodySy: -0.04,
|
|
40
|
+
partAmp: -0.6,
|
|
41
|
+
},
|
|
42
|
+
angry: { lidTop: 0.34, lidAngle: 26, eyeSy: -0.05, partL: -6, partR: 6, bodySy: 0.02 },
|
|
43
|
+
smiling: { lidBottom: 0.3, eyeSy: 0.04, partL: 6, partR: -6 },
|
|
44
|
+
};
|
|
45
|
+
export const MOOD_NAMES = Object.keys(MOODS);
|
|
46
|
+
/** Accepts a name (`'curious'`) or a blend (`{ curious: 0.5, focused: 0.5 }`). */
|
|
47
|
+
export function normalizeMood(h) {
|
|
48
|
+
if (!h)
|
|
49
|
+
return { neutral: 1 };
|
|
50
|
+
return typeof h === "string" ? { [h]: 1 } : h;
|
|
51
|
+
}
|
package/dist/motion.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { DeltaField, PartialDelta } from "./delta.js";
|
|
2
|
+
export type Rng = () => number;
|
|
3
|
+
export declare const clamp: (v: number, a: number, b: number) => number;
|
|
4
|
+
export declare const lerp: (a: number, b: number, t: number) => number;
|
|
5
|
+
/** Number formatter for SVG attributes (which reject exponential notation such as `1e-7`). */
|
|
6
|
+
export declare const fmtNum: (v: number) => string;
|
|
7
|
+
/** Smoothstep between `a` and `b`: 0 before `a`, 1 after `b`, a smooth curve in between. */
|
|
8
|
+
export declare function smooth(a: number, b: number, x: number): number;
|
|
9
|
+
/** Moves `current` toward `target` independent of frame rate (exponential smoothing). */
|
|
10
|
+
export declare const damp: (current: number, target: number, rate: number, dt: number) => number;
|
|
11
|
+
export type Spring = {
|
|
12
|
+
a: number;
|
|
13
|
+
v: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Largest step any explicit-Euler integrator takes. The stiffest spring in the
|
|
17
|
+
* engine (the eye, K = 900, C ≈ 37) diverges above ~38 ms; the others tolerate
|
|
18
|
+
* 75 to 110 ms. At 60 fps (16.7 ms) a frame is a single step, so nothing
|
|
19
|
+
* changes there; a longer frame is split into equal steps of at most 20 ms.
|
|
20
|
+
*/
|
|
21
|
+
export declare const MAX_STEP = 0.02;
|
|
22
|
+
/** Calls `fn` with equal sub-steps that add up to `dt`, none above `MAX_STEP`. */
|
|
23
|
+
export declare function substep(dt: number, fn: (h: number) => void): void;
|
|
24
|
+
/** Damped spring: `k` is the stiffness, `c` the damping. Mutates `m` in place. */
|
|
25
|
+
export declare function spring(m: Spring, target: number, k: number, c: number, dt: number): void;
|
|
26
|
+
export type Curve = (t: number) => number;
|
|
27
|
+
export declare const CURVES: {
|
|
28
|
+
linear: (t: number) => number;
|
|
29
|
+
easeIn: (t: number) => number;
|
|
30
|
+
easeOut: (t: number) => number;
|
|
31
|
+
smooth: (t: number) => number;
|
|
32
|
+
};
|
|
33
|
+
export type CurveName = keyof typeof CURVES;
|
|
34
|
+
/** A track keyframe: [time from 0 to 1, value, curve up to the next keyframe]. */
|
|
35
|
+
export type Keyframe = readonly [number, number, CurveName?];
|
|
36
|
+
/**
|
|
37
|
+
* Piecewise interpolation between keyframes. Before the first keyframe the
|
|
38
|
+
* first value holds; after the last, the last. Each keyframe's curve applies
|
|
39
|
+
* up to the next one (default: `smooth`).
|
|
40
|
+
*/
|
|
41
|
+
export declare function track(keyframes: readonly Keyframe[]): Curve;
|
|
42
|
+
/** Keyframe tracks per delta field. */
|
|
43
|
+
export type Tracks = Partial<Record<DeltaField, readonly Keyframe[]>>;
|
|
44
|
+
/** Combines several per-field tracks into one function of progress `t` returning a partial delta. */
|
|
45
|
+
export declare function fromTracks(tracks: Tracks): (t: number) => PartialDelta;
|
|
46
|
+
export type Blink = {
|
|
47
|
+
active: boolean;
|
|
48
|
+
t0: number;
|
|
49
|
+
remaining: number;
|
|
50
|
+
next: number;
|
|
51
|
+
};
|
|
52
|
+
export declare const newBlink: (now: number) => Blink;
|
|
53
|
+
/**
|
|
54
|
+
* Advances the blink and returns how closed the eye is (0 open, 1 closed).
|
|
55
|
+
* Random interval of 1.8 to 6 s; 20% of the time it blinks twice in a row.
|
|
56
|
+
*/
|
|
57
|
+
export declare function stepBlink(p: Blink, now: number, rng: Rng): number;
|
|
58
|
+
export type Jump = {
|
|
59
|
+
y: number;
|
|
60
|
+
vy: number;
|
|
61
|
+
};
|
|
62
|
+
export type Squash = {
|
|
63
|
+
s: number;
|
|
64
|
+
v: number;
|
|
65
|
+
};
|
|
66
|
+
export declare const atRest: (p: Jump) => boolean;
|
|
67
|
+
/** Starts a jump if on the ground. Negative `vy` is up (drawing px per second). */
|
|
68
|
+
export declare function launch(p: Jump, vy?: number): void;
|
|
69
|
+
/**
|
|
70
|
+
* Gravity, a short bounce (32% of the velocity) and a squash on landing.
|
|
71
|
+
* Mutates `p` and `squash` in place.
|
|
72
|
+
*/
|
|
73
|
+
export declare function stepJump(p: Jump, squash: Squash, dt: number): void;
|
|
74
|
+
/** A smoothed value with its own velocity (critically damped spring). */
|
|
75
|
+
export type Damped = {
|
|
76
|
+
x: number;
|
|
77
|
+
v: number;
|
|
78
|
+
};
|
|
79
|
+
export declare const newDamped: (x?: number) => Damped;
|
|
80
|
+
/**
|
|
81
|
+
* Takes `m` to `target` in about `time` seconds, without overshooting and
|
|
82
|
+
* without a jerk: it starts slowly, speeds up and arrives slowly (the
|
|
83
|
+
* velocity is continuous, unlike `damp`). Stable for any `dt` (the SmoothDamp
|
|
84
|
+
* formula).
|
|
85
|
+
*/
|
|
86
|
+
export declare function smoothDamp(m: Damped, target: number, time: number, dt: number): number;
|
|
87
|
+
/** A peak that holds: rises instantly and only starts releasing `holdMs` after the last peak. */
|
|
88
|
+
export type Hold = {
|
|
89
|
+
v: number;
|
|
90
|
+
until: number;
|
|
91
|
+
};
|
|
92
|
+
export declare const newHold: () => Hold;
|
|
93
|
+
export declare function stepHold(s: Hold, input: number, now: number, holdMs: number, releasePerSecond: number, dt: number): number;
|
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
|
+
}
|
package/dist/mount.d.ts
ADDED
|
@@ -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;
|