{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"brass-pendulum-wall-clock-that-beats-once-a-second-in-true-time-and-throws-a-moving-shadow","type":"registry:component","title":"Brass Pendulum Wall Clock That Beats Once a Second in True Time and Throws a Moving Shadow","description":"Pendulum clock drawn on a canvas: a brass wall clock whose bob beats once every real second and casts a moving shadow. Free React and HTML code to copy.","dependencies":[],"files":[{"path":"components/we-inc/brass-pendulum-wall-clock-that-beats-once-a-second-in-true-time-and-throws-a-moving-shadow.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\nconst SWING = 5.5; // degrees the pendulum travels either side of centre at rest\nconst PUSH = 17; // degrees it reaches when you press the button\nconst SETTLE = 0.3; // how quickly a wide swing falls back into step (per second)\nconst FACE = \"#f3ecdb\"; // enamel dial\nconst NUMERALS = [\"XII\", \"I\", \"II\", \"III\", \"IIII\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\", \"X\", \"XI\"];\nconst W = 440, H = 600, CX = 220, CY = 140, R = 112, ROD = 352, BOB = 37;\n\nconst pad2 = (n: number) => String(n).padStart(2, \"0\");\n\nexport default function BrassPendulumWallClock() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const [clock, setClock] = useState(\"--:--:--\");\n  const push = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const cv = canvas.current, area = hit.current;\n    if (!cv || !area) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(2, window.devicePixelRatio || 1);\n    cv.width = W * dpr; cv.height = H * dpr;\n    const conic = typeof ctx.createConicGradient === \"function\";\n    const A0 = (SWING * Math.PI) / 180;\n\n    // Brass from a brightness value: dark bronze through yellow to an almost white glint.\n    const brass = (v: number) => { const c = Math.max(0, Math.min(255, v)), hi = Math.max(0, c - 196); return `rgb(${Math.min(255, c * 1.02 + 6) | 0},${Math.min(255, c * 0.78 + hi * 0.95) | 0},${Math.min(255, c * 0.36 + hi * 2) | 0})`; };\n    const turned = (x: number, y: number, angle: number, lo: number, hi: number) => {\n      if (!conic) { const g = ctx.createLinearGradient(x - 60, y - 60, x + 60, y + 60); g.addColorStop(0, brass(hi)); g.addColorStop(0.5, brass(lo)); g.addColorStop(1, brass(hi)); return g; }\n      const g = ctx.createConicGradient(angle, x, y), mid = (lo + hi) / 2;\n      const st: [number, number][] = [[0, hi], [0.04, hi - 8], [0.1, mid + 14], [0.18, lo + 6], [0.26, lo - 12], [0.34, mid - 20], [0.42, mid + 10], [0.47, hi - 30], [0.5, hi - 10], [0.54, hi - 40], [0.62, mid - 4], [0.7, lo - 6], [0.78, lo + 12], [0.86, mid - 12], [0.93, mid + 34], [0.97, hi - 6], [1, hi]];\n      for (const [p, v] of st) g.addColorStop(p, brass(v));\n      return g;\n    };\n    // Lathe rings for the bob, drawn once.\n    const rings = document.createElement(\"canvas\"); rings.width = rings.height = BOB * 2 * dpr;\n    const rx = rings.getContext(\"2d\");\n    if (rx) {\n      rx.scale(dpr, dpr); let seed = 5; const rnd = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let r = 1; r < BOB; r += 0.6) { const v = rnd(); rx.beginPath(); rx.arc(BOB, BOB, r, 0, Math.PI * 2); rx.lineWidth = 0.5; rx.strokeStyle = v > 0.5 ? `rgba(255,244,200,${(v - 0.5) * 0.36})` : `rgba(50,24,0,${(0.5 - v) * 0.34})`; rx.stroke(); }\n    }\n\n    // The shape is drawn far off the canvas and only its blurred shadow lands in view.\n    const pendulumShadow = (theta: number, ox: number, oy: number, blur: number, alpha: number) => {\n      ctx.save(); ctx.shadowColor = `rgba(0,0,0,${alpha})`; ctx.shadowBlur = blur * dpr; ctx.shadowOffsetX = (ox + 2000) * dpr; ctx.shadowOffsetY = oy * dpr;\n      ctx.translate(CX - 2000, CY); ctx.rotate(theta); ctx.fillStyle = \"#000\";\n      ctx.beginPath(); ctx.rect(-2.2, 0, 4.4, ROD); ctx.arc(0, ROD, BOB, 0, Math.PI * 2); ctx.rect(-5, ROD + BOB, 10, 20); ctx.fill();\n      ctx.restore();\n    };\n    const pendulum = (theta: number, la: number) => {\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(theta);\n      // rod: a flat strip with a bright edge\n      const rod = ctx.createLinearGradient(-2.4, 0, 2.4, 0); rod.addColorStop(0, brass(92)); rod.addColorStop(0.35, brass(250)); rod.addColorStop(0.6, brass(170)); rod.addColorStop(1, brass(70));\n      ctx.fillStyle = rod; ctx.fillRect(-2.2, 0, 4.4, ROD + BOB + 8);\n      // rating nut under the bob\n      const nut = ctx.createLinearGradient(-6, 0, 6, 0); nut.addColorStop(0, brass(70)); nut.addColorStop(0.3, brass(252)); nut.addColorStop(0.55, brass(150)); nut.addColorStop(0.8, brass(210)); nut.addColorStop(1, brass(80));\n      ctx.fillStyle = nut; ctx.fillRect(-6, ROD + BOB + 6, 12, 9);\n      for (let k = -5; k <= 5; k += 2) { ctx.fillStyle = \"rgba(50,24,0,0.4)\"; ctx.fillRect(k, ROD + BOB + 6, 0.7, 9); }\n      ctx.fillRect(-1.6, ROD + BOB + 15, 3.2, 7);\n      // bob: a lens of turned brass. The sheen stays with the lamp while the disc swings.\n      ctx.translate(0, ROD);\n      ctx.beginPath(); ctx.arc(0, 0, BOB, 0, Math.PI * 2); ctx.fillStyle = turned(0, 0, la - theta, 104, 256); ctx.fill();\n      ctx.save(); ctx.clip(); ctx.drawImage(rings, -BOB, -BOB, BOB * 2, BOB * 2);\n      const dome = ctx.createRadialGradient(-10, -14, 2, 0, 0, BOB);\n      dome.addColorStop(0, \"rgba(255,250,225,0.5)\"); dome.addColorStop(0.5, \"rgba(255,250,225,0)\"); dome.addColorStop(1, \"rgba(40,18,0,0.5)\");\n      ctx.fillStyle = dome; ctx.fillRect(-BOB, -BOB, BOB * 2, BOB * 2); ctx.restore();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(50,24,0,0.6)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(0, 0, BOB - 5, 0, Math.PI * 2); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(50,24,0,0.4)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(0, 0, BOB - 6, 0, Math.PI * 2); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(255,246,210,0.6)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(0, 0, 5.4, 0, Math.PI * 2); ctx.fillStyle = turned(0, 0, la - theta + 1, 70, 250); ctx.fill(); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(50,24,0,0.6)\"; ctx.stroke();\n      ctx.restore();\n    };\n\n    const hand = (angle: number, len: number, w: number, tail: number, la: number) => {\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(angle);\n      ctx.shadowColor = \"rgba(40,26,8,0.42)\"; ctx.shadowBlur = 4 * dpr; ctx.shadowOffsetX = -Math.cos(la) * 5 * dpr; ctx.shadowOffsetY = -Math.sin(la) * 5 * dpr + 2 * dpr;\n      // a spade hand: slim stem, a swelling near the tip, a fine point\n      ctx.beginPath(); ctx.moveTo(-w * 0.5, tail); ctx.lineTo(-w * 0.36, -len * 0.62);\n      ctx.bezierCurveTo(-w * 2.3, -len * 0.7, -w * 1.6, -len * 0.86, 0, -len);\n      ctx.bezierCurveTo(w * 1.6, -len * 0.86, w * 2.3, -len * 0.7, w * 0.36, -len * 0.62);\n      ctx.lineTo(w * 0.5, tail); ctx.closePath();\n      ctx.fillStyle = \"#14171c\"; ctx.fill(); ctx.shadowColor = \"transparent\";\n      ctx.beginPath(); ctx.moveTo(-w * 0.12, tail); ctx.lineTo(-w * 0.1, -len * 0.6); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(120,150,210,0.6)\"; ctx.stroke();\n      ctx.restore();\n    };\n\n    const draw = (ms: number, theta: number, la: number) => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      const lx = Math.cos(la), ly = Math.sin(la); // direction the light comes from\n\n      // Shadows on the wall. The pendulum hangs further out than the case, so its shadow falls further away.\n      pendulumShadow(theta, -lx * 24, -ly * 24 + 8, 16, 0.5);\n      pendulumShadow(theta, -lx * 21, -ly * 21 + 6, 4, 0.5);\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.6)\"; ctx.shadowBlur = 30 * dpr; ctx.shadowOffsetX = -lx * 22 * dpr; ctx.shadowOffsetY = (-ly * 22 + 12) * dpr;\n      ctx.beginPath(); ctx.arc(CX, CY, R - 3, 0, Math.PI * 2); ctx.fillStyle = \"#000\"; ctx.fill(); ctx.restore();\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.7)\"; ctx.shadowBlur = 6 * dpr; ctx.shadowOffsetX = -lx * 5 * dpr; ctx.shadowOffsetY = (-ly * 5 + 3) * dpr;\n      ctx.beginPath(); ctx.arc(CX, CY, R - 1, 0, Math.PI * 2); ctx.fillStyle = \"#000\"; ctx.fill(); ctx.restore();\n\n      pendulum(theta, la);\n\n      // Case: a rolled brass bezel in three steps.\n      ctx.beginPath(); ctx.arc(CX, CY, R, 0, Math.PI * 2); ctx.fillStyle = turned(CX, CY, la + 0.2, 60, 250); ctx.fill();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(40,18,0,0.7)\"; ctx.stroke();\n      const roll = ctx.createRadialGradient(CX, CY, R - 20, CX, CY, R);\n      roll.addColorStop(0, \"rgba(40,18,0,0.55)\"); roll.addColorStop(0.3, \"rgba(255,246,214,0.22)\"); roll.addColorStop(0.62, \"rgba(255,246,214,0)\"); roll.addColorStop(1, \"rgba(40,18,0,0.6)\");\n      ctx.beginPath(); ctx.arc(CX, CY, R, 0, Math.PI * 2); ctx.fillStyle = roll; ctx.fill();\n      ctx.beginPath(); ctx.arc(CX, CY, R - 17, 0, Math.PI * 2); ctx.fillStyle = turned(CX, CY, la + 2.2, 50, 245); ctx.fill();\n      ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(40,18,0,0.75)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(CX, CY, R - 18.4, 0, Math.PI * 2); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(255,246,214,0.6)\"; ctx.stroke();\n      const DIAL = R - 24;\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL + 1.5, 0, Math.PI * 2); ctx.fillStyle = \"#1f1408\"; ctx.fill();\n\n      // Enamel dial.\n      const face = ctx.createRadialGradient(CX + lx * 30, CY + ly * 30, 6, CX, CY, DIAL);\n      face.addColorStop(0, \"#fffaf0\"); face.addColorStop(0.55, FACE); face.addColorStop(1, \"#d3c8ae\");\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL, 0, Math.PI * 2); ctx.fillStyle = face; ctx.fill();\n      ctx.save(); ctx.clip();\n      ctx.shadowColor = \"rgba(40,26,8,0.7)\"; ctx.shadowBlur = 10 * dpr; ctx.shadowOffsetX = -lx * 6 * dpr; ctx.shadowOffsetY = -ly * 6 * dpr;\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL + 8, 0, Math.PI * 2); ctx.lineWidth = 14; ctx.strokeStyle = \"#000\"; ctx.stroke();\n      ctx.restore();\n      ctx.strokeStyle = \"#16181c\"; ctx.fillStyle = \"#16181c\";\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL - 5, 0, Math.PI * 2); ctx.lineWidth = 0.8; ctx.stroke();\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL - 11, 0, Math.PI * 2); ctx.lineWidth = 0.8; ctx.stroke();\n      for (let i = 0; i < 60; i++) {\n        const a = (i / 60) * Math.PI * 2, s = Math.sin(a), c = -Math.cos(a);\n        if (i % 5 === 0) { ctx.beginPath(); ctx.arc(CX + s * (DIAL - 8), CY + c * (DIAL - 8), 1.9, 0, Math.PI * 2); ctx.fill(); }\n        else { ctx.beginPath(); ctx.moveTo(CX + s * (DIAL - 11), CY + c * (DIAL - 11)); ctx.lineTo(CX + s * (DIAL - 5), CY + c * (DIAL - 5)); ctx.lineWidth = 0.7; ctx.stroke(); }\n      }\n      ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\"; ctx.font = '400 17px Didot, \"Bodoni MT\", \"Times New Roman\", Georgia, serif';\n      NUMERALS.forEach((n, i) => { ctx.save(); ctx.translate(CX, CY); ctx.rotate((i / 12) * Math.PI * 2); ctx.translate(0, -(DIAL - 27)); ctx.scale(0.86, 1.25); ctx.fillText(n, 0, 0.5); ctx.restore(); });\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL - 44, 0, Math.PI * 2); ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(22,24,28,0.5)\"; ctx.stroke();\n      ctx.font = 'italic 400 8px Didot, \"Times New Roman\", Georgia, serif'; ctx.fillStyle = \"rgba(22,24,28,0.75)\"; ctx.fillText(\"Eight Day\", CX, CY + 22);\n\n      // Blued steel hands. The minute hand moves on each beat.\n      const d = new Date(ms), mins = d.getMinutes() + d.getSeconds() / 60, hrs = (d.getHours() % 12) + mins / 60;\n      hand((hrs / 12) * Math.PI * 2, 46, 3.4, 9, la);\n      hand((mins / 60) * Math.PI * 2, 72, 2.9, 12, la);\n      ctx.beginPath(); ctx.arc(CX, CY, 5, 0, Math.PI * 2); ctx.fillStyle = turned(CX, CY, la, 70, 250); ctx.fill(); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(40,18,0,0.7)\"; ctx.stroke();\n\n      // Glass over the dial.\n      ctx.save(); ctx.beginPath(); ctx.arc(CX, CY, DIAL + 1, 0, Math.PI * 2); ctx.clip();\n      const gx = CX + lx * 118, gy = CY + ly * 118;\n      const gl = ctx.createRadialGradient(gx, gy, 20, gx, gy, 132);\n      gl.addColorStop(0, \"rgba(255,255,255,0.46)\"); gl.addColorStop(0.9, \"rgba(255,255,255,0.1)\"); gl.addColorStop(0.915, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = gl; ctx.fillRect(CX - R, CY - R, R * 2, R * 2);\n      ctx.beginPath(); ctx.arc(CX, CY, DIAL - 2.5, la + Math.PI - 0.45, la + Math.PI + 0.45); ctx.lineWidth = 1.8; ctx.lineCap = \"round\"; ctx.strokeStyle = \"rgba(255,255,255,0.5)\"; ctx.stroke();\n      ctx.restore();\n    };\n\n    // amp and phase describe the swing: theta = amp * cos(pi * seconds + phase). At rest phase is 0, so the\n    // bob reaches one side on every whole second. After a push both drift back to that.\n    const s = { amp: A0, phase: 0, held: false, grab: 0, la: -2.25, laTo: -2.25, touched: -1e9 };\n    const thetaAt = (ms: number) => (s.held ? s.grab : s.amp * Math.cos(Math.PI * (ms / 1000) + s.phase));\n    let raf = 0, seen = true, last = 0, said = \"\";\n    const t0 = performance.now();\n    const step = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, (now - last) / 1000); last = now;\n      const ms = Date.now();\n      if (!s.held) { const k = Math.min(1, dt * SETTLE * 1.2); s.amp += (A0 - s.amp) * k; s.phase += (0 - s.phase) * Math.min(1, dt * SETTLE * 0.9); }\n      if (now - s.touched > 4000) s.laTo = -2.25 + 0.4 * Math.sin((now - t0) / 2600);\n      s.la = reduce ? s.laTo : s.la + (s.laTo - s.la) * Math.min(1, dt * 5);\n      draw(ms, reduce ? 0 : thetaAt(ms), s.la);\n      const d = new Date(ms), text = `${pad2(d.getHours())}:${pad2(d.getMinutes())}:${pad2(d.getSeconds())}`;\n      if (text !== said) { said = text; setClock(text); }\n      if (!reduce && seen && !document.hidden) raf = requestAnimationFrame(step);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(step); } };\n    // Letting go at angle g: keep the bob exactly where it is and carry on from there.\n    const release = (g: number) => {\n      const ms = Date.now(), lim = (26 * Math.PI) / 180, a = Math.max(-lim, Math.min(lim, g));\n      s.amp = Math.max(Math.abs(a), 0.01);\n      let p = (a < 0 ? Math.PI : 0) - Math.PI * (ms / 1000);\n      p = ((p + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;\n      s.phase = p; s.held = false;\n    };\n    // A push keeps the bob where it is and the way it is heading, and only makes the swing wider.\n    push.current = () => {\n      if (reduce) return;\n      s.touched = performance.now();\n      const ms = Date.now(), cur = Math.PI * (ms / 1000) + s.phase, big = (PUSH * Math.PI) / 180;\n      let a = Math.acos(Math.max(-1, Math.min(1, thetaAt(ms) / big))); if (Math.sin(cur) < 0) a = -a;\n      let p = a - Math.PI * (ms / 1000);\n      p = ((p + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;\n      s.amp = big; s.phase = p; wake();\n    };\n    step(performance.now());\n    const slow = reduce ? window.setInterval(() => step(performance.now()), 500) : 0;\n\n    const toAngle = (e: PointerEvent) => { const b = cv.getBoundingClientRect(), k = W / b.width; return Math.atan2(-((e.clientX - b.left) * k - CX), (e.clientY - b.top) * k - CY); };\n    const onDown = (e: PointerEvent) => { if (reduce) return; s.held = true; s.grab = toAngle(e); s.touched = performance.now(); area.setPointerCapture(e.pointerId); e.preventDefault(); };\n    const onMove = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect(), k = W / b.width;\n      if (s.held) { const lim = (26 * Math.PI) / 180; s.grab = Math.max(-lim, Math.min(lim, toAngle(e))); s.touched = performance.now(); return; }\n      // the lamp stays above the clock and slides left and right with the pointer\n      s.touched = performance.now() - 2400; s.laTo = -Math.PI / 2 + Math.max(-1, Math.min(1, (((e.clientX - b.left) * k - CX) / W) * 2.4));\n      if (reduce) step(performance.now());\n    };\n    const onUp = () => { if (s.held) release(s.grab); };\n    area.addEventListener(\"pointerdown\", onDown); area.addEventListener(\"pointermove\", onMove);\n    area.addEventListener(\"pointerup\", onUp); area.addEventListener(\"pointercancel\", onUp);\n    const io = new IntersectionObserver(([en]) => { seen = en.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); if (slow) window.clearInterval(slow); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n      area.removeEventListener(\"pointerdown\", onDown); area.removeEventListener(\"pointermove\", onMove);\n      area.removeEventListener(\"pointerup\", onUp); area.removeEventListener(\"pointercancel\", onUp);\n    };\n  }, []);\n\n  return (\n    <div className=\"flex min-h-[760px] w-full flex-col items-center justify-center bg-[#15231e] bg-[radial-gradient(75%_60%_at_40%_28%,#4a7163_0%,#2a463c_40%,#132019_100%)] px-4 py-6 sm:min-h-[800px]\">\n      <div className=\"relative w-full max-w-[440px]\" style={{ aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <div ref={hit} aria-hidden=\"true\" className=\"absolute inset-x-0 bottom-0 top-[42%] cursor-grab touch-pan-y active:cursor-grabbing\" />\n      </div>\n      <div className=\"mt-1 flex flex-col items-center text-center\">\n        <time className=\"font-mono text-[30px] font-semibold tabular-nums leading-none tracking-tight text-[#f3ecdb]\">{clock}</time>\n        <p className=\"mt-3 max-w-[300px] font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-emerald-100/80 sm:max-w-none\">One swing, one second. Pull the bob aside and let go.</p>\n        <button type=\"button\" onClick={() => push.current()} className=\"mt-4 h-10 rounded-full border border-[#e9c46a]/50 px-5 font-mono text-[11px] font-semibold uppercase tracking-[0.16em] text-[#f1d488] outline-none transition-colors hover:bg-[#e9c46a]/10 focus-visible:ring-2 focus-visible:ring-[#f1d488] motion-reduce:hidden\">\n          Give it a push\n        </button>\n      </div>\n    </div>\n  );\n}\n"}]}
