"use client";
import { useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
/**
* Slingshot — an original take inspired by bencho.dev's "Slingshot".
* Pull the ball back and an elastic band stretches from the anchor ring,
* ticking as it lengthens and trembling once it hits its limit. Let go and
* the ball flies the other way, bounces off the walls of the card and then
* springs home to the ring.
*/
type Mode = "idle" | "drag" | "fly" | "home";
const W = 320;
const H = 260;
const R = 22; // ball radius
const MAX_PULL = 92;
const TICK = 18; // px of pull per tick mark
export function Slingshot({
fill = "dark",
power = 100,
bounce = 30,
onLaunch,
}: {
/** Ball colour: dark ink or a light ball. */
fill?: "dark" | "light";
/** 0–100: launch speed for a full pull. */
power?: number;
/** 0–100: how much speed the ball keeps off each wall. */
bounce?: number;
/** Called on release with the pull length (0–1). */
onLaunch?: (strength: number) => void;
}) {
const root = useRef<HTMLDivElement>(null);
const ball = useRef<HTMLDivElement>(null);
const arrow = useRef<SVGSVGElement>(null);
const bandA = useRef<SVGLineElement>(null);
const bandB = useRef<SVGLineElement>(null);
const ring = useRef<HTMLDivElement>(null);
const [ticks, setTicks] = useState(0);
const [mode, setModeState] = useState<Mode>("idle");
const s = useRef({ x: 0, y: 0, vx: 0, vy: 0, mode: "idle" as Mode, px: 0, py: 0, flyT: 0, angle: -45, ticks: 0, pointer: null as null | { ox: number; oy: number } });
const opts = useRef({ power, bounce, onLaunch });
opts.current = { power, bounce, onLaunch };
const setMode = (m: Mode) => {
s.current.mode = m;
setModeState(m);
};
const scaleOf = () => {
const el = root.current;
return el && el.offsetWidth ? el.getBoundingClientRect().width / el.offsetWidth || 1 : 1;
};
const release = () => {
const st = s.current;
const len = Math.hypot(st.x, st.y);
if (len < 8) {
setMode("home");
return;
}
const k = 0.11 * (0.25 + (opts.current.power / 100) * 0.75); // px/ms per px of pull
st.vx = -st.x * k * 60;
st.vy = -st.y * k * 60;
st.flyT = 0;
setMode("fly");
opts.current.onLaunch?.(Math.min(1, len / MAX_PULL));
};
const pullTo = (dx: number, dy: number) => {
const st = s.current;
const len = Math.hypot(dx, dy) || 1;
// Linear up to the limit, then a stiff rubber-band past it.
const eased = len <= MAX_PULL ? len : MAX_PULL + (len - MAX_PULL) * 0.08;
st.px = (dx / len) * eased;
st.py = (dy / len) * eased;
};
useEffect(() => {
let raf = 0;
let last = performance.now();
const frame = (now: number) => {
const dt = Math.min(0.033, (now - last) / 1000);
last = now;
const st = s.current;
const { bounce: bo } = opts.current;
let jitter = 0;
if (st.mode === "drag") {
st.x += (st.px - st.x) * Math.min(1, dt * 30);
st.y += (st.py - st.y) * Math.min(1, dt * 30);
const len = Math.hypot(st.x, st.y);
if (len >= MAX_PULL - 0.5) jitter = 1;
const t = Math.min(5, Math.floor(len / TICK));
if (t !== st.ticks) {
st.ticks = t;
setTicks(t);
}
if (len > 4) st.angle = (Math.atan2(-st.y, -st.x) * 180) / Math.PI;
} else if (st.mode === "fly") {
st.flyT += dt;
const drag = Math.pow(0.35, dt); // air friction
st.vx *= drag;
st.vy *= drag;
st.x += st.vx * dt;
st.y += st.vy * dt;
const e = 0.2 + (bo / 100) * 0.75;
const bx = W / 2 - R - 6;
const by = H / 2 - R - 6;
if (st.x > bx) { st.x = bx; st.vx = -Math.abs(st.vx) * e; }
if (st.x < -bx) { st.x = -bx; st.vx = Math.abs(st.vx) * e; }
if (st.y > by) { st.y = by; st.vy = -Math.abs(st.vy) * e; }
if (st.y < -by) { st.y = -by; st.vy = Math.abs(st.vy) * e; }
const speed = Math.hypot(st.vx, st.vy);
if (speed > 30) st.angle = (Math.atan2(st.vy, st.vx) * 180) / Math.PI;
if (st.flyT > 0.5 && (speed < 60 || st.flyT > 2.4)) setMode("home");
if (st.ticks) { st.ticks = 0; setTicks(0); }
} else if (st.mode === "home" || st.mode === "idle") {
const k = 120;
const c = 2 * 0.62 * Math.sqrt(k);
st.vx += (-k * st.x - c * st.vx) * dt;
st.vy += (-k * st.y - c * st.vy) * dt;
st.x += st.vx * dt;
st.y += st.vy * dt;
st.angle += (-45 - st.angle) * Math.min(1, dt * 6);
if (st.mode === "home" && Math.hypot(st.x, st.y) < 0.4 && Math.hypot(st.vx, st.vy) < 2) {
st.x = st.y = st.vx = st.vy = 0;
setMode("idle");
}
}
const jx = jitter ? (Math.random() - 0.5) * 2.4 : 0;
const jy = jitter ? (Math.random() - 0.5) * 2.4 : 0;
const x = st.x + jx;
const y = st.y + jy;
if (ball.current) {
const stretch = st.mode === "drag" ? Math.min(Math.hypot(st.x, st.y) / MAX_PULL, 1) * 0.08 : 0;
ball.current.style.transform = `translate3d(${x.toFixed(2)}px, ${y.toFixed(2)}px, 0) scale(${(1 - stretch).toFixed(3)})`;
}
if (arrow.current) arrow.current.style.transform = `rotate(${(st.angle + 45).toFixed(1)}deg)`;
// Band: two strands from the ring's sides to the ball, visible only while pulled.
const len = Math.hypot(st.x, st.y);
const show = st.mode === "drag" && len > 3;
const nx = len ? -st.y / len : 0;
const ny = len ? st.x / len : 0;
const cx = W / 2;
const cy = H / 2;
const set = (el: SVGLineElement | null, side: number) => {
if (!el) return;
el.setAttribute("x1", String(cx + nx * (R + 10) * side));
el.setAttribute("y1", String(cy + ny * (R + 10) * side));
el.setAttribute("x2", String(cx + x + nx * R * 0.55 * side));
el.setAttribute("y2", String(cy + y + ny * R * 0.55 * side));
el.style.opacity = show ? "1" : "0";
el.setAttribute("stroke-width", String(Math.max(1.2, 3.2 - (len / MAX_PULL) * 2)));
};
set(bandA.current, 1);
set(bandB.current, -1);
if (ring.current) ring.current.style.transform = `scale(${st.mode === "drag" ? 1 + Math.min(len / MAX_PULL, 1) * 0.08 : 1})`;
raf = requestAnimationFrame(frame);
};
raf = requestAnimationFrame(frame);
return () => cancelAnimationFrame(raf);
}, []);
const dark = fill === "dark";
return (
<div ref={root} className="relative select-none touch-none" style={{ width: W, height: H }}>
<svg className="pointer-events-none absolute inset-0" width={W} height={H} aria-hidden>
<line ref={bandA} stroke="var(--b-fg)" strokeOpacity={0.55} strokeLinecap="round" style={{ transition: "opacity 120ms" }} />
<line ref={bandB} stroke="var(--b-fg)" strokeOpacity={0.55} strokeLinecap="round" style={{ transition: "opacity 120ms" }} />
</svg>
{/* Anchor ring with tick marks that light as the band lengthens */}
<div
ref={ring}
aria-hidden
className="absolute rounded-full bg-[var(--b-line)] transition-transform duration-150"
style={{ left: W / 2 - R - 10, top: H / 2 - R - 10, width: (R + 10) * 2, height: (R + 10) * 2, boxShadow: "inset 0 0 0 1px var(--b-line)" }}
>
{[0, 1, 2, 3, 4].map((i) => (
<span
key={i}
className="absolute left-1/2 top-1/2 h-[5px] w-[2px] rounded-full transition-[background-color,opacity] duration-100"
style={{
transform: `rotate(${i * 72}deg) translateY(-${R + 16}px)`,
transformOrigin: "0 0",
background: "var(--b-fg)",
opacity: i < ticks ? 0.7 : 0.12,
}}
/>
))}
</div>
<div
ref={ball}
role="button"
tabIndex={0}
aria-label="Slingshot ball. Drag to pull back and let go to launch, or press Space."
className={cn(
"absolute flex cursor-grab items-center justify-center rounded-full will-change-transform outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg-muted)]",
mode === "drag" && "cursor-grabbing",
dark ? "bg-[var(--b-invert)] text-[var(--b-invert-fg)]" : "bg-[var(--b-surface)] text-[var(--b-fg)]"
)}
style={{ left: W / 2 - R, top: H / 2 - R, width: R * 2, height: R * 2, boxShadow: "0 6px 16px -6px rgba(0,0,0,0.45), 0 0 0 1px var(--b-line)" }}
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId);
const st = s.current;
const k = scaleOf();
st.pointer = { ox: e.clientX - st.x * k, oy: e.clientY - st.y * k };
st.vx = st.vy = 0;
st.px = st.x;
st.py = st.y;
setMode("drag");
}}
onPointerMove={(e) => {
const st = s.current;
if (st.mode !== "drag" || !st.pointer) return;
const k = scaleOf();
pullTo((e.clientX - st.pointer.ox) / k, (e.clientY - st.pointer.oy) / k);
}}
onPointerUp={() => {
if (s.current.mode !== "drag") return;
s.current.pointer = null;
release();
}}
onPointerCancel={() => {
s.current.pointer = null;
setMode("home");
}}
onKeyDown={(e) => {
const st = s.current;
const step = 14;
const dirs: Record<string, [number, number]> = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step] };
if (dirs[e.key]) {
if (st.mode !== "drag") {
st.px = st.x;
st.py = st.y;
setMode("drag");
}
pullTo(st.px + dirs[e.key][0], st.py + dirs[e.key][1]);
} else if (e.key === " " || e.key === "Enter") {
if (st.mode !== "drag" || Math.hypot(st.px, st.py) < 8) {
st.px = st.py = 0;
pullTo(-MAX_PULL * 0.7, MAX_PULL * 0.7);
st.x = st.px;
st.y = st.py;
}
release();
} else if (e.key === "Escape") {
setMode("home");
} else return;
e.preventDefault();
}}
>
<svg ref={arrow} width="18" height="18" viewBox="0 0 18 18" fill="none" aria-hidden>
<path d="M5 13 13 5M6.5 5H13v6.5" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</div>
<span className="sr-only" aria-live="polite">
{mode === "fly" ? "Launched" : mode === "drag" ? `Pulled ${ticks} of 5` : ""}
</span>
</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;
}