@klex/mascot 0.4.0 → 0.5.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,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
@@ -22,6 +22,11 @@ import { type Extra, KlexExtras } from './extras';
22
22
  import { type Expression, KlexEyes } from './eyes';
23
23
  import { phoneAnchor, waveAnchor } from './geometry';
24
24
  import { createGlide, type KlexPosition, klexBody } from './glide';
25
+ import { REGION } from './inflate/atlas';
26
+ import type { InflateOptions, KlexMaterial } from './inflate/params';
27
+ import type { KlexAppearance } from './inflate/support';
28
+ import type { KlexTurn } from './inflate/turn';
29
+ import { useInflatedBody } from './inflate/use-inflated-body';
25
30
  import { KlexLaptop } from './laptop';
26
31
  import { observeVisibility } from './observe-visibility';
27
32
  import { bodyOutline } from './outline';
@@ -40,8 +45,12 @@ export type {
40
45
  BodyShape,
41
46
  Expression,
42
47
  Extra,
48
+ InflateOptions,
43
49
  KlexActivity,
50
+ KlexAppearance,
51
+ KlexMaterial,
44
52
  KlexPosition,
53
+ KlexTurn,
45
54
  MovementMode,
46
55
  };
47
56
  export type EmoteId = Emote['id'];
@@ -109,6 +118,18 @@ export type KlexProps = {
109
118
  onDepthChange?: (y: number) => void;
110
119
  /** Also fires when the active emote is cancelled. */
111
120
  onEmoteComplete?: () => void;
121
+ /**
122
+ * `inflated` shades the body as a soft 3D form through one WebGL context
123
+ * shared by every character on the page. It falls back to the flat SVG
124
+ * without WebGL2, with reduced motion, and in the `svg` layout.
125
+ */
126
+ appearance?: KlexAppearance;
127
+ /** Surface of the inflated body. */
128
+ material?: KlexMaterial;
129
+ /** Overrides the shape's puffiness, edge profile, and bumps. */
130
+ inflate?: InflateOptions;
131
+ /** Inflated look only: turn the body in 3D, in degrees, or glance around. */
132
+ turn?: KlexTurn;
112
133
  };
113
134
 
