{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"mechanical-stopwatch-with-a-crown-you-press-a-ticking-sweep-hand-and-a-flyback-reset","type":"registry:component","title":"Mechanical Stopwatch with a Crown You Press, a Ticking Sweep Hand and a Flyback Reset","description":"Stopwatch drawn on a canvas: a chrome case and enamel dial, a crown you press to start and stop the hand, and a flyback reset. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/mechanical-stopwatch-with-a-crown-you-press-a-ticking-sweep-hand-and-a-flyback-reset.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst START_AT = 198.2; // seconds already on the watch when the page loads\nconst BEATS = 5; // steps the hand takes each second (a fifth-second movement)\nconst MINUTES = 30; // one turn of the small recorder dial\nconst ACCENT = \"#e5482c\"; // quarter markers, hand tip and focus ring\nconst DEMO = true; // press its own buttons until a visitor does\n\nconst W = 520, H = 566, CX = 260, CY = 306, R = 178, RD = 146; // design pixels: canvas, case centre, case radius, dial radius\nconst PUSH = (32 * Math.PI) / 180; // where the reset pusher sits, clockwise from the crown\nconst TAU = Math.PI * 2;\nconst pct = (v: number, of: number) => `${(v / of) * 100}%`;\nconst clock = (s: number) => `${String(Math.floor(s / 60)).padStart(2, \"0\")}:${(s % 60).toFixed(1).padStart(4, \"0\")}`;\n\ntype Phase = \"running\" | \"stopped\" | \"zeroed\";\n\nexport default function CrownStopwatch() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [phase, setPhase] = useState<Phase>(\"running\");\n  const [shown, setShown] = useState(START_AT);\n  const api = useRef<{ crown: (byUser: boolean) => void; reset: (byUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current;\n    if (!box || !cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const hasConic = typeof ctx.createConicGradient === \"function\";\n    let k = 1;\n\n    // The movement: time is kept from the clock, so it stays right while the page is hidden.\n    const S = {\n      base: START_AT, since: performance.now(), running: !reduce,\n      hand: 0, handV: 0, min: 0, minV: 0, fly: false,\n      crown: 0, crownUntil: 0, pusher: 0, pusherUntil: 0,\n      lx: -0.5, ly: -0.6, tx: -0.5, ty: -0.6, auto: DEMO && !reduce, demoAt: performance.now(),\n    };\n    const elapsed = (now: number) => (S.running ? S.base + (now - S.since) / 1000 : S.base);\n    const beat = (s: number) => Math.floor(s * BEATS + 1e-6) / BEATS;\n    S.hand = (beat(START_AT) / 60) * TAU; S.min = (START_AT / 60 / MINUTES) * TAU;\n\n    const chrome = (c: CanvasRenderingContext2D, x: number, y: number, r: number, turn: number) => {\n      if (!hasConic) {\n        const g = c.createLinearGradient(x - r, y - r, x + r, y + r);\n        g.addColorStop(0, \"#fbfbfc\"); g.addColorStop(0.3, \"#8d9198\"); g.addColorStop(0.5, \"#e6e8eb\"); g.addColorStop(0.75, \"#4c4f55\"); g.addColorStop(1, \"#b5b8bd\");\n        return g;\n      }\n      const g = c.createConicGradient(turn, x, y);\n      const stops: [number, string][] = [[0, \"#ffffff\"], [0.05, \"#d2d5da\"], [0.14, \"#62666d\"], [0.22, \"#a4a8af\"], [0.31, \"#f3f4f6\"], [0.4, \"#8b8f96\"], [0.5, \"#26282c\"], [0.58, \"#767a81\"], [0.68, \"#e4e6e9\"], [0.77, \"#9a9ea5\"], [0.88, \"#3f4247\"], [0.95, \"#b9bcc1\"], [1, \"#ffffff\"]];\n      for (const [p, col] of stops) g.addColorStop(p, col);\n      return g;\n    };\n    // A knurled steel cylinder seen from the side: used for the crown and the pusher.\n    const barrel = (c: CanvasRenderingContext2D, w: number, h: number, y: number, ridges: boolean) => {\n      const g = c.createLinearGradient(-w / 2, 0, w / 2, 0);\n      g.addColorStop(0, \"#17181b\"); g.addColorStop(0.1, \"#7c8087\"); g.addColorStop(0.27, \"#f6f7f9\"); g.addColorStop(0.42, \"#b4b8be\"); g.addColorStop(0.68, \"#666a71\"); g.addColorStop(0.9, \"#2a2c31\"); g.addColorStop(1, \"#101113\");\n      c.beginPath(); c.moveTo(-w / 2, y + h); c.lineTo(-w / 2, y + 3); c.quadraticCurveTo(-w / 2, y, -w / 2 + 4, y); c.lineTo(w / 2 - 4, y); c.quadraticCurveTo(w / 2, y, w / 2, y + 3); c.lineTo(w / 2, y + h); c.closePath();\n      c.fillStyle = g; c.fill();\n      if (ridges) {\n        for (let i = -11; i <= 11; i++) {\n          const a = (i / 12) * (Math.PI / 2), x = Math.sin(a) * (w / 2);\n          c.beginPath(); c.moveTo(x, y + 2.5); c.lineTo(x, y + h - 1.5); c.lineWidth = 0.6 + Math.cos(a) * 1.1; c.strokeStyle = `rgba(8,9,11,${0.34 + 0.3 * (1 - Math.cos(a))})`; c.stroke();\n          c.beginPath(); c.moveTo(x + 1.2 * Math.cos(a), y + 2.5); c.lineTo(x + 1.2 * Math.cos(a), y + h - 1.5); c.lineWidth = 0.5; c.strokeStyle = `rgba(255,255,255,${0.3 * Math.cos(a)})`; c.stroke();\n        }\n      }\n      const cap = c.createLinearGradient(0, y, 0, y + 5);\n      cap.addColorStop(0, \"rgba(255,255,255,0.75)\"); cap.addColorStop(1, \"rgba(255,255,255,0)\");\n      c.fillStyle = cap; c.fillRect(-w / 2 + 3, y + 0.5, w - 6, 5);\n      const foot = c.createLinearGradient(0, y + h - 6, 0, y + h);\n      foot.addColorStop(0, \"rgba(0,0,0,0)\"); foot.addColorStop(1, \"rgba(0,0,0,0.5)\");\n      c.fillStyle = foot; c.fillRect(-w / 2, y + h - 6, w, 6);\n    };\n    const spaced = (c: CanvasRenderingContext2D, text: string, x: number, y: number, gap: number) => {\n      const widths = [...text].map((ch) => c.measureText(ch).width + gap);\n      let at = x - (widths.reduce((a, b) => a + b, 0) - gap) / 2;\n      c.textAlign = \"left\";\n      [...text].forEach((ch, i) => { c.fillText(ch, at, y); at += widths[i]; });\n      c.textAlign = \"center\";\n    };\n\n    // The printed enamel dial never changes, so it is painted once.\n    const face = document.createElement(\"canvas\");\n    const SUB = { y: -50, r: 35 };\n    const paintFace = () => {\n      const size = Math.ceil((RD * 2 + 4) * k);\n      face.width = size; face.height = size;\n      const f = face.getContext(\"2d\");\n      if (!f) return;\n      f.setTransform(k, 0, 0, k, size / 2, size / 2);\n      const enamel = f.createRadialGradient(-30, -44, 10, 0, 0, RD);\n      enamel.addColorStop(0, \"#fffdf6\"); enamel.addColorStop(0.6, \"#f6f1e3\"); enamel.addColorStop(1, \"#e2dac6\");\n      f.beginPath(); f.arc(0, 0, RD + 1, 0, TAU); f.fillStyle = enamel; f.fill();\n      let seed = 7;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let i = 0; i < 900; i++) {\n        const a = rand() * TAU, d = Math.sqrt(rand()) * RD;\n        f.fillStyle = rand() > 0.5 ? \"rgba(120,100,60,0.05)\" : \"rgba(255,255,255,0.35)\";\n        f.fillRect(Math.cos(a) * d, Math.sin(a) * d, 0.7, 0.7);\n      }\n      const ink = \"#17181b\";\n      // Railway track and the fifth-second scale.\n      f.strokeStyle = ink; f.lineWidth = 0.7;\n      for (const r of [RD - 5, RD - 13]) { f.beginPath(); f.arc(0, 0, r, 0, TAU); f.stroke(); }\n      for (let i = 0; i < 60 * BEATS; i++) {\n        const a = (i / (60 * BEATS)) * TAU, five = i % (5 * BEATS) === 0, one = i % BEATS === 0;\n        const r0 = RD - 5, r1 = five ? RD - 24 : one ? RD - 19 : RD - 13;\n        f.beginPath(); f.moveTo(Math.sin(a) * r0, -Math.cos(a) * r0); f.lineTo(Math.sin(a) * r1, -Math.cos(a) * r1);\n        f.lineWidth = five ? 2.3 : one ? 1.2 : 0.55; f.strokeStyle = five || one ? ink : \"rgba(23,24,27,0.6)\"; f.stroke();\n      }\n      f.fillStyle = ink; f.textAlign = \"center\"; f.textBaseline = \"middle\";\n      for (let n = 5; n <= 60; n += 5) {\n        const a = (n / 60) * TAU, big = n % 15 === 0, r = RD - (big ? 44 : 41);\n        f.font = `${big ? 700 : 500} ${big ? 24 : 16}px Georgia, \"Times New Roman\", serif`;\n        f.fillText(String(n), Math.sin(a) * r, -Math.cos(a) * r + 1);\n      }\n      // Red quarter markers outside the track.\n      f.fillStyle = ACCENT;\n      for (let q = 0; q < 4; q++) {\n        f.save(); f.rotate((q * Math.PI) / 2); f.beginPath(); f.moveTo(0, -RD + 4.5); f.lineTo(-3.4, -RD - 0.5); f.lineTo(3.4, -RD - 0.5); f.closePath(); f.fill(); f.restore();\n      }\n      // The sunk minute recorder.\n      f.save(); f.translate(0, SUB.y);\n      const well = f.createRadialGradient(0, 0, SUB.r * 0.2, 0, 0, SUB.r);\n      well.addColorStop(0, \"#f1ebdb\"); well.addColorStop(0.85, \"#e9e2cf\"); well.addColorStop(1, \"#cfc6ae\");\n      f.beginPath(); f.arc(0, 0, SUB.r, 0, TAU); f.fillStyle = well; f.fill();\n      for (let r = 4; r < SUB.r - 13; r += 2.2) { f.beginPath(); f.arc(0, 0, r, 0, TAU); f.lineWidth = 0.5; f.strokeStyle = \"rgba(90,74,40,0.11)\"; f.stroke(); }\n      const lip = f.createLinearGradient(-SUB.r, -SUB.r, SUB.r, SUB.r);\n      lip.addColorStop(0, \"rgba(60,48,24,0.55)\"); lip.addColorStop(0.5, \"rgba(60,48,24,0.12)\"); lip.addColorStop(1, \"rgba(255,255,255,0.95)\");\n      f.beginPath(); f.arc(0, 0, SUB.r - 0.6, 0, TAU); f.lineWidth = 1.4; f.strokeStyle = lip; f.stroke();\n      f.strokeStyle = ink; f.fillStyle = ink;\n      for (let i = 0; i < MINUTES; i++) {\n        const a = (i / MINUTES) * TAU, long = i % 5 === 0, r0 = SUB.r - 5, r1 = SUB.r - (long ? 12 : 9);\n        f.beginPath(); f.moveTo(Math.sin(a) * r0, -Math.cos(a) * r0); f.lineTo(Math.sin(a) * r1, -Math.cos(a) * r1); f.lineWidth = long ? 1.3 : 0.6; f.stroke();\n      }\n      f.font = `600 8.5px Georgia, \"Times New Roman\", serif`;\n      for (let n = MINUTES / 3; n <= MINUTES; n += MINUTES / 3) { const a = (n / MINUTES) * TAU; f.fillText(String(n), Math.sin(a) * (SUB.r - 18), -Math.cos(a) * (SUB.r - 18) + 0.5); }\n      f.restore();\n      f.fillStyle = ink; f.font = `600 8.5px \"Helvetica Neue\", Helvetica, Arial, sans-serif`;\n      spaced(f, `1/${BEATS} SECOND`, 0, 50, 2.4);\n      f.fillStyle = \"rgba(23,24,27,0.62)\"; f.font = `500 7px \"Helvetica Neue\", Helvetica, Arial, sans-serif`;\n      spaced(f, `${MINUTES} MINUTE RECORDER`, 0, 64, 1.8);\n    };\n\n    const hand = (len: number, tail: number, w: number, tip: boolean) => {\n      const steel = ctx.createLinearGradient(-w, 0, w, 0);\n      steel.addColorStop(0, \"#0b1330\"); steel.addColorStop(0.45, \"#3d63c9\"); steel.addColorStop(0.6, \"#22408f\"); steel.addColorStop(1, \"#080d22\");\n      ctx.beginPath();\n      ctx.moveTo(-w * 0.5, 0); ctx.lineTo(-w * 0.22, -len); ctx.lineTo(w * 0.22, -len); ctx.lineTo(w * 0.5, 0);\n      ctx.lineTo(w * 0.7, tail * 0.45); ctx.arc(0, tail * 0.72, w * 1.55, -0.5, Math.PI + 0.5); ctx.lineTo(-w * 0.7, tail * 0.45);\n      ctx.closePath(); ctx.fillStyle = steel; ctx.fill();\n      if (tip) { ctx.shadowColor = \"transparent\"; ctx.fillStyle = ACCENT; ctx.fillRect(-w * 0.26, -len, w * 0.52, 20); }\n    };\n\n    const draw = (now: number) => {\n      const t = elapsed(now);\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      const la = Math.atan2(S.ly, S.lx); // direction the lamp sits in\n      const crownY = S.crown * 6, pushY = S.pusher * 5;\n\n      // Shadow on the table.\n      ctx.save(); ctx.translate(CX - S.lx * 16, CY - S.ly * 22 + 8); ctx.scale(1, 1);\n      const sh = ctx.createRadialGradient(0, 0, R * 0.7, 0, 0, R * 1.32);\n      sh.addColorStop(0, \"rgba(0,0,0,0.7)\"); sh.addColorStop(0.55, \"rgba(0,0,0,0.3)\"); sh.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = sh; ctx.beginPath(); ctx.arc(0, 0, R * 1.32, 0, TAU); ctx.fill(); ctx.restore();\n\n      // The bow: a polished steel ring the lanyard goes through.\n      const bow = { x: CX, y: CY - R - 62, r: 37 };\n      ctx.save();\n      ctx.shadowColor = \"rgba(0,0,0,0.55)\"; ctx.shadowBlur = 10 * k; ctx.shadowOffsetX = -S.lx * 7 * k; ctx.shadowOffsetY = (6 - S.ly * 6) * k;\n      ctx.beginPath(); ctx.arc(bow.x, bow.y, bow.r, 0, TAU); ctx.lineWidth = 11; ctx.strokeStyle = \"#1b1c1f\"; ctx.stroke();\n      ctx.restore();\n      ctx.beginPath(); ctx.arc(bow.x, bow.y, bow.r, 0, TAU); ctx.lineWidth = 9.4; ctx.strokeStyle = chrome(ctx, bow.x, bow.y, bow.r, la + 0.6); ctx.stroke();\n      ctx.beginPath(); ctx.arc(bow.x, bow.y, bow.r + 2.2, la - 0.7, la + 0.7); ctx.lineWidth = 1.6; ctx.strokeStyle = \"rgba(255,255,255,0.9)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(bow.x, bow.y, bow.r - 2.4, la + Math.PI - 0.6, la + Math.PI + 0.6); ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(255,255,255,0.55)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(bow.x, bow.y, bow.r - 4.9, 0, TAU); ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(0,0,0,0.55)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(bow.x, bow.y, bow.r + 4.9, 0, TAU); ctx.stroke();\n\n      // Pusher, then crown, each a neck fixed to the case and a cap that travels.\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(PUSH);\n      barrel(ctx, 15, 16, -R - 9, false);\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.5)\"; ctx.shadowBlur = 6 * k; ctx.shadowOffsetY = 3 * k;\n      barrel(ctx, 25, 15, -R - 22 + pushY, true); ctx.restore();\n      ctx.restore();\n      ctx.save(); ctx.translate(CX, CY);\n      barrel(ctx, 27, 22, -R - 14, false);\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.5)\"; ctx.shadowBlur = 7 * k; ctx.shadowOffsetX = -S.lx * 3 * k; ctx.shadowOffsetY = 3 * k;\n      barrel(ctx, 56, 30, -R - 41 + crownY, true); ctx.restore();\n      ctx.restore();\n\n      // Case: polished outer ring, a bevel that catches the opposite light, the inner bezel.\n      ctx.beginPath(); ctx.arc(CX, CY, R, 0, TAU); ctx.fillStyle = chrome(ctx, CX, CY, R, la); ctx.fill();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(0,0,0,0.6)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(CX, CY, R - 1.6, 0, TAU); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.5)\"; ctx.stroke();\n      const bevel = ctx.createLinearGradient(CX + S.lx * R, CY + S.ly * R, CX - S.lx * R, CY - S.ly * R);\n      bevel.addColorStop(0, \"#3a3c41\"); bevel.addColorStop(0.45, \"#8e9299\"); bevel.addColorStop(1, \"#fafbfc\");\n      ctx.beginPath(); ctx.arc(CX, CY, R - 15, 0, TAU); ctx.fillStyle = bevel; ctx.fill();\n      ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(0,0,0,0.45)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(CX, CY, R - 20, 0, TAU); ctx.fillStyle = chrome(ctx, CX, CY, R, la + Math.PI * 0.86); ctx.fill();\n      ctx.beginPath(); ctx.arc(CX, CY, RD + 3.5, 0, TAU); ctx.fillStyle = \"#0c0d0f\"; ctx.fill();\n\n      // Dial, the shadow the bezel throws on it, then the hands.\n      ctx.drawImage(face, CX - RD - 2, CY - RD - 2, RD * 2 + 4, RD * 2 + 4);\n      ctx.save(); ctx.beginPath(); ctx.arc(CX, CY, RD + 1, 0, TAU); ctx.clip();\n      const cast = ctx.createRadialGradient(CX - S.lx * 9, CY - S.ly * 9, RD * 0.86, CX - S.lx * 9, CY - S.ly * 9, RD + 6);\n      cast.addColorStop(0, \"rgba(40,28,8,0)\"); cast.addColorStop(1, \"rgba(40,28,8,0.36)\");\n      ctx.fillStyle = cast; ctx.fillRect(CX - RD - 4, CY - RD - 4, RD * 2 + 8, RD * 2 + 8);\n\n      const drop = (lift: number) => { ctx.shadowColor = \"rgba(30,20,5,0.4)\"; ctx.shadowBlur = lift * 1.2 * k; ctx.shadowOffsetX = -S.lx * lift * k; ctx.shadowOffsetY = -S.ly * lift * k + lift * 0.5 * k; };\n      ctx.save(); ctx.translate(CX, CY + SUB.y); ctx.rotate(S.min); drop(2.5); hand(SUB.r - 9, 9, 3.2, false); ctx.restore();\n      ctx.save(); ctx.translate(CX, CY + SUB.y); ctx.beginPath(); ctx.arc(0, 0, 2.6, 0, TAU); ctx.fillStyle = \"#c9ccd1\"; ctx.fill(); ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(0,0,0,0.6)\"; ctx.stroke(); ctx.restore();\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(S.hand); drop(6); hand(RD - 9, 46, 5.2, true); ctx.restore();\n      ctx.save(); ctx.translate(CX, CY); ctx.shadowColor = \"rgba(0,0,0,0.45)\"; ctx.shadowBlur = 3 * k; ctx.shadowOffsetY = 1.5 * k;\n      ctx.beginPath(); ctx.arc(0, 0, 7.5, 0, TAU); ctx.fillStyle = chrome(ctx, 0, 0, 7.5, la); ctx.fill(); ctx.restore();\n      ctx.beginPath(); ctx.arc(CX, CY, 2.3, 0, TAU); ctx.fillStyle = \"#15161a\"; ctx.fill();\n\n      // The domed crystal: a broad soft bloom, a window, and a rim light on the far side.\n      const gx = CX + S.lx * RD * 0.52, gy = CY + S.ly * RD * 0.52;\n      const bloom = ctx.createRadialGradient(gx, gy, 0, gx, gy, RD * 0.95);\n      bloom.addColorStop(0, \"rgba(255,255,255,0.34)\"); bloom.addColorStop(0.45, \"rgba(255,255,255,0.09)\"); bloom.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = bloom; ctx.fillRect(CX - RD, CY - RD, RD * 2, RD * 2);\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(la + Math.PI / 2);\n      const pane = ctx.createLinearGradient(0, -RD, 0, -RD * 0.5);\n      pane.addColorStop(0, \"rgba(255,255,255,0.85)\"); pane.addColorStop(1, \"rgba(255,255,255,0.1)\");\n      ctx.fillStyle = pane;\n      ctx.beginPath(); ctx.arc(0, 0, RD - 7, Math.PI * 1.14, Math.PI * 1.86); ctx.arc(0, RD * 0.62, RD * 1.36, Math.PI * 1.69, Math.PI * 1.31, true); ctx.closePath(); ctx.fill();\n      ctx.beginPath(); ctx.arc(0, 0, RD - 2.5, Math.PI / 2 - 0.55, Math.PI / 2 + 0.55); ctx.lineWidth = 2.2; ctx.lineCap = \"round\"; ctx.strokeStyle = \"rgba(255,255,255,0.4)\"; ctx.stroke();\n      ctx.restore();\n      const dark = ctx.createRadialGradient(CX - S.lx * RD, CY - S.ly * RD, RD * 0.5, CX - S.lx * RD, CY - S.ly * RD, RD * 1.6);\n      dark.addColorStop(0, \"rgba(10,14,20,0.2)\"); dark.addColorStop(1, \"rgba(10,14,20,0)\");\n      ctx.fillStyle = dark; ctx.fillRect(CX - RD, CY - RD, RD * 2, RD * 2);\n      ctx.restore();\n      ctx.beginPath(); ctx.arc(CX, CY, RD + 1.2, 0, TAU); ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(255,255,255,0.3)\"; ctx.stroke();\n      return t;\n    };\n\n    const settle = (now: number) => { const t = elapsed(now); S.hand = (beat(t) / 60) * TAU; S.min = (t / 60 / MINUTES) * TAU; S.crown = 0; S.pusher = 0; };\n\n    api.current = {\n      crown: (byUser) => {\n        const now = performance.now();\n        if (byUser) S.auto = false;\n        if (S.running) { S.base = beat(elapsed(now)); S.running = false; setShown(S.base); setPhase(\"stopped\"); }\n        else { S.since = now; S.running = true; setPhase(\"running\"); }\n        S.crownUntil = now + 130;\n        if (reduce) { settle(now); draw(now); } else wake();\n      },\n      reset: (byUser) => {\n        const now = performance.now();\n        if (byUser) S.auto = false;\n        S.running = false; S.base = 0; S.fly = true;\n        // Fly back the short way round, as the heart cam does.\n        S.hand = Math.atan2(Math.sin(S.hand), Math.cos(S.hand)); S.min = Math.atan2(Math.sin(S.min), Math.cos(S.min));\n        S.pusherUntil = now + 130; setShown(0); setPhase(\"zeroed\");\n        if (reduce) { settle(now); draw(now); } else wake();\n      },\n    };\n\n    let raf = 0, last = performance.now(), seen = true;\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      if (S.auto) {\n        // Self-running demo: stop, fly back, start again.\n        const d = ((now - S.demoAt) / 1000) % 13;\n        const stage3 = d < 8.2 ? 0 : d < 10 ? 1 : d < 11.2 ? 2 : 3;\n        if (stage3 === 1 && S.running) api.current?.crown(false);\n        if (stage3 === 2 && (S.base !== 0 || S.running)) api.current?.reset(false);\n        if (stage3 === 3 && !S.running) api.current?.crown(false);\n        S.tx = -0.5 + Math.sin(now / 2600) * 0.32; S.ty = -0.62 + Math.cos(now / 3400) * 0.14;\n      }\n      const t = elapsed(now);\n      const goal = (beat(t) / 60) * TAU, goalM = (t / 60 / MINUTES) * TAU;\n      const stiff = S.fly ? 330 : 2600, damp = S.fly ? 21 : 64;\n      for (let i = 0; i < 2; i++) {\n        const h = dt / 2;\n        S.handV += ((goal - S.hand) * stiff - S.handV * damp) * h; S.hand += S.handV * h;\n        S.minV += ((goalM - S.min) * 240 - S.minV * 19) * h; S.min += S.minV * h;\n      }\n      if (S.fly && Math.abs(S.hand - goal) < 0.002 && Math.abs(S.handV) < 0.02 && S.running) S.fly = false;\n      if (S.fly && S.running && Math.abs(S.hand - goal) < 0.05) S.fly = false;\n      S.crown += ((now < S.crownUntil ? 1 : 0) - S.crown) * Math.min(1, dt * 26);\n      S.pusher += ((now < S.pusherUntil ? 1 : 0) - S.pusher) * Math.min(1, dt * 26);\n      S.lx += (S.tx - S.lx) * Math.min(1, dt * 5); S.ly += (S.ty - S.ly) * Math.min(1, dt * 5);\n      draw(now);\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    function wake() { if (!reduce && !raf && seen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(frame); } }\n    const still = () => { const now = performance.now(); settle(now); draw(now); };\n\n    const onMove = (e: PointerEvent) => {\n      const r = cv.getBoundingClientRect();\n      const dx = (e.clientX - (r.left + (CX / W) * r.width)) / r.width, dy = (e.clientY - (r.top + (CY / H) * r.height)) / r.height;\n      const d = Math.hypot(dx, dy) || 1, reach = Math.min(0.85, 0.35 + d);\n      S.tx = (dx / d) * reach; S.ty = (dy / d) * reach;\n    };\n    const onLeave = () => { S.tx = -0.5; S.ty = -0.62; };\n    const size = () => {\n      const w = cv.clientWidth;\n      if (!w) return;\n      k = (w / W) * Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(W * k); cv.height = Math.round(H * k);\n      paintFace();\n      if (reduce) still(); else { draw(performance.now()); wake(); }\n    };\n    const ro = new ResizeObserver(size); ro.observe(cv);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    box.addEventListener(\"pointermove\", onMove); box.addEventListener(\"pointerleave\", onLeave);\n    // With motion reduced the hand still has to tell the time: redraw once a second, no springs.\n    const slow = reduce ? window.setInterval(still, 1000) : 0;\n    size();\n    return () => {\n      cancelAnimationFrame(raf); if (slow) window.clearInterval(slow);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n      box.removeEventListener(\"pointermove\", onMove); box.removeEventListener(\"pointerleave\", onLeave);\n      api.current = null;\n    };\n  }, []);\n\n  const px = CX + Math.sin(PUSH) * (R + 16), py = CY - Math.cos(PUSH) * (R + 16);\n  const line = phase === \"running\" ? \"Press the crown to stop\" : phase === \"stopped\" ? `Held at ${clock(shown)}` : \"Press the crown to start\";\n\n  return (\n    <div ref={stage} className=\"flex min-h-[780px] w-full flex-col items-center justify-center bg-[#0d1512] bg-[radial-gradient(80%_62%_at_50%_46%,#20352c_0%,#122019_45%,#0a100e_100%)] px-4 py-12\">\n      <div className=\"relative w-full\" style={{ maxWidth: 590, aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <button\n          type=\"button\"\n          aria-pressed={phase === \"running\"}\n          aria-label={phase === \"running\" ? \"Stop the stopwatch\" : \"Start the stopwatch\"}\n          onClick={() => api.current?.crown(true)}\n          style={{ left: pct(CX - 48, W), top: pct(CY - R - 106, H), width: pct(96, W), height: pct(118, H), [\"--tw-ring-color\" as string]: ACCENT }}\n          className=\"absolute cursor-pointer rounded-[28px] outline-none focus-visible:ring-2\"\n        />\n        <button\n          type=\"button\"\n          aria-label=\"Reset to zero\"\n          onClick={() => api.current?.reset(true)}\n          style={{ left: pct(px - 35, W), top: pct(py - 35, H), width: pct(70, W), height: pct(70, H), [\"--tw-ring-color\" as string]: ACCENT }}\n          className=\"absolute cursor-pointer rounded-full outline-none focus-visible:ring-2\"\n        />\n      </div>\n      <p aria-live=\"polite\" className=\"mt-6 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-emerald-100/60\">\n        <span className=\"text-emerald-50\">{phase === \"running\" ? \"Running\" : phase === \"stopped\" ? \"Stopped\" : \"Zeroed\"}</span>\n        <span className=\"mx-2 text-emerald-100/30\">/</span>\n        {line}\n      </p>\n    </div>\n  );\n}\n"}]}
