{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pocket-radio-with-a-thumbwheel-tuning-needle-and-backlit-band","type":"registry:component","title":"Pocket Radio with a Thumbwheel, Tuning Needle and Backlit Band","description":"Radio tuner drawn as a pocket transistor set: slide the needle along a backlit band or flick the wheel, and stations pull it in. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/pocket-radio-with-a-thumbwheel-tuning-needle-and-backlit-band.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// Stations on the dial. Frequencies are in megahertz, between LOW and HIGH.\nconst stations = [\n  { at: 89.3, name: \"Harbour Jazz\", tempo: 84 },\n  { at: 93.7, name: \"The Night Desk\", tempo: 112 },\n  { at: 98.1, name: \"Cedar Street Soul\", tempo: 96 },\n  { at: 102.6, name: \"Radio Lantern\", tempo: 126 },\n  { at: 106.2, name: \"Open Water\", tempo: 72 },\n];\nconst LOW = 87.5, HIGH = 108.5;\nconst SHELL = [\"#ee5f41\", \"#d9462b\", \"#b93521\"]; // the moulded case, light to dark\nconst NEEDLE = \"#d92b1c\";\n\n// Design pixels. The case is 360 by 560 inside a 480 by 740 drawing, which leaves room for the aerial and the shadow.\nconst W = 480, H = 740, BX = 50, BY = 124, BW = 360, BH = 560, BR = 36;\nconst FX = 84, FY = 160, FW = 292, FH = 92; // the dial card behind the glass\nconst X0 = FX + 46, X1 = FX + FW - 46; // where 88 and 108 sit\nconst GX = 230, GY = 474, GR = 148; // speaker grille\nconst xOf = (f: number) => X0 + ((f - 88) / 20) * (X1 - X0);\nconst nearest = (f: number) => stations.reduce((a, b) => (Math.abs(b.at - f) < Math.abs(a.at - f) ? b : a));\nconst lock = (f: number) => Math.exp(-Math.pow((nearest(f).at - f) / 0.26, 2)); // 1 on a station, 0 between\n\nexport default function PocketRadio() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const dialHit = useRef<HTMLDivElement>(null);\n  const wheelHit = useRef<HTMLDivElement>(null);\n  const [freq, setFreq] = useState(98.1);\n  const [on, setOn] = useState(true);\n  const live = useRef({ freq: 98.1, on: true });\n  const api = useRef<{ tune: (f: number) => void; step: (by: number) => void; poke: () => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = dialHit.current, wheel = wheelHit.current;\n    if (!cv || !pad || !wheel) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let k = 1;\n\n    const rr = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) => {\n      c.beginPath(); c.moveTo(x + r, y); c.arcTo(x + w, y, x + w, y + h, r); c.arcTo(x + w, y + h, x, y + h, r); c.arcTo(x, y + h, x, y, r); c.arcTo(x, y, x + w, y, r); c.closePath();\n    };\n    const disc = (c: CanvasRenderingContext2D, x: number, y: number, r: number) => { c.beginPath(); c.arc(x, y, r, 0, Math.PI * 2); };\n    const chrome = (c: CanvasRenderingContext2D, x0: number, y0: number, x1: number, y1: number) => {\n      const g = c.createLinearGradient(x0, y0, x1, y1);\n      g.addColorStop(0, \"#fdfdfd\"); g.addColorStop(0.22, \"#b3b6bb\"); g.addColorStop(0.42, \"#f4f5f6\"); g.addColorStop(0.6, \"#7d8086\"); g.addColorStop(0.8, \"#d4d6da\"); g.addColorStop(1, \"#8f9298\");\n      return g;\n    };\n\n    const body = document.createElement(\"canvas\"); // the case, with the grille holes punched right through\n    const dial = document.createElement(\"canvas\"); // the print on the dial card\n\n    const build = () => {\n      const b = body.getContext(\"2d\");\n      if (!b) return;\n      body.width = cv.width; body.height = cv.height;\n      b.setTransform(k, 0, 0, k, 0, 0);\n      let seed = 3;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n      // Telescopic aerial lying up and to the right of the case, with the shadow it throws on the table.\n      const aerial = (shadow: boolean) => {\n        b.save(); b.translate(322, BY + 9); b.rotate(-1.19);\n        if (shadow) { b.shadowColor = \"rgba(40,48,38,0.38)\"; b.shadowBlur = 7 * k; b.shadowOffsetX = 7 * k; b.shadowOffsetY = 12 * k; }\n        let x = 0;\n        for (const [len, half] of [[44, 3.6], [38, 2.7], [36, 1.9]] as const) {\n          b.fillStyle = chrome(b, 0, -half, 0, half); b.fillRect(x, -half, len + 1, half * 2);\n          if (!shadow) { b.fillStyle = \"rgba(0,0,0,0.35)\"; b.fillRect(x + len - 0.6, -half, 0.9, half * 2); }\n          x += len;\n        }\n        disc(b, x + 2.5, 0, 4.2);\n        const tip = b.createRadialGradient(x + 1.2, -1.6, 0.4, x + 2.5, 0, 4.2); tip.addColorStop(0, \"#fff\"); tip.addColorStop(0.5, \"#b5b8bd\"); tip.addColorStop(1, \"#5e6166\");\n        b.fillStyle = tip; b.fill();\n        b.restore();\n      };\n      aerial(true); aerial(false);\n\n      // Case shadow, then the case.\n      b.save(); b.shadowColor = \"rgba(40,48,38,0.42)\"; b.shadowBlur = 34 * k; b.shadowOffsetX = 10 * k; b.shadowOffsetY = 22 * k; rr(b, BX, BY, BW, BH, BR); b.fillStyle = SHELL[2]; b.fill(); b.restore();\n      b.save(); b.shadowColor = \"rgba(30,20,14,0.6)\"; b.shadowBlur = 6 * k; b.shadowOffsetX = 1.5 * k; b.shadowOffsetY = 3 * k; rr(b, BX, BY, BW, BH, BR); b.fillStyle = SHELL[2]; b.fill(); b.restore();\n      const shell = b.createLinearGradient(BX, BY, BX + BW * 0.5, BY + BH);\n      shell.addColorStop(0, SHELL[0]); shell.addColorStop(0.5, SHELL[1]); shell.addColorStop(1, SHELL[2]);\n      rr(b, BX, BY, BW, BH, BR); b.fillStyle = shell; b.fill();\n      b.save(); rr(b, BX, BY, BW, BH, BR); b.clip();\n      // Rounded-over edges: light catches the top and left, the bottom and right fall away.\n      for (let i = 0; i < 16; i++) {\n        const t = i / 16;\n        rr(b, BX + i * 0.8, BY + i * 0.8, BW - i * 1.6, BH - i * 1.6, BR - i * 0.8); b.lineWidth = 1.4;\n        const g = b.createLinearGradient(BX, BY, BX + BW, BY + BH);\n        g.addColorStop(0, `rgba(255,236,224,${0.42 * (1 - t) * (1 - t)})`); g.addColorStop(0.5, \"rgba(255,255,255,0)\"); g.addColorStop(1, `rgba(60,8,0,${0.5 * (1 - t) * (1 - t)})`);\n        b.strokeStyle = g; b.stroke();\n      }\n      // A long soft reflection of the window in the gloss.\n      const gloss = b.createLinearGradient(BX, BY, BX + BW, BY + BH * 0.7);\n      gloss.addColorStop(0, \"rgba(255,255,255,0.2)\"); gloss.addColorStop(0.3, \"rgba(255,255,255,0.03)\"); gloss.addColorStop(0.5, \"rgba(255,255,255,0)\"); gloss.addColorStop(0.72, \"rgba(255,255,255,0.05)\"); gloss.addColorStop(1, \"rgba(0,0,0,0.08)\");\n      b.fillStyle = gloss; b.fillRect(BX, BY, BW, BH);\n      for (let i = 0; i < 5200; i++) { b.fillStyle = rand() > 0.5 ? \"rgba(255,255,255,0.045)\" : \"rgba(60,10,0,0.05)\"; b.fillRect(BX + rand() * BW, BY + rand() * BH, 0.9, 0.9); }\n      b.restore();\n      // The seam where the two halves of the case meet.\n      rr(b, BX + 9, BY + 9, BW - 18, BH - 18, BR - 8); b.lineWidth = 1; b.strokeStyle = \"rgba(70,12,2,0.55)\"; b.stroke();\n      rr(b, BX + 10, BY + 10, BW - 20, BH - 20, BR - 9); b.lineWidth = 0.8; b.strokeStyle = \"rgba(255,214,196,0.35)\"; b.stroke();\n\n      // Chrome trim round the dial and the dark well behind it.\n      b.save(); b.shadowColor = \"rgba(60,8,0,0.6)\"; b.shadowBlur = 5 * k; b.shadowOffsetY = 2 * k; rr(b, FX - 9, FY - 9, FW + 18, FH + 18, 13); b.fillStyle = \"#999\"; b.fill(); b.restore();\n      rr(b, FX - 9, FY - 9, FW + 18, FH + 18, 13); b.fillStyle = chrome(b, FX, FY - 9, FX + FW * 0.4, FY + FH + 9); b.fill();\n      rr(b, FX - 8.4, FY - 8.4, FW + 16.8, FH + 16.8, 12.4); b.lineWidth = 0.8; b.strokeStyle = \"rgba(255,255,255,0.9)\"; b.stroke();\n      rr(b, FX - 3, FY - 3, FW + 6, FH + 6, 8); b.fillStyle = \"#0c0a08\"; b.fill();\n\n      // Slot for the tuning wheel and a ring of groove round the speaker.\n      rr(b, BX + BW - 17, FY - 4, 20, FH + 8, 5); b.fillStyle = \"#1a0705\"; b.fill();\n      rr(b, BX + BW - 17, FY - 4, 20, FH + 8, 5); b.lineWidth = 1; b.strokeStyle = \"rgba(255,214,196,0.3)\"; b.stroke();\n      disc(b, GX, GY, GR + 9); b.lineWidth = 1.6; b.strokeStyle = \"rgba(70,12,2,0.5)\"; b.stroke();\n      disc(b, GX, GY, GR + 10.2); b.lineWidth = 0.9; b.strokeStyle = \"rgba(255,214,196,0.32)\"; b.stroke();\n\n      // Legends moulded into the case and filled with cream paint.\n      const paint = (text: string, x: number, y: number, size: number, align: CanvasTextAlign = \"center\") => {\n        b.font = `700 ${size}px ui-sans-serif, system-ui, \"Helvetica Neue\", Arial, sans-serif`; b.textAlign = align; b.textBaseline = \"middle\";\n        b.fillStyle = \"rgba(70,12,2,0.5)\"; b.fillText(text, x, y + 0.8); b.fillStyle = \"#fbeede\"; b.fillText(text, x, y);\n      };\n      paint(\"T U N E D\", FX + 16, FY + FH + 30, 6.5, \"left\");\n      paint(\"P O W E R\", FX + FW - 62, FY + FH + 30, 6.5, \"right\");\n      paint(\"TWO BAND  ·  SOLID STATE\", GX, BY + BH - 30, 7);\n      // Wells for the tuned lamp and the power key.\n      disc(b, FX + 4, FY + FH + 30, 5.4); b.fillStyle = \"#3a0d06\"; b.fill(); disc(b, FX + 4, FY + FH + 30.6, 5.6); b.lineWidth = 0.8; b.strokeStyle = \"rgba(255,214,196,0.35)\"; b.stroke();\n      rr(b, FX + FW - 54, FY + FH + 17, 58, 26, 13); b.fillStyle = \"#3a0d06\"; b.fill(); rr(b, FX + FW - 54, FY + FH + 17.6, 58, 26, 13); b.lineWidth = 0.8; b.strokeStyle = \"rgba(255,214,196,0.35)\"; b.stroke();\n\n      // The grille: every hole has a shadowed upper wall and a lit lower lip, then goes right through the layer.\n      const step = 11.2, rows = Math.ceil(GR / (step * 0.866)), holes: [number, number][] = [];\n      for (let j = -rows; j <= rows; j++) for (let i = -14; i <= 14; i++) {\n        const x = GX + (i + (j & 1 ? 0.5 : 0)) * step, y = GY + j * step * 0.866;\n        if (Math.hypot(x - GX, y - GY) < GR - 3) holes.push([x, y]);\n      }\n      for (const [x, y] of holes) { disc(b, x + 0.5, y + 0.7, 4.1); b.fillStyle = \"rgba(255,206,186,0.42)\"; b.fill(); }\n      for (const [x, y] of holes) { disc(b, x - 0.3, y - 0.45, 4); b.fillStyle = \"rgba(84,16,4,0.9)\"; b.fill(); }\n      b.globalCompositeOperation = \"destination-out\";\n      for (const [x, y] of holes) { disc(b, x + 0.25, y + 0.35, 3.25); b.fill(); }\n      b.globalCompositeOperation = \"source-over\";\n\n      // Print on the dial card.\n      const d = dial.getContext(\"2d\");\n      if (!d) return;\n      dial.width = Math.ceil(FW * k); dial.height = Math.ceil(FH * k);\n      d.setTransform(k, 0, 0, k, -FX * k, -FY * k);\n      const INK = \"#231a10\";\n      d.textBaseline = \"middle\"; d.textAlign = \"center\"; d.fillStyle = INK; d.strokeStyle = INK;\n      d.font = `800 10px ui-sans-serif, system-ui, sans-serif`; d.fillText(\"FM\", FX + 18, FY + 24); d.fillText(\"AM\", FX + 18, FY + 70);\n      d.font = `600 6px ui-sans-serif, system-ui, sans-serif`; d.fillText(\"MHz\", FX + FW - 18, FY + 24); d.fillText(\"kHz\", FX + FW - 18, FY + 70);\n      d.beginPath(); d.moveTo(X0 - 8, FY + 36); d.lineTo(X1 + 8, FY + 36); d.lineWidth = 1; d.stroke();\n      d.beginPath(); d.moveTo(X0 - 8, FY + 58); d.lineTo(X1 + 8, FY + 58); d.stroke();\n      for (let f = 88; f <= 108; f++) {\n        const x = xOf(f), long = f % 4 === 0;\n        d.beginPath(); d.moveTo(x, FY + 36); d.lineTo(x, FY + 36 - (long ? 7 : 4)); d.lineWidth = long ? 1.2 : 0.8; d.stroke();\n        if (long) { d.font = `700 9.5px ui-sans-serif, system-ui, sans-serif`; d.fillText(String(f), x, FY + 20); }\n      }\n      // Medium wave is printed the way it really falls on a tuning capacitor: wide at the bottom, cramped at the top.\n      const am = (kHz: number) => X0 + (Math.log(kHz / 530) / Math.log(1600 / 530)) * (X1 - X0);\n      for (let f = 550; f <= 1600; f += 50) { const x = am(f); d.beginPath(); d.moveTo(x, FY + 58); d.lineTo(x, FY + 58 + (f % 100 === 0 ? 5.5 : 3.5)); d.lineWidth = 0.8; d.stroke(); }\n      d.font = `700 8.5px ui-sans-serif, system-ui, sans-serif`;\n      for (const f of [530, 600, 700, 800, 1000, 1200, 1400, 1600]) d.fillText(String(f / 10), am(f), FY + 74);\n      // A coloured wedge marks each station.\n      for (const st of stations) { const x = xOf(st.at); d.beginPath(); d.moveTo(x - 3.2, FY + 41); d.lineTo(x + 3.2, FY + 41); d.lineTo(x, FY + 46.5); d.closePath(); d.fillStyle = NEEDLE; d.fill(); }\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    const s = { f: 98.1, v: 0, auto: 98.1 as number | null, drag: false, lamp: 1, cone: 0, coneV: 0, light: 0, lightTo: 0, roll: 0, key: 1, hiss: 0, hissTo: 0, hissAt: 0, tuned: 1, hold: false };\n\n    const draw = () => {\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      // The loudspeaker seen through the holes: paper cone, roll surround, dust cap. It pumps with the programme.\n      const push = s.cone;\n      ctx.save(); disc(ctx, GX, GY, GR); ctx.clip();\n      ctx.fillStyle = \"#0d0907\"; ctx.fillRect(GX - GR, GY - GR, GR * 2, GR * 2);\n      const cone = ctx.createRadialGradient(GX - 26, GY - 34, 10, GX, GY, 124 + push * 3);\n      cone.addColorStop(0, \"#5a4a40\"); cone.addColorStop(0.5, \"#33281f\"); cone.addColorStop(1, \"#16100c\");\n      disc(ctx, GX, GY, 122 + push * 2.5); ctx.fillStyle = cone; ctx.fill();\n      disc(ctx, GX, GY, 128 + push * 1.5); ctx.lineWidth = 9; ctx.strokeStyle = \"#241b15\"; ctx.stroke();\n      disc(ctx, GX - 1.5, GY - 2, 126 + push * 1.5); ctx.lineWidth = 2; ctx.strokeStyle = \"rgba(190,160,140,0.3)\"; ctx.stroke();\n      const cap = ctx.createRadialGradient(GX - 12 - push * 2, GY - 16 - push * 2, 2, GX, GY, 46 + push * 7);\n      cap.addColorStop(0, \"#b39a88\"); cap.addColorStop(0.45, \"#6b5749\"); cap.addColorStop(1, \"#2a2019\");\n      disc(ctx, GX, GY, 44 + push * 7); ctx.fillStyle = cap; ctx.fill();\n      ctx.restore();\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(body, 0, 0); ctx.setTransform(k, 0, 0, k, 0, 0);\n\n      // Dial card, lit from behind by two small lamps at the ends.\n      ctx.save(); rr(ctx, FX, FY, FW, FH, 5); ctx.clip();\n      ctx.fillStyle = \"#93886a\"; ctx.fillRect(FX, FY, FW, FH);\n      ctx.globalAlpha = s.lamp;\n      const card = ctx.createLinearGradient(FX, 0, FX + FW, 0);\n      card.addColorStop(0, \"#fff7d6\"); card.addColorStop(0.2, \"#fbe3a6\"); card.addColorStop(0.5, \"#f2cd85\"); card.addColorStop(0.8, \"#fbe3a6\"); card.addColorStop(1, \"#fff7d6\");\n      ctx.fillStyle = card; ctx.fillRect(FX, FY, FW, FH);\n      ctx.globalAlpha = 1;\n      ctx.drawImage(dial, FX, FY, FW, FH);\n      // Needle on its carriage, and its shadow on the card.\n      const nx = xOf(s.f);\n      ctx.fillStyle = `rgba(70,36,4,${0.22 + 0.16 * s.lamp})`; ctx.fillRect(nx + 3 + s.light * 2.4, FY, 2.6, FH);\n      const ng = ctx.createLinearGradient(nx - 1.3, 0, nx + 1.3, 0); ng.addColorStop(0, \"#ff6a55\"); ng.addColorStop(0.5, NEEDLE); ng.addColorStop(1, \"#8e1409\");\n      ctx.fillStyle = ng; ctx.fillRect(nx - 1.3, FY, 2.6, FH);\n      ctx.fillStyle = \"#1a1512\"; ctx.fillRect(nx - 5, FY + FH - 7, 10, 7); ctx.fillStyle = \"rgba(255,255,255,0.25)\"; ctx.fillRect(nx - 5, FY + FH - 7, 10, 0.8);\n      // Glass.\n      const top = ctx.createLinearGradient(0, FY, 0, FY + 22); top.addColorStop(0, \"rgba(20,10,0,0.55)\"); top.addColorStop(1, \"rgba(20,10,0,0)\");\n      ctx.fillStyle = top; ctx.fillRect(FX, FY, FW, 22);\n      const side = ctx.createLinearGradient(FX, 0, FX + FW, 0); side.addColorStop(0, \"rgba(20,10,0,0.3)\"); side.addColorStop(0.06, \"rgba(20,10,0,0)\"); side.addColorStop(0.94, \"rgba(20,10,0,0)\"); side.addColorStop(1, \"rgba(20,10,0,0.3)\");\n      ctx.fillStyle = side; ctx.fillRect(FX, FY, FW, FH);\n      const sx = FX + FW * (0.3 + 0.22 * s.light), sh = ctx.createLinearGradient(0, FY, 0, FY + FH);\n      sh.addColorStop(0, \"rgba(255,255,255,0.3)\"); sh.addColorStop(0.7, \"rgba(255,255,255,0.06)\"); sh.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = sh;\n      ctx.beginPath(); ctx.moveTo(sx, FY); ctx.lineTo(sx + 54, FY); ctx.lineTo(sx + 8, FY + FH); ctx.lineTo(sx - 46, FY + FH); ctx.closePath(); ctx.fill();\n      ctx.beginPath(); ctx.moveTo(sx + 66, FY); ctx.lineTo(sx + 78, FY); ctx.lineTo(sx + 32, FY + FH); ctx.lineTo(sx + 20, FY + FH); ctx.closePath(); ctx.fill();\n      ctx.restore();\n\n      // Tuning wheel: ridges roll past, the top and bottom curve away into shade.\n      const wx = BX + BW - 14, wy = FY, ww = 24, wh = FH;\n      ctx.save(); ctx.shadowColor = \"rgba(40,48,38,0.45)\"; ctx.shadowBlur = 6 * k; ctx.shadowOffsetX = 4 * k; ctx.shadowOffsetY = 6 * k; rr(ctx, wx, wy, ww, wh, 4); ctx.fillStyle = \"#141414\"; ctx.fill(); ctx.restore();\n      ctx.save(); rr(ctx, wx, wy, ww, wh, 4); ctx.clip();\n      for (let i = -1; i < 26; i++) {\n        const u = ((i * 0.13 + s.roll) % 3.2 + 3.2) % 3.2 - 1.6; // angle of this ridge on the visible half of the wheel\n        if (Math.abs(u) > 1.5) continue;\n        const y = wy + wh / 2 + Math.sin(u) * (wh / 2), thick = Math.max(0.6, 3.2 * Math.cos(u)), lit = Math.max(0, Math.cos(u + 0.5));\n        ctx.fillStyle = `rgb(${(26 + lit * 70) | 0},${(26 + lit * 70) | 0},${(28 + lit * 72) | 0})`; ctx.fillRect(wx, y - thick / 2, ww, thick);\n        ctx.fillStyle = `rgba(255,255,255,${0.32 * lit})`; ctx.fillRect(wx, y - thick / 2, ww, 0.7);\n      }\n      const round = ctx.createLinearGradient(0, wy, 0, wy + wh); round.addColorStop(0, \"rgba(0,0,0,0.85)\"); round.addColorStop(0.3, \"rgba(0,0,0,0)\"); round.addColorStop(0.7, \"rgba(0,0,0,0.1)\"); round.addColorStop(1, \"rgba(0,0,0,0.9)\");\n      ctx.fillStyle = round; ctx.fillRect(wx, wy, ww, wh);\n      const across = ctx.createLinearGradient(wx, 0, wx + ww, 0); across.addColorStop(0, \"rgba(0,0,0,0.7)\"); across.addColorStop(0.3, \"rgba(0,0,0,0)\"); across.addColorStop(1, \"rgba(0,0,0,0.25)\");\n      ctx.fillStyle = across; ctx.fillRect(wx, wy, ww, wh);\n      ctx.restore();\n\n      // Tuned lamp and power key.\n      const lx = FX + 4, ly = FY + FH + 30, lit = s.tuned * s.lamp;\n      ctx.save(); ctx.shadowColor = \"#ffd27a\"; ctx.shadowBlur = 14 * k * lit;\n      const jewel = ctx.createRadialGradient(lx - 1.2, ly - 1.4, 0.3, lx, ly, 4.2);\n      jewel.addColorStop(0, `rgb(255,${(170 + 80 * lit) | 0},${(120 + 110 * lit) | 0})`); jewel.addColorStop(0.6, `rgb(${(120 + 135 * lit) | 0},${(60 + 130 * lit) | 0},${(20 + 60 * lit) | 0})`); jewel.addColorStop(1, `rgb(${(70 + 120 * lit) | 0},${(28 + 70 * lit) | 0},10)`);\n      disc(ctx, lx, ly, 4.2); ctx.fillStyle = jewel; ctx.fill(); ctx.restore();\n      const kx = FX + FW - 51 + 26 * s.key, ky = FY + FH + 20;\n      ctx.save(); ctx.shadowColor = \"rgba(40,6,0,0.7)\"; ctx.shadowBlur = 4 * k; ctx.shadowOffsetX = 1.5 * k; ctx.shadowOffsetY = 2.5 * k; rr(ctx, kx, ky, 26, 20, 10); ctx.fillStyle = chrome(ctx, kx, ky, kx + 12, ky + 22); ctx.fill(); ctx.restore();\n      rr(ctx, kx + 0.5, ky + 0.5, 25, 19, 9.5); ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(255,255,255,0.85)\"; ctx.stroke();\n      for (const o of [-3, 0, 3]) { ctx.fillStyle = \"rgba(0,0,0,0.3)\"; ctx.fillRect(kx + 12.6 + o, ky + 5, 0.9, 10); }\n    };\n\n    const say = () => { const r = Math.round(s.f * 10) / 10; if (r !== live.current.freq) { live.current.freq = r; setFreq(r); } };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 2250;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.04, (now - last) / 1000); last = now;\n      const t = (now - t0) / 1000, pw = live.current.on, before = s.f;\n      if (!s.drag) {\n        if (s.auto !== null) {\n          // Tuning by key or by the demo: a firm spring with a touch of overshoot.\n          s.v += (34 * (s.auto - s.f) - 8.4 * s.v) * dt; s.f += s.v * dt;\n          if (Math.abs(s.auto - s.f) < 0.004 && Math.abs(s.v) < 0.02) { s.f = s.auto; s.v = 0; s.auto = null; }\n        } else {\n          // After a flick the wheel coasts, then a station pulls the needle in when it passes close and slowly.\n          const st = nearest(s.f), off = st.at - s.f;\n          if (!s.hold && Math.abs(off) < 0.42 && Math.abs(s.v) < 2.4) s.v += (40 * off - 7 * s.v) * dt;\n          s.v *= Math.exp(-2.6 * dt); s.f += s.v * dt;\n        }\n        if (s.f < LOW) { s.f = LOW; s.v = Math.abs(s.v) * 0.3; } else if (s.f > HIGH) { s.f = HIGH; s.v = -Math.abs(s.v) * 0.3; }\n      }\n      s.roll -= (s.f - before) * 0.42;\n      if (now - touched > 5000) {\n        s.lightTo = Math.sin(t * 0.55) * 0.8;\n        if (now - t0 > nextDemo && !s.drag && s.auto === null) { const i = stations.indexOf(nearest(s.f)); s.auto = stations[(i + 1) % stations.length].at; nextDemo = now - t0 + 3400; }\n      }\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      s.lamp += ((pw ? 1 : 0) - s.lamp) * Math.min(1, dt * (pw ? 6 : 10));\n      s.key += ((pw ? 1 : 0) - s.key) * Math.min(1, dt * 16);\n      // What would be coming out of the speaker: the station's rhythm when locked, hiss when not.\n      const q = lock(s.f), st = nearest(s.f), beat = (t * st.tempo) / 60, ph = beat % 1;\n      if (t > s.hissAt) { s.hissAt = t + 0.03; s.hissTo = Math.random() * 2 - 1; }\n      s.hiss += (s.hissTo - s.hiss) * Math.min(1, dt * 40);\n      const music = Math.exp(-ph * 6) * (Math.floor(beat) % 2 ? 0.6 : 1) + 0.25 * Math.sin(t * 9 + st.at);\n      const drive = pw ? q * music + (1 - q) * s.hiss * 0.35 : 0;\n      s.coneV += (700 * (drive - s.cone) - 30 * s.coneV) * dt; s.cone += s.coneV * dt;\n      s.tuned += (q * (0.85 + 0.15 * Math.sin(t * 31) * (1 - q)) - s.tuned) * Math.min(1, dt * 14);\n      draw(); say();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => { if (s.auto !== null) { s.f = s.auto; s.auto = null; } s.v = 0; s.lamp = live.current.on ? 1 : 0; s.key = s.lamp; s.cone = 0; s.tuned = lock(s.f); s.roll = -s.f * 0.42; draw(); say(); };\n    const wake = () => { if (reduce) still(); else if (!raf) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    api.current = {\n      tune: (f) => { touched = performance.now(); s.hold = true; s.auto = Math.max(LOW, Math.min(HIGH, f)); wake(); },\n      // Key presses add up even while the needle is still on its way, and a station does not pull a keyed value back.\n      step: (by) => { touched = performance.now(); s.hold = true; s.auto = Math.max(LOW, Math.min(HIGH, Math.round(((s.auto ?? s.f) + by) * 10) / 10)); wake(); },\n      poke: () => { touched = performance.now(); wake(); },\n    };\n    fit(); wake();\n\n    // Drag the needle along the dial, or roll the wheel on the edge of the case.\n    let px = 0, py = 0, pt = 0, byWheel = false;\n    const down = (wheelDrag: boolean) => (e: PointerEvent) => {\n      touched = performance.now(); s.drag = true; s.hold = false; s.auto = null; s.v = 0; byWheel = wheelDrag; px = e.clientX; py = e.clientY; pt = performance.now();\n      (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); e.preventDefault(); wake();\n    };\n    const move = (e: PointerEvent) => {\n      if (!s.drag) return;\n      const b = cv.getBoundingClientRect(), now = performance.now(), unit = b.width / W;\n      const d = byWheel ? -(e.clientY - py) / unit * 0.05 : ((e.clientX - px) / unit / (X1 - X0)) * 20;\n      const dt = Math.max(0.008, (now - pt) / 1000); px = e.clientX; py = e.clientY; pt = now; touched = now;\n      s.f = Math.max(LOW, Math.min(HIGH, s.f + d)); s.v = s.v * 0.6 + (d / dt) * 0.4;\n      if (reduce) still();\n    };\n    const up = () => { if (!s.drag) return; s.drag = false; touched = performance.now(); if (performance.now() - pt > 90) s.v = 0; if (reduce) { s.auto = lock(s.f) > 0.1 ? nearest(s.f).at : null; still(); } };\n    const onDial = down(false), onWheel = down(true);\n    pad.addEventListener(\"pointerdown\", onDial); wheel.addEventListener(\"pointerdown\", onWheel);\n    for (const el of [pad, wheel]) { el.addEventListener(\"pointermove\", move); el.addEventListener(\"pointerup\", up); el.addEventListener(\"pointercancel\", up); }\n    const onLight = (e: PointerEvent) => { const b = cv.getBoundingClientRect(); if (!s.drag) touched = Math.max(touched, performance.now() - 3500); s.lightTo = Math.max(-1, Math.min(1, ((e.clientX - b.left) / b.width) * 2 - 1)); };\n    const host = cv.parentElement;\n    host?.addEventListener(\"pointermove\", onLight);\n    const ro = new ResizeObserver(() => { fit(); if (reduce) still(); });\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\", onDial); wheel.removeEventListener(\"pointerdown\", onWheel);\n      for (const el of [pad, wheel]) { el.removeEventListener(\"pointermove\", move); el.removeEventListener(\"pointerup\", up); el.removeEventListener(\"pointercancel\", up); }\n      host?.removeEventListener(\"pointermove\", onLight);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const here = Math.abs(nearest(freq).at - freq) < 0.16 ? nearest(freq) : null;\n  const onKey = (e: ReactKeyboardEvent) => {\n    const i = stations.indexOf(nearest(freq));\n    const seek = (dir: number) => { const list = dir > 0 ? stations.filter((x) => x.at > freq + 0.05) : stations.filter((x) => x.at < freq - 0.05); return dir > 0 ? (list[0] ?? stations[i]).at : (list[list.length - 1] ?? stations[i]).at; };\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\" || e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") { e.preventDefault(); api.current?.step(e.key === \"ArrowRight\" || e.key === \"ArrowUp\" ? 0.1 : -0.1); return; }\n    const next = e.key === \"PageUp\" ? seek(1) : e.key === \"PageDown\" ? seek(-1) : e.key === \"Home\" ? LOW : e.key === \"End\" ? HIGH : null;\n    if (next === null) return;\n    e.preventDefault(); api.current?.tune(Math.round(next * 10) / 10);\n  };\n  const power = () => { live.current.on = !live.current.on; setOn(live.current.on); api.current?.poke(); };\n  const pct = (v: number, of: number) => `${(v / of) * 100}%`;\n  const ring = \"outline-none focus-visible:ring-2 focus-visible:ring-[#fff3d6] focus-visible:ring-offset-2 focus-visible:ring-offset-[#d9462b]\";\n\n  return (\n    <div className=\"flex min-h-[790px] w-full flex-col items-center justify-center bg-[#cdd6cb] bg-[radial-gradient(75%_65%_at_50%_42%,#dfe6dc_0%,#cdd6cb_58%,#bcc7ba_100%)] px-4 py-8\">\n      <div className=\"relative w-full max-w-[470px]\" style={{ aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <div\n          ref={dialHit}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label=\"Tuning\"\n          aria-valuemin={LOW}\n          aria-valuemax={HIGH}\n          aria-valuenow={freq}\n          aria-valuetext={`${freq.toFixed(1)} megahertz, ${here ? here.name : \"between stations\"}`}\n          onKeyDown={onKey}\n          style={{ left: pct(FX - 6, W), top: pct(FY - 6, H), width: pct(FW + 12, W), height: pct(FH + 12, H) }}\n          className={`absolute cursor-ew-resize touch-none rounded-lg ${ring}`}\n        />\n        <div\n          ref={wheelHit}\n          aria-hidden=\"true\"\n          style={{ left: pct(BX + BW - 26, W), top: pct(FY - 14, H), width: pct(60, W), height: pct(FH + 28, H) }}\n          className=\"absolute cursor-ns-resize touch-none\"\n        />\n        <button\n          type=\"button\"\n          aria-label=\"Power\"\n          aria-pressed={on}\n          onClick={power}\n          style={{ left: pct(FX + FW - 64, W), top: pct(FY + FH + 4, H), width: pct(78, W), height: pct(54, H) }}\n          className={`absolute cursor-pointer rounded-full ${ring}`}\n        />\n      </div>\n      <p className=\"mt-1 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-emerald-950/70\">\n        <span className=\"tabular-nums text-emerald-950\">{freq.toFixed(1)} FM</span>\n        <span className=\"mx-2 text-emerald-950/40\">/</span>\n        <span aria-live=\"polite\">{!on ? \"Switched off\" : here ? here.name : \"Static\"}</span>\n      </p>\n    </div>\n  );\n}\n"}]}
