{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"brass-aneroid-barometer-gauge-with-blued-needle-and-a-set-hand-that-marks-the-target","type":"registry:component","title":"Brass Aneroid Barometer Gauge with Blued Needle and a Set Hand That Marks the Target","description":"Gauge drawn as a brass wall barometer: a blued needle swings to the reading and a set hand marks the target. Copy the React code or an HTML version free.","dependencies":[],"files":[{"path":"components/we-inc/brass-aneroid-barometer-gauge-with-blued-needle-and-a-set-hand-that-marks-the-target.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst dial = { title: \"WEEKLY ORDERS\", maker: \"HALE & ORCHARD\", noun: \"orders\" };\nconst MIN = 0, MAX = 600; // the two ends of the printed scale\nconst STEP = 10; // one press of an arrow key moves the set hand this far\nconst bands = [{ at: 75, word: \"SLOW\" }, { at: 225, word: \"STEADY\" }, { at: 375, word: \"BRISK\" }, { at: 525, word: \"RUSH\" }];\nconst readings = [412, 436, 398, 455, 471, 428]; // the needle visits these in turn\nconst START_TARGET = 380; // where the brass set hand is parked\nconst HOLD = 3800; // milliseconds between readings\nconst W0 = 8.5, ZETA = 0.3; // how fast the needle swings and how soon it settles\n\nconst W = 600, H = 640, CX = 300, CY = 328, RO = 250, RD = 201; // canvas, centre, case radius, dial radius\nconst TAU = Math.PI * 2;\nconst clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v));\nconst ang = (v: number) => ((-135 + (270 * (v - MIN)) / (MAX - MIN)) * Math.PI) / 180; // 0 is straight up\n\nexport default function BrassAneroidBarometerGauge() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const knobHit = useRef<HTMLDivElement>(null);\n  const [idx, setIdx] = useState(0);\n  const [target, setTarget] = useState(START_TARGET);\n  const [said, setSaid] = useState(\"\");\n  const live = useRef({ reading: readings[0], target: START_TARGET, touched: 0 });\n  const api = useRef<{ poke: (kick?: boolean) => void } | null>(null);\n\n  const reading = readings[idx];\n  live.current.reading = reading;\n\n  useEffect(() => {\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const id = window.setInterval(() => {\n      if (document.hidden || performance.now() - live.current.touched < HOLD) return;\n      setIdx((i) => (i + 1) % readings.length);\n      api.current?.poke();\n    }, HOLD);\n    return () => window.clearInterval(id);\n  }, []);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = knobHit.current;\n    if (!cv || !pad) 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; // canvas pixels per design pixel\n\n    const brass = (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, \"#fff0bd\"); g.addColorStop(0.3, \"#c79a3c\"); g.addColorStop(0.55, \"#f3d584\"); g.addColorStop(0.8, \"#7a5616\"); g.addColorStop(1, \"#c79a3c\");\n        return g;\n      }\n      const g = c.createConicGradient(turn, x, y);\n      const stops: [number, string][] = [[0, \"#fffbe4\"], [0.04, \"#f0cd76\"], [0.13, \"#a97824\"], [0.25, \"#5b3d0c\"], [0.34, \"#b08028\"], [0.44, \"#f4d886\"], [0.5, \"#fffbe6\"], [0.56, \"#dbae4e\"], [0.7, \"#66460f\"], [0.8, \"#a27522\"], [0.92, \"#ecc96e\"], [1, \"#fffbe4\"]];\n      for (const [o, col] of stops) g.addColorStop(o, col);\n      return g;\n    };\n    const ring = (c: CanvasRenderingContext2D, r1: number, r0: number) => { c.beginPath(); c.arc(CX, CY, r1, 0, TAU); c.arc(CX, CY, r0, 0, TAU, true); c.closePath(); };\n    const at = (a: number, r: number): [number, number] => [CX + Math.sin(a) * r, CY - Math.cos(a) * r];\n    const spaced = (c: CanvasRenderingContext2D, text: string, x: number, y: number, gap: number) => {\n      const ws = [...text].map((ch) => c.measureText(ch).width + gap);\n      let px = x - (ws.reduce((p, q) => p + q, 0) - gap) / 2;\n      c.textAlign = \"left\";\n      [...text].forEach((ch, i) => { c.fillText(ch, px, y); px += ws[i]; });\n    };\n    const curved = (c: CanvasRenderingContext2D, text: string, mid: number, r: number, gap: number) => {\n      const ws = [...text].map((ch) => c.measureText(ch).width + gap);\n      let a = mid - (ws.reduce((p, q) => p + q, 0) - gap) / r / 2;\n      c.textAlign = \"center\";\n      [...text].forEach((ch, i) => {\n        const ac = a + (ws[i] - gap) / r / 2, [x, y] = at(ac, r);\n        c.save(); c.translate(x, y); c.rotate(ac); c.fillText(ch, 0, 0); c.restore();\n        a += ws[i] / r;\n      });\n    };\n\n    // Layer 1: case, bezel and printed dial. None of it moves, so it is painted once.\n    const base = document.createElement(\"canvas\");\n    const build = () => {\n      const b = base.getContext(\"2d\");\n      if (!b) return;\n      base.width = cv.width; base.height = cv.height;\n      b.setTransform(k, 0, 0, k, 0, 0);\n      const SERIF = 'Georgia, \"Times New Roman\", Times, serif';\n\n      // Shadows on the wall: one wide and soft, one tight under the case.\n      for (const [blur, ox, oy, al] of [[46, 18, 34, 0.8], [12, 4, 7, 0.7]]) {\n        b.save(); b.shadowColor = `rgba(0,0,0,${al})`; b.shadowBlur = blur * k; b.shadowOffsetX = ox * k; b.shadowOffsetY = oy * k;\n        b.beginPath(); b.arc(CX, CY, RO - 2, 0, TAU); b.fillStyle = \"#2a1c06\"; b.fill(); b.restore();\n      }\n      // The hanging loop and its lug.\n      const ly = CY - RO - 20;\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.5)\"; b.shadowBlur = 8 * k; b.shadowOffsetX = 5 * k; b.shadowOffsetY = 9 * k;\n      b.beginPath(); b.arc(CX, ly, 15, 0, TAU); b.lineWidth = 6; b.strokeStyle = brass(b, CX, ly, 18, -2.2); b.stroke(); b.restore();\n      b.beginPath(); b.arc(CX, ly, 17.2, Math.PI * 1.05, Math.PI * 1.6); b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.7)\"; b.stroke();\n      b.beginPath(); b.arc(CX, ly, 12.4, Math.PI * 0.1, Math.PI * 0.6); b.lineWidth = 1; b.strokeStyle = \"rgba(40,20,0,0.5)\"; b.stroke();\n      b.beginPath(); b.arc(CX, CY - RO + 2, 11, Math.PI, 0); b.fillStyle = brass(b, CX, CY - RO, 12, -2.4); b.fill();\n      b.beginPath(); b.arc(CX, ly - 15.5, 2.6, 0, TAU); b.fillStyle = \"#1a1408\"; b.fill(); // the pin it hangs on\n\n      // Case: a rolled outer edge, a coin-milled band, a sloped bezel and the seat for the glass.\n      b.beginPath(); b.arc(CX, CY, RO, 0, TAU); b.fillStyle = brass(b, CX, CY, RO, -2.25); b.fill();\n      const roll = b.createRadialGradient(CX, CY, RO - 15, CX, CY, RO);\n      roll.addColorStop(0, \"rgba(40,22,0,0.7)\"); roll.addColorStop(0.22, \"rgba(40,22,0,0.05)\"); roll.addColorStop(0.5, \"rgba(255,250,224,0.42)\"); roll.addColorStop(0.8, \"rgba(40,22,0,0.1)\"); roll.addColorStop(1, \"rgba(24,12,0,0.75)\");\n      ring(b, RO, RO - 15); b.fillStyle = roll; b.fill();\n      b.save(); b.lineCap = \"round\"; b.shadowColor = \"rgba(255,250,220,0.9)\"; b.shadowBlur = 6 * k;\n      b.beginPath(); b.arc(CX, CY, RO - 7.5, Math.PI * 1.08, Math.PI * 1.46); b.lineWidth = 2.2; b.strokeStyle = \"rgba(255,253,240,0.85)\"; b.stroke();\n      b.beginPath(); b.arc(CX, CY, RO - 7.5, Math.PI * 0.12, Math.PI * 0.34); b.lineWidth = 1.6; b.strokeStyle = \"rgba(255,246,214,0.45)\"; b.stroke();\n      b.restore();\n      ring(b, RO - 15, RO - 26); b.fillStyle = \"rgba(40,24,2,0.84)\"; b.fill();\n      for (let i = 0; i < 200; i++) {\n        const a = (i / 200) * TAU, lit = Math.max(0, Math.cos(a + Math.PI * 0.75)), back = Math.pow(Math.max(0, Math.cos(a - Math.PI * 0.25)), 3);\n        b.beginPath(); b.moveTo(CX + Math.cos(a) * (RO - 25.4), CY + Math.sin(a) * (RO - 25.4)); b.lineTo(CX + Math.cos(a) * (RO - 15.6), CY + Math.sin(a) * (RO - 15.6));\n        b.lineWidth = 1.7; b.strokeStyle = `rgba(255,232,160,${Math.min(1, 0.12 + 0.88 * lit + 0.5 * back)})`; b.stroke();\n      }\n      ring(b, RO - 26, RD + 5); b.fillStyle = brass(b, CX, CY, RO, 0.95); b.fill();\n      const slope = b.createRadialGradient(CX, CY, RD + 5, CX, CY, RO - 26);\n      slope.addColorStop(0, \"rgba(30,15,0,0.62)\"); slope.addColorStop(0.2, \"rgba(30,15,0,0.12)\"); slope.addColorStop(0.62, \"rgba(255,250,224,0.2)\"); slope.addColorStop(1, \"rgba(30,15,0,0.5)\");\n      ring(b, RO - 26, RD + 5); b.fillStyle = slope; b.fill();\n      b.save(); b.lineCap = \"round\"; b.shadowColor = \"rgba(255,250,220,0.8)\"; b.shadowBlur = 10 * k;\n      b.beginPath(); b.arc(CX, CY, (RO - 26 + RD + 5) / 2 + 1, Math.PI * 1.12, Math.PI * 1.42); b.lineWidth = 7; b.strokeStyle = \"rgba(255,250,225,0.5)\"; b.stroke();\n      b.beginPath(); b.arc(CX, CY, (RO - 26 + RD + 5) / 2 + 1, Math.PI * 0.16, Math.PI * 0.36); b.lineWidth = 5; b.strokeStyle = \"rgba(255,244,205,0.3)\"; b.stroke();\n      b.restore();\n      b.beginPath(); b.arc(CX, CY, RO - 26, Math.PI * 0.95, Math.PI * 1.6); b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.55)\"; b.stroke();\n      const seat = b.createRadialGradient(CX, CY, RD, CX, CY, RD + 5);\n      seat.addColorStop(0, \"#120c03\"); seat.addColorStop(1, \"#5c4011\");\n      ring(b, RD + 5, RD - 1); b.fillStyle = seat; b.fill();\n\n      // Silvered dial with a faint spun grain, shaded where the bezel stands over it.\n      const face = b.createRadialGradient(CX - 46, CY - 66, 20, CX, CY, RD);\n      face.addColorStop(0, \"#fdfaf0\"); face.addColorStop(0.55, \"#f1ead6\"); face.addColorStop(1, \"#d8cdae\");\n      b.beginPath(); b.arc(CX, CY, RD, 0, TAU); b.fillStyle = face; b.fill();\n      b.save(); b.beginPath(); b.arc(CX, CY, RD, 0, TAU); b.clip();\n      for (let r = 4; r < RD; r += 2.2) { b.beginPath(); b.arc(CX, CY, r, 0, TAU); b.lineWidth = 0.6; b.strokeStyle = (r / 2.2) % 2 < 1 ? \"rgba(255,255,255,0.2)\" : \"rgba(90,70,30,0.05)\"; b.stroke(); }\n      b.shadowColor = \"rgba(40,24,0,0.6)\"; b.shadowBlur = 16 * k; b.shadowOffsetX = 7 * k; b.shadowOffsetY = 11 * k;\n      b.beginPath(); b.arc(CX, CY, RD + 12, 0, TAU); b.lineWidth = 22; b.strokeStyle = \"#000\"; b.stroke();\n      b.restore();\n\n      // The printed scale.\n      const INK = \"#1d1710\", R1 = RD - 15, R0 = R1 - 12;\n      b.lineCap = \"butt\";\n      for (const r of [R1, R0]) { b.beginPath(); b.arc(CX, CY, r, ang(MIN) - Math.PI / 2, ang(MAX) - Math.PI / 2); b.lineWidth = r === R1 ? 1.5 : 0.8; b.strokeStyle = INK; b.stroke(); }\n      b.textBaseline = \"middle\"; b.fillStyle = INK;\n      for (let v = MIN; v <= MAX + 0.001; v += 10) {\n        const a = ang(v), big = v % 100 === 0, mid = v % 50 === 0;\n        const [x0, y0] = at(a, R1), [x1, y1] = at(a, big ? R0 - 9 : mid ? R0 - 5 : R0);\n        b.beginPath(); b.moveTo(x0, y0); b.lineTo(x1, y1); b.lineWidth = big ? 2.2 : mid ? 1.3 : 0.8; b.strokeStyle = INK; b.stroke();\n        if (big) { const [x, y] = at(a, R0 - 27); b.font = `700 19px ${SERIF}`; b.textAlign = \"center\"; b.fillText(String(v), x, y); }\n      }\n      b.font = `700 12.5px ${SERIF}`; b.fillStyle = \"#2a2118\";\n      for (const band of bands) curved(b, band.word, ang(band.at), R0 - 63, 3.2);\n      // Dividers between the words, as on a weather dial.\n      for (const v of [150, 300, 450]) { const [x0, y0] = at(ang(v), R0 - 55), [x1, y1] = at(ang(v), R0 - 70); b.beginPath(); b.moveTo(x0, y0); b.lineTo(x1, y1); b.lineWidth = 0.8; b.strokeStyle = \"rgba(29,23,16,0.6)\"; b.stroke(); }\n      for (const [r, w] of [[R0 - 80, 1.2], [R0 - 83.5, 0.6]]) { b.beginPath(); b.arc(CX, CY, r, 0, TAU); b.lineWidth = w; b.strokeStyle = \"rgba(29,23,16,0.75)\"; b.stroke(); }\n      // Two blued screws hold the dial to the movement.\n      for (const sx of [CX - 62, CX + 62]) {\n        b.beginPath(); b.arc(sx, CY + 0.8, 4.4, 0, TAU); b.fillStyle = \"rgba(60,40,0,0.35)\"; b.fill();\n        const sg = b.createRadialGradient(sx - 1.2, CY - 1.4, 0.4, sx, CY, 4);\n        sg.addColorStop(0, \"#8fb0ee\"); sg.addColorStop(0.45, \"#2b4686\"); sg.addColorStop(1, \"#0c1630\");\n        b.beginPath(); b.arc(sx, CY, 3.8, 0, TAU); b.fillStyle = sg; b.fill();\n        b.save(); b.translate(sx, CY); b.rotate(sx * 0.4); b.fillStyle = \"rgba(4,8,20,0.9)\"; b.fillRect(-3.4, -0.6, 6.8, 1.2); b.restore();\n      }\n      b.fillStyle = INK; b.font = `700 12px ${SERIF}`; spaced(b, dial.title, CX, CY + 126, 2.6);\n      b.fillStyle = \"rgba(29,23,16,0.72)\"; b.font = `600 7.5px ${SERIF}`; spaced(b, dial.maker, CX, CY + 144, 2.2);\n      b.beginPath(); b.moveTo(CX - 20, CY + 135.5); b.lineTo(CX + 20, CY + 135.5); b.lineWidth = 0.6; b.strokeStyle = \"rgba(29,23,16,0.6)\"; b.stroke();\n    };\n\n    const fit = () => {\n      const w = cv.clientWidth || W;\n      k = (w / W) * Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(W * k); cv.height = Math.round(H * k);\n      build();\n    };\n\n    // x is the needle's reading and v its speed. tg is where the set hand is drawn.\n    const s = { x: MIN, v: 0, tg: live.current.target, light: -0.2, lightTo: -0.2 };\n\n    const needle = (a: number) => {\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(a);\n      ctx.shadowColor = \"rgba(52,32,0,0.42)\"; ctx.shadowBlur = 5 * k; ctx.shadowOffsetX = 5 * k; ctx.shadowOffsetY = 8 * k;\n      const steel = ctx.createLinearGradient(-4, 0, 4, 0);\n      steel.addColorStop(0, \"#0c1630\"); steel.addColorStop(0.42, \"#33528f\"); steel.addColorStop(0.6, \"#1a2c57\"); steel.addColorStop(1, \"#080f22\");\n      ctx.fillStyle = steel;\n      ctx.beginPath(); ctx.moveTo(0, -R_TIP); ctx.lineTo(4.6, -R_TIP + 26); ctx.lineTo(1.3, -R_TIP + 22); ctx.lineTo(3, 0); ctx.lineTo(1.8, 36); ctx.lineTo(-1.8, 36); ctx.lineTo(-3, 0); ctx.lineTo(-1.3, -R_TIP + 22); ctx.lineTo(-4.6, -R_TIP + 26); ctx.closePath(); ctx.fill();\n      // The counterweight: an open crescent on the tail.\n      ctx.beginPath(); ctx.arc(0, 47, 12, 0, TAU); ctx.arc(0, 43.5, 7.5, 0, TAU, true); ctx.fill(\"evenodd\");\n      ctx.shadowColor = \"transparent\";\n      ctx.beginPath(); ctx.moveTo(-0.7, -R_TIP + 20); ctx.lineTo(-1.6, -4); ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(150,190,255,0.75)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(0, 47, 11.4, Math.PI * 1.1, Math.PI * 1.6); ctx.lineWidth = 0.6; ctx.stroke();\n      ctx.restore();\n    };\n    const R_TIP = RD - 22;\n\n    const setHand = (a: number) => {\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(a);\n      // It rides on the glass, well clear of the dial, so its shadow falls further away and softer.\n      ctx.shadowColor = \"rgba(52,32,0,0.3)\"; ctx.shadowBlur = 9 * k; ctx.shadowOffsetX = 10 * k; ctx.shadowOffsetY = 16 * k;\n      const g = ctx.createLinearGradient(-5, 0, 5, 0);\n      g.addColorStop(0, \"#6a480e\"); g.addColorStop(0.38, \"#f0cc6c\"); g.addColorStop(0.6, \"#b58222\"); g.addColorStop(1, \"#553909\");\n      ctx.fillStyle = g;\n      const tip = RD - 26;\n      ctx.beginPath(); ctx.moveTo(-2, 0); ctx.lineTo(-1.25, -tip + 28); ctx.lineTo(-6.6, -tip + 16); ctx.lineTo(0, -tip); ctx.lineTo(6.6, -tip + 16); ctx.lineTo(1.25, -tip + 28); ctx.lineTo(2, 0); ctx.closePath();\n      ctx.moveTo(0, -tip + 9); ctx.lineTo(-2.7, -tip + 16.4); ctx.lineTo(0, -tip + 22); ctx.lineTo(2.7, -tip + 16.4); ctx.closePath();\n      ctx.fill(\"evenodd\");\n      ctx.restore();\n    };\n\n    const knob = (a: number) => {\n      const la = -2.3 + s.light * 0.4;\n      ctx.save(); ctx.shadowColor = \"rgba(30,18,0,0.6)\"; ctx.shadowBlur = 13 * k; ctx.shadowOffsetX = 7 * k; ctx.shadowOffsetY = 12 * k;\n      ctx.beginPath(); ctx.arc(CX, CY, 22, 0, TAU); ctx.fillStyle = \"#4a3208\"; ctx.fill(); ctx.restore();\n      const teeth = 34, step = TAU / teeth;\n      for (let i = 0; i < teeth; i++) {\n        const t0 = a + i * step, f = Math.cos(t0 - la - Math.PI / 2), lit = 0.5 + 0.5 * f, glint = Math.pow(Math.max(0, f), 6);\n        for (let h = 0; h < 2; h++) {\n          const a0 = t0 + h * step * 0.5 - Math.PI / 2, a1 = a0 + step * 0.5 + 0.004;\n          const v = h === 0 ? 0.36 + lit * 0.5 + glint * 0.5 : 0.14 + lit * 0.2;\n          ctx.beginPath(); ctx.arc(CX, CY, h === 0 ? 23 : 21, a0, a1); ctx.arc(CX, CY, 14, a1, a0, true); ctx.closePath();\n          ctx.fillStyle = `rgb(${Math.min(255, 255 * v) | 0},${Math.min(255, 206 * v) | 0},${Math.min(255, 104 * v) | 0})`; ctx.fill();\n        }\n      }\n      ctx.beginPath(); ctx.arc(CX, CY, 16.4, 0, TAU); ctx.fillStyle = \"#3c2806\"; ctx.fill();\n      ctx.beginPath(); ctx.arc(CX, CY, 15.4, 0, TAU); ctx.fillStyle = brass(ctx, CX, CY, 16, la); ctx.fill();\n      for (let r = 2.5; r < 15; r += 1.7) { ctx.beginPath(); ctx.arc(CX, CY, r, 0, TAU); ctx.lineWidth = 0.5; ctx.strokeStyle = (r / 1.7) % 2 < 1 ? \"rgba(255,255,255,0.22)\" : \"rgba(60,36,0,0.16)\"; ctx.stroke(); }\n      ctx.beginPath(); ctx.arc(CX, CY, 15, 0, TAU); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(255,248,214,0.7)\"; ctx.stroke();\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(a);\n      ctx.beginPath(); ctx.arc(0, -9.6, 1.9, 0, TAU); ctx.fillStyle = \"#231704\"; ctx.fill();\n      ctx.restore();\n    };\n\n    const glass = () => {\n      const sl = s.light;\n      ctx.save(); ctx.beginPath(); ctx.arc(CX, CY, RD, 0, TAU); ctx.clip();\n      const edge = ctx.createRadialGradient(CX, CY, RD * 0.8, CX, CY, RD);\n      edge.addColorStop(0, \"rgba(40,24,0,0)\"); edge.addColorStop(1, \"rgba(40,24,0,0.2)\");\n      ctx.fillStyle = edge; ctx.fillRect(CX - RD, CY - RD, RD * 2, RD * 2);\n      const bloom = ctx.createRadialGradient(CX - 78 + sl * 46, CY - 112, 6, CX - 60 + sl * 46, CY - 90, 190);\n      bloom.addColorStop(0, \"rgba(255,255,255,0.3)\"); bloom.addColorStop(0.4, \"rgba(255,255,255,0.08)\"); bloom.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = bloom; ctx.fillRect(CX - RD, CY - RD, RD * 2, RD * 2);\n      // The crescent a domed glass throws back from a window.\n      const a0 = Math.PI * (1.06 + sl * 0.1), a1 = Math.PI * (1.6 + sl * 0.1);\n      const cres = ctx.createLinearGradient(CX - RD, CY - RD, CX, CY);\n      cres.addColorStop(0, \"rgba(255,255,255,0.8)\"); cres.addColorStop(1, \"rgba(255,255,255,0.04)\");\n      ctx.beginPath(); ctx.arc(CX, CY, RD - 7, a0, a1); ctx.arc(CX + 15, CY + 20, RD - 7, a1 - 0.07, a0 + 0.1, true); ctx.closePath(); ctx.fillStyle = cres; ctx.fill();\n      ctx.beginPath(); ctx.arc(CX, CY, RD - 5, Math.PI * (0.08 + sl * 0.06), Math.PI * (0.42 + sl * 0.06)); ctx.lineWidth = 2.4; ctx.lineCap = \"round\"; ctx.strokeStyle = \"rgba(255,255,255,0.22)\"; ctx.stroke();\n      // Four window panes, bent a little by the curve of the glass.\n      const wx = CX + 58 + sl * 38, wy = CY - 132;\n      ctx.fillStyle = \"rgba(255,255,255,0.13)\";\n      for (let i = 0; i < 2; i++) for (let j = 0; j < 2; j++) {\n        const x = wx + i * 27 + j * 5, y = wy + j * 31 + i * 7;\n        ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + 23, y + 6); ctx.lineTo(x + 27, y + 33); ctx.lineTo(x + 4.5, y + 27); ctx.closePath(); ctx.fill();\n      }\n      ctx.restore();\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, cv.width, cv.height);\n      ctx.drawImage(base, 0, 0);\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      needle(ang(s.x));\n      setHand(ang(s.tg));\n      knob(ang(s.tg));\n      glass();\n    };\n\n    let raf = 0, last = 0, seen = true, moved = -1e9;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      const dt = Math.min(0.04, (now - last) / 1000); last = now;\n      const span = MAX - MIN;\n      for (let i = 0; i < 4; i++) {\n        const h = dt / 4;\n        s.v += (W0 * W0 * (live.current.reading - s.x) - 2 * ZETA * W0 * s.v) * h; s.x += s.v * h;\n        // End pins: the needle taps them and comes back off.\n        if (s.x > MAX + span * 0.03) { s.x = MAX + span * 0.03; s.v *= -0.3; }\n        if (s.x < MIN - span * 0.03) { s.x = MIN - span * 0.03; s.v *= -0.3; }\n      }\n      s.tg += (live.current.target - s.tg) * Math.min(1, dt * 16);\n      if (now - moved > 2500) s.lightTo = 0.5 * Math.sin(now / 2100) - 0.1;\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => { s.x = live.current.reading; s.v = 0; s.tg = live.current.target; draw(); };\n    const wake = (kick?: boolean) => {\n      if (reduce) { still(); return; }\n      if (kick) s.v += (Math.random() < 0.5 ? -1 : 1) * (220 + Math.random() * 120); // a knuckle on the glass\n      if (!raf) { last = 0; raf = requestAnimationFrame(frame); }\n    };\n    api.current = { poke: wake };\n    fit(); wake();\n\n    // The knob in the middle of the glass turns the set hand.\n    let drag = false, lastA = 0, val = 0;\n    const angleAt = (e: PointerEvent) => { const r = pad.getBoundingClientRect(); return Math.atan2(e.clientX - (r.left + r.width / 2), -(e.clientY - (r.top + r.height / 2))); };\n    const onDown = (e: PointerEvent) => { drag = true; lastA = angleAt(e); val = live.current.target; pad.setPointerCapture(e.pointerId); e.preventDefault(); };\n    const onMove = (e: PointerEvent) => {\n      if (!drag) return;\n      const a = angleAt(e); let d = a - lastA; if (d > Math.PI) d -= TAU; if (d < -Math.PI) d += TAU; lastA = a;\n      val = clamp(val + (d / ((270 * Math.PI) / 180)) * (MAX - MIN), MIN, MAX);\n      const next = Math.round(val / STEP) * STEP;\n      if (next !== live.current.target) { live.current.target = next; live.current.touched = performance.now(); setTarget(next); wake(); }\n    };\n    const onUp = () => { drag = false; };\n    const onLight = (e: PointerEvent) => { const r = cv.getBoundingClientRect(); moved = performance.now(); s.lightTo = clamp(((e.clientX - r.left) / r.width) * 2 - 1, -1, 1); };\n    pad.addEventListener(\"pointerdown\", onDown); pad.addEventListener(\"pointermove\", onMove);\n    pad.addEventListener(\"pointerup\", onUp); pad.addEventListener(\"pointercancel\", onUp);\n    const host = cv.parentElement;\n    host?.addEventListener(\"pointermove\", onLight);\n    const ro = new ResizeObserver(() => { fit(); if (reduce) still(); else draw(); });\n    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    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      pad.removeEventListener(\"pointerdown\", onDown); pad.removeEventListener(\"pointermove\", onMove);\n      pad.removeEventListener(\"pointerup\", onUp); pad.removeEventListener(\"pointercancel\", onUp);\n      host?.removeEventListener(\"pointermove\", onLight);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const gap = reading - target;\n  const verdict = gap === 0 ? \"On target\" : `${Math.abs(gap)} ${gap > 0 ? \"ahead\" : \"behind\"}`;\n  const moveTarget = (next: number) => {\n    const v = clamp(next, MIN, MAX);\n    live.current.target = v; live.current.touched = performance.now();\n    setTarget(v); setSaid(`Target ${v}. Reading ${reading}, ${v === reading ? \"on target\" : `${Math.abs(reading - v)} ${reading > v ? \"ahead\" : \"behind\"}`}.`);\n    api.current?.poke();\n  };\n  const tap = () => {\n    const next = (idx + 1) % readings.length;\n    live.current.touched = performance.now(); live.current.reading = readings[next];\n    setIdx(next); setSaid(`Reading ${readings[next]} ${dial.noun}.`);\n    api.current?.poke(true);\n  };\n  const onKey = (e: ReactKeyboardEvent) => {\n    const next = e.key === \"ArrowRight\" || e.key === \"ArrowUp\" ? target + STEP : e.key === \"ArrowLeft\" || e.key === \"ArrowDown\" ? target - STEP : e.key === \"PageUp\" ? target + STEP * 5 : e.key === \"PageDown\" ? target - STEP * 5 : e.key === \"Home\" ? MIN : e.key === \"End\" ? MAX : null;\n    if (next === null) return;\n    e.preventDefault(); moveTarget(next);\n  };\n  const pct = (v: number, of: number) => `${(v / of) * 100}%`;\n\n  return (\n    <div className=\"flex min-h-[720px] w-full flex-col items-center justify-center bg-[#14271f] bg-[radial-gradient(80%_72%_at_44%_40%,#2f5a4a_0%,#1b382d_46%,#0d1a15_100%)] px-4 py-8\">\n      <div className=\"relative w-full\" style={{ maxWidth: 548, aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <button\n          type=\"button\"\n          onClick={tap}\n          aria-label=\"Tap the glass for the next reading\"\n          style={{ left: pct(CX - RD, W), top: pct(CY - RD, H), width: pct(RD * 2, W), height: pct(RD * 2, H) }}\n          className=\"absolute cursor-pointer rounded-full outline-none focus-visible:ring-2 focus-visible:ring-[#ffe08a] focus-visible:ring-offset-4 focus-visible:ring-offset-transparent\"\n        />\n        <div\n          ref={knobHit}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label=\"Target, shown by the brass set hand\"\n          aria-valuemin={MIN}\n          aria-valuemax={MAX}\n          aria-valuenow={target}\n          aria-valuetext={`${target} ${dial.noun}`}\n          onKeyDown={onKey}\n          style={{ left: pct(CX - 44, W), top: pct(CY - 44, H), width: pct(88, W), height: pct(88, H) }}\n          className=\"absolute cursor-grab touch-none rounded-full outline-none focus-visible:ring-2 focus-visible:ring-[#ffe08a] active:cursor-grabbing\"\n        />\n      </div>\n      <p className=\"-mt-1 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-[#b9c8bd]\">\n        <span className=\"text-[#f4ecd4]\">{reading} {dial.noun}</span>\n        <span className=\"mx-2 text-[#5d7568]\">/</span>\n        Target {target}\n        <span className=\"mx-2 text-[#5d7568]\">/</span>\n        <span className=\"text-[#ffd98a]\">{verdict}</span>\n      </p>\n      <p aria-live=\"polite\" className=\"sr-only\">{said}</p>\n    </div>\n  );\n}\n"}]}
