@klex/mascot 0.0.0-stage → 0.1.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.
@@ -0,0 +1,144 @@
1
+ import { spring } from 'motion';
2
+
3
+ import { createCalling } from './calling';
4
+ import { REST_EMOTE } from './emotes';
5
+ import { createIdle } from './idle';
6
+ import { createWorking } from './working';
7
+
8
+ export const ACTIVITIES = [
9
+ { id: 'idle', name: 'Idle' },
10
+ { id: 'sleeping', name: 'Sleeping' },
11
+ { id: 'working', name: 'Working' },
12
+ { id: 'calling', name: 'On a call' },
13
+ ] as const;
14
+ export type KlexActivity = (typeof ACTIVITIES)[number]['id'];
15
+
16
+ function activityPose(activity: KlexActivity | false, time: number) {
17
+ const phase = (time * Math.PI * 2) / 6;
18
+ if (activity === 'sleeping') {
19
+ return {
20
+ ...REST_EMOTE,
21
+ compression: 0.1 + 0.025 * (1 - Math.cos(phase)),
22
+ headTilt: -8 + 1.5 * Math.sin(phase),
23
+ headSway: -3 + Math.sin(phase),
24
+ nod: 0.65 + 0.15 * Math.sin(phase),
25
+ blinkLeft: 1,
26
+ blinkRight: 1,
27
+ };
28
+ }
29
+ if (activity === 'working') {
30
+ return {
31
+ ...REST_EMOTE,
32
+ compression: 0.025,
33
+ nod: 0.15,
34
+ lookY: 2,
35
+ };
36
+ }
37
+ if (activity === 'calling')
38
+ return {
39
+ ...REST_EMOTE,
40
+ headTilt: 7,
41
+ headSway: 1,
42
+ nod: 0.1,
43
+ lookX: 1.5,
44
+ lookY: -1.5,
45
+ };
46
+ return REST_EMOTE;
47
+ }
48
+
49
+ // Blend persistent poses on the existing ticker, including interrupted changes.
50
+ export function createActivity() {
51
+ const idle = createIdle();
52
+ const working = createWorking();
53
+ const calling = createCalling();
54
+ const weights = { idle: 0, sleeping: 0, working: 0, calling: 0 };
55
+ let time = Math.random() * 6;
56
+ let laptopScale = 0;
57
+ let laptopVisible = false;
58
+ let laptopSpring: ReturnType<typeof spring> | null = null;
59
+ let laptopTime = 0;
60
+
61
+ return {
62
+ get active() {
63
+ return (
64
+ laptopSpring !== null || ACTIVITIES.some(({ id }) => weights[id] > 0)
65
+ );
66
+ },
67
+ get laptopScale() {
68
+ return laptopScale;
69
+ },
70
+ snapLaptop(visible: boolean) {
71
+ laptopVisible = visible;
72
+ laptopScale = Number(visible);
73
+ laptopSpring = null;
74
+ },
75
+ get workingBob() {
76
+ const { laptopFloat, laptopTap } = working.pose();
77
+ return (
78
+ (Math.sin((time * Math.PI * 2) / 3.6) * 2.5 * laptopFloat + laptopTap) *
79
+ weights.working
80
+ );
81
+ },
82
+ get callingExpression() {
83
+ return calling.expression;
84
+ },
85
+ get thinking() {
86
+ return working.thinking;
87
+ },
88
+ advance(dt: number, activity: KlexActivity | false) {
89
+ time += dt;
90
+ const showLaptop = activity === 'working';
91
+ if (showLaptop) working.advance(dt);
92
+ if (activity === 'calling') calling.advance(dt);
93
+ if (showLaptop !== laptopVisible) {
94
+ laptopVisible = showLaptop;
95
+ laptopTime = 0;
96
+ laptopSpring = spring({
97
+ keyframes: [laptopScale, Number(showLaptop)],
98
+ bounce: showLaptop ? 0.6 : 0,
99
+ visualDuration: showLaptop ? 0.28 : 0.12,
100
+ });
101
+ }
102
+ if (laptopSpring) {
103
+ laptopTime += dt;
104
+ const frame = laptopSpring.next(laptopTime * 1000);
105
+ laptopScale = frame.value;
106
+ if (frame.done) laptopSpring = null;
107
+ }
108
+ idle.advance(dt, activity === 'idle');
109
+ const decay = Math.exp(-6 * dt);
110
+ for (const { id } of ACTIVITIES) {
111
+ const target = activity === id ? 1 : 0;
112
+ const next = target + (weights[id] - target) * decay;
113
+ weights[id] = Math.abs(next - target) < 0.0001 ? target : next;
114
+ }
115
+ },
116
+ pose(staticActivity?: KlexActivity | false) {
117
+ // Reduced motion keeps a recognizable pose without a running clock.
118
+ if (staticActivity !== undefined)
119
+ return activityPose(staticActivity, 1.5);
120
+ const resting = idle.pose(weights.idle);
121
+ const sleeping = activityPose('sleeping', time);
122
+ const workingPose = working.pose();
123
+ const callingPose = calling.pose();
124
+ const pose = { ...REST_EMOTE };
125
+ for (const key of [
126
+ 'compression',
127
+ 'headTilt',
128
+ 'headSway',
129
+ 'nod',
130
+ 'lookX',
131
+ 'lookY',
132
+ 'blinkLeft',
133
+ 'blinkRight',
134
+ ] as const) {
135
+ pose[key] =
136
+ resting[key] +
137
+ sleeping[key] * weights.sleeping +
138
+ workingPose[key] * weights.working +
139
+ callingPose[key] * weights.calling;
140
+ }
141
+ return pose;
142
+ },
143
+ };
144
+ }
package/src/calling.ts ADDED
@@ -0,0 +1,85 @@
1
+ import { emotePose } from './emotes';
2
+
3
+ const phases = ['listening', 'explaining', 'agreeing'] as const;
4
+ const between = (min: number, max: number) => min + Math.random() * (max - min);
5
+
6
+ // Conversation gestures are ambient, not an indication of actual audio activity.
7
+ export function createCalling() {
8
+ let phase = phases[Math.floor(Math.random() * phases.length)];
9
+ const duration = () =>
10
+ phase === 'listening'
11
+ ? between(3.5, 6)
12
+ : phase === 'explaining'
13
+ ? between(2.5, 4.5)
14
+ : between(1.3, 2);
15
+ let phaseDuration = duration();
16
+ let time = 0;
17
+ let direction = Math.random() < 0.5 ? -1 : 1;
18
+ let blinkIn = between(2, 4);
19
+ let blinkTime = Infinity;
20
+ let beatIn = 0;
21
+ let beatTime = Infinity;
22
+ const pose = {
23
+ compression: 0.015,
24
+ headTilt: 5,
25
+ headSway: 0,
26
+ nod: 0.1,
27
+ lookX: 1,
28
+ lookY: -1,
29
+ };
30
+ return {
31
+ get expression() {
32
+ return phase === 'agreeing' ? ('happy' as const) : ('neutral' as const);
33
+ },
34
+ advance(dt: number) {
35
+ time += dt;
36
+ blinkTime += dt;
37
+ blinkIn -= dt;
38
+ beatTime += dt;
39
+ if (time >= phaseDuration) {
40
+ const choices = phases.filter((next) => next !== phase);
41
+ phase = choices[Math.floor(Math.random() * choices.length)];
42
+ phaseDuration = duration();
43
+ time = 0;
44
+ direction = Math.random() < 0.5 ? -1 : 1;
45
+ beatIn = 0;
46
+ beatTime = Infinity;
47
+ }
48
+ if (blinkIn <= 0) {
49
+ blinkTime = 0;
50
+ blinkIn = between(2, 4);
51
+ }
52
+ if (phase === 'explaining') {
53
+ beatIn -= dt;
54
+ if (beatIn <= 0) {
55
+ beatTime = 0;
56
+ beatIn = Math.random() < 0.3 ? between(0.6, 1) : between(0.22, 0.4);
57
+ }
58
+ }
59
+ const beat =
60
+ phase === 'explaining' && beatTime < 0.28
61
+ ? Math.sin((beatTime / 0.28) * Math.PI)
62
+ : 0;
63
+ const nod =
64
+ phase === 'agreeing'
65
+ ? Math.sin(Math.min(time / 1.15, 1) * Math.PI * 2) ** 2
66
+ : 0;
67
+ const listening = phase === 'listening';
68
+ const target = {
69
+ compression: 0.015 + beat * 0.018 + nod * 0.025,
70
+ headTilt: listening ? 7 : 3 + direction * beat * 3,
71
+ headSway: listening ? 1 : direction * beat * 1.3,
72
+ nod: 0.1 + nod * 0.9 + beat * 0.22,
73
+ lookX: listening ? 1.5 : direction * 3,
74
+ lookY: listening ? -1.5 : phase === 'agreeing' ? 1 : -3,
75
+ };
76
+ const blend = 1 - Math.exp(-12 * dt);
77
+ for (const key of Object.keys(pose) as (keyof typeof pose)[])
78
+ pose[key] += (target[key] - pose[key]) * blend;
79
+ },
80
+ pose() {
81
+ const blink = emotePose('blink', blinkTime / 0.3).blinkLeft;
82
+ return { ...pose, blinkLeft: blink, blinkRight: blink };
83
+ },
84
+ };
85
+ }