114
135
  export function Klex({
@@ -134,6 +155,10 @@ export function Klex({
134
155
  onMoveArrival,
135
156
  onDepthChange,
136
157
  onEmoteComplete,
158
+ appearance = 'flat',
159
+ material = 'plush',
160
+ inflate,
161
+ turn,
137
162
  }: KlexProps) {
138
163
  const settings = useMemo(() => {
139
164
  const base =
@@ -163,6 +188,10 @@ export function Klex({
163
188
  const eyesRef = useRef<SVGGElement>(null);
164
189
  const laptopRef = useRef<SVGGElement>(null);
165
190
  const motionRef = useRef<ReturnType<typeof createGlide> | null>(null);
191
+ const canvasRef = useRef<HTMLCanvasElement>(null);
192
+ const softShadowRef = useRef<SVGEllipseElement>(null);
193
+ const faceRef = useRef<SVGGElement>(null);
194
+ const shadowGradientId = useId();
166
195
  const currentSettings = useRef(settings);
167
196
  const startingPosition = useRef(initialPosition);
168
197
  const restingBody = useMemo(
@@ -178,6 +207,24 @@ export function Klex({
178
207
  const arrive = useEffectEvent(() => onMoveArrival?.());
179
208
  const reportDepth = useEffectEvent((y: number) => onDepthChange?.(y));
180
209
  const completeEmote = useEffectEvent(() => onEmoteComplete?.());
210
+ const {
211
+ active: inflated,
212
+ onFrame,
213
+ turnsInDepth,
214
+ } = useInflatedBody({
215
+ appearance,
216
+ reducedMotion,
217
+ layout,
218
+ canvasRef,
219
+ shadowRef: softShadowRef,
220
+ faceRef,
221
+ size,
222
+ shape,
223
+ color,
224
+ material,
225
+ inflate,
226
+ turn,
227
+ });
181
228
  const configureSettings = useEffectEvent((next: typeof DEFAULT_GLIDE) => {
182
229
  motionRef.current?.configure(
183
230
  next,
@@ -211,7 +258,7 @@ export function Klex({
211
258
  if (!travel || !actor || !shadow || !body || !eyes || !laptop) return;
212
259
 
213
260
  const motion = createGlide(
214
- { travel, actor, shadow, body, eyes, laptop },
261
+ { travel, actor, shadow, body, eyes, laptop, onFrame, turnsInDepth },
215
262
  completeMove,
216
263
  completeEmote,
217
264
  setReactionLook,
@@ -225,7 +272,7 @@ export function Klex({
225
272
  motion.dispose();
226
273
  motionRef.current = null;
227
274
  };
228
- }, []);
275
+ }, [onFrame, turnsInDepth]);
229
276
 
230
277
  useLayoutEffect(() => {
231
278
  motionRef.current?.pause(paused || !visible);
@@ -320,6 +367,7 @@ export function Klex({
320
367
  id={bodyId}
321
368
  d={restingBody}
322
369
  fill={color}
370
+ visibility={inflated ? 'hidden' : undefined}
323
371
  data-testid="klex-body"
324
372
  />
325
373
  <use
@@ -333,27 +381,29 @@ export function Klex({
333
381
  pointerEvents="none"
334
382
  data-testid="klex-body-outline"
335
383
  />
336
- <g
337
- ref={eyesRef}
338
- fill={eyeColor}
339
- style={{ color: eyeColor }}
340
- transform={`translate(${80 + shape.eyeX} ${shape.eyeY})`}
341
- data-testid="klex-eyes"
342
- >
343
- <KlexEyes expression={reactionLook.expression ?? expression} />
344
- <g style={{ color }}>
345
- <KlexExtras
346
- handAnchor={handAnchor}
347
- phoneAnchor={earAnchor}
348
- paused={paused || !visible}
349
- transient={reactionLook.extra === 'wave'}
350
- extra={extrasEnabled ? (reactionLook.extra ?? extra) : 'none'}
351
- headTop={
352
- Math.min(...shape.outline.map((point) => point[1])) -
353
- shape.eyeY -
354
- 10
355
- }
356
- />
384
+ <g ref={faceRef}>
385
+ <g
386
+ ref={eyesRef}
387
+ fill={eyeColor}
388
+ style={{ color: eyeColor }}
389
+ transform={`translate(${80 + shape.eyeX} ${shape.eyeY})`}
390
+ data-testid="klex-eyes"
391
+ >
392
+ <KlexEyes expression={reactionLook.expression ?? expression} />
393
+ <g style={{ color }}>
394
+ <KlexExtras
395
+ handAnchor={handAnchor}
396
+ phoneAnchor={earAnchor}
397
+ paused={paused || !visible}
398
+ transient={reactionLook.extra === 'wave'}
399
+ extra={extrasEnabled ? (reactionLook.extra ?? extra) : 'none'}
400
+ headTop={
401
+ Math.min(...shape.outline.map((point) => point[1])) -
402
+ shape.eyeY -
403
+ 10
404
+ }
405
+ />
406
+ </g>
357
407
  </g>
358
408
  </g>
359
409
  <g
@@ -367,16 +417,40 @@ export function Klex({
367
417
  </>
368
418
  );
369
419
  const shadow = (
370
- <ellipse
371
- ref={shadowRef}
372
- cx="80"
373
- cy="129"
374
- rx="51"
375
- ry="3"
376
- fill="black"
377
- opacity="0.12"
378
- data-testid="klex-shadow"
379
- />
420
+ <>
421
+ <ellipse
422
+ ref={shadowRef}
423
+ cx="80"
424
+ cy="129"
425
+ rx="51"
426
+ ry="3"
427
+ fill="black"
428
+ opacity="0.12"
429
+ visibility={inflated ? 'hidden' : undefined}
430
+ data-testid="klex-shadow"
431
+ />
432
+ {inflated ? (
433
+ <>
434
+ <defs>
435
+ <radialGradient id={shadowGradientId}>
436
+ <stop offset="0" stopColor="black" />
437
+ <stop offset="0.45" stopColor="black" stopOpacity="0.6" />
438
+ <stop offset="1" stopColor="black" stopOpacity="0" />
439
+ </radialGradient>
440
+ </defs>
441
+ <ellipse
442
+ ref={softShadowRef}
443
+ cx="80"
444
+ cy="129"
445
+ rx="56"
446
+ ry="7"
447
+ fill={`url(#${shadowGradientId})`}
448
+ opacity="0.32"
449
+ data-testid="klex-soft-shadow"
450
+ />
451
+ </>
452
+ ) : null}
453
+ </>
380
454
  );
381
455
 
382
456
  // Native SVG keeps scene transforms and painter order in one coordinate
@@ -412,12 +486,28 @@ export function Klex({
412
486
  data-testid="klex-actor"
413
487
  >
414
488
  <KlexSpeech speech={speech} />
489
+ {inflated ? (
490
+ <canvas
491
+ ref={canvasRef}
492
+ data-testid="klex-inflated-body"
493
+ style={{
494
+ position: 'absolute',
495
+ left: REGION.x * scale,
496
+ top: (REGION.y - (avatarLayout ? -10 : 0)) * scale,
497
+ width: REGION.width * scale,
498
+ height: REGION.height * scale,
499
+ pointerEvents: 'none',
500
+ }}
501
+ />
502
+ ) : null}
415
503
  <svg
416
504
  viewBox={viewBox}
417
505
  role={children ? undefined : 'img'}
418
506
  aria-label={children ? undefined : (name ?? 'Klex')}
419
507
  aria-hidden={children ? true : undefined}
420
508
  className="block size-full! overflow-visible"
509
+ // Positioned, so the eyes paint above the absolutely placed body.
510
+ style={inflated ? { position: 'relative' } : undefined}
421
511
  >
422
512
  {drawing}
423
513
  </svg>