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.
- package/README.md +167 -47
- package/dist/animation.d.ts +90 -0
- package/dist/animation.js +396 -0
- package/dist/brain.d.ts +194 -35
- package/dist/brain.js +578 -54
- 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 +41 -53
- package/dist/gaze.js +125 -82
- package/dist/index.d.ts +18 -11
- package/dist/index.js +18 -11
- 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 +78 -46
- package/dist/moods.js +48 -73
- 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 -44
- package/dist/reactions.js +1851 -116
- package/dist/rig.d.ts +119 -0
- package/dist/rig.js +59 -0
- package/dist/scroll-traveler.js +1 -1
- 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 +7 -11
- package/dist/bind.d.ts +0 -27
- package/dist/bind.js +0 -42
- package/dist/blink.d.ts +0 -25
- package/dist/blink.js +0 -48
- package/dist/fake3d.d.ts +0 -20
- package/dist/fake3d.js +0 -24
- package/dist/noise.d.ts +0 -16
- package/dist/noise.js +0 -17
- package/dist/render.d.ts +0 -29
- package/dist/render.js +0 -97
- package/dist/rng.d.ts +0 -10
- package/dist/rng.js +0 -18
- package/dist/spring.d.ts +0 -21
- package/dist/spring.js +0 -33
- package/dist/types.d.ts +0 -63
- package/dist/types.js +0 -46
package/dist/index.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
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 "./
|
|
9
|
-
export * from "./
|
|
10
|
-
export * from "./
|
|
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 "./
|
|
13
|
-
export * from "./
|
|
14
|
-
export * from "./
|
|
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();
|
package/dist/moods.d.ts
CHANGED
|
@@ -1,48 +1,80 @@
|
|
|
1
|
-
import type { PoseDelta } from "./types.js";
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* replacing the other outright — the thing Open Bot Avatar's three-string
|
|
7
|
-
* `mood` can't do.
|
|
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.
|
|
8
5
|
*/
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
CHANGED
|
@@ -1,76 +1,51 @@
|
|
|
1
|
-
import { clamp, lerp } from "./spring.js";
|
|
2
|
-
/** A starting set covering most app states; add your own with the same shape. */
|
|
3
|
-
export const builtinMoods = {
|
|
4
|
-
neutral: {},
|
|
5
|
-
curious: { eyeScaleY: 1.06, eyeOffsetY: -0.3, bodyRotate: 1.5 },
|
|
6
|
-
focused: { eyeScaleY: 0.9, bodyScaleY: 0.99 },
|
|
7
|
-
suspicious: { eyeScaleY: 0.58, eyeRotate: -8, bodyY: 0.6 },
|
|
8
|
-
sleepy: { eyeScaleY: 0.5, eyeShut: 0.3, bodyY: 1.2 },
|
|
9
|
-
proud: { eyeArcAmount: 1, bodyScaleY: 1.03 },
|
|
10
|
-
sad: { eyeScaleY: 0.8, eyeOffsetY: 1, bodyY: 2 },
|
|
11
|
-
};
|
|
12
1
|
/**
|
|
13
|
-
*
|
|
14
|
-
* pose
|
|
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.
|
|
15
5
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
/** Sets (or clears, at weight 0 / omission) target weights for the named moods. Others are left as they are. */
|
|
63
|
-
set(weights) {
|
|
64
|
-
Object.assign(this.target, weights);
|
|
65
|
-
}
|
|
66
|
-
update(dt) {
|
|
67
|
-
const names = new Set([...Object.keys(this.current), ...Object.keys(this.target)]);
|
|
68
|
-
for (const name of names) {
|
|
69
|
-
const cur = this.current[name] ?? 0;
|
|
70
|
-
const target = this.target[name] ?? 0;
|
|
71
|
-
const next = lerp(cur, target, 1 - Math.exp(-this.rate * dt));
|
|
72
|
-
this.current[name] = Math.abs(next) < 1e-4 && target === 0 ? 0 : next;
|
|
73
|
-
}
|
|
74
|
-
return blendMoods(this.current, this.moods);
|
|
75
|
-
}
|
|
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;
|
|
76
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;
|