"use client";
import { useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
/**
* Eye Tracker — an original take inspired by bencho.dev's "Eye tracker".
* A soft cube with two eyes painted on an imaginary ball inside it. The
* pointer turns the ball, so an eye heading for the rim travels less and
* flattens along its direction of travel, which is what sells the depth.
*/
type Shape = "squircle" | "capsule" | "circle";
type Eyes = "slant" | "dots" | "squares";
const EYE: Record<Eyes, { w: number; h: number; r: string }> = {
slant: { w: 9, h: 16, r: "999px" },
dots: { w: 10, h: 10, r: "999px" },
squares: { w: 10, h: 10, r: "3px" },
};
export function EyeTracker({
size = 88,
shape = "squircle",
eyes = "slant",
follow = 60,
bounce = 30,
}: {
/** Edge of the cube in px. */
size?: number;
shape?: Shape;
eyes?: Eyes;
/** 0–100: how quickly the gaze catches up with the pointer. */
follow?: number;
/** 0–100: how much the gaze overshoots before it settles. */
bounce?: number;
}) {
const root = useRef<HTMLDivElement>(null);
const eyeEls = useRef<(HTMLSpanElement | null)[]>([]);
const target = useRef({ yaw: 0, pitch: 0 });
const keyAim = useRef<{ yaw: number; pitch: number } | null>(null);
const opts = useRef({ follow, bounce, size });
opts.current = { follow, bounce, size };
useEffect(() => {
const el = root.current;
if (!el) return;
const field = (el.closest(".bencho") as HTMLElement | null) ?? el.parentElement ?? el;
const MAX = 1.25; // ~70° each way
const onMove = (e: PointerEvent) => {
const f = field.getBoundingClientRect();
const inside = e.clientX >= f.left && e.clientX <= f.right && e.clientY >= f.top && e.clientY <= f.bottom;
if (!inside) {
target.current = { yaw: 0, pitch: 0 };
return;
}
keyAim.current = null;
const c = el.getBoundingClientRect();
const dx = e.clientX - (c.left + c.width / 2);
const dy = e.clientY - (c.top + c.height / 2);
const reach = Math.max(f.width, f.height) * 0.3;
const sat = (v: number) => Math.tanh(v / reach) * MAX;
target.current = { yaw: sat(dx), pitch: -sat(dy) };
};
const onLeave = () => (target.current = { yaw: 0, pitch: 0 });
window.addEventListener("pointermove", onMove);
document.documentElement.addEventListener("pointerleave", onLeave);
// a damped spring per axis, integrated by hand so every frame is one style write
const s = { yaw: 0, pitch: 0, vy: 0, vp: 0 };
let raf = 0;
let last = performance.now();
const tick = (now: number) => {
const dt = Math.min(0.032, (now - last) / 1000);
last = now;
const { follow: fo, bounce: bo, size: sz } = opts.current;
const stiff = 40 + fo * 4; // 40–440
const zeta = 1.05 - (bo / 100) * 0.8; // 1.05 (no overshoot) – 0.25
const damp = 2 * zeta * Math.sqrt(stiff);
const aim = keyAim.current ?? target.current;
s.vy += (stiff * (aim.yaw - s.yaw) - damp * s.vy) * dt;
s.vp += (stiff * (aim.pitch - s.pitch) - damp * s.vp) * dt;
s.yaw += s.vy * dt;
s.pitch += s.vp * dt;
const R = sz * 0.5; // radius of the imaginary ball
[-1, 1].forEach((side, i) => {
const node = eyeEls.current[i];
if (!node) return;
// the eye's resting point on the ball's front face
const x0 = side * 0.25;
const y0 = 0.06;
const z0 = Math.sqrt(1 - x0 * x0 - y0 * y0);
// turn about the vertical axis, then tilt about the horizontal one
const x1 = x0 * Math.cos(s.yaw) + z0 * Math.sin(s.yaw);
const z1 = -x0 * Math.sin(s.yaw) + z0 * Math.cos(s.yaw);
const y2 = y0 * Math.cos(s.pitch) + z1 * Math.sin(s.pitch);
const z2 = -y0 * Math.sin(s.pitch) + z1 * Math.cos(s.pitch);
// flatten along the direction the eye has travelled from the centre
const ang = Math.atan2(-y2, x1);
const flat = Math.max(0.18, Math.min(1, 0.3 + 0.7 * z2));
const hide = z2 < 0.02 ? 0 : 1;
node.style.transform =
`translate(${(x1 * R).toFixed(2)}px, ${(-y2 * R).toFixed(2)}px) ` +
`rotate(${ang}rad) scaleX(${flat.toFixed(3)}) rotate(${-ang}rad)`;
node.style.opacity = String(hide);
});
raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("pointermove", onMove);
document.documentElement.removeEventListener("pointerleave", onLeave);
};
}, []);
const nudge = (e: React.KeyboardEvent) => {
const step = 0.35;
const cur = keyAim.current ?? { ...target.current };
const map: Record<string, [number, number]> = {
ArrowLeft: [-step, 0],
ArrowRight: [step, 0],
ArrowUp: [0, step],
ArrowDown: [0, -step],
};
if (e.key === "Escape" || e.key === "Home") {
keyAim.current = { yaw: 0, pitch: 0 };
e.preventDefault();
return;
}
const d = map[e.key];
if (!d) return;
e.preventDefault();
const clamp = (v: number) => Math.max(-1.05, Math.min(1.05, v));
keyAim.current = { yaw: clamp(cur.yaw + d[0]), pitch: clamp(cur.pitch + d[1]) };
};
const eye = EYE[eyes];
const w = shape === "capsule" ? size * 1.45 : size;
return (
<div
ref={root}
tabIndex={0}
role="img"
aria-label="Two eyes that follow your pointer. Arrow keys look around, Escape looks back."
onKeyDown={nudge}
className={cn(
"relative flex items-center justify-center bg-[var(--b-surface)] shadow-[var(--b-shadow)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30",
shape === "circle" ? "rounded-full" : shape === "capsule" ? "rounded-full" : ""
)}
style={{ width: w, height: size, borderRadius: shape === "squircle" ? size * 0.32 : undefined }}
>
{[0, 1].map((i) => (
<span
key={i}
ref={(n) => {
eyeEls.current[i] = n;
}}
aria-hidden
className="absolute left-1/2 top-1/2 bg-[var(--b-fg)] will-change-transform"
style={{
width: eye.w,
height: eye.h,
marginLeft: -eye.w / 2,
marginTop: -eye.h / 2,
borderRadius: eye.r,
transform: `translate(${(i ? 1 : -1) * size * 0.125}px, ${-size * 0.03}px)`,
}}
/>
))}
</div>
);
}
"use client";
import type { ReactNode } from "react";
import { cn } from "@/lib/utils";
/**
* Neutral stage the Bencho Originals sit on. The block renders at its
* natural size and is scaled as a whole (like a device preview), so
* strokes, radii and type keep their proportions at any stage size.
*/
export function BenchoStage({
children,
scale = 1.25,
className,
}: {
children: ReactNode;
scale?: number;
className?: string;
}) {
return (
<div
className={cn(
"bencho bencho-font relative flex items-center justify-center overflow-hidden rounded-2xl text-[var(--b-fg)]",
"bg-[var(--b-stage)]",
className
)}
>
<div style={{ transform: `scale(${scale})` }} className="origin-center">
{children}
</div>
</div>
);
}
/* Bencho Originals — shared surface tokens (light / dark). */
.bencho {
--b-stage: #f1f1f1;
--b-surface: #ffffff;
--b-surface-2: #f1f1f1;
--b-fg: #171717;
--b-fg-muted: rgb(23 23 23 / 0.5);
--b-ink: #171717;
--b-invert: #171717;
--b-invert-fg: #ffffff;
--b-line: rgb(0 0 0 / 0.06);
--b-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 0 0 1px rgb(0 0 0 / 0.04);
}
.dark .bencho {
--b-stage: #171717;
--b-surface: #262626;
--b-surface-2: #333333;
--b-fg: #ffffff;
--b-fg-muted: rgb(255 255 255 / 0.55);
--b-ink: #ffffff;
--b-invert: #ffffff;
--b-invert-fg: #262626;
--b-line: rgb(255 255 255 / 0.06);
--b-shadow: none;
}
.bencho-font {
font-family: var(--font-bencho-inter), var(--font-geist-sans), ui-sans-serif, system-ui, sans-serif;
font-feature-settings: "cv11", "ss01";
-webkit-font-smoothing: antialiased;
}
.bencho-tabular { font-variant-numeric: tabular-nums; }
/* Code panel: Shiki dual-theme output, with line numbers */
.bencho-code pre.shiki { background: transparent !important; margin: 0; }
.bencho-code .shiki span { color: var(--shiki-light); }
.dark .bencho-code .shiki span { color: var(--shiki-dark); }
.bencho-code .shiki code { counter-reset: line; display: block; }
.bencho-code .shiki .line::before {
counter-increment: line;
content: counter(line);
display: inline-block;
width: 2.2em;
margin-right: 1.1em;
text-align: right;
opacity: 0.32;
}