@klex/mascot 0.3.0 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@klex/mascot",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "The animated Klex mascot: a React component, its GSAP motion engine, body presets, and SVG helpers.",
5
5
  "type": "module",
6
6
  "license": "MIT AND CC-BY-4.0",
@@ -19,6 +19,7 @@
19
19
  ],
20
20
  "exports": {
21
21
  ".": "./src/index.ts",
22
+ "./glide": "./src/glide.ts",
22
23
  "./svg": "./src/svg.ts"
23
24
  },
24
25
  "repository": {
package/src/eyes.tsx CHANGED
@@ -84,27 +84,21 @@ export function KlexEyes({
84
84
  const mask = `${id}-${side}`;
85
85
  const sign = index === 0 ? 1 : -1;
86
86
  const cut =
87
- expression === 'angry' ||
88
- expression === 'sad' ||
89
- expression === 'focused'
87
+ expression === 'angry' || expression === 'sad'
90
88
  ? -2
91
- : expression === 'annoyed' || expression === 'suspicious'
92
- ? -0.5
93
- : expression === 'sleepy'
94
- ? 1
95
- : expression === 'skeptical' && index === 0
96
- ? -0.5
97
- : expression === 'unsure' && index === 1
98
- ? -2
99
- : -10;
89
+ : expression === 'focused'
90
+ ? -4
91
+ : expression === 'annoyed' || expression === 'suspicious'
92
+ ? -0.5
93
+ : expression === 'sleepy'
94
+ ? 1
95
+ : expression === 'skeptical' && index === 0
96
+ ? -0.5
97
+ : expression === 'unsure' && index === 1
98
+ ? -2
99
+ : -10;
100
100
  const slope =
101
- (expression === 'angry'
102
- ? 27
103
- : expression === 'sad'
104
- ? -24
105
- : expression === 'focused'
106
- ? 12
107
- : 0) * sign;
101
+ (expression === 'angry' ? 27 : expression === 'sad' ? -24 : 0) * sign;
108
102
  const shiftX =
109
103
  expression === 'suspicious'
110
104
  ? 1.5
package/src/klex.tsx CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  type ReactNode,
7
7
  type Ref,
8
8
  useEffectEvent,
9
+ useId,
9
10
  useImperativeHandle,
10
11
  useLayoutEffect,
11
12
  useMemo,
@@ -23,6 +24,7 @@ import { phoneAnchor, waveAnchor } from './geometry';
23
24
  import { createGlide, type KlexPosition, klexBody } from './glide';
24
25
  import { KlexLaptop } from './laptop';
25
26
  import { observeVisibility } from './observe-visibility';
27
+ import { bodyOutline } from './outline';
26
28
  import {
27
29
  BODY_SHAPES,
28
30
  type BodyShape,
@@ -140,7 +142,10 @@ export function Klex({
140
142
  DEFAULT_GLIDE;
141
143
  return movementMode ? { ...base, mode: movementMode } : base;
142
144
  }, [suppliedSettings, movementMode]);
145
+ const bodyId = useId();
146
+ const bodyClipId = useId();
143
147
  const eyeColor = getKlexEyeColor(color);
148
+ const outline = useMemo(() => bodyOutline(color, size), [color, size]);
144
149
  const [expression, setExpression] = useState(initialExpression);
145
150
  const [extra, setExtra] = useState(initialExtra);
146
151
  const [paused, setPaused] = useState(false);
@@ -305,12 +310,29 @@ export function Klex({
305
310
  const drawing = (
306
311
  <>
307
312
  <title>{name ?? 'Klex'}</title>
313
+ <defs>
314
+ <clipPath id={bodyClipId} clipPathUnits="userSpaceOnUse">
315
+ <use href={`#${bodyId}`} />
316
+ </clipPath>
317
+ </defs>
308
318
  <path
309
319
  ref={bodyRef}
320
+ id={bodyId}
310
321
  d={restingBody}
311
322
  fill={color}
312
323
  data-testid="klex-body"
313
324
  />
325
+ <use
326
+ href={`#${bodyId}`}
327
+ fill="none"
328
+ stroke={outline.color}
329
+ // Clipping removes the outer half. Small sizes get more relative weight.
330
+ strokeWidth={outline.width}
331
+ strokeLinejoin="round"
332
+ clipPath={`url(#${bodyClipId})`}
333
+ pointerEvents="none"
334
+ data-testid="klex-body-outline"
335
+ />
314
336
  <g
315
337
  ref={eyesRef}
316
338
  fill={eyeColor}
package/src/outline.ts ADDED
@@ -0,0 +1,56 @@
1
+ const LOG_SIZE_RANGE = Math.log(5);
2
+
3
+ const clamp01 = (value: number) => Math.max(0, Math.min(1, value));
4
+ const smoothstep = (value: number) => value * value * (3 - 2 * value);
5
+
6
+ // One linear-sRGB → OKLab conversion supplies both lightness and chroma.
7
+ // Hue is not needed. Keep final mixing in sRGB to preserve the tuned colors.
8
+ function hexOklab(color: string) {
9
+ if (!/^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(color)) return null;
10
+ const hex = color.slice(1);
11
+ const rgb = Number.parseInt(
12
+ hex.length === 3 ? [...hex].map((digit) => digit + digit).join('') : hex,
13
+ 16,
14
+ );
15
+ const linear = (channel: number) => {
16
+ const value = channel / 255;
17
+ return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
18
+ };
19
+ const r = linear(rgb >> 16);
20
+ const g = linear((rgb >> 8) & 255);
21
+ const b = linear(rgb & 255);
22
+ const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
23
+ const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
24
+ const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
25
+ return {
26
+ lightness: 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
27
+ chroma: Math.hypot(
28
+ 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
29
+ 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
30
+ ),
31
+ };
32
+ }
33
+
34
+ export function bodyOutline(color: string, size: number) {
35
+ const lab = hexOklab(color);
36
+ // Unknown CSS colors retain base mixing without guessing their chroma.
37
+ const chromaWeight = lab ? 1.5 - 0.7 * clamp01(lab.chroma / 0.25) : 1;
38
+ const darkBoost = lab
39
+ ? 1 + smoothstep(clamp01((0.6 - lab.lightness) / 0.4))
40
+ : 1;
41
+ const smallness = clamp01((160 - size) / 128);
42
+ const colorWeight = chromaWeight * (1 + 0.2 * smoothstep(smallness));
43
+ const mix = (base: number, ink: string) => {
44
+ // Integer hundredths keep the complementary percentages exact.
45
+ const amount = Math.round(base * colorWeight * 100);
46
+ return `color-mix(in srgb, ${color} ${(10000 - amount) / 100}%, ${ink} ${amount / 100}%)`;
47
+ };
48
+ // Log-size interpolation keeps actual pixel thickness increasing with size.
49
+ const widthWeight = smoothstep(
50
+ Math.log(160 / (160 - 128 * smallness)) / LOG_SIZE_RANGE,
51
+ );
52
+ return {
53
+ color: `light-dark(${mix(12, 'black')}, ${mix(8 * darkBoost, 'white')})`,
54
+ width: 3 * 2.25 ** widthWeight,
55
+ };
56
+ }