{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"turntable-with-a-tonearm-you-lift-and-drop-onto-a-spinning-record","type":"registry:component","title":"Turntable with a Tonearm You Lift and Drop onto a Spinning Record","description":"Record player seen from above on a canvas: drag the headshell to lift the tonearm, drop it onto a track and the platter spins up. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/turntable-with-a-tonearm-you-lift-and-drop-onto-a-spinning-record.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// The record on the platter. Change the names and the bands on the vinyl follow.\nconst record = { artist: \"Marlowe Vane Quartet\", side: \"A\", label: \"#e2512b\", ink: \"#fbeedd\" };\nconst tracks = [\"Low Tide Motel\", \"Glass Harbour\", \"Nine Lamps\", \"Paper Weather\", \"Slow Ferry Home\"];\nconst SIDE_SECONDS = 150; // how long the needle takes to cross the whole side\nconst ACCENT = \"#e2512b\"; // lamps, cartridge and focus ring\n\n// Geometry in design pixels. The plinth is 660 by 500.\nconst W = 660, H = 500, CX = 252, CY = 250, RIM = 208, REC = 192, LBL = 61, OUTER = 185, INNER = 76;\nconst TX = 566, TY = 104; // tonearm pivot\nconst TUBE = 288, SHELL = 0.36, TIP = 50; // tube length, headshell angle, pivot of the shell to the stylus\nconst SX = TUBE + TIP * Math.cos(SHELL), SY = TIP * Math.sin(SHELL);\nconst LA = Math.hypot(SX, SY), SKEW = Math.atan2(SY, SX);\nconst D = Math.hypot(CX - TX, CY - TY), BETA = Math.atan2(CY - TY, CX - TX);\n// phi is the angle at the pivot between the stylus and the spindle: small near the label, large at the rest.\nconst phiOf = (r: number) => Math.acos((LA * LA + D * D - r * r) / (2 * LA * D));\nconst rOf = (phi: number) => Math.sqrt(LA * LA + D * D - 2 * LA * D * Math.cos(phi));\nconst REST = (59 * Math.PI) / 180, PHI_OUT = phiOf(OUTER), PHI_IN = phiOf(INNER);\nconst BAND = (OUTER - INNER) / tracks.length;\nconst trackAt = (phi: number) => (phi > PHI_OUT + 0.01 ? 0 : Math.max(1, Math.min(tracks.length, Math.floor((OUTER - rOf(phi)) / BAND) + 1)));\nconst cuePhi = (track: number) => phiOf(OUTER - (track - 1) * BAND - 1.2);\n\nexport default function TurntableWithTonearm() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const armHit = useRef<HTMLDivElement>(null);\n  const [status, setStatus] = useState({ track: 1, state: \"Playing\" });\n  const [fast, setFast] = useState(false);\n  const live = useRef({ track: 1, state: \"Playing\", fast: false });\n  const api = useRef<{ cue: (track: number) => void; toggle: () => void; poke: () => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = armHit.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;\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    // Spun aluminium: bright and dark wedges round a centre.\n    const spun = (c: CanvasRenderingContext2D, x: number, y: number, r: number, turn: number, lo: number, hi: number) => {\n      if (!hasConic) { const g = c.createLinearGradient(x - r, y - r, x + r, y + r); g.addColorStop(0, `rgb(${hi},${hi},${hi})`); g.addColorStop(0.5, `rgb(${lo},${lo},${lo})`); g.addColorStop(1, `rgb(${hi},${hi},${hi})`); return g; }\n      const g = c.createConicGradient(turn, x, y), mid = (lo + hi) / 2;\n      const stops: [number, number][] = [[0, hi], [0.07, mid + 16], [0.17, lo], [0.27, mid - 8], [0.38, mid + 22], [0.5, hi - 8], [0.58, mid + 6], [0.68, lo + 4], [0.8, mid - 4], [0.91, mid + 30], [1, hi]];\n      for (const [o, v] of stops) g.addColorStop(o, `rgb(${(v - 3) | 0},${v | 0},${Math.min(255, v + 5) | 0})`);\n      return g;\n    };\n    const tubeFill = (c: CanvasRenderingContext2D, half: number, dark = false) => {\n      const g = c.createLinearGradient(0, -half, 0, half);\n      if (dark) { g.addColorStop(0, \"#3d3f44\"); g.addColorStop(0.28, \"#8b8e94\"); g.addColorStop(0.5, \"#33353a\"); g.addColorStop(1, \"#111214\"); }\n      else { g.addColorStop(0, \"#9da0a6\"); g.addColorStop(0.26, \"#ffffff\"); g.addColorStop(0.55, \"#b9bcc1\"); g.addColorStop(1, \"#5d6066\"); }\n      return g;\n    };\n\n    const base = document.createElement(\"canvas\");\n    const label = document.createElement(\"canvas\");\n\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      // Powder-coated plinth with a faint orange-peel grain.\n      const coat = b.createLinearGradient(0, 0, W * 0.4, H);\n      coat.addColorStop(0, \"#f7f6f2\"); coat.addColorStop(0.6, \"#ecebe6\"); coat.addColorStop(1, \"#e0dfd9\");\n      rr(b, 0, 0, W, H, 20); b.fillStyle = coat; b.fill();\n      b.save(); rr(b, 0, 0, W, H, 20); b.clip();\n      let seed = 5;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let i = 0; i < 9000; i++) { const v = rand(); b.fillStyle = v > 0.5 ? \"rgba(255,255,255,0.35)\" : \"rgba(60,58,50,0.07)\"; b.fillRect(rand() * W, rand() * H, 0.9, 0.9); }\n      b.restore();\n      const edge = b.createLinearGradient(0, 0, 0, H);\n      edge.addColorStop(0, \"rgba(255,255,255,1)\"); edge.addColorStop(0.5, \"rgba(255,255,255,0.2)\"); edge.addColorStop(1, \"rgba(90,86,76,0.5)\");\n      rr(b, 0.75, 0.75, W - 1.5, H - 1.5, 19.3); b.lineWidth = 1.5; b.strokeStyle = edge; b.stroke();\n\n      // Printed legends.\n      b.font = \"600 7.5px ui-monospace, SFMono-Regular, Menlo, monospace\"; b.textAlign = \"center\"; b.textBaseline = \"middle\"; b.fillStyle = \"rgba(60,58,52,0.72)\";\n      b.fillText(\"START\", 48, 417); b.fillText(\"STOP\", 48, 487); b.fillText(\"33\", 36, 84); b.fillText(\"45\", 62, 84);\n      b.textAlign = \"right\"; b.fillText(\"BELT DRIVE  /  TWO SPEED\", W - 22, H - 20);\n\n      // Platter well, platter and the record lying on it.\n      b.save(); b.shadowColor = \"rgba(40,36,28,0.5)\"; b.shadowBlur = 26 * k; b.shadowOffsetX = 7 * k; b.shadowOffsetY = 13 * k; disc(b, CX, CY, RIM); b.fillStyle = \"#777\"; b.fill(); b.restore();\n      b.save(); b.shadowColor = \"rgba(20,18,14,0.75)\"; b.shadowBlur = 5 * k; b.shadowOffsetX = 1 * k; b.shadowOffsetY = 2.5 * k; disc(b, CX, CY, RIM); b.fillStyle = \"#777\"; b.fill(); b.restore();\n      disc(b, CX, CY, RIM); b.fillStyle = spun(b, CX, CY, RIM, -2.2, 96, 250); b.fill();\n      b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.75)\"; disc(b, CX, CY, RIM - 0.8); b.stroke();\n      b.strokeStyle = \"rgba(0,0,0,0.3)\"; disc(b, CX, CY, RIM - 3); b.stroke();\n      disc(b, CX, CY, REC + 5.5); b.lineWidth = 3; b.strokeStyle = \"rgba(0,0,0,0.16)\"; b.stroke();\n      // Rubber mat showing as a thin line, then the vinyl.\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.8)\"; b.shadowBlur = 5 * k; b.shadowOffsetX = 1.5 * k; b.shadowOffsetY = 2.5 * k; disc(b, CX, CY, REC); b.fillStyle = \"#0b0b0c\"; b.fill(); b.restore();\n      for (let r = LBL + 3; r < REC - 4.5; r += 0.62) {\n        const v = rand(); disc(b, CX, CY, r); b.lineWidth = 0.45;\n        b.strokeStyle = v > 0.45 ? `rgba(255,255,255,${(v - 0.45) * 0.13})` : `rgba(0,0,0,${(0.45 - v) * 0.9})`; b.stroke();\n      }\n      // Gaps between tracks, the lead-in and the run-out are smooth vinyl.\n      const smooth = (r: number, w: number) => { disc(b, CX, CY, r); b.lineWidth = w; b.strokeStyle = \"#08080a\"; b.stroke(); disc(b, CX, CY, r + w / 2); b.lineWidth = 0.4; b.strokeStyle = \"rgba(255,255,255,0.16)\"; b.stroke(); };\n      for (let i = 1; i < tracks.length; i++) smooth(OUTER - i * BAND, 2.2);\n      smooth(REC - 3, 5.5); smooth((LBL + INNER) / 2 - 0.5, INNER - LBL - 3);\n      disc(b, CX, CY, (LBL + INNER) / 2 + 2); b.lineWidth = 0.5; b.strokeStyle = \"rgba(255,255,255,0.12)\"; b.stroke();\n      disc(b, CX, CY, REC - 0.6); b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.22)\"; b.stroke();\n\n      // Tonearm base, armrest and the small anti-skate dial.\n      b.save(); b.shadowColor = \"rgba(40,36,28,0.5)\"; b.shadowBlur = 14 * k; b.shadowOffsetX = 4 * k; b.shadowOffsetY = 8 * k; disc(b, TX, TY, 39); b.fillStyle = \"#18191b\"; b.fill(); b.restore();\n      const baseG = b.createLinearGradient(TX - 39, TY - 39, TX + 39, TY + 39);\n      baseG.addColorStop(0, \"#4a4c51\"); baseG.addColorStop(0.5, \"#1b1c1f\"); baseG.addColorStop(1, \"#0c0c0e\");\n      disc(b, TX, TY, 39); b.fillStyle = baseG; b.fill();\n      disc(b, TX, TY, 38.2); b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.3)\"; b.stroke();\n      disc(b, TX, TY, 29); b.fillStyle = spun(b, TX, TY, 29, -2.2, 80, 235); b.fill(); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.5)\"; b.stroke();\n      disc(b, TX, TY, 23); b.fillStyle = \"#101113\"; b.fill();\n      const rest = BETA - REST - SKEW, ax = TX + Math.cos(rest) * 214, ay = TY + Math.sin(rest) * 214;\n      b.save(); b.translate(ax, ay); b.rotate(rest);\n      b.shadowColor = \"rgba(40,36,28,0.5)\"; b.shadowBlur = 8 * k; b.shadowOffsetX = 3 * k; b.shadowOffsetY = 5 * k;\n      rr(b, -9, -15, 18, 30, 5); b.fillStyle = \"#1a1b1e\"; b.fill(); b.shadowColor = \"transparent\";\n      rr(b, -9, -15, 18, 30, 5); b.lineWidth = 0.8; b.strokeStyle = \"rgba(255,255,255,0.28)\"; b.stroke();\n      rr(b, -9.5, -5.5, 19, 11, 2); b.fillStyle = \"#050506\"; b.fill();\n      b.restore();\n      const dx = TX + 56, dy = TY + 62;\n      b.save(); b.shadowColor = \"rgba(40,36,28,0.5)\"; b.shadowBlur = 7 * k; b.shadowOffsetX = 2 * k; b.shadowOffsetY = 4 * k; disc(b, dx, dy, 13); b.fillStyle = \"#1a1b1e\"; b.fill(); b.restore();\n      for (let i = 0; i < 36; i++) { const a = (i / 36) * Math.PI * 2; b.beginPath(); b.moveTo(dx + Math.cos(a) * 10, dy + Math.sin(a) * 10); b.lineTo(dx + Math.cos(a) * 13, dy + Math.sin(a) * 13); b.lineWidth = 0.8; b.strokeStyle = `rgba(255,255,255,${0.1 + 0.3 * Math.max(0, Math.cos(a + 2.2))})`; b.stroke(); }\n      disc(b, dx, dy, 9.6); b.fillStyle = spun(b, dx, dy, 9.6, -2.2, 90, 240); b.fill();\n      b.beginPath(); b.moveTo(dx, dy - 2); b.lineTo(dx, dy - 8.5); b.lineWidth = 1.4; b.strokeStyle = \"#141416\"; b.stroke();\n\n      // Start button and speed key: bodies only, their lamps are drawn live.\n      b.save(); b.shadowColor = \"rgba(40,36,28,0.55)\"; b.shadowBlur = 8 * k; b.shadowOffsetX = 2 * k; b.shadowOffsetY = 4 * k; disc(b, 48, 452, 22); b.fillStyle = \"#888\"; b.fill(); b.restore();\n      disc(b, 48, 452, 22); b.fillStyle = spun(b, 48, 452, 22, -2.2, 110, 252); b.fill(); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.35)\"; b.stroke();\n      disc(b, 48, 452, 16.5); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.3)\"; b.stroke(); disc(b, 48, 452, 17.4); b.strokeStyle = \"rgba(255,255,255,0.7)\"; b.stroke();\n      b.save(); b.shadowColor = \"rgba(40,36,28,0.55)\"; b.shadowBlur = 6 * k; b.shadowOffsetX = 2 * k; b.shadowOffsetY = 3 * k; rr(b, 24, 38, 50, 24, 12); b.fillStyle = \"#888\"; b.fill(); b.restore();\n      const key = b.createLinearGradient(0, 38, 0, 62); key.addColorStop(0, \"#fbfbfc\"); key.addColorStop(0.5, \"#c3c6cb\"); key.addColorStop(1, \"#8d9096\");\n      rr(b, 24, 38, 50, 24, 12); b.fillStyle = key; b.fill(); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.35)\"; b.stroke();\n      b.beginPath(); b.moveTo(49, 42); b.lineTo(49, 58); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.3)\"; b.stroke();\n\n      // The paper label, drawn once and spun every frame.\n      const l = label.getContext(\"2d\");\n      if (!l) return;\n      const S = LBL * 2;\n      label.width = Math.ceil(S * k); label.height = Math.ceil(S * k);\n      l.setTransform(k, 0, 0, k, 0, 0);\n      const paper = l.createRadialGradient(LBL - 12, LBL - 16, 4, LBL, LBL, LBL);\n      paper.addColorStop(0, \"#f06a40\"); paper.addColorStop(1, record.label);\n      disc(l, LBL, LBL, LBL); l.fillStyle = paper; l.fill();\n      for (let i = 0; i < 900; i++) { l.fillStyle = rand() > 0.5 ? \"rgba(255,255,255,0.05)\" : \"rgba(0,0,0,0.05)\"; l.fillRect(rand() * S, rand() * S, 1, 1); }\n      disc(l, LBL, LBL, LBL - 5); l.lineWidth = 0.7; l.strokeStyle = record.ink; l.stroke();\n      disc(l, LBL, LBL, 27); l.lineWidth = 0.7; l.stroke();\n      const arc = (text: string, radius: number, mid: number, size: number, flip: boolean) => {\n        l.font = `700 ${size}px ui-sans-serif, system-ui, \"Helvetica Neue\", Arial, sans-serif`; l.textAlign = \"center\"; l.textBaseline = \"middle\"; l.fillStyle = record.ink;\n        const stepA = (size * 0.78) / radius, start = mid - ((text.length - 1) * stepA * (flip ? -1 : 1)) / 2;\n        [...text].forEach((ch, i) => { const a = start + i * stepA * (flip ? -1 : 1); l.save(); l.translate(LBL + Math.cos(a) * radius, LBL + Math.sin(a) * radius); l.rotate(a + (flip ? -Math.PI / 2 : Math.PI / 2)); l.fillText(ch, 0, 0); l.restore(); });\n      };\n      arc(record.artist.toUpperCase(), 43, -Math.PI / 2, 6.8, false);\n      arc(`SIDE ${record.side} · STEREO · ${tracks.length} TITLES`, 44, Math.PI / 2, 5.6, true);\n      l.font = `800 17px ui-sans-serif, system-ui, sans-serif`; l.fillStyle = record.ink; l.textAlign = \"center\"; l.textBaseline = \"middle\";\n      l.fillText(record.side, LBL - 16, LBL + 1); l.font = `700 6px ui-sans-serif, system-ui, sans-serif`; l.fillText(\"33\", LBL + 16, LBL + 1);\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 first = phiOf(OUTER - BAND * 0.45);\n    const s = { rot: 0, om: 3.49, phi: first, pv: 0, target: first, h: 0, drag: false, raw: first, light: 0, lightTo: 0, lampOn: 1, lampFast: 0, press: 0 };\n\n    const arm = () => {\n      const wob = s.h < 0.01 && s.target !== REST ? 0.0022 * Math.sin(s.rot) : 0;\n      const a = BETA - (s.phi + wob) - SKEW, lift = s.h;\n      const shadow = (blur: number, far: number, alpha: number) => { ctx.shadowColor = `rgba(30,26,20,${alpha})`; ctx.shadowBlur = (blur + lift * 12) * k; ctx.shadowOffsetX = (far * 0.55 + lift * 9) * k; ctx.shadowOffsetY = (far + lift * 16) * k; };\n      ctx.save(); ctx.translate(TX, TY); ctx.rotate(a); ctx.scale(1 + lift * 0.022, 1 + lift * 0.022);\n      // Counterweight and its stub.\n      ctx.save(); shadow(9, 9, 0.42);\n      ctx.fillStyle = tubeFill(ctx, 5); ctx.fillRect(-46, -5, 40, 10);\n      rr(ctx, -92, -19, 48, 38, 5); ctx.fillStyle = tubeFill(ctx, 19, true); ctx.fill(); ctx.restore();\n      for (let x = -89; x < -78; x += 2.2) { ctx.beginPath(); ctx.moveTo(x, -18); ctx.lineTo(x, 18); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(0,0,0,0.5)\"; ctx.stroke(); }\n      ctx.fillStyle = \"rgba(255,255,255,0.75)\"; ctx.fillRect(-52, -19, 1.2, 38);\n      // The tube.\n      ctx.save(); shadow(6, 8, 0.45); ctx.fillStyle = tubeFill(ctx, 3.8); ctx.fillRect(0, -3.8, TUBE - 6, 7.6); ctx.restore();\n      // Locking collar and headshell.\n      ctx.save(); ctx.translate(TUBE, 0);\n      ctx.save(); shadow(5, 7, 0.45); rr(ctx, -16, -6, 15, 12, 2); ctx.fillStyle = tubeFill(ctx, 6); ctx.fill(); ctx.restore();\n      for (let x = -14; x < -3; x += 2) { ctx.beginPath(); ctx.moveTo(x, -6); ctx.lineTo(x, 6); ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(0,0,0,0.35)\"; ctx.stroke(); }\n      ctx.rotate(SHELL);\n      ctx.save(); shadow(6, 7, 0.5);\n      // Finger lift first, so the shell sits over its root.\n      ctx.beginPath(); ctx.moveTo(34, -9); ctx.lineTo(41, -9); ctx.lineTo(54, -33); ctx.quadraticCurveTo(52, -38, 47, -35); ctx.closePath();\n      const liftG = ctx.createLinearGradient(34, -9, 54, -33); liftG.addColorStop(0, \"#8e9197\"); liftG.addColorStop(0.5, \"#fdfdfd\"); liftG.addColorStop(1, \"#aeb1b6\"); ctx.fillStyle = liftG; ctx.fill();\n      // Cartridge body poking out in front of the shell.\n      rr(ctx, 30, -9, 34, 18, 2.5); ctx.fillStyle = ACCENT; ctx.fill();\n      ctx.beginPath(); ctx.moveTo(-2, -5); ctx.lineTo(12, -12); ctx.lineTo(56, -12); ctx.quadraticCurveTo(60, -12, 60, -8); ctx.lineTo(60, 8); ctx.quadraticCurveTo(60, 12, 56, 12); ctx.lineTo(12, 12); ctx.lineTo(-2, 5); ctx.closePath();\n      const shell = ctx.createLinearGradient(0, -12, 0, 12); shell.addColorStop(0, \"#f6f7f8\"); shell.addColorStop(0.3, \"#c9ccd1\"); shell.addColorStop(0.7, \"#a3a6ac\"); shell.addColorStop(1, \"#6f7278\"); ctx.fillStyle = shell; ctx.fill();\n      ctx.restore();\n      ctx.beginPath(); ctx.moveTo(12, -11.2); ctx.lineTo(56, -11.2); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(255,255,255,0.9)\"; ctx.stroke();\n      ctx.beginPath(); ctx.moveTo(12, 11.4); ctx.lineTo(56, 11.4); ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(0,0,0,0.4)\"; ctx.stroke();\n      ctx.fillStyle = \"#17181a\"; rr(ctx, 20, -7, 22, 2.8, 1.4); ctx.fill(); rr(ctx, 20, 4.2, 22, 2.8, 1.4); ctx.fill();\n      for (const y of [-5.6, 5.6]) { disc(ctx, 31, y, 2.3); ctx.fillStyle = \"#2a2b2e\"; ctx.fill(); disc(ctx, 30.5, y - 0.5, 0.9); ctx.fillStyle = \"rgba(255,255,255,0.6)\"; ctx.fill(); }\n      ctx.fillStyle = \"rgba(255,255,255,0.3)\"; ctx.fillRect(60, -8, 3.2, 16);\n      ctx.restore();\n      // Pivot housing on top of everything.\n      ctx.save(); shadow(6, 6, 0.5); disc(ctx, 0, 0, 16.5); ctx.fillStyle = \"#222\"; ctx.fill(); ctx.restore();\n      ctx.restore();\n      disc(ctx, TX, TY, 16.5); ctx.fillStyle = spun(ctx, TX, TY, 16.5, -2.2 + s.light * 0.4, 96, 252); ctx.fill(); ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(0,0,0,0.45)\"; ctx.stroke();\n      disc(ctx, TX, TY, 6); ctx.fillStyle = \"#17181a\"; ctx.fill(); disc(ctx, TX - 1.2, TY - 1.4, 1.6); ctx.fillStyle = \"rgba(255,255,255,0.45)\"; ctx.fill();\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      // Strobe marks on the rim of the platter.\n      ctx.fillStyle = \"rgba(20,20,22,0.78)\";\n      for (let i = 0; i < 90; i++) { const a = s.rot + (i / 90) * Math.PI * 2; ctx.save(); ctx.translate(CX + Math.cos(a) * (RIM - 7.5), CY + Math.sin(a) * (RIM - 7.5)); ctx.rotate(a); ctx.fillRect(-2.6, -2.1, 5.2, 4.2); ctx.restore(); }\n      // Light on the grooves. It stays where the lamp is; a slight warp in the pressing makes it breathe.\n      const la = -2.25 + s.light * 0.35 + 0.035 * Math.sin(s.rot);\n      ctx.save(); ctx.beginPath(); ctx.arc(CX, CY, REC - 1, 0, Math.PI * 2); ctx.arc(CX, CY, LBL, 0, Math.PI * 2, true); ctx.clip(\"evenodd\");\n      if (hasConic) {\n        const g = ctx.createConicGradient(la, CX, CY);\n        const st: [number, string][] = [[0, \"rgba(255,246,232,0.5)\"], [0.022, \"rgba(255,246,232,0.2)\"], [0.07, \"rgba(255,255,255,0.05)\"], [0.16, \"rgba(255,255,255,0)\"], [0.36, \"rgba(255,255,255,0)\"], [0.45, \"rgba(214,228,255,0.07)\"], [0.485, \"rgba(214,228,255,0.24)\"], [0.5, \"rgba(230,238,255,0.4)\"], [0.515, \"rgba(214,228,255,0.22)\"], [0.56, \"rgba(255,255,255,0.05)\"], [0.66, \"rgba(255,255,255,0)\"], [0.84, \"rgba(255,255,255,0)\"], [0.93, \"rgba(255,255,255,0.06)\"], [0.978, \"rgba(255,246,232,0.2)\"], [1, \"rgba(255,246,232,0.5)\"]];\n        for (const [o, c] of st) g.addColorStop(o, c);\n        ctx.fillStyle = g; ctx.fillRect(CX - REC, CY - REC, REC * 2, REC * 2);\n      }\n      ctx.restore();\n      // Label, printed a hair off centre like every pressing.\n      ctx.save(); ctx.translate(CX, CY); ctx.rotate(s.rot); ctx.drawImage(label, -LBL + 0.7, -LBL, LBL * 2, LBL * 2); ctx.restore();\n      const dome = ctx.createLinearGradient(CX - LBL, CY - LBL, CX + LBL, CY + LBL);\n      dome.addColorStop(0, \"rgba(255,255,255,0.16)\"); dome.addColorStop(0.5, \"rgba(255,255,255,0)\"); dome.addColorStop(1, \"rgba(0,0,0,0.2)\");\n      disc(ctx, CX, CY, LBL); ctx.fillStyle = dome; ctx.fill();\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.6)\"; ctx.shadowBlur = 4 * k; ctx.shadowOffsetX = 2 * k; ctx.shadowOffsetY = 3 * k; disc(ctx, CX, CY, 5); ctx.fillStyle = \"#999\"; ctx.fill(); ctx.restore();\n      const pin = ctx.createRadialGradient(CX - 1.6, CY - 1.8, 0.4, CX, CY, 5); pin.addColorStop(0, \"#fff\"); pin.addColorStop(0.5, \"#b8bbc0\"); pin.addColorStop(1, \"#55585d\");\n      disc(ctx, CX, CY, 5); ctx.fillStyle = pin; ctx.fill();\n\n      // Lamps: one in the start button, one for each speed.\n      const lamp = (x: number, y: number, r: number, on: number) => {\n        disc(ctx, x, y, r + 1.2); ctx.fillStyle = \"rgba(20,18,16,0.85)\"; ctx.fill();\n        ctx.save(); ctx.shadowColor = ACCENT; ctx.shadowBlur = 10 * k * on; ctx.globalAlpha = 0.22 + 0.78 * on; disc(ctx, x, y, r); ctx.fillStyle = on > 0.5 ? \"#ff8a4a\" : ACCENT; ctx.fill(); ctx.restore();\n        disc(ctx, x - r * 0.3, y - r * 0.35, r * 0.3); ctx.fillStyle = `rgba(255,255,255,${0.2 + 0.6 * on})`; ctx.fill();\n      };\n      lamp(48, 452 + s.press, 3.4, s.lampOn); lamp(36, 72, 2.4, 1 - s.lampFast); lamp(62, 72, 2.4, s.lampFast);\n      arm();\n      // Keep the invisible handle over the headshell.\n      const ha = BETA - s.phi, hx = TX + Math.cos(ha) * (LA - 18), hy = TY + Math.sin(ha) * (LA - 18);\n      pad.style.left = `${((hx - 48) / W) * 100}%`; pad.style.top = `${((hy - 48) / H) * 100}%`;\n    };\n\n    const report = () => {\n      const track = s.drag ? trackAt(s.phi) : s.target === REST ? 0 : trackAt(s.target);\n      const state = s.drag ? \"Arm lifted\" : s.h > 0.04 ? (s.target === REST ? \"Returning\" : \"Cueing\") : track === 0 ? \"Stopped\" : \"Playing\";\n      if (track !== live.current.track || state !== live.current.state) { live.current.track = track; live.current.state = state; setStatus({ track, state }); }\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 2300;\n    const RATE = (PHI_OUT - PHI_IN) / SIDE_SECONDS;\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, motor = s.target !== REST || s.drag;\n      const want = motor ? (live.current.fast ? 4.712 : 3.491) : 0;\n      s.om += (want - s.om) * Math.min(1, dt * (motor ? 1.7 : 1.1)); s.rot = (s.rot + s.om * dt) % (Math.PI * 2);\n      let up = false;\n      if (s.drag) { up = true; s.phi += (s.raw - s.phi) * Math.min(1, dt * 16); s.pv = 0; }\n      else if (Math.abs(s.target - s.phi) > 0.003 || Math.abs(s.pv) > 0.03) {\n        up = true;\n        if (s.h > 0.8) { s.pv += (46 * (s.target - s.phi) - 12.5 * s.pv) * dt; s.phi += s.pv * dt; }\n      } else { s.phi = s.target; s.pv = 0; }\n      // Up is quick, down is slowed by the cueing damper.\n      s.h = up ? s.h + (1 - s.h) * Math.min(1, dt * 9) : Math.max(0, s.h - dt * (0.35 + s.h * 2.2));\n      if (!up && s.h === 0 && s.target !== REST && s.om > 1) {\n        s.target -= RATE * dt * (s.om / 3.491); s.phi = s.target;\n        if (s.target <= PHI_IN) s.target = REST; // end of the side: the arm goes home by itself\n      }\n      if (now - touched > 6000) {\n        s.lightTo = Math.sin(t / 2600);\n        if (t > nextDemo && !s.drag) { const n = s.target === REST ? 1 : trackAt(s.target) + 1; s.target = n > tracks.length ? REST : cuePhi(n); nextDemo = t + 5200; }\n      }\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      s.lampOn += ((motor ? 1 : 0) - s.lampOn) * Math.min(1, dt * 10);\n      s.lampFast += ((live.current.fast ? 1 : 0) - s.lampFast) * Math.min(1, dt * 10);\n      s.press = Math.max(0, s.press - dt * 6);\n      draw(); report();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => { s.phi = s.drag ? s.raw : s.target; s.pv = 0; s.h = s.drag ? 1 : 0; s.om = 0; s.lampOn = s.target !== REST ? 1 : 0; s.lampFast = live.current.fast ? 1 : 0; s.press = 0; draw(); report(); };\n    const wake = () => { if (reduce) still(); else if (!raf) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    api.current = {\n      cue: (track) => { touched = performance.now(); s.target = track <= 0 ? REST : cuePhi(Math.min(tracks.length, track)); wake(); },\n      toggle: () => { touched = performance.now(); s.press = 1.4; s.target = s.target === REST ? cuePhi(1) : REST; wake(); },\n      poke: () => { touched = performance.now(); wake(); },\n    };\n    fit(); wake();\n\n    // Lifting the arm by the headshell.\n    let lastA = 0;\n    const angleAt = (e: PointerEvent) => { const b = cv.getBoundingClientRect(); return Math.atan2(((e.clientY - b.top) / b.height) * H - TY, ((e.clientX - b.left) / b.width) * W - TX); };\n    const onDown = (e: PointerEvent) => { touched = performance.now(); s.drag = true; s.raw = s.phi; lastA = angleAt(e); pad.setPointerCapture(e.pointerId); e.preventDefault(); wake(); };\n    const onMove = (e: PointerEvent) => {\n      if (!s.drag) return;\n      touched = performance.now();\n      const a = angleAt(e); let d = a - lastA; if (d > Math.PI) d -= Math.PI * 2; if (d < -Math.PI) d += Math.PI * 2; lastA = a;\n      s.raw = Math.max(PHI_IN - 0.02, Math.min(REST + 0.03, s.raw - d));\n      if (reduce) still();\n    };\n    const onUp = () => {\n      if (!s.drag) return;\n      s.drag = false; touched = performance.now();\n      // Let go beyond the edge of the record and the arm goes back to its rest; anywhere else it comes down where it is.\n      s.target = s.raw > PHI_OUT + 0.07 ? REST : Math.max(PHI_IN + 0.004, Math.min(phiOf(OUTER + 2), s.raw));\n      wake();\n    };\n    const onLight = (e: PointerEvent) => { const b = cv.getBoundingClientRect(); if (!s.drag) touched = Math.max(touched, performance.now() - 4000); s.lightTo = Math.max(-1, Math.min(1, ((e.clientX - b.left) / b.width) * 2 - 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(); });\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 onKey = (e: ReactKeyboardEvent) => {\n    const t = status.track;\n    const next = e.key === \"ArrowRight\" || e.key === \"ArrowUp\" ? Math.min(tracks.length, t + 1) : e.key === \"ArrowLeft\" || e.key === \"ArrowDown\" ? Math.max(0, t - 1) : e.key === \"Home\" ? 0 : e.key === \"End\" ? tracks.length : -1;\n    if (next < 0) return;\n    e.preventDefault(); api.current?.cue(next);\n  };\n  const speed = () => { live.current.fast = !live.current.fast; setFast(live.current.fast); 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-[#e2512b] focus-visible:ring-offset-2 focus-visible:ring-offset-[#efeeea]\";\n\n  return (\n    <div className=\"flex min-h-[720px] w-full flex-col items-center justify-center bg-[#e3e1db] bg-[radial-gradient(80%_70%_at_50%_40%,#f1f0ec_0%,#e3e1db_60%,#d6d3cc_100%)] px-4 py-14\">\n      <div\n        className=\"relative w-full max-w-[660px] rounded-[20px] shadow-[0_1px_1px_rgba(60,54,44,0.35),0_10px_14px_-6px_rgba(60,54,44,0.35),0_34px_44px_-14px_rgba(60,54,44,0.4),0_80px_90px_-30px_rgba(60,54,44,0.45)]\"\n        style={{ aspectRatio: `${W} / ${H}` }}\n      >\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full rounded-[20px]\" />\n        <button\n          type=\"button\"\n          aria-label={status.track === 0 ? \"Start the record\" : \"Stop and return the arm\"}\n          aria-pressed={status.track !== 0}\n          onClick={() => api.current?.toggle()}\n          style={{ left: pct(8, W), top: pct(412, H), width: pct(80, W), height: pct(80, H) }}\n          className={`absolute cursor-pointer rounded-full ${ring}`}\n        />\n        <button\n          type=\"button\"\n          aria-label=\"Play at 45 revolutions a minute\"\n          aria-pressed={fast}\n          onClick={speed}\n          style={{ left: pct(9, W), top: pct(14, H), width: pct(80, W), height: pct(80, H) }}\n          className={`absolute cursor-pointer rounded-2xl ${ring}`}\n        />\n        <div\n          ref={armHit}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label=\"Tonearm\"\n          aria-valuemin={0}\n          aria-valuemax={tracks.length}\n          aria-valuenow={status.track}\n          aria-valuetext={status.track === 0 ? \"On the armrest\" : `Track ${status.track}, ${tracks[status.track - 1]}`}\n          onKeyDown={onKey}\n          style={{ width: pct(96, W), height: pct(96, H) }}\n          className={`absolute cursor-grab touch-none rounded-full active:cursor-grabbing ${ring}`}\n        />\n      </div>\n      <p aria-live=\"polite\" className=\"mt-10 min-h-[20px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-stone-600\">\n        <span className=\"text-stone-900\">{status.state}</span>\n        <span className=\"mx-2 text-stone-400\">/</span>\n        {status.track === 0 ? `${record.artist}, side ${record.side}` : `${record.side}${status.track} ${tracks[status.track - 1]}`}\n        <span className=\"mx-2 text-stone-400\">/</span>\n        {fast ? \"45\" : \"33\"} rpm\n      </p>\n    </div>\n  );\n}\n"}]}
