@klex/mascot 0.4.0 → 0.6.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 +20 -5
- package/package.json +1 -1
- package/src/activity.ts +109 -28
- package/src/book.tsx +94 -0
- package/src/emotes.ts +3 -1
- package/src/extras.css +55 -1
- package/src/extras.tsx +54 -0
- package/src/geometry.ts +27 -0
- package/src/glide.ts +187 -36
- package/src/index.ts +11 -0
- package/src/inflate/atlas.ts +74 -0
- package/src/inflate/params.ts +302 -0
- package/src/inflate/renderer.ts +762 -0
- package/src/inflate/shaders.ts +447 -0
- package/src/inflate/shared.ts +39 -0
- package/src/inflate/support.ts +54 -0
- package/src/inflate/turn.ts +198 -0
- package/src/inflate/use-inflated-body.ts +229 -0
- package/src/klex.tsx +147 -34
- package/src/learning.ts +126 -0
- package/src/presets.ts +35 -0
- package/src/recalling.ts +111 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import type { GlideTurn } from '../glide';
|
|
2
|
+
import {
|
|
3
|
+
profileHeight,
|
|
4
|
+
type ResolvedInflate,
|
|
5
|
+
turnPivot,
|
|
6
|
+
turnPoint,
|
|
7
|
+
} from './params';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Degrees. `yaw` and `pitch` turn the whole body, `headYaw` and `headPitch`
|
|
11
|
+
* only the part above the neck. `lookAround` adds slow glances, mostly with
|
|
12
|
+
* the head.
|
|
13
|
+
*/
|
|
14
|
+
export type KlexTurn = {
|
|
15
|
+
yaw?: number;
|
|
16
|
+
pitch?: number;
|
|
17
|
+
headYaw?: number;
|
|
18
|
+
headPitch?: number;
|
|
19
|
+
lookAround?: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type TurnAngles = {
|
|
23
|
+
yaw: number;
|
|
24
|
+
pitch: number;
|
|
25
|
+
headYaw: number;
|
|
26
|
+
headPitch: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const RADIANS = Math.PI / 180;
|
|
30
|
+
const LOOK_YAW = 34 * RADIANS;
|
|
31
|
+
const LOOK_PITCH = 12 * RADIANS;
|
|
32
|
+
// Glances and gaze are carried mostly by the head.
|
|
33
|
+
const HEAD_SHARE = 0.7;
|
|
34
|
+
const STIFFNESS = 5;
|
|
35
|
+
// Per eye unit of gaze; a full glance travels about six units.
|
|
36
|
+
const GAZE_BODY = 1 * RADIANS;
|
|
37
|
+
const GAZE_HEAD = 3.5 * RADIANS;
|
|
38
|
+
// The head turns about a point between the body's center and the face; the
|
|
39
|
+
// twist starts just above the eyes and fades out towards the sole.
|
|
40
|
+
const NECK_ABOVE_EYES = 4;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The engine's turn in depth. The rig draws the right-facing body; walking
|
|
44
|
+
* left turns it round with its nose towards the viewer. Turning away and
|
|
45
|
+
* spinning go towards the side the character faces; gaze turns the head.
|
|
46
|
+
*/
|
|
47
|
+
export function engineTurn(turn: GlideTurn): TurnAngles {
|
|
48
|
+
const side = turn.facing < 0 ? -1 : 1;
|
|
49
|
+
const around = turn.spin > 0 ? 2 * Math.PI * turn.spin : Math.PI * turn.turn;
|
|
50
|
+
return {
|
|
51
|
+
yaw:
|
|
52
|
+
(-Math.PI * (1 - turn.facing)) / 2 +
|
|
53
|
+
side * around +
|
|
54
|
+
GAZE_BODY * turn.lookX,
|
|
55
|
+
pitch: -GAZE_BODY * turn.lookY,
|
|
56
|
+
headYaw: GAZE_HEAD * turn.lookX,
|
|
57
|
+
headPitch: -GAZE_HEAD * turn.lookY,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Glances at a new direction every couple of seconds and eases there with
|
|
63
|
+
* critically damped springs, so turns start and settle softly.
|
|
64
|
+
*/
|
|
65
|
+
export function createTurnAnimator(random: () => number = Math.random) {
|
|
66
|
+
const state: TurnAngles = { yaw: 0, pitch: 0, headYaw: 0, headPitch: 0 };
|
|
67
|
+
const speed: TurnAngles = { yaw: 0, pitch: 0, headYaw: 0, headPitch: 0 };
|
|
68
|
+
let glance = { yaw: 0, pitch: 0 };
|
|
69
|
+
let nextGlance = 0;
|
|
70
|
+
let time = 0;
|
|
71
|
+
return {
|
|
72
|
+
state,
|
|
73
|
+
advance(elapsed: number, turn: KlexTurn | undefined) {
|
|
74
|
+
const dt = Math.min(0.05, Math.max(0, elapsed));
|
|
75
|
+
time += dt;
|
|
76
|
+
if (!turn?.lookAround) glance = { yaw: 0, pitch: 0 };
|
|
77
|
+
else if (time >= nextGlance) {
|
|
78
|
+
// Half the glances return to the viewer.
|
|
79
|
+
glance =
|
|
80
|
+
random() < 0.5
|
|
81
|
+
? {
|
|
82
|
+
yaw: (random() * 2 - 1) * LOOK_YAW,
|
|
83
|
+
pitch: (random() * 2 - 1) * LOOK_PITCH,
|
|
84
|
+
}
|
|
85
|
+
: { yaw: 0, pitch: 0 };
|
|
86
|
+
nextGlance = time + 1.2 + random() * 2.4;
|
|
87
|
+
}
|
|
88
|
+
const target: TurnAngles = {
|
|
89
|
+
yaw: (turn?.yaw ?? 0) * RADIANS + glance.yaw * (1 - HEAD_SHARE),
|
|
90
|
+
pitch: (turn?.pitch ?? 0) * RADIANS + glance.pitch * (1 - HEAD_SHARE),
|
|
91
|
+
headYaw: (turn?.headYaw ?? 0) * RADIANS + glance.yaw * HEAD_SHARE,
|
|
92
|
+
headPitch: (turn?.headPitch ?? 0) * RADIANS + glance.pitch * HEAD_SHARE,
|
|
93
|
+
};
|
|
94
|
+
// Substeps keep the springs stable at low frame rates.
|
|
95
|
+
const step = dt / 4;
|
|
96
|
+
for (let i = 0; i < 4; i++) {
|
|
97
|
+
for (const key of ['yaw', 'pitch', 'headYaw', 'headPitch'] as const) {
|
|
98
|
+
speed[key] +=
|
|
99
|
+
(STIFFNESS * STIFFNESS * (target[key] - state[key]) -
|
|
100
|
+
2 * STIFFNESS * speed[key]) *
|
|
101
|
+
step;
|
|
102
|
+
state[key] += speed[key] * step;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return state;
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Where the head turns, from the face position and the body's bounds. */
|
|
111
|
+
export function headFrame(
|
|
112
|
+
points: readonly (readonly [number, number])[],
|
|
113
|
+
face: { x: number; y: number },
|
|
114
|
+
) {
|
|
115
|
+
const [pivotX] = turnPivot(points);
|
|
116
|
+
return { neckY: face.y - NECK_ABOVE_EYES, pivotX: (pivotX + face.x) / 2 };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Distance from a point to the closest edge of a closed polygon. */
|
|
120
|
+
export function edgeDistance(
|
|
121
|
+
points: readonly (readonly [number, number])[],
|
|
122
|
+
x: number,
|
|
123
|
+
y: number,
|
|
124
|
+
) {
|
|
125
|
+
let nearest = Infinity;
|
|
126
|
+
for (let i = 0; i < points.length; i++) {
|
|
127
|
+
const [ax, ay] = points[i] ?? [x, y];
|
|
128
|
+
const [bx, by] = points[(i + 1) % points.length] ?? [ax, ay];
|
|
129
|
+
const dx = bx - ax;
|
|
130
|
+
const dy = by - ay;
|
|
131
|
+
const t = Math.min(
|
|
132
|
+
1,
|
|
133
|
+
Math.max(0, ((x - ax) * dx + (y - ay) * dy) / (dx * dx + dy * dy || 1)),
|
|
134
|
+
);
|
|
135
|
+
nearest = Math.min(nearest, Math.hypot(x - ax - t * dx, y - ay - t * dy));
|
|
136
|
+
}
|
|
137
|
+
return nearest;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Where the face lands once body and head turn. Both sides of the body carry
|
|
142
|
+
* the face; the side towards the viewer shows it, mirrored on the far side.
|
|
143
|
+
*/
|
|
144
|
+
export function projectFace(
|
|
145
|
+
points: readonly (readonly [number, number])[],
|
|
146
|
+
face: { x: number; y: number },
|
|
147
|
+
inflate: ResolvedInflate,
|
|
148
|
+
angles: TurnAngles,
|
|
149
|
+
) {
|
|
150
|
+
const yaw = angles.yaw + angles.headYaw;
|
|
151
|
+
const pitch = angles.pitch + angles.headPitch;
|
|
152
|
+
const front = Math.cos(yaw) >= 0;
|
|
153
|
+
const depth =
|
|
154
|
+
(profileHeight(inflate, edgeDistance(points, face.x, face.y)) + 0.5) *
|
|
155
|
+
(front ? 1 : -1);
|
|
156
|
+
// The head turns about its own pivot, then the body about its center.
|
|
157
|
+
const pivot = turnPivot(points);
|
|
158
|
+
const [headX, headY, headZ] = turnPoint(
|
|
159
|
+
[face.x, face.y, depth],
|
|
160
|
+
[headFrame(points, face).pivotX, pivot[1]],
|
|
161
|
+
angles.headYaw,
|
|
162
|
+
angles.headPitch,
|
|
163
|
+
);
|
|
164
|
+
const [x, y] = turnPoint(
|
|
165
|
+
[headX, headY, headZ],
|
|
166
|
+
pivot,
|
|
167
|
+
angles.yaw,
|
|
168
|
+
angles.pitch,
|
|
169
|
+
);
|
|
170
|
+
const facing = Math.abs(Math.cos(yaw)) * Math.cos(pitch);
|
|
171
|
+
const width = Math.max(0.05, Math.abs(Math.cos(yaw)));
|
|
172
|
+
return {
|
|
173
|
+
x,
|
|
174
|
+
y,
|
|
175
|
+
scaleX: front ? width : -width,
|
|
176
|
+
scaleY: Math.max(0.05, Math.cos(pitch)),
|
|
177
|
+
opacity: Math.min(1, Math.max(0, (facing - 0.08) / 0.25)),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The shadow widens as the body's depth turns into view. */
|
|
182
|
+
export function turnedWidth(
|
|
183
|
+
points: readonly (readonly [number, number])[],
|
|
184
|
+
inflate: ResolvedInflate,
|
|
185
|
+
yaw: number,
|
|
186
|
+
) {
|
|
187
|
+
let minX = Infinity;
|
|
188
|
+
let maxX = -Infinity;
|
|
189
|
+
for (const [x] of points) {
|
|
190
|
+
minX = Math.min(minX, x);
|
|
191
|
+
maxX = Math.max(maxX, x);
|
|
192
|
+
}
|
|
193
|
+
const width = Math.max(1, maxX - minX);
|
|
194
|
+
const depth = inflate.amplitude * 2;
|
|
195
|
+
return (
|
|
196
|
+
(width * Math.abs(Math.cos(yaw)) + depth * Math.abs(Math.sin(yaw))) / width
|
|
197
|
+
);
|
|
198
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RefObject,
|
|
3
|
+
useLayoutEffect,
|
|
4
|
+
useMemo,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type { GlideFrameListener, GlideTurn } from '../glide';
|
|
10
|
+
import type { BodyShape } from '../presets';
|
|
11
|
+
import type { InflateOptions, KlexMaterial } from './params';
|
|
12
|
+
import { resolveInflate } from './params';
|
|
13
|
+
import {
|
|
14
|
+
acquireInflateRenderer,
|
|
15
|
+
type InflateBot,
|
|
16
|
+
type InflateFrame,
|
|
17
|
+
invalidateInflateRenderer,
|
|
18
|
+
releaseInflateRenderer,
|
|
19
|
+
} from './renderer';
|
|
20
|
+
import {
|
|
21
|
+
chooseAppearance,
|
|
22
|
+
detectInflateSupport,
|
|
23
|
+
type KlexAppearance,
|
|
24
|
+
} from './support';
|
|
25
|
+
import {
|
|
26
|
+
createTurnAnimator,
|
|
27
|
+
engineTurn,
|
|
28
|
+
type KlexTurn,
|
|
29
|
+
projectFace,
|
|
30
|
+
turnedWidth,
|
|
31
|
+
} from './turn';
|
|
32
|
+
|
|
33
|
+
type Options = {
|
|
34
|
+
appearance: KlexAppearance;
|
|
35
|
+
reducedMotion: boolean;
|
|
36
|
+
layout: 'track' | 'avatar' | 'svg';
|
|
37
|
+
canvasRef: RefObject<HTMLCanvasElement | null>;
|
|
38
|
+
shadowRef: RefObject<SVGEllipseElement | null>;
|
|
39
|
+
/** Wraps the eyes so they can follow the turned front surface. */
|
|
40
|
+
faceRef: RefObject<SVGGElement | null>;
|
|
41
|
+
size: number;
|
|
42
|
+
shape: BodyShape;
|
|
43
|
+
color: string;
|
|
44
|
+
material: KlexMaterial;
|
|
45
|
+
inflate: InflateOptions | undefined;
|
|
46
|
+
turn: KlexTurn | undefined;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Soft contact shadow: it shrinks and fades as the body lifts off.
|
|
50
|
+
export function contactShadow(lift: number) {
|
|
51
|
+
return {
|
|
52
|
+
rx: 56 * Math.max(0.3, 1 - lift / 170),
|
|
53
|
+
opacity: 0.32 * Math.max(0.12, 1 - lift / 110),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function useInflatedBody({
|
|
58
|
+
appearance,
|
|
59
|
+
reducedMotion,
|
|
60
|
+
layout,
|
|
61
|
+
canvasRef,
|
|
62
|
+
shadowRef,
|
|
63
|
+
faceRef,
|
|
64
|
+
size,
|
|
65
|
+
shape,
|
|
66
|
+
color,
|
|
67
|
+
material,
|
|
68
|
+
inflate,
|
|
69
|
+
turn,
|
|
70
|
+
}: Options) {
|
|
71
|
+
const [lost, setLost] = useState(false);
|
|
72
|
+
const supported = appearance === 'inflated' && detectInflateSupport();
|
|
73
|
+
const active =
|
|
74
|
+
!lost &&
|
|
75
|
+
chooseAppearance({
|
|
76
|
+
requested: appearance,
|
|
77
|
+
reducedMotion,
|
|
78
|
+
layout,
|
|
79
|
+
supported,
|
|
80
|
+
}) === 'inflated';
|
|
81
|
+
const botRef = useRef<InflateBot | null>(null);
|
|
82
|
+
const frameRef = useRef<InflateFrame | null>(null);
|
|
83
|
+
const activeRef = useRef(active);
|
|
84
|
+
activeRef.current = active;
|
|
85
|
+
const resolved = useMemo(
|
|
86
|
+
() => resolveInflate(shape, inflate),
|
|
87
|
+
[shape, inflate],
|
|
88
|
+
);
|
|
89
|
+
const resolvedRef = useRef(resolved);
|
|
90
|
+
resolvedRef.current = resolved;
|
|
91
|
+
const turnRef = useRef(turn);
|
|
92
|
+
turnRef.current = turn;
|
|
93
|
+
const [animator] = useState(() => createTurnAnimator());
|
|
94
|
+
const clockRef = useRef<number | null>(null);
|
|
95
|
+
const engineRef = useRef<GlideTurn>({
|
|
96
|
+
lookX: 0,
|
|
97
|
+
lookY: 0,
|
|
98
|
+
turn: 0,
|
|
99
|
+
spin: 0,
|
|
100
|
+
facing: 1,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Rendering side effects of a frame: the body, then the face and shadow
|
|
104
|
+
// that have to agree with how far it turned.
|
|
105
|
+
const present = useMemo(
|
|
106
|
+
() => (frame: InflateFrame) => {
|
|
107
|
+
const now = performance.now();
|
|
108
|
+
const elapsed =
|
|
109
|
+
clockRef.current === null ? 0 : (now - clockRef.current) / 1000;
|
|
110
|
+
clockRef.current = now;
|
|
111
|
+
const glance = animator.advance(elapsed, turnRef.current);
|
|
112
|
+
const engine = engineTurn(engineRef.current);
|
|
113
|
+
const angles = {
|
|
114
|
+
yaw: glance.yaw + engine.yaw,
|
|
115
|
+
pitch: glance.pitch + engine.pitch,
|
|
116
|
+
headYaw: glance.headYaw + engine.headYaw,
|
|
117
|
+
headPitch: glance.headPitch + engine.headPitch,
|
|
118
|
+
};
|
|
119
|
+
Object.assign(frame, angles);
|
|
120
|
+
botRef.current?.draw(frame);
|
|
121
|
+
const inflate = resolvedRef.current;
|
|
122
|
+
const face = faceRef.current;
|
|
123
|
+
if (face) {
|
|
124
|
+
if (
|
|
125
|
+
Math.abs(angles.yaw) +
|
|
126
|
+
Math.abs(angles.pitch) +
|
|
127
|
+
Math.abs(angles.headYaw) +
|
|
128
|
+
Math.abs(angles.headPitch) <
|
|
129
|
+
1e-4
|
|
130
|
+
) {
|
|
131
|
+
face.removeAttribute('transform');
|
|
132
|
+
face.removeAttribute('opacity');
|
|
133
|
+
} else {
|
|
134
|
+
const view = projectFace(frame.points, frame.face, inflate, angles);
|
|
135
|
+
face.setAttribute(
|
|
136
|
+
'transform',
|
|
137
|
+
`translate(${view.x.toFixed(2)} ${view.y.toFixed(2)}) scale(${view.scaleX.toFixed(3)} ${view.scaleY.toFixed(3)}) translate(${-frame.face.x} ${-frame.face.y})`,
|
|
138
|
+
);
|
|
139
|
+
face.setAttribute('opacity', view.opacity.toFixed(3));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const shadow = shadowRef.current;
|
|
143
|
+
if (shadow) {
|
|
144
|
+
const { rx, opacity } = contactShadow(frame.lift);
|
|
145
|
+
const width = turnedWidth(frame.points, inflate, angles.yaw);
|
|
146
|
+
shadow.setAttribute('rx', (rx * width).toFixed(2));
|
|
147
|
+
shadow.setAttribute('opacity', opacity.toFixed(3));
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
[animator, faceRef, shadowRef],
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// Stable for the engine's lifetime; it always keeps the latest frame so a
|
|
154
|
+
// late switch to the inflated look can draw immediately.
|
|
155
|
+
const onFrame = useMemo<GlideFrameListener>(
|
|
156
|
+
() => (points, faceX, faceY, faceAngle, facing, lift, turn) => {
|
|
157
|
+
engineRef.current = turn;
|
|
158
|
+
let frame = frameRef.current;
|
|
159
|
+
if (!frame) {
|
|
160
|
+
frame = {
|
|
161
|
+
points,
|
|
162
|
+
face: { x: 0, y: 0, angle: 0 },
|
|
163
|
+
facing,
|
|
164
|
+
lift,
|
|
165
|
+
yaw: 0,
|
|
166
|
+
pitch: 0,
|
|
167
|
+
headYaw: 0,
|
|
168
|
+
headPitch: 0,
|
|
169
|
+
};
|
|
170
|
+
frameRef.current = frame;
|
|
171
|
+
}
|
|
172
|
+
frame.points = points;
|
|
173
|
+
frame.face.x = faceX;
|
|
174
|
+
frame.face.y = faceY;
|
|
175
|
+
frame.face.angle = faceAngle;
|
|
176
|
+
frame.facing = facing;
|
|
177
|
+
frame.lift = lift;
|
|
178
|
+
if (activeRef.current) present(frame);
|
|
179
|
+
},
|
|
180
|
+
[present],
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
useLayoutEffect(() => {
|
|
184
|
+
if (!active) return;
|
|
185
|
+
const renderer = acquireInflateRenderer();
|
|
186
|
+
if (!renderer) {
|
|
187
|
+
setLost(true);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const bot = renderer.register();
|
|
191
|
+
botRef.current = bot;
|
|
192
|
+
const stopListening = renderer.onLost(() => {
|
|
193
|
+
invalidateInflateRenderer(renderer);
|
|
194
|
+
setLost(true);
|
|
195
|
+
});
|
|
196
|
+
return () => {
|
|
197
|
+
stopListening();
|
|
198
|
+
bot.release();
|
|
199
|
+
botRef.current = null;
|
|
200
|
+
releaseInflateRenderer(renderer);
|
|
201
|
+
};
|
|
202
|
+
}, [active]);
|
|
203
|
+
|
|
204
|
+
useLayoutEffect(() => {
|
|
205
|
+
if (!active) return;
|
|
206
|
+
botRef.current?.setTarget(canvasRef.current, size);
|
|
207
|
+
}, [active, canvasRef, size]);
|
|
208
|
+
|
|
209
|
+
useLayoutEffect(() => {
|
|
210
|
+
if (!active) return;
|
|
211
|
+
const bot = botRef.current;
|
|
212
|
+
if (!bot) return;
|
|
213
|
+
bot.setLook({ shape, color, material, inflate: resolved });
|
|
214
|
+
const frame = frameRef.current;
|
|
215
|
+
if (frame) present(frame);
|
|
216
|
+
}, [active, shape, color, material, resolved, present]);
|
|
217
|
+
|
|
218
|
+
// Leaving the inflated look restores the flat face.
|
|
219
|
+
useLayoutEffect(() => {
|
|
220
|
+
if (active) return;
|
|
221
|
+
faceRef.current?.removeAttribute('transform');
|
|
222
|
+
faceRef.current?.removeAttribute('opacity');
|
|
223
|
+
}, [active, faceRef]);
|
|
224
|
+
|
|
225
|
+
// The engine leaves turning emotes to the renderer while it is active.
|
|
226
|
+
const turnsInDepth = useMemo(() => () => activeRef.current, []);
|
|
227
|
+
|
|
228
|
+
return { active, onFrame, turnsInDepth };
|
|
229
|
+
}
|
package/src/klex.tsx
CHANGED
|
@@ -17,11 +17,17 @@ import {
|
|
|
17
17
|
import { Badge } from '@stagewise/ui';
|
|
18
18
|
|
|
19
19
|
import type { KlexActivity } from './activity';
|
|
20
|
+
import { KlexBook } from './book';
|
|
20
21
|
import { EMOTES, type Emote, type EmoteLook } from './emotes';
|
|
21
22
|
import { type Extra, KlexExtras } from './extras';
|
|
22
23
|
import { type Expression, KlexEyes } from './eyes';
|
|
23
24
|
import { phoneAnchor, waveAnchor } from './geometry';
|
|
24
25
|
import { createGlide, type KlexPosition, klexBody } from './glide';
|
|
26
|
+
import { REGION } from './inflate/atlas';
|
|
27
|
+
import type { InflateOptions, KlexMaterial } from './inflate/params';
|
|
28
|
+
import type { KlexAppearance } from './inflate/support';
|
|
29
|
+
import type { KlexTurn } from './inflate/turn';
|
|
30
|
+
import { useInflatedBody } from './inflate/use-inflated-body';
|
|
25
31
|
import { KlexLaptop } from './laptop';
|
|
26
32
|
import { observeVisibility } from './observe-visibility';
|
|
27
33
|
import { bodyOutline } from './outline';
|
|
@@ -40,8 +46,12 @@ export type {
|
|
|
40
46
|
BodyShape,
|
|
41
47
|
Expression,
|
|
42
48
|
Extra,
|
|
49
|
+
InflateOptions,
|
|
43
50
|
KlexActivity,
|
|
51
|
+
KlexAppearance,
|
|
52
|
+
KlexMaterial,
|
|
44
53
|
KlexPosition,
|
|
54
|
+
KlexTurn,
|
|
45
55
|
MovementMode,
|
|
46
56
|
};
|
|
47
57
|
export type EmoteId = Emote['id'];
|
|
@@ -109,6 +119,18 @@ export type KlexProps = {
|
|
|
109
119
|
onDepthChange?: (y: number) => void;
|
|
110
120
|
/** Also fires when the active emote is cancelled. */
|
|
111
121
|
onEmoteComplete?: () => void;
|
|
122
|
+
/**
|
|
123
|
+
* `inflated` shades the body as a soft 3D form through one WebGL context
|
|
124
|
+
* shared by every character on the page. It falls back to the flat SVG
|
|
125
|
+
* without WebGL2, with reduced motion, and in the `svg` layout.
|
|
126
|
+
*/
|
|
127
|
+
appearance?: KlexAppearance;
|
|
128
|
+
/** Surface of the inflated body. */
|
|
129
|
+
material?: KlexMaterial;
|
|
130
|
+
/** Overrides the shape's puffiness, edge profile, and bumps. */
|
|
131
|
+
inflate?: InflateOptions;
|
|
132
|
+
/** Inflated look only: turn the body in 3D, in degrees, or glance around. */
|
|
133
|
+
turn?: KlexTurn;
|
|
112
134
|
};
|
|
113
135
|
|
|
114
136
|
export function Klex({
|
|
@@ -134,6 +156,10 @@ export function Klex({
|
|
|
134
156
|
onMoveArrival,
|
|
135
157
|
onDepthChange,
|
|
136
158
|
onEmoteComplete,
|
|
159
|
+
appearance = 'flat',
|
|
160
|
+
material = 'plush',
|
|
161
|
+
inflate,
|
|
162
|
+
turn,
|
|
137
163
|
}: KlexProps) {
|
|
138
164
|
const settings = useMemo(() => {
|
|
139
165
|
const base =
|
|
@@ -162,7 +188,12 @@ export function Klex({
|
|
|
162
188
|
const bodyRef = useRef<SVGPathElement>(null);
|
|
163
189
|
const eyesRef = useRef<SVGGElement>(null);
|
|
164
190
|
const laptopRef = useRef<SVGGElement>(null);
|
|
191
|
+
const bookRef = useRef<SVGGElement>(null);
|
|
165
192
|
const motionRef = useRef<ReturnType<typeof createGlide> | null>(null);
|
|
193
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
194
|
+
const softShadowRef = useRef<SVGEllipseElement>(null);
|
|
195
|
+
const faceRef = useRef<SVGGElement>(null);
|
|
196
|
+
const shadowGradientId = useId();
|
|
166
197
|
const currentSettings = useRef(settings);
|
|
167
198
|
const startingPosition = useRef(initialPosition);
|
|
168
199
|
const restingBody = useMemo(
|
|
@@ -178,6 +209,24 @@ export function Klex({
|
|
|
178
209
|
const arrive = useEffectEvent(() => onMoveArrival?.());
|
|
179
210
|
const reportDepth = useEffectEvent((y: number) => onDepthChange?.(y));
|
|
180
211
|
const completeEmote = useEffectEvent(() => onEmoteComplete?.());
|
|
212
|
+
const {
|
|
213
|
+
active: inflated,
|
|
214
|
+
onFrame,
|
|
215
|
+
turnsInDepth,
|
|
216
|
+
} = useInflatedBody({
|
|
217
|
+
appearance,
|
|
218
|
+
reducedMotion,
|
|
219
|
+
layout,
|
|
220
|
+
canvasRef,
|
|
221
|
+
shadowRef: softShadowRef,
|
|
222
|
+
faceRef,
|
|
223
|
+
size,
|
|
224
|
+
shape,
|
|
225
|
+
color,
|
|
226
|
+
material,
|
|
227
|
+
inflate,
|
|
228
|
+
turn,
|
|
229
|
+
});
|
|
181
230
|
const configureSettings = useEffectEvent((next: typeof DEFAULT_GLIDE) => {
|
|
182
231
|
motionRef.current?.configure(
|
|
183
232
|
next,
|
|
@@ -208,10 +257,22 @@ export function Klex({
|
|
|
208
257
|
const body = bodyRef.current;
|
|
209
258
|
const eyes = eyesRef.current;
|
|
210
259
|
const laptop = laptopRef.current;
|
|
211
|
-
|
|
260
|
+
const book = bookRef.current;
|
|
261
|
+
if (!travel || !actor || !shadow || !body || !eyes || !laptop || !book)
|
|
262
|
+
return;
|
|
212
263
|
|
|
213
264
|
const motion = createGlide(
|
|
214
|
-
{
|
|
265
|
+
{
|
|
266
|
+
travel,
|
|
267
|
+
actor,
|
|
268
|
+
shadow,
|
|
269
|
+
body,
|
|
270
|
+
eyes,
|
|
271
|
+
laptop,
|
|
272
|
+
book,
|
|
273
|
+
onFrame,
|
|
274
|
+
turnsInDepth,
|
|
275
|
+
},
|
|
215
276
|
completeMove,
|
|
216
277
|
completeEmote,
|
|
217
278
|
setReactionLook,
|
|
@@ -225,7 +286,7 @@ export function Klex({
|
|
|
225
286
|
motion.dispose();
|
|
226
287
|
motionRef.current = null;
|
|
227
288
|
};
|
|
228
|
-
}, []);
|
|
289
|
+
}, [onFrame, turnsInDepth]);
|
|
229
290
|
|
|
230
291
|
useLayoutEffect(() => {
|
|
231
292
|
motionRef.current?.pause(paused || !visible);
|
|
@@ -320,6 +381,7 @@ export function Klex({
|
|
|
320
381
|
id={bodyId}
|
|
321
382
|
d={restingBody}
|
|
322
383
|
fill={color}
|
|
384
|
+
visibility={inflated ? 'hidden' : undefined}
|
|
323
385
|
data-testid="klex-body"
|
|
324
386
|
/>
|
|
325
387
|
<use
|
|
@@ -333,27 +395,29 @@ export function Klex({
|
|
|
333
395
|
pointerEvents="none"
|
|
334
396
|
data-testid="klex-body-outline"
|
|
335
397
|
/>
|
|
336
|
-
<g
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
<
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
398
|
+
<g ref={faceRef}>
|
|
399
|
+
<g
|
|
400
|
+
ref={eyesRef}
|
|
401
|
+
fill={eyeColor}
|
|
402
|
+
style={{ color: eyeColor }}
|
|
403
|
+
transform={`translate(${80 + shape.eyeX} ${shape.eyeY})`}
|
|
404
|
+
data-testid="klex-eyes"
|
|
405
|
+
>
|
|
406
|
+
<KlexEyes expression={reactionLook.expression ?? expression} />
|
|
407
|
+
<g style={{ color }}>
|
|
408
|
+
<KlexExtras
|
|
409
|
+
handAnchor={handAnchor}
|
|
410
|
+
phoneAnchor={earAnchor}
|
|
411
|
+
paused={paused || !visible}
|
|
412
|
+
transient={reactionLook.extra === 'wave'}
|
|
413
|
+
extra={extrasEnabled ? (reactionLook.extra ?? extra) : 'none'}
|
|
414
|
+
headTop={
|
|
415
|
+
Math.min(...shape.outline.map((point) => point[1])) -
|
|
416
|
+
shape.eyeY -
|
|
417
|
+
10
|
|
418
|
+
}
|
|
419
|
+
/>
|
|
420
|
+
</g>
|
|
357
421
|
</g>
|
|
358
422
|
</g>
|
|
359
423
|
<g
|
|
@@ -364,19 +428,52 @@ export function Klex({
|
|
|
364
428
|
>
|
|
365
429
|
<KlexLaptop shape={shape} />
|
|
366
430
|
</g>
|
|
431
|
+
<g
|
|
432
|
+
ref={bookRef}
|
|
433
|
+
opacity="0"
|
|
434
|
+
visibility={extrasEnabled ? 'visible' : 'hidden'}
|
|
435
|
+
style={{ color }}
|
|
436
|
+
data-testid="klex-book"
|
|
437
|
+
>
|
|
438
|
+
<KlexBook shape={shape} />
|
|
439
|
+
</g>
|
|
367
440
|
</>
|
|
368
441
|
);
|
|
369
442
|
const shadow = (
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
443
|
+
<>
|
|
444
|
+
<ellipse
|
|
445
|
+
ref={shadowRef}
|
|
446
|
+
cx="80"
|
|
447
|
+
cy="129"
|
|
448
|
+
rx="51"
|
|
449
|
+
ry="3"
|
|
450
|
+
fill="black"
|
|
451
|
+
opacity="0.12"
|
|
452
|
+
visibility={inflated ? 'hidden' : undefined}
|
|
453
|
+
data-testid="klex-shadow"
|
|
454
|
+
/>
|
|
455
|
+
{inflated ? (
|
|
456
|
+
<>
|
|
457
|
+
<defs>
|
|
458
|
+
<radialGradient id={shadowGradientId}>
|
|
459
|
+
<stop offset="0" stopColor="black" />
|
|
460
|
+
<stop offset="0.45" stopColor="black" stopOpacity="0.6" />
|
|
461
|
+
<stop offset="1" stopColor="black" stopOpacity="0" />
|
|
462
|
+
</radialGradient>
|
|
463
|
+
</defs>
|
|
464
|
+
<ellipse
|
|
465
|
+
ref={softShadowRef}
|
|
466
|
+
cx="80"
|
|
467
|
+
cy="129"
|
|
468
|
+
rx="56"
|
|
469
|
+
ry="7"
|
|
470
|
+
fill={`url(#${shadowGradientId})`}
|
|
471
|
+
opacity="0.32"
|
|
472
|
+
data-testid="klex-soft-shadow"
|
|
473
|
+
/>
|
|
474
|
+
</>
|
|
475
|
+
) : null}
|
|
476
|
+
</>
|
|
380
477
|
);
|
|
381
478
|
|
|
382
479
|
// Native SVG keeps scene transforms and painter order in one coordinate
|
|
@@ -412,12 +509,28 @@ export function Klex({
|
|
|
412
509
|
data-testid="klex-actor"
|
|
413
510
|
>
|
|
414
511
|
<KlexSpeech speech={speech} />
|
|
512
|
+
{inflated ? (
|
|
513
|
+
<canvas
|
|
514
|
+
ref={canvasRef}
|
|
515
|
+
data-testid="klex-inflated-body"
|
|
516
|
+
style={{
|
|
517
|
+
position: 'absolute',
|
|
518
|
+
left: REGION.x * scale,
|
|
519
|
+
top: (REGION.y - (avatarLayout ? -10 : 0)) * scale,
|
|
520
|
+
width: REGION.width * scale,
|
|
521
|
+
height: REGION.height * scale,
|
|
522
|
+
pointerEvents: 'none',
|
|
523
|
+
}}
|
|
524
|
+
/>
|
|
525
|
+
) : null}
|
|
415
526
|
<svg
|
|
416
527
|
viewBox={viewBox}
|
|
417
528
|
role={children ? undefined : 'img'}
|
|
418
529
|
aria-label={children ? undefined : (name ?? 'Klex')}
|
|
419
530
|
aria-hidden={children ? true : undefined}
|
|
420
531
|
className="block size-full! overflow-visible"
|
|
532
|
+
// Positioned, so the eyes paint above the absolutely placed body.
|
|
533
|
+
style={inflated ? { position: 'relative' } : undefined}
|
|
421
534
|
>
|
|
422
535
|
{drawing}
|
|
423
536
|
</svg>
|