@klex/mascot 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/LICENSE-TWEMOJI.txt +393 -0
- package/README.md +160 -2
- package/package.json +68 -4
- package/src/activity.ts +144 -0
- package/src/calling.ts +85 -0
- package/src/emotes.ts +582 -0
- package/src/extras.css +50 -0
- package/src/extras.tsx +241 -0
- package/src/eyes.tsx +179 -0
- package/src/gaze.ts +48 -0
- package/src/geometry.ts +103 -0
- package/src/glide.ts +848 -0
- package/src/ground-grid.tsx +51 -0
- package/src/idle.ts +61 -0
- package/src/index.ts +32 -0
- package/src/klex.tsx +460 -0
- package/src/laptop.tsx +41 -0
- package/src/name-tag.css +26 -0
- package/src/observe-visibility.ts +42 -0
- package/src/presets.ts +247 -0
- package/src/speech.css +21 -0
- package/src/speech.tsx +129 -0
- package/src/svg.ts +56 -0
- package/src/transitions.ts +5 -0
- package/src/working.ts +99 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type Listener = (visible: boolean) => void;
|
|
2
|
+
const targets = new Map<
|
|
3
|
+
Element,
|
|
4
|
+
{ visible?: boolean; listeners: Set<Listener> }
|
|
5
|
+
>();
|
|
6
|
+
let observer: IntersectionObserver | undefined;
|
|
7
|
+
|
|
8
|
+
// Characters in one scene share its viewport element; late subscribers get the
|
|
9
|
+
// last known visibility immediately.
|
|
10
|
+
export function observeVisibility(element: Element, listener: Listener) {
|
|
11
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
12
|
+
listener(true);
|
|
13
|
+
return () => {};
|
|
14
|
+
}
|
|
15
|
+
observer ??= new IntersectionObserver((entries) => {
|
|
16
|
+
for (const entry of entries) {
|
|
17
|
+
const target = targets.get(entry.target);
|
|
18
|
+
if (!target) continue;
|
|
19
|
+
target.visible = entry.isIntersecting;
|
|
20
|
+
for (const notify of target.listeners) notify(entry.isIntersecting);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
let target = targets.get(element);
|
|
24
|
+
if (!target) {
|
|
25
|
+
target = { listeners: new Set() };
|
|
26
|
+
targets.set(element, target);
|
|
27
|
+
observer.observe(element);
|
|
28
|
+
}
|
|
29
|
+
target.listeners.add(listener);
|
|
30
|
+
if (target.visible !== undefined) listener(target.visible);
|
|
31
|
+
return () => {
|
|
32
|
+
target.listeners.delete(listener);
|
|
33
|
+
if (!target.listeners.size) {
|
|
34
|
+
observer?.unobserve(element);
|
|
35
|
+
targets.delete(element);
|
|
36
|
+
}
|
|
37
|
+
if (!targets.size) {
|
|
38
|
+
observer?.disconnect();
|
|
39
|
+
observer = undefined;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
package/src/presets.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
export type BodyShape = {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
outline: readonly (readonly [number, number])[];
|
|
5
|
+
eyeX: number;
|
|
6
|
+
eyeY: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type KlexPalette = {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
body: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Matching rig points and a shared 160×140 frame keep every form compatible.
|
|
16
|
+
export const BODY_SHAPES = [
|
|
17
|
+
{
|
|
18
|
+
id: 'klex',
|
|
19
|
+
name: 'Klex',
|
|
20
|
+
eyeX: 23.294,
|
|
21
|
+
eyeY: 56.824,
|
|
22
|
+
// Logo contour from packages/ui/src/components/ui/klex-logo.tsx, scaled
|
|
23
|
+
// uniformly to 110px high, with three sole points at y=128 for ground contact.
|
|
24
|
+
outline: [
|
|
25
|
+
[94.855, 17.957],
|
|
26
|
+
[97.196, 18.073],
|
|
27
|
+
[100.237, 18.406],
|
|
28
|
+
[103.049, 18.981],
|
|
29
|
+
[105.852, 19.8],
|
|
30
|
+
[108.557, 20.865],
|
|
31
|
+
[111.159, 22.171],
|
|
32
|
+
[113.63, 23.706],
|
|
33
|
+
[115.955, 25.456],
|
|
34
|
+
[118.12, 27.4],
|
|
35
|
+
[120.114, 29.519],
|
|
36
|
+
[121.931, 31.791],
|
|
37
|
+
[123.569, 34.195],
|
|
38
|
+
[125.028, 36.711],
|
|
39
|
+
[126.308, 39.323],
|
|
40
|
+
[127.412, 42.014],
|
|
41
|
+
[128.342, 44.769],
|
|
42
|
+
[129.103, 47.576],
|
|
43
|
+
[129.696, 50.424],
|
|
44
|
+
[130.124, 53.3],
|
|
45
|
+
[130.389, 56.196],
|
|
46
|
+
[130.499, 59.102],
|
|
47
|
+
[130.426, 62.009],
|
|
48
|
+
[130.301, 64.913],
|
|
49
|
+
[130.16, 67.817],
|
|
50
|
+
[129.991, 70.719],
|
|
51
|
+
[129.795, 73.619],
|
|
52
|
+
[129.571, 76.517],
|
|
53
|
+
[129.314, 79.413],
|
|
54
|
+
[129.024, 82.306],
|
|
55
|
+
[128.697, 85.194],
|
|
56
|
+
[128.329, 88.078],
|
|
57
|
+
[127.917, 90.956],
|
|
58
|
+
[127.455, 93.826],
|
|
59
|
+
[126.939, 96.687],
|
|
60
|
+
[126.362, 99.536],
|
|
61
|
+
[125.716, 102.371],
|
|
62
|
+
[124.991, 105.186],
|
|
63
|
+
[124.176, 107.977],
|
|
64
|
+
[123.255, 110.735],
|
|
65
|
+
[122.21, 113.448],
|
|
66
|
+
[121.016, 116.1],
|
|
67
|
+
[119.645, 118.666],
|
|
68
|
+
[118.055, 121.104],
|
|
69
|
+
[116.207, 123.354],
|
|
70
|
+
[114.037, 125.309],
|
|
71
|
+
[111.587, 126.863],
|
|
72
|
+
[108.68, 128],
|
|
73
|
+
[106.295, 128],
|
|
74
|
+
[101.436, 128],
|
|
75
|
+
[97.718, 124.739],
|
|
76
|
+
[93.627, 122.862],
|
|
77
|
+
[89.126, 122.189],
|
|
78
|
+
[84.858, 123.73],
|
|
79
|
+
[80.763, 125.432],
|
|
80
|
+
[76.514, 126.789],
|
|
81
|
+
[71.962, 127.006],
|
|
82
|
+
[67.8, 125.264],
|
|
83
|
+
[63.978, 122.989],
|
|
84
|
+
[59.954, 121.07],
|
|
85
|
+
[55.401, 120.392],
|
|
86
|
+
[51.257, 122.241],
|
|
87
|
+
[47.072, 123.721],
|
|
88
|
+
[42.751, 124.799],
|
|
89
|
+
[38.249, 125.228],
|
|
90
|
+
[33.988, 124.661],
|
|
91
|
+
[29.268, 122.234],
|
|
92
|
+
[30.515, 117.838],
|
|
93
|
+
[34.329, 115.181],
|
|
94
|
+
[37.101, 111.707],
|
|
95
|
+
[39.694, 108.08],
|
|
96
|
+
[41.92, 104.218],
|
|
97
|
+
[43.888, 100.221],
|
|
98
|
+
[45.621, 96.117],
|
|
99
|
+
[47.157, 91.936],
|
|
100
|
+
[48.522, 87.696],
|
|
101
|
+
[49.738, 83.411],
|
|
102
|
+
[50.824, 79.092],
|
|
103
|
+
[51.794, 74.745],
|
|
104
|
+
[52.662, 70.377],
|
|
105
|
+
[53.434, 65.991],
|
|
106
|
+
[54.134, 61.592],
|
|
107
|
+
[54.714, 57.176],
|
|
108
|
+
[55.399, 52.775],
|
|
109
|
+
[56.325, 48.417],
|
|
110
|
+
[57.513, 44.122],
|
|
111
|
+
[59.023, 39.929],
|
|
112
|
+
[60.911, 35.89],
|
|
113
|
+
[63.232, 32.079],
|
|
114
|
+
[66.021, 28.594],
|
|
115
|
+
[69.277, 25.538],
|
|
116
|
+
[72.948, 22.997],
|
|
117
|
+
[76.949, 21.011],
|
|
118
|
+
[81.161, 19.568],
|
|
119
|
+
[85.559, 18.621],
|
|
120
|
+
[89.835, 18.1],
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 'knubbel',
|
|
125
|
+
name: 'Blob',
|
|
126
|
+
eyeX: 23,
|
|
127
|
+
eyeY: 65,
|
|
128
|
+
outline: [
|
|
129
|
+
[88, 28],
|
|
130
|
+
[115, 33],
|
|
131
|
+
[135, 52],
|
|
132
|
+
[139, 79],
|
|
133
|
+
[129, 109],
|
|
134
|
+
[112, 126],
|
|
135
|
+
[92, 128],
|
|
136
|
+
[68, 128],
|
|
137
|
+
[46, 128],
|
|
138
|
+
[28, 117],
|
|
139
|
+
[23, 92],
|
|
140
|
+
[30, 64],
|
|
141
|
+
[47, 39],
|
|
142
|
+
[67, 28],
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 'slime',
|
|
147
|
+
name: 'Slime',
|
|
148
|
+
eyeX: 31,
|
|
149
|
+
eyeY: 87,
|
|
150
|
+
outline: [
|
|
151
|
+
[100, 58],
|
|
152
|
+
[123, 64],
|
|
153
|
+
[140, 78],
|
|
154
|
+
[143, 96],
|
|
155
|
+
[133, 114],
|
|
156
|
+
[115, 127],
|
|
157
|
+
[92, 128],
|
|
158
|
+
[68, 128],
|
|
159
|
+
[42, 128],
|
|
160
|
+
[22, 122],
|
|
161
|
+
[15, 108],
|
|
162
|
+
[26, 89],
|
|
163
|
+
[51, 78],
|
|
164
|
+
[75, 65],
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
id: 'tropfen',
|
|
169
|
+
name: 'Drop',
|
|
170
|
+
eyeX: 18,
|
|
171
|
+
eyeY: 63,
|
|
172
|
+
outline: [
|
|
173
|
+
[92, 12],
|
|
174
|
+
[108, 26],
|
|
175
|
+
[115, 50],
|
|
176
|
+
[128, 78],
|
|
177
|
+
[131, 108],
|
|
178
|
+
[114, 126],
|
|
179
|
+
[92, 128],
|
|
180
|
+
[68, 128],
|
|
181
|
+
[46, 128],
|
|
182
|
+
[31, 117],
|
|
183
|
+
[30, 91],
|
|
184
|
+
[47, 68],
|
|
185
|
+
[64, 43],
|
|
186
|
+
[77, 21],
|
|
187
|
+
],
|
|
188
|
+
},
|
|
189
|
+
] as const satisfies readonly BodyShape[];
|
|
190
|
+
|
|
191
|
+
export const PALETTES = [
|
|
192
|
+
{ id: 'porzellan', name: 'Porcelain', body: '#2559fe' },
|
|
193
|
+
{ id: 'pfirsich', name: 'Peach', body: '#ffbd91' },
|
|
194
|
+
{ id: 'lavendel', name: 'Lavender', body: '#d9c4ff' },
|
|
195
|
+
{ id: 'pistazie', name: 'Pistachio', body: '#c8e7a7' },
|
|
196
|
+
{ id: 'rose', name: 'Rose', body: '#ffbdd7' },
|
|
197
|
+
{ id: 'himmel', name: 'Sky', body: '#a5caff' },
|
|
198
|
+
] as const satisfies readonly KlexPalette[];
|
|
199
|
+
|
|
200
|
+
export type MovementMode = 'crawl' | 'fly' | 'hop';
|
|
201
|
+
|
|
202
|
+
export const DEFAULT_GLIDE = {
|
|
203
|
+
mode: 'crawl' as MovementMode,
|
|
204
|
+
speed: 120,
|
|
205
|
+
softness: 0.8,
|
|
206
|
+
wavelength: 100,
|
|
207
|
+
nod: 1,
|
|
208
|
+
lag: 1,
|
|
209
|
+
turn: 7,
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export const MOVEMENT_STYLES = [
|
|
213
|
+
{
|
|
214
|
+
id: 'crawl',
|
|
215
|
+
name: 'Crawl',
|
|
216
|
+
description: 'Lead along the ground and let the soft body follow.',
|
|
217
|
+
settings: DEFAULT_GLIDE,
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
id: 'fly',
|
|
221
|
+
name: 'Fly',
|
|
222
|
+
description: 'Lift off gently, glide through the air, and hover calmly.',
|
|
223
|
+
settings: {
|
|
224
|
+
mode: 'fly',
|
|
225
|
+
speed: 160,
|
|
226
|
+
softness: 0.6,
|
|
227
|
+
wavelength: 140,
|
|
228
|
+
nod: 0.8,
|
|
229
|
+
lag: 1,
|
|
230
|
+
turn: 8,
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
id: 'hop',
|
|
235
|
+
name: 'Hop',
|
|
236
|
+
description: 'Crouch briefly, jump, and land softly.',
|
|
237
|
+
settings: {
|
|
238
|
+
mode: 'hop',
|
|
239
|
+
speed: 145,
|
|
240
|
+
softness: 0.85,
|
|
241
|
+
wavelength: 140,
|
|
242
|
+
nod: 0.8,
|
|
243
|
+
lag: 0.8,
|
|
244
|
+
turn: 9,
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
] as const;
|
package/src/speech.css
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
.klex-speech-float {
|
|
2
|
+
animation: klex-speech-float 2s cubic-bezier(0.37, 0, 0.63, 1) infinite
|
|
3
|
+
alternate;
|
|
4
|
+
will-change: transform;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
@keyframes klex-speech-float {
|
|
8
|
+
from {
|
|
9
|
+
transform: translate3d(0, 0, 0);
|
|
10
|
+
}
|
|
11
|
+
to {
|
|
12
|
+
transform: translate3d(0, -4px, 0);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@media (prefers-reduced-motion: reduce) {
|
|
17
|
+
.klex-speech-float {
|
|
18
|
+
animation: none;
|
|
19
|
+
will-change: auto;
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/speech.tsx
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import './speech.css';
|
|
2
|
+
|
|
3
|
+
import { AnimatePresence, motion, useReducedMotion } from 'motion/react';
|
|
4
|
+
import { useLayoutEffect, useRef } from 'react';
|
|
5
|
+
|
|
6
|
+
import { Bubble, BubbleContent } from '@stagewise/ui';
|
|
7
|
+
|
|
8
|
+
import { popTransition } from './transitions';
|
|
9
|
+
|
|
10
|
+
export type KlexSpeechValue = { text: string; waiting?: boolean };
|
|
11
|
+
|
|
12
|
+
export function KlexSpeech({ speech }: { speech: KlexSpeechValue | null }) {
|
|
13
|
+
return (
|
|
14
|
+
<AnimatePresence mode="wait">
|
|
15
|
+
{speech && <SpeechBubble key={speech.text} speech={speech} />}
|
|
16
|
+
</AnimatePresence>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function SpeechBubble({ speech }: { speech: KlexSpeechValue }) {
|
|
21
|
+
const reducedMotion = useReducedMotion();
|
|
22
|
+
const bubbleRef = useRef<HTMLDivElement>(null);
|
|
23
|
+
const textRef = useRef<HTMLSpanElement>(null);
|
|
24
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: Speech changes resize the rendered text and waiting dots measured below.
|
|
25
|
+
useLayoutEffect(() => {
|
|
26
|
+
const fitText = () => {
|
|
27
|
+
const bubble = bubbleRef.current;
|
|
28
|
+
const label = textRef.current;
|
|
29
|
+
if (!bubble || !label) return;
|
|
30
|
+
bubble.style.width = '';
|
|
31
|
+
const content = label.parentElement;
|
|
32
|
+
if (!content) return;
|
|
33
|
+
const style = getComputedStyle(content);
|
|
34
|
+
// Measure outside the actor so jumping, scaling, and rotation cannot
|
|
35
|
+
// change the text's layout width.
|
|
36
|
+
const measurement = content.cloneNode(true) as HTMLElement;
|
|
37
|
+
Object.assign(measurement.style, {
|
|
38
|
+
position: 'fixed',
|
|
39
|
+
visibility: 'hidden',
|
|
40
|
+
left: '0',
|
|
41
|
+
top: '0',
|
|
42
|
+
width: `${content.offsetWidth}px`,
|
|
43
|
+
font: style.font,
|
|
44
|
+
});
|
|
45
|
+
document.body.append(measurement);
|
|
46
|
+
const lines = [...(measurement.lastElementChild?.getClientRects() ?? [])];
|
|
47
|
+
measurement.remove();
|
|
48
|
+
if (!lines.length) return;
|
|
49
|
+
const padding =
|
|
50
|
+
Number.parseFloat(style.paddingLeft) +
|
|
51
|
+
Number.parseFloat(style.paddingRight) +
|
|
52
|
+
Number.parseFloat(style.borderLeftWidth) +
|
|
53
|
+
Number.parseFloat(style.borderRightWidth);
|
|
54
|
+
bubble.style.width = `${Math.ceil(Math.max(...lines.map((line) => line.width)) + padding)}px`;
|
|
55
|
+
};
|
|
56
|
+
fitText();
|
|
57
|
+
window.addEventListener('resize', fitText);
|
|
58
|
+
return () => window.removeEventListener('resize', fitText);
|
|
59
|
+
}, [speech]);
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<motion.div
|
|
63
|
+
ref={bubbleRef}
|
|
64
|
+
data-klex-speech=""
|
|
65
|
+
className="pointer-events-none absolute bottom-1/2 left-[calc(100%+0.5rem)] z-10 w-max max-w-[min(11rem,calc(50vw-5rem))] origin-bottom-left text-left"
|
|
66
|
+
initial={{ opacity: 0, scale: reducedMotion ? 1 : 0 }}
|
|
67
|
+
animate={{ opacity: 1, scale: 1 }}
|
|
68
|
+
exit={{
|
|
69
|
+
opacity: 0,
|
|
70
|
+
scale: reducedMotion ? 1 : 0,
|
|
71
|
+
transition: { duration: reducedMotion ? 0 : 0.12, ease: 'easeIn' },
|
|
72
|
+
}}
|
|
73
|
+
transition={
|
|
74
|
+
reducedMotion
|
|
75
|
+
? { duration: 0 }
|
|
76
|
+
: {
|
|
77
|
+
scale: popTransition,
|
|
78
|
+
opacity: { duration: 0.12 },
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
>
|
|
82
|
+
<Bubble
|
|
83
|
+
variant="secondary"
|
|
84
|
+
className="klex-speech-float w-full max-w-none"
|
|
85
|
+
>
|
|
86
|
+
<svg
|
|
87
|
+
aria-hidden="true"
|
|
88
|
+
viewBox="0 0 32 24"
|
|
89
|
+
className="absolute -left-2 bottom-0 h-5 w-6 fill-secondary"
|
|
90
|
+
>
|
|
91
|
+
<path d="M18 0C18 10 11 19 1 23C14 24 26 18 32 10Z" />
|
|
92
|
+
</svg>
|
|
93
|
+
<BubbleContent className="relative w-full rounded-2xl text-balance text-xs">
|
|
94
|
+
<span
|
|
95
|
+
className="sr-only"
|
|
96
|
+
role="status"
|
|
97
|
+
aria-live="polite"
|
|
98
|
+
aria-atomic="true"
|
|
99
|
+
>
|
|
100
|
+
{speech.text}
|
|
101
|
+
{speech.waiting ? '…' : ''}
|
|
102
|
+
</span>
|
|
103
|
+
<span ref={textRef} aria-hidden="true">
|
|
104
|
+
{speech.text}
|
|
105
|
+
{speech.waiting && (
|
|
106
|
+
<span className="ml-1 inline-flex">
|
|
107
|
+
{[0, 1, 2].map((dot) => (
|
|
108
|
+
<motion.span
|
|
109
|
+
key={dot}
|
|
110
|
+
animate={{
|
|
111
|
+
opacity: reducedMotion ? 1 : [0.25, 1, 0.25],
|
|
112
|
+
}}
|
|
113
|
+
transition={{
|
|
114
|
+
duration: 1.2,
|
|
115
|
+
delay: dot * 0.18,
|
|
116
|
+
repeat: reducedMotion ? 0 : Infinity,
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
.
|
|
120
|
+
</motion.span>
|
|
121
|
+
))}
|
|
122
|
+
</span>
|
|
123
|
+
)}
|
|
124
|
+
</span>
|
|
125
|
+
</BubbleContent>
|
|
126
|
+
</Bubble>
|
|
127
|
+
</motion.div>
|
|
128
|
+
);
|
|
129
|
+
}
|
package/src/svg.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { BodyShape } from './presets';
|
|
2
|
+
|
|
3
|
+
function fixed2(value: number) {
|
|
4
|
+
// Neutral rig transforms can introduce roundoff at half-cent boundaries.
|
|
5
|
+
const rounded = Math.round(
|
|
6
|
+
(value + 8 * Number.EPSILON * Math.abs(value)) * 100,
|
|
7
|
+
);
|
|
8
|
+
const absolute = Math.abs(rounded);
|
|
9
|
+
const integer = Math.floor(absolute / 100);
|
|
10
|
+
const fraction = absolute - integer * 100;
|
|
11
|
+
const digits =
|
|
12
|
+
fraction === 0
|
|
13
|
+
? String(integer)
|
|
14
|
+
: fraction % 10 === 0
|
|
15
|
+
? `${integer}.${fraction / 10}`
|
|
16
|
+
: `${integer}${fraction < 10 ? '.0' : '.'}${fraction}`;
|
|
17
|
+
return rounded < 0 ? `-${digits}` : digits;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function roundedPath(points: readonly (readonly [number, number])[]) {
|
|
21
|
+
let path = '';
|
|
22
|
+
// A periodic cubic B-spline rounds across the rig points instead of passing
|
|
23
|
+
// through each one. Its curvature stays continuous, including at the sole.
|
|
24
|
+
for (let i = 0; i < points.length; i++) {
|
|
25
|
+
const current = points[i];
|
|
26
|
+
if (!current) continue;
|
|
27
|
+
const previous = points[(i + points.length - 1) % points.length] ?? current;
|
|
28
|
+
const next = points[(i + 1) % points.length] ?? current;
|
|
29
|
+
const after = points[(i + 2) % points.length] ?? current;
|
|
30
|
+
if (i === 0) {
|
|
31
|
+
path = `M${fixed2((previous[0] + 4 * current[0] + next[0]) / 6)} ${fixed2((previous[1] + 4 * current[1] + next[1]) / 6)}`;
|
|
32
|
+
}
|
|
33
|
+
path += `C${fixed2((2 * current[0] + next[0]) / 3)} ${fixed2((2 * current[1] + next[1]) / 3)} ${fixed2((current[0] + 2 * next[0]) / 3)} ${fixed2((current[1] + 2 * next[1]) / 3)} ${fixed2((current[0] + 4 * next[0] + after[0]) / 6)} ${fixed2((current[1] + 4 * next[1] + after[1]) / 6)}`;
|
|
34
|
+
}
|
|
35
|
+
return `${path}Z`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getKlexEyeColor(bodyColor: string): string {
|
|
39
|
+
const match = /^#([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(bodyColor);
|
|
40
|
+
if (!match) return '#172033';
|
|
41
|
+
|
|
42
|
+
const red = Number.parseInt(match[1] ?? '7c', 16);
|
|
43
|
+
const green = Number.parseInt(match[2] ?? '3a', 16);
|
|
44
|
+
const blue = Number.parseInt(match[3] ?? 'ed', 16);
|
|
45
|
+
const brightness = red * 0.299 + green * 0.587 + blue * 0.114;
|
|
46
|
+
|
|
47
|
+
return brightness > 160 ? '#172033' : '#ffffff';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function klexSvg(shape: BodyShape, color: string) {
|
|
51
|
+
const fill = color.replace(
|
|
52
|
+
/[&<>"']/g,
|
|
53
|
+
(character) => `&#${character.charCodeAt(0)};`,
|
|
54
|
+
);
|
|
55
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 140"><path d="${roundedPath(shape.outline)}" fill="${fill}"/><g fill="${getKlexEyeColor(color)}" transform="translate(${80 + shape.eyeX} ${shape.eyeY})"><ellipse cx="-9" rx="4.5" ry="6.5"/><ellipse cx="9" rx="4.5" ry="6.5"/></g></svg>`;
|
|
56
|
+
}
|
package/src/working.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { emotePose } from './emotes';
|
|
2
|
+
|
|
3
|
+
const phases = ['reading', 'typing', 'thinking'] as const;
|
|
4
|
+
const between = (min: number, max: number) => min + Math.random() * (max - min);
|
|
5
|
+
|
|
6
|
+
// Each avatar chooses new phases and timings throughout its lifetime.
|
|
7
|
+
export function createWorking() {
|
|
8
|
+
let phase = phases[Math.floor(Math.random() * phases.length)];
|
|
9
|
+
const duration = () =>
|
|
10
|
+
phase === 'reading'
|
|
11
|
+
? between(4, 7)
|
|
12
|
+
: phase === 'typing'
|
|
13
|
+
? between(2.5, 5)
|
|
14
|
+
: between(1.8, 3.5);
|
|
15
|
+
let phaseDuration = duration();
|
|
16
|
+
let time = between(0, phaseDuration * 0.7);
|
|
17
|
+
let direction = Math.random() < 0.5 ? -1 : 1;
|
|
18
|
+
let blinkIn = between(2, 5);
|
|
19
|
+
let blinkTime = Infinity;
|
|
20
|
+
let tapIn = 0;
|
|
21
|
+
let tapTime = Infinity;
|
|
22
|
+
const pose = {
|
|
23
|
+
compression: 0.025,
|
|
24
|
+
headTilt: 0,
|
|
25
|
+
headSway: 0,
|
|
26
|
+
nod: 0.15,
|
|
27
|
+
lookX: 0,
|
|
28
|
+
lookY: 1,
|
|
29
|
+
laptopTap: 0,
|
|
30
|
+
laptopFloat: 1,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
get thinking() {
|
|
35
|
+
return phase === 'thinking' && time > 0.25;
|
|
36
|
+
},
|
|
37
|
+
advance(dt: number) {
|
|
38
|
+
time += dt;
|
|
39
|
+
blinkTime += dt;
|
|
40
|
+
blinkIn -= dt;
|
|
41
|
+
tapTime += dt;
|
|
42
|
+
if (time >= phaseDuration) {
|
|
43
|
+
const choices = phases.filter((next) => next !== phase);
|
|
44
|
+
phase = choices[Math.floor(Math.random() * choices.length)];
|
|
45
|
+
phaseDuration = duration();
|
|
46
|
+
time = 0;
|
|
47
|
+
direction = Math.random() < 0.5 ? -1 : 1;
|
|
48
|
+
blinkIn = 0;
|
|
49
|
+
tapIn = 0;
|
|
50
|
+
tapTime = Infinity;
|
|
51
|
+
}
|
|
52
|
+
if (blinkIn <= 0) {
|
|
53
|
+
blinkTime = 0;
|
|
54
|
+
blinkIn = between(2, 5);
|
|
55
|
+
}
|
|
56
|
+
if (phase === 'typing') {
|
|
57
|
+
tapIn -= dt;
|
|
58
|
+
if (tapIn <= 0) {
|
|
59
|
+
tapTime = 0;
|
|
60
|
+
tapIn =
|
|
61
|
+
Math.random() < 0.22 ? between(0.35, 0.7) : between(0.1, 0.24);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const tap =
|
|
65
|
+
phase === 'typing' && tapTime < 0.16
|
|
66
|
+
? Math.sin((tapTime / 0.16) * Math.PI)
|
|
67
|
+
: 0;
|
|
68
|
+
const line = (time / phaseDuration) * 4;
|
|
69
|
+
const column = Math.floor((line % 1) * 5) / 4;
|
|
70
|
+
const thinking = phase === 'thinking';
|
|
71
|
+
const target = {
|
|
72
|
+
compression: 0.025 + tap * 0.014,
|
|
73
|
+
headTilt: thinking ? direction * 4 : 0,
|
|
74
|
+
headSway: thinking ? direction * 0.8 : tap * direction * 0.3,
|
|
75
|
+
nod: thinking ? -0.3 : 0.15 + tap * 0.45,
|
|
76
|
+
lookX: thinking
|
|
77
|
+
? direction * 3
|
|
78
|
+
: phase === 'reading'
|
|
79
|
+
? 3 - column * 6
|
|
80
|
+
: direction * 0.7,
|
|
81
|
+
lookY: thinking
|
|
82
|
+
? -6
|
|
83
|
+
: phase === 'reading'
|
|
84
|
+
? -1 + Math.floor(line) * 1.7
|
|
85
|
+
: 3,
|
|
86
|
+
laptopTap: tap * 0.65,
|
|
87
|
+
laptopFloat: phase === 'typing' ? 0 : 1,
|
|
88
|
+
};
|
|
89
|
+
const blend = 1 - Math.exp(-18 * dt);
|
|
90
|
+
for (const key of Object.keys(pose) as (keyof typeof pose)[]) {
|
|
91
|
+
pose[key] += (target[key] - pose[key]) * blend;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
pose() {
|
|
95
|
+
const blink = emotePose('blink', blinkTime / 0.3).blinkLeft;
|
|
96
|
+
return { ...pose, blinkLeft: blink, blinkRight: blink };
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|