"use client"; /** * waxseal.tsx — an envelope with a brass seal floating above it. * Hover lowers the stamp toward the paper; click presses a wax seal into it, then the wax fades away again. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type MouseEvent, type Ref, type ReactNode } from "react"; type Palette = { paper: string; wax: string; brass: string; wood: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; crest?: "fleur" | "star" | "letter"; }; const W = 240; const H = 92; const PAD_X = 12; const PAD_T = 150; const PAD_B = 12; const OW = W + PAD_X * 2; const OH = PAD_T + H + PAD_B; const CX = OW / 2; // stamp / wax centre x in overlay coords const CY = PAD_T + 48; // wax centre y const HEAD_Y = 113; // head side-band centre at rest const CONTACT = CY - 12 - 120; // translateY at which the head sinks into the wax const HOVER_Y = 24; const LIFT_Y = 14; const ACCENT = "#e2bb63"; const LOBES = 14; const BASE_R = [32, 27, 31, 28, 33, 27, 32, 28, 31, 27, 33, 28, 32, 29]; const RESEAL = 3400; const RAYS = [198, 216, 234, 306, 324, 342]; const DEFAULT_PALETTE: Palette = { paper: "#efe6d6", wax: "#8e0f12", brass: "#d9b25e", wood: "#8a3b22" }; const clamp01 = (u: number) => (u < 0 ? 0 : u > 1 ? 1 : u); const outCubic = (u: number) => 1 - Math.pow(1 - clamp01(u), 3); const inOut = (u: number) => { const v = clamp01(u); return v * v * (3 - 2 * v); }; const lerp = (a: number, b: number, u: number) => a + (b - a) * u; function blob(f: number, bead: number, phase: number) { const pts: number[] = []; for (let i = 0; i < LOBES; i++) { const a = (i / LOBES) * Math.PI * 2 + phase; const r = (BASE_R[i] + (i === 3 || i === 9 ? bead : 0)) * f; pts.push(Math.cos(a) * r, Math.sin(a) * r); } const n = LOBES; const mx = (i: number) => (pts[(i % n) * 2] + pts[((i + 1) % n) * 2]) / 2; const my = (i: number) => (pts[(i % n) * 2 + 1] + pts[((i + 1) % n) * 2 + 1]) / 2; let d = `M${mx(n - 1).toFixed(2)},${my(n - 1).toFixed(2)}`; for (let i = 0; i < n; i++) d += `Q${pts[i * 2].toFixed(2)},${pts[i * 2 + 1].toFixed(2)} ${mx(i).toFixed(2)},${my(i).toFixed(2)}`; return d + "Z"; } const FLEUR = "M0,-15 C6,-9 6,-1 0,7 C-6,-1 -6,-9 0,-15Z M-2,4 C-8,-8 -16,-6 -15,1 C-14,6 -9,7 -5,5 Z M2,4 C8,-8 16,-6 15,1 C14,6 9,7 5,5 Z M-9,7 L9,7 L9,10 L-9,10 Z M-8,11 C-5,15 -2,14 0,18 C2,14 5,15 8,11 Z"; const STAR = "M0,-16 L4.6,-5 L16,-4.6 L7,3 L10,14.5 L0,8 L-10,14.5 L-7,3 L-16,-4.6 L-4.6,-5Z"; export function WaxSealButton({ children = "seal", palette = DEFAULT_PALETTE, crest = "fleur", onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); useImperativeHandle(ref, () => btnRef.current!, []); const wrapRef = useRef(null); const envRef = useRef(null); const labelRef = useRef(null); const stampRef = useRef(null); const waxRef = useRef(null); const blobRef = useRef(null); const crestRef = useRef(null); const specRef = useRef(null); const tintRef = useRef(null); const ticksRef = useRef(null); const wetRef = useRef(null); const shadowRef = useRef(null); const api = useRef<{ play: () => void }>({ play: () => {} }); const uid = `tb-waxseal-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const letter = (typeof children === "string" ? children.trim()[0] : "") || "s"; const crestShape = crest === "star" ? STAR : FLEUR; useEffect(() => { const btn = btnRef.current, wrap = wrapRef.current, env = envRef.current, label = labelRef.current; const stamp = stampRef.current, wax = waxRef.current, blobEl = blobRef.current, crestEl = crestRef.current; const blobEls = Array.from(waxRef.current?.querySelectorAll("[data-blob]") ?? []); const rayEls = Array.from(ticksRef.current?.children ?? []); const wet = wetRef.current; const spec = specRef.current, tint = tintRef.current, ticks = ticksRef.current, shadow = shadowRef.current; if (!btn || !wrap || !env || !label || !stamp || !wax || !blobEl || !crestEl || !spec || !tint || !ticks || !shadow || !wet) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let reduced = media.matches; let visible = true; let hover = btn.matches(":hover"); let hy = hover && !disabled ? HOVER_Y : 0; let raf = 0, last = 0; let click: { t0: number; y0: number; rot: number } | null = null; const timers = new Set(); const later = (fn: () => void, ms: number) => { const id = window.setTimeout(() => { timers.delete(id); fn(); }, ms); timers.add(id); }; const clearTimers = () => { timers.forEach((id) => window.clearTimeout(id)); timers.clear(); }; const allowed = () => !disabled && !btn.matches(":disabled") && visible && !document.hidden; const setState = (s: string) => { wrap.dataset.tbState = s; }; // write every animated attribute for one pose const paint = (o: { sy: number; rot: number; wax: number; ws: number; wf: number; bead: number; wdy: number; spec: number; tint: number; ticks: number; label: number; env: number; shadow: number; crest: number; ca?: number; wet?: number; sx?: number; sys?: number; tkd?: number; swy?: number }) => { stamp.setAttribute("transform", `translate(0 ${o.sy.toFixed(2)}) rotate(${o.rot.toFixed(2)} ${CX} ${HEAD_Y + 7}) translate(132 120) scale(${(o.sx ?? 1).toFixed(3)} ${(o.sys ?? 1).toFixed(3)}) translate(-132 -120)`); wax.setAttribute("transform", `translate(${CX} ${(CY + o.wdy).toFixed(2)}) scale(${o.ws.toFixed(3)})`); wax.setAttribute("opacity", o.wax.toFixed(3)); if (o.wax > 0.001 && !reduced) { const bd = blob(o.wf, o.bead, 0); blobEls.forEach((el) => el.setAttribute("d", bd)); } crestEl.setAttribute("transform", `rotate(${o.crest.toFixed(2)})`); crestEl.setAttribute("opacity", (o.ca ?? 1).toFixed(3)); spec.setAttribute("opacity", o.spec.toFixed(3)); tint.setAttribute("opacity", o.tint.toFixed(3)); ticks.setAttribute("opacity", o.ticks.toFixed(3)); rayEls.forEach((g, i) => { const a = (RAYS[i] * Math.PI) / 180; const d = o.tkd ?? 0; g.setAttribute("transform", `translate(${(Math.cos(a) * d).toFixed(2)} ${(Math.sin(a) * d).toFixed(2)})`); }); wet.setAttribute("transform", `translate(132 ${(120 + o.sy).toFixed(2)})`); wet.setAttribute("opacity", (o.wet ?? 0).toFixed(3)); label.style.opacity = o.label.toFixed(3); env.style.transform = o.env === 1 ? "" : `scaleY(${o.env.toFixed(4)})`; const t = clamp01(o.sy / HOVER_Y); shadow.setAttribute("transform", `translate(${CX} ${(o.swy ?? 156).toFixed(2)}) scale(${(1 - 0.24 * t).toFixed(3)} ${(1 - 0.2 * t).toFixed(3)})`); shadow.setAttribute("opacity", (o.shadow * (0.14 + 0.38 * t)).toFixed(3)); }; const rest = { sy: 0, rot: 0, wax: 0, ws: 0.4, sx: 1, sys: 1, tkd: 0, swy: 156, wf: 0.73, bead: 0, wdy: 0, spec: 0.7, tint: 0, ticks: 0, label: 1, env: 1, shadow: 1, crest: 0, ca: 1, wet: 0 }; const pose = (now: number) => { if (!click) { paint({ ...rest, sy: hy, crest: crestRot }); return; } const ct = now - click.t0; const o = { ...rest, crest: click.rot }; const hT = hover ? HOVER_Y : 0; if (ct < 50) { o.sy = click.y0 - 4 * outCubic(ct / 50); o.shadow = 1; } else if (ct < 120) { const u = (ct - 50) / 70; o.sy = lerp(click.y0 - 4, CONTACT, u * u * u); o.shadow = 1 - u; o.swy = lerp(156, CY - 10, u); o.label = 1; } else if (ct < 420) { const u = (ct - 120) / 300; o.sy = CONTACT + 3 * outCubic(u); if (ct < 220) { const k = Math.sin(((ct - 120) / 100) * Math.PI); o.sx = 1 + 0.035 * k; o.sys = 1 - 0.05 * k; } o.rot = 1.5 * Math.sin(u * Math.PI * 2); o.shadow = 0; } else if (ct < 700) { const u = (ct - 420) / 280; o.sy = lerp(CONTACT + 3, LIFT_Y, outCubic(u)); o.rot = 3 * outCubic(u); o.shadow = 0; } else if (ct < 1000) { o.sy = LIFT_Y; o.rot = 3 * (1 - inOut((ct - 700) / 300)); o.shadow = 0; } else if (ct < 2600) { o.sy = LIFT_Y; o.shadow = 0; } else if (ct < 3000) { const u = inOut((ct - 2600) / 400); o.sy = lerp(LIFT_Y, hT, u); o.shadow = u; } else { o.sy = hT; } // wax if (ct >= 120 && ct < RESEAL) { const u = clamp01((ct - 120) / 420); o.wax = 1; o.ws = ct < 480 ? lerp(0.45, 1.17, outCubic((ct - 120) / 360)) : ct < 760 ? lerp(1.17, 1.08, inOut((ct - 480) / 280)) : 1.08; o.wf = lerp(0.73, 1, outCubic(u)); o.bead = 4 * outCubic((ct - 120) / 560); o.spec = ct < 420 ? 0.7 : ct < 470 ? lerp(0.7, 0.95, (ct - 420) / 50) : lerp(0.95, 0.35, inOut((ct - 470) / 630)); if (ct >= 2600) { const d = clamp01((ct - 2600) / 800); const e = d * d; o.wax = 1 - e; o.ws = lerp(1.08, 0.99, e); o.wdy = 4 * e; } } o.ca = ct < 420 ? 0 : ct < 540 ? outCubic((ct - 420) / 120) : 1; // label sits under the wax if (ct < 120) o.label = 1; else if (ct < 165) o.label = 1 - (ct - 120) / 45; else if (ct < 2600) o.label = 0; else o.label = clamp01((ct - 2600) / 600); // faint red tint on the lifted face o.tint = ct < 420 ? 0 : ct < 520 ? (ct - 420) / 100 : ct < 2420 ? 1 : clamp01(1 - (ct - 2420) / 400); o.wet = ct < 120 ? 0 : ct < 160 ? (ct - 120) / 40 : ct < 420 ? 1 : ct < 480 ? 1 - (ct - 420) / 60 : 0; o.ticks = ct < 120 ? 0 : ct < 150 ? (ct - 120) / 30 : ct < 300 ? 1 : ct < 460 ? 1 - inOut((ct - 300) / 160) : 0; o.tkd = ct >= 120 && ct < 460 ? 8 * outCubic((ct - 120) / 340) : 0; o.env = ct >= 120 && ct < 200 ? 1 - 0.035 * Math.sin(((ct - 120) / 80) * Math.PI) : 1; paint(o); if (ct >= RESEAL) { click = null; hy = hT; setState(hover ? "hover" : "rest"); } }; let crestRot = 0; const tick = (now: number) => { raf = 0; if (!allowed() || reduced) return; const dt = last ? Math.min(0.048, (now - last) / 1000) : 0.016; last = now; if (!click) hy += ((hover ? HOVER_Y : 0) - hy) * (1 - Math.exp(-dt / 0.075)); pose(now); if (click || Math.abs((hover ? HOVER_Y : 0) - hy) > 0.05) raf = requestAnimationFrame(tick); else { hy = hover ? HOVER_Y : 0; pose(now); } }; const start = () => { if (!raf && allowed() && !reduced) { last = 0; raf = requestAnimationFrame(tick); } }; const staticPose = (sealed: boolean) => { paint({ ...rest, sy: 0, wax: sealed ? 1 : 0, ws: 1.08, wf: 1, bead: 3, spec: 0.5, label: sealed ? 0 : 1, crest: sealed ? crestRot : 0 }); if (sealed) blobEl.setAttribute("d", blob(1, 3, 0)); }; const pause = () => { wrap.dataset.tbPaused = String(disabled || !visible || document.hidden); if (!allowed() || reduced) { cancelAnimationFrame(raf); raf = 0; } if (reduced) staticPose(wrap.dataset.tbState === "sealed"); else if (allowed()) start(); }; const enter = () => { if (disabled) return; hover = true; if (!click) setState("hover"); start(); }; const leave = () => { hover = false; if (!click) setState("rest"); start(); }; api.current.play = () => { if (disabled) return; if (reduced) { if (wrap.dataset.tbState === "sealed") return; crestRot = 0; setState("sealed"); staticPose(true); later(() => { setState("rest"); staticPose(false); }, 2600); return; } if (click) return; // a seal is already on: onClick fired, the effect ignores repeats crestRot = Math.random() * 16 - 8; click = { t0: performance.now(), y0: hy, rot: crestRot }; setState("click"); blobEl.setAttribute("d", blob(0.73, 0, 0)); start(); }; const onMedia = () => { reduced = media.matches; click = null; clearTimers(); setState(hover ? "hover" : "rest"); hy = hover && !reduced ? HOVER_Y : 0; if (reduced) staticPose(false); else pose(performance.now()); pause(); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; pause(); }); io?.observe(wrap); document.addEventListener("visibilitychange", pause); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointerleave", leave); media.addEventListener("change", onMedia); setState(hover && !disabled ? "hover" : "rest"); if (reduced) staticPose(false); else pose(performance.now()); pause(); return () => { api.current.play = () => {}; cancelAnimationFrame(raf); clearTimers(); io?.disconnect(); document.removeEventListener("visibilitychange", pause); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointerleave", leave); media.removeEventListener("change", onMedia); }; }, [disabled]); const press = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; api.current.play(); }; const brass = palette.brass; return ( ); } export default WaxSealButton; const css = ` .tb-waxseal-wrap{display:inline-block;position:relative;width:${W}px;height:${H}px;vertical-align:middle} .tb-waxseal-btn{position:relative;display:block;width:${W}px;height:${H}px;padding:0;margin:0;border:0;border-radius:${H / 2}px;background:none;cursor:pointer;overflow:visible;box-shadow:0 10px 18px rgba(0,0,0,.5);-webkit-tap-highlight-color:transparent;font:inherit} .tb-waxseal-btn:focus-visible{outline:3px solid ${ACCENT};outline-offset:6px} .tb-waxseal-env{position:absolute;inset:0;display:block;pointer-events:none;transform-origin:50% 52%;transition:transform .26s cubic-bezier(.3,.7,.4,1)} .tb-waxseal-wrap[data-tb-state=hover] .tb-waxseal-env{transform:translateY(-1.5px)} .tb-waxseal-envsvg{display:block;overflow:visible} .tb-waxseal-rim{opacity:0;transition:opacity .26s} .tb-waxseal-wrap[data-tb-state=hover] .tb-waxseal-rim{opacity:.95} .tb-waxseal-label{position:absolute;left:0;right:0;top:19px;height:30px;display:flex;align-items:center;justify-content:center;text-transform:uppercase;pointer-events:none; font:600 22px/1 "Iowan Old Style","Palatino Linotype",Palatino,"Book Antiqua",Georgia,serif;letter-spacing:.1em;padding-left:.1em;color:#4a2f17;text-shadow:0 1px 0 rgba(255,250,240,.6),0 -.5px 0 rgba(60,35,15,.22)} .tb-waxseal-over{position:absolute;left:-${PAD_X}px;top:-${PAD_T}px;width:${OW}px;height:${OH}px;max-width:none;overflow:visible;pointer-events:none} .tb-waxseal-bob{animation:tb-waxseal-bob 2.6s ease-in-out infinite} .tb-waxseal-wrap:not([data-tb-state=rest]) .tb-waxseal-bob{animation:none} @keyframes tb-waxseal-bob{0%,100%{transform:translateY(-2px)}50%{transform:translateY(2px)}} .tb-waxseal-btn:disabled{cursor:not-allowed;opacity:.6;filter:saturate(.5)} .tb-waxseal-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-waxseal-wrap *,.tb-waxseal-wrap *::before,.tb-waxseal-wrap *::after{animation:none!important;transition:none!important}} `;