{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"open-gate-manual-gear-lever-selector-with-a-chrome-h-pattern-plate-and-a-ball-knob-you-drag-through-the-slots","type":"registry:component","title":"Open-Gate Manual Gear Lever Selector with a Chrome H-Pattern Plate and a Ball Knob You Drag Through the Slots","description":"Selector drawn as an open-gate gear lever: drag the ball knob through an H pattern set in stitched leather to pick an option. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/open-gate-manual-gear-lever-selector-with-a-chrome-h-pattern-plate-and-a-ball-knob-you-drag-through-the-slots.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// The options. `col` is the slot (0 is the far left), `row` is -1 for the top of a slot, 1 for the bottom, 0 for the cross channel.\nconst gears = [\n  { id: \"R\", name: \"Reverse\", note: \"Backing out of the bay\", col: 0, row: -1 },\n  { id: \"N\", name: \"Neutral\", note: \"Out of gear, lever centred\", col: 2, row: 0 },\n  { id: \"1\", name: \"First\", note: \"Pulling away\", col: 1, row: -1 },\n  { id: \"2\", name: \"Second\", note: \"Hairpins and car parks\", col: 1, row: 1 },\n  { id: \"3\", name: \"Third\", note: \"Town traffic\", col: 2, row: -1 },\n  { id: \"4\", name: \"Fourth\", note: \"Open country road\", col: 2, row: 1 },\n  { id: \"5\", name: \"Fifth\", note: \"Dual carriageway\", col: 3, row: -1 },\n  { id: \"6\", name: \"Sixth\", note: \"Motorway cruise\", col: 3, row: 1 },\n];\nconst START = 3; // index of the option selected on load\nconst LAMP = \"#ff6a1a\"; // light behind the numeral of the engaged gear, and the focus ring\nconst LEATHER = [\"#a8643a\", \"#84492a\", \"#4a2614\"]; // highlight, body and shade of the hide\nconst COLS = [-108, -36, 36, 108]; // slot centres, in design pixels from the middle of the plate\nconst THROW = 84; // how far a slot runs above and below the cross channel\nconst SLOT = 25; // slot width\nconst W = 400, H = 500, CX = 200, CY = 252;\n\nconst point = (i: number) => ({ x: COLS[gears[i].col], y: gears[i].row * THROW });\nconst NEUTRAL = gears.findIndex((g) => g.row === 0);\n\nexport default function OpenGateGearLever() {\n  const wrap = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const radios = useRef<(HTMLButtonElement | null)[]>([]);\n  const [gear, setGear] = useState(START);\n  const gearRef = useRef(START);\n  const api = useRef<{ go: (i: number) => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = hit.current, box = wrap.current;\n    if (!cv || !pad || !box) 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    const base = document.createElement(\"canvas\");\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    // The gate as one path: the cross channel and every slot that has a gear at its end.\n    const gate = (c: CanvasRenderingContext2D, dx = 0, dy = 0) => {\n      c.beginPath();\n      c.moveTo(CX + COLS[0] + dx, CY + dy); c.lineTo(CX + COLS[COLS.length - 1] + dx, CY + dy);\n      gears.forEach((g) => { if (g.row !== 0) { c.moveTo(CX + COLS[g.col] + dx, CY + dy); c.lineTo(CX + COLS[g.col] + dx, CY + g.row * THROW + dy); } });\n    };\n    const labelAt = (i: number) => ({ x: CX + COLS[gears[i].col], y: CY + gears[i].row * (THROW + SLOT / 2 + 21) });\n    const WIN = { x: CX - 136, y: CY + 44, w: 56, h: 46 };\n    const FONT = \"700 19px ui-sans-serif, system-ui, 'Helvetica Neue', Arial, sans-serif\";\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      let seed = 23;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n      // The leather pad: body colour, pebbled grain, a padded roll at the edge.\n      const PX = 10, PY = 10, PW = W - 20, PH = H - 20, PR = 40;\n      b.save(); b.shadowColor = \"rgba(0,0,0,0.75)\"; b.shadowBlur = 26 * k; b.shadowOffsetY = 14 * k;\n      rr(b, PX, PY, PW, PH, PR); b.fillStyle = LEATHER[2]; b.fill(); b.restore();\n      const hide = b.createRadialGradient(CX - 60, 90, 20, CX, CY, 330);\n      hide.addColorStop(0, LEATHER[0]); hide.addColorStop(0.45, LEATHER[1]); hide.addColorStop(1, LEATHER[2]);\n      rr(b, PX, PY, PW, PH, PR); b.fillStyle = hide; b.fill();\n      b.save(); rr(b, PX, PY, PW, PH, PR); b.clip();\n      for (let i = 0; i < 26000; i++) {\n        // Pores, the pale shoulder beside each one, and now and then a short crease.\n        const x = rand() * W, y = rand() * H, r = 0.3 + rand() * 0.75;\n        b.beginPath(); b.arc(x, y, r, 0, Math.PI * 2); b.fillStyle = `rgba(30,10,2,${0.1 + rand() * 0.22})`; b.fill();\n        if (i % 2 === 0) { b.beginPath(); b.arc(x - 0.8, y - 0.9, r * 0.9, 0, Math.PI * 2); b.fillStyle = `rgba(255,214,170,${0.03 + rand() * 0.07})`; b.fill(); }\n        if (i % 22 === 0) { const a = rand() * Math.PI, n = 3 + rand() * 7; b.beginPath(); b.moveTo(x, y); b.quadraticCurveTo(x + Math.cos(a) * n * 0.5 + rand() * 3 - 1.5, y + Math.sin(a) * n * 0.5 + rand() * 3 - 1.5, x + Math.cos(a) * n, y + Math.sin(a) * n); b.lineWidth = 0.5; b.strokeStyle = `rgba(30,10,2,${0.1 + rand() * 0.14})`; b.stroke(); }\n      }\n      for (const [wd, al] of [[58, 0.07], [40, 0.08], [24, 0.1], [10, 0.16]]) { rr(b, PX, PY, PW, PH, PR); b.lineWidth = wd; b.strokeStyle = `rgba(20,6,0,${al})`; b.stroke(); }\n      const sheen = b.createLinearGradient(40, 20, 300, 330);\n      sheen.addColorStop(0, \"rgba(255,226,190,0.2)\"); sheen.addColorStop(0.3, \"rgba(255,226,190,0.04)\"); sheen.addColorStop(0.6, \"rgba(255,226,190,0)\"); sheen.addColorStop(1, \"rgba(0,0,0,0.16)\");\n      b.fillStyle = sheen; b.fillRect(0, 0, W, H);\n      b.restore();\n      const rim = b.createLinearGradient(0, PY, 0, PY + PH);\n      rim.addColorStop(0, \"rgba(255,220,180,0.5)\"); rim.addColorStop(0.2, \"rgba(255,220,180,0.06)\"); rim.addColorStop(1, \"rgba(0,0,0,0.5)\");\n      rr(b, PX + 0.7, PY + 0.7, PW - 1.4, PH - 1.4, PR - 0.7); b.lineWidth = 1.4; b.strokeStyle = rim; b.stroke();\n\n      // The seam: a pressed groove, then one saddle stitch at a time, each with its own shadow.\n      const SI = 19, sx = PX + SI, sy = PY + SI, sw = PW - SI * 2, sh = PH - SI * 2, sr = PR - SI + 4;\n      rr(b, sx, sy + 1, sw, sh, sr); b.lineWidth = 4.5; b.strokeStyle = \"rgba(255,214,170,0.12)\"; b.stroke();\n      rr(b, sx, sy, sw, sh, sr); b.lineWidth = 3.6; b.strokeStyle = \"rgba(22,7,0,0.5)\"; b.stroke();\n      const pts: { x: number; y: number; a: number }[] = [];\n      const edge = (x0: number, y0: number, x1: number, y1: number) => { const n = Math.hypot(x1 - x0, y1 - y0), a = Math.atan2(y1 - y0, x1 - x0); for (let d = 0; d < n; d += 1) pts.push({ x: x0 + Math.cos(a) * d, y: y0 + Math.sin(a) * d, a }); };\n      const bend = (cx: number, cy: number, a0: number) => { for (let d = 0; d < (Math.PI / 2) * sr; d += 1) { const a = a0 + d / sr; pts.push({ x: cx + Math.cos(a) * sr, y: cy + Math.sin(a) * sr, a: a + Math.PI / 2 }); } };\n      edge(sx + sr, sy, sx + sw - sr, sy); bend(sx + sw - sr, sy + sr, -Math.PI / 2);\n      edge(sx + sw, sy + sr, sx + sw, sy + sh - sr); bend(sx + sw - sr, sy + sh - sr, 0);\n      edge(sx + sw - sr, sy + sh, sx + sr, sy + sh); bend(sx + sr, sy + sh - sr, Math.PI / 2);\n      edge(sx, sy + sh - sr, sx, sy + sr); bend(sx + sr, sy + sr, Math.PI);\n      b.lineCap = \"round\";\n      for (let i = 4; i < pts.length - 4; i += 9) {\n        const p = pts[i], ca = Math.cos(p.a + 0.2) * 3, sa = Math.sin(p.a + 0.2) * 3;\n        b.beginPath(); b.moveTo(p.x - ca + 0.6, p.y - sa + 1.3); b.lineTo(p.x + ca + 0.6, p.y + sa + 1.3); b.lineWidth = 2.4; b.strokeStyle = \"rgba(15,4,0,0.55)\"; b.stroke();\n        b.beginPath(); b.moveTo(p.x - ca, p.y - sa); b.lineTo(p.x + ca, p.y + sa); b.lineWidth = 2; b.strokeStyle = \"#e7d4ae\"; b.stroke();\n        b.beginPath(); b.moveTo(p.x - ca * 0.7, p.y - sa * 0.7 - 0.5); b.lineTo(p.x + ca * 0.5, p.y + sa * 0.5 - 0.5); b.lineWidth = 0.7; b.strokeStyle = \"rgba(255,255,255,0.6)\"; b.stroke();\n      }\n      b.lineCap = \"butt\";\n\n      // The plate: polished steel, sunk a little into the hide.\n      const GX = CX - 158, GY = CY - 152, GW = 316, GH = 304, GR = 24;\n      b.save(); b.shadowColor = \"rgba(10,3,0,0.85)\"; b.shadowBlur = 22 * k; b.shadowOffsetY = 10 * k;\n      rr(b, GX, GY, GW, GH, GR); b.fillStyle = \"#111\"; b.fill(); b.restore();\n      rr(b, GX - 1.5, GY - 1.5, GW + 3, GH + 3, GR + 1.5); b.fillStyle = \"rgba(12,4,0,0.8)\"; b.fill();\n      const steel = b.createLinearGradient(GX, GY, GX + GW, GY + GH);\n      for (const [p, c] of [[0, \"#fbfcfd\"], [0.12, \"#d3d6da\"], [0.27, \"#8b8f95\"], [0.4, \"#c9ccd1\"], [0.5, \"#f5f6f7\"], [0.6, \"#b3b7bd\"], [0.76, \"#72767c\"], [0.9, \"#c5c8cd\"], [1, \"#9a9ea4\"]] as [number, string][]) steel.addColorStop(p, c);\n      rr(b, GX, GY, GW, GH, GR); b.fillStyle = steel; b.fill();\n      b.save(); rr(b, GX, GY, GW, GH, GR); b.clip();\n      for (let y = GY; y < GY + GH; y += 0.6) { const v = rand(); b.beginPath(); b.moveTo(GX + rand() * 90, y); b.lineTo(GX + GW - rand() * 90, y); b.lineWidth = 0.5; b.strokeStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.22})` : `rgba(0,0,0,${(0.5 - v) * 0.12})`; b.stroke(); }\n      // The warm hide shows faintly in the lower edge of the plate.\n      const warm = b.createLinearGradient(0, GY + GH - 70, 0, GY + GH);\n      warm.addColorStop(0, \"rgba(160,90,45,0)\"); warm.addColorStop(1, \"rgba(160,90,45,0.22)\");\n      b.fillStyle = warm; b.fillRect(GX, GY, GW, GH);\n      b.restore();\n      const bev = b.createLinearGradient(GX, GY, GX + GW * 0.4, GY + GH);\n      bev.addColorStop(0, \"#ffffff\"); bev.addColorStop(0.5, \"#a4a8ae\"); bev.addColorStop(1, \"#3c3f44\");\n      rr(b, GX + 1.5, GY + 1.5, GW - 3, GH - 3, GR - 1.5); b.lineWidth = 3; b.strokeStyle = bev; b.stroke();\n      rr(b, GX + 4, GY + 4, GW - 8, GH - 8, GR - 4); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.28)\"; b.stroke();\n      rr(b, GX + 4.9, GY + 4.9, GW - 9.8, GH - 9.8, GR - 4.9); b.lineWidth = 0.8; b.strokeStyle = \"rgba(255,255,255,0.75)\"; b.stroke();\n\n      // Four countersunk slotted screws.\n      [[GX + 21, GY + 21, 0.5], [GX + GW - 21, GY + 21, 2.1], [GX + 21, GY + GH - 21, 1.2], [GX + GW - 21, GY + GH - 21, 2.8]].forEach(([x, y, turn]) => {\n        b.beginPath(); b.arc(x, y, 8.2, 0, Math.PI * 2); const sink = b.createLinearGradient(x, y - 8, x, y + 8); sink.addColorStop(0, \"#2a2c30\"); sink.addColorStop(1, \"#f4f5f6\"); b.fillStyle = sink; b.fill();\n        const head = b.createLinearGradient(x - 6, y - 6, x + 6, y + 6); head.addColorStop(0, \"#fdfdfd\"); head.addColorStop(0.5, \"#9da1a7\"); head.addColorStop(1, \"#5c5f65\");\n        b.beginPath(); b.arc(x, y, 6.6, 0, Math.PI * 2); b.fillStyle = head; b.fill();\n        b.save(); b.translate(x, y); b.rotate(turn); b.fillStyle = \"#16171a\"; b.fillRect(-6.2, -1.1, 12.4, 2.2); b.fillStyle = \"rgba(255,255,255,0.55)\"; b.fillRect(-6.2, 1.1, 12.4, 0.6); b.restore();\n      });\n\n      // The gate itself: a chamfered lip, then the cut, darkest where the plate overhangs it.\n      b.lineCap = \"round\"; b.lineJoin = \"round\";\n      gate(b, 0, 1.8); b.lineWidth = SLOT + 7; b.strokeStyle = \"#ffffff\"; b.stroke();\n      gate(b, 0, -1.8); b.lineWidth = SLOT + 7; b.strokeStyle = \"#3a3d42\"; b.stroke();\n      gate(b); b.lineWidth = SLOT + 5.5; b.strokeStyle = \"#a9adb3\"; b.stroke();\n      const cut = document.createElement(\"canvas\"); cut.width = cv.width; cut.height = cv.height;\n      const c2 = cut.getContext(\"2d\");\n      if (c2) {\n        c2.setTransform(k, 0, 0, k, 0, 0); c2.lineCap = \"round\"; c2.lineJoin = \"round\";\n        gate(c2); c2.lineWidth = SLOT; c2.strokeStyle = \"#030303\"; c2.stroke();\n        c2.globalCompositeOperation = \"source-atop\";\n        c2.shadowColor = \"#2b2623\"; c2.shadowBlur = 7 * k;\n        gate(c2, 5, 9); c2.lineWidth = SLOT - 6; c2.strokeStyle = \"#2b2623\"; c2.stroke();\n        b.save(); b.setTransform(1, 0, 0, 1, 0, 0); b.drawImage(cut, 0, 0); b.restore();\n      }\n      b.lineCap = \"butt\";\n\n      // A small glass window in the empty corner of the plate that repeats the engaged gear.\n      b.save(); b.shadowColor = \"rgba(255,255,255,0.9)\"; b.shadowBlur = 0; b.shadowOffsetY = 1.2 * k;\n      rr(b, WIN.x - 3.5, WIN.y - 3.5, WIN.w + 7, WIN.h + 7, 10.5); b.fillStyle = \"#2c2e32\"; b.fill(); b.restore();\n      const wb = b.createLinearGradient(WIN.x, WIN.y - 4, WIN.x + WIN.w, WIN.y + WIN.h + 4);\n      wb.addColorStop(0, \"#55585d\"); wb.addColorStop(0.5, \"#e9ebee\"); wb.addColorStop(1, \"#6c7076\");\n      rr(b, WIN.x - 2.2, WIN.y - 2.2, WIN.w + 4.4, WIN.h + 4.4, 9.2); b.lineWidth = 2.2; b.strokeStyle = wb; b.stroke();\n      const glass = b.createLinearGradient(0, WIN.y, 0, WIN.y + WIN.h);\n      glass.addColorStop(0, \"#050506\"); glass.addColorStop(1, \"#17120f\");\n      rr(b, WIN.x, WIN.y, WIN.w, WIN.h, 7); b.fillStyle = glass; b.fill();\n      b.font = \"600 7px ui-monospace, SFMono-Regular, Menlo, monospace\"; b.textAlign = \"center\"; b.textBaseline = \"middle\";\n      b.fillStyle = \"rgba(255,255,255,0.8)\"; b.fillText(\"GEAR\", WIN.x + WIN.w / 2, WIN.y + WIN.h + 13.9);\n      b.fillStyle = \"#1b1c1f\"; b.fillText(\"GEAR\", WIN.x + WIN.w / 2, WIN.y + WIN.h + 13);\n\n      // Numerals, cut into the plate and filled with black enamel.\n      b.font = FONT; b.textAlign = \"center\"; b.textBaseline = \"middle\";\n      gears.forEach((g, i) => {\n        if (g.row === 0) return;\n        const l = labelAt(i);\n        b.fillStyle = \"rgba(255,255,255,0.85)\"; b.fillText(g.id, l.x, l.y + 1.1);\n        b.fillStyle = g.id === \"R\" ? \"#a3201a\" : \"#15161a\"; b.fillText(g.id, l.x, l.y);\n      });\n    };\n\n    const p0 = point(START);\n    const s = { x: p0.x, y: p0.y, gx: p0.x, gy: p0.y, kx: 0, ky: 0, kvx: 0, kvy: 0, drag: false, lamp: gears.map((_, i) => (i === START ? 1 : 0)) };\n    const knobHome = () => ({ x: s.x * 1.2, y: s.y * 1.2 - 54 });\n    { const h = knobHome(); s.kx = h.x; s.ky = h.y; }\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\n      // The engaged numeral lights from behind.\n      ctx.font = FONT; ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\";\n      gears.forEach((g, i) => {\n        const on = s.lamp[i];\n        if (g.row === 0 || on < 0.02) return;\n        const l = labelAt(i);\n        ctx.save(); ctx.globalAlpha = on; ctx.shadowColor = LAMP; ctx.shadowBlur = 14 * k; ctx.fillStyle = LAMP; ctx.fillText(g.id, l.x, l.y);\n        ctx.shadowBlur = 0; ctx.globalAlpha = on * 0.55; ctx.fillStyle = \"#fff3d6\"; ctx.fillText(g.id, l.x, l.y); ctx.restore();\n      });\n\n      // The window: the outgoing character fades as the new one warms up, under a streak of glass glare.\n      ctx.save(); rr(ctx, WIN.x, WIN.y, WIN.w, WIN.h, 7); ctx.clip();\n      ctx.font = \"700 30px ui-monospace, SFMono-Regular, Menlo, monospace\";\n      gears.forEach((g, i) => {\n        const on = s.lamp[i];\n        if (on < 0.02) return;\n        ctx.globalAlpha = on; ctx.shadowColor = LAMP; ctx.shadowBlur = 12 * k; ctx.fillStyle = LAMP; ctx.fillText(g.id, WIN.x + WIN.w / 2, WIN.y + WIN.h / 2 + 1.5);\n        ctx.shadowBlur = 0; ctx.globalAlpha = on * 0.5; ctx.fillStyle = \"#fff1d2\"; ctx.fillText(g.id, WIN.x + WIN.w / 2, WIN.y + WIN.h / 2 + 1.5);\n      });\n      ctx.globalAlpha = 1; ctx.shadowBlur = 0;\n      const glare = ctx.createLinearGradient(WIN.x, WIN.y, WIN.x + WIN.w * 0.8, WIN.y + WIN.h);\n      glare.addColorStop(0, \"rgba(255,255,255,0.22)\"); glare.addColorStop(0.38, \"rgba(255,255,255,0.05)\"); glare.addColorStop(0.4, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = glare; ctx.fillRect(WIN.x, WIN.y, WIN.w, WIN.h);\n      ctx.restore();\n\n      const bx = CX + s.x, by = CY + s.y, kx = CX + s.kx, ky = CY + s.ky, R = 40;\n      // Ball joint down in the slot.\n      const boot = ctx.createRadialGradient(bx - 2, by - 3, 1, bx, by, SLOT / 2);\n      boot.addColorStop(0, \"#5a5d62\"); boot.addColorStop(0.6, \"#1c1d20\"); boot.addColorStop(1, \"#000\");\n      ctx.beginPath(); ctx.arc(bx, by, SLOT / 2 - 1.5, 0, Math.PI * 2); ctx.fillStyle = boot; ctx.fill();\n\n      // Shadows thrown on the plate by the shaft and the knob.\n      const hx = kx + 20, hy = ky + 40;\n      ctx.lineCap = \"round\"; ctx.beginPath(); ctx.moveTo(bx + 2, by + 3); ctx.lineTo(hx, hy); ctx.lineWidth = 11; ctx.strokeStyle = \"rgba(0,0,0,0.22)\"; ctx.stroke();\n      const sh = ctx.createRadialGradient(hx, hy, R * 0.25, hx, hy, R * 1.55);\n      sh.addColorStop(0, \"rgba(0,0,0,0.55)\"); sh.addColorStop(0.55, \"rgba(0,0,0,0.3)\"); sh.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.beginPath(); ctx.arc(hx, hy, R * 1.55, 0, Math.PI * 2); ctx.fillStyle = sh; ctx.fill();\n\n      // The shaft: a polished rod, lit along one side.\n      const dx = kx - bx, dy = ky - by, len = Math.hypot(dx, dy) || 1, nx = -dy / len, ny = dx / len;\n      const rod = ctx.createLinearGradient(bx - nx * 7, by - ny * 7, bx + nx * 7, by + ny * 7);\n      for (const [p, c] of [[0, \"#4b4e53\"], [0.22, \"#d9dbdf\"], [0.36, \"#ffffff\"], [0.55, \"#9da1a7\"], [0.8, \"#33363a\"], [1, \"#85898f\"]] as [number, string][]) rod.addColorStop(p, c);\n      ctx.beginPath(); ctx.moveTo(bx - nx * 6.2, by - ny * 6.2); ctx.lineTo(kx - nx * 7.2, ky - ny * 7.2); ctx.lineTo(kx + nx * 7.2, ky + ny * 7.2); ctx.lineTo(bx + nx * 6.2, by + ny * 6.2); ctx.closePath();\n      ctx.fillStyle = rod; ctx.fill();\n      ctx.beginPath(); ctx.arc(bx, by, 5.6, 0, Math.PI * 2); ctx.fillStyle = \"#2a2c30\"; ctx.fill();\n      ctx.lineCap = \"butt\";\n\n      // The knob: a mirror-polished ball. Sky in the middle, the room squeezed into a ring near the edge.\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.5)\"; ctx.shadowBlur = 8 * k; ctx.shadowOffsetY = 4 * k;\n      ctx.beginPath(); ctx.arc(kx, ky, R, 0, Math.PI * 2); ctx.fillStyle = \"#222\"; ctx.fill(); ctx.restore();\n      const ox = -s.x * 0.035, oy = -s.y * 0.035;\n      const ball = ctx.createRadialGradient(kx - R * 0.16 + ox, ky - R * 0.22 + oy, R * 0.04, kx, ky, R);\n      for (const [p, c] of [[0, \"#ffffff\"], [0.24, \"#eef0f2\"], [0.44, \"#b9bcc2\"], [0.58, \"#63666c\"], [0.67, \"#1d1e21\"], [0.75, \"#3b281c\"], [0.85, \"#6b4732\"], [0.93, \"#2e1c13\"], [1, \"#0d0807\"]] as [number, string][]) ball.addColorStop(p, c);\n      ctx.beginPath(); ctx.arc(kx, ky, R, 0, Math.PI * 2); ctx.fillStyle = ball; ctx.fill();\n      // The plate, mirrored as a pale crescent low on the ball.\n      ctx.save(); ctx.beginPath(); ctx.arc(kx, ky, R, 0, Math.PI * 2); ctx.clip();\n      const cres = ctx.createRadialGradient(kx, ky + R * 1.5, R * 0.9, kx, ky + R * 1.5, R * 1.5);\n      cres.addColorStop(0, \"rgba(235,238,242,0.75)\"); cres.addColorStop(0.5, \"rgba(200,204,210,0.28)\"); cres.addColorStop(1, \"rgba(200,204,210,0)\");\n      ctx.fillStyle = cres; ctx.fillRect(kx - R, ky, R * 2, R);\n      // Soft window light, upper left, and its small echo opposite.\n      ctx.translate(kx - R * 0.36 + ox * 2, ky - R * 0.44 + oy * 2); ctx.rotate(-0.72);\n      const win = ctx.createRadialGradient(0, 0, 0, 0, 0, R * 0.42);\n      win.addColorStop(0, \"rgba(255,255,255,1)\"); win.addColorStop(0.5, \"rgba(255,255,255,0.8)\"); win.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.scale(1, 0.5); ctx.beginPath(); ctx.arc(0, 0, R * 0.42, 0, Math.PI * 2); ctx.fillStyle = win; ctx.fill();\n      ctx.restore();\n      ctx.beginPath(); ctx.arc(kx, ky, R - 0.6, Math.PI * 0.95, Math.PI * 1.7); ctx.lineWidth = 1.1; ctx.strokeStyle = \"rgba(255,255,255,0.55)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(kx, ky, R - 0.6, Math.PI * 0.05, Math.PI * 0.6); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,200,150,0.35)\"; ctx.stroke();\n    };\n\n    const select = (i: number) => { if (gearRef.current !== i) { gearRef.current = i; setGear(i); } };\n    const EPS = 0.01;\n    const travel = (dist: number) => {\n      let left = dist;\n      for (let n = 0; n < 3 && left > 0; n++) {\n        if (Math.abs(s.x - s.gx) > EPS) {\n          if (Math.abs(s.y) > EPS) { const d = Math.min(left, Math.abs(s.y)); s.y -= Math.sign(s.y) * d; left -= d; }\n          else { s.y = 0; const d = Math.min(left, Math.abs(s.gx - s.x)); s.x += Math.sign(s.gx - s.x) * d; left -= d; }\n        } else { s.x = s.gx; const d = Math.min(left, Math.abs(s.gy - s.y)); s.y += Math.sign(s.gy - s.y) * d; left -= d; if (d === 0) break; }\n      }\n    };\n    const pathLeft = () => (Math.abs(s.x - s.gx) > EPS ? Math.abs(s.y) + Math.abs(s.gx - s.x) + Math.abs(s.gy) : Math.abs(s.gy - s.y));\n    // Which option the lever is in right now: a gear once it is most of the way down a slot, otherwise neutral.\n    const engaged = () => { let best = NEUTRAL; gears.forEach((g, i) => { if (g.row !== 0 && Math.abs(s.x - COLS[g.col]) < 2 && s.y * g.row > THROW * 0.72) best = i; }); return best; };\n    const nearestGear = (x: number, y: number, reach: number) => { let best = -1, bd = reach; gears.forEach((_, i) => { const q = point(i), d = Math.hypot(q.x - x, q.y - y); if (d < bd) { bd = d; best = i; } }); return best; };\n    // The closest spot the gate allows to a free point.\n    const clampToGate = (x: number, y: number) => {\n      let best = { x: Math.max(COLS[0], Math.min(COLS[COLS.length - 1], x)), y: 0 }, bd = Math.hypot(best.x - x, y);\n      gears.forEach((g) => { if (g.row === 0) return; const cy = g.row < 0 ? Math.max(-THROW, Math.min(0, y)) : Math.max(0, Math.min(THROW, y)), d = Math.hypot(COLS[g.col] - x, cy - y); if (d < bd) { bd = d; best = { x: COLS[g.col], y: cy }; } });\n      return best;\n    };\n\n    let raf = 0, last = 0, seen = true, touched = -1e9, t0 = 0, step = 0;\n    const tour = [4, 5, 6, 7, 1, 2, 3]; // the order the idle demo walks through\n    let nextDemo = 900;\n\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      const t = now - t0;\n      if (now - touched > 5200 && !s.drag && t > nextDemo) {\n        const n = tour[step % tour.length]; step++;\n        const q = point(n); s.gx = q.x; s.gy = q.y;\n        nextDemo = t + (step === 1 ? 3000 : 1900);\n      }\n      const left = pathLeft();\n      if (left > 0) travel(Math.min(left, Math.max(left * (1 - Math.exp(-dt * (s.drag ? 30 : 9))), (s.drag ? 500 : 150) * dt)));\n      select(engaged());\n      const h = knobHome();\n      s.kvx += (420 * (h.x - s.kx) - 24 * s.kvx) * dt; s.kvy += (420 * (h.y - s.ky) - 24 * s.kvy) * dt;\n      s.kx += s.kvx * dt; s.ky += s.kvy * dt;\n      s.lamp = s.lamp.map((v, i) => v + ((i === gearRef.current ? 1 : 0) - v) * Math.min(1, dt * (i === gearRef.current ? 16 : 6)));\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const settle = () => { s.x = s.gx; s.y = s.gy; const h = knobHome(); s.kx = h.x; s.ky = h.y; s.kvx = 0; s.kvy = 0; select(engaged()); s.lamp = gears.map((_, i) => (i === gearRef.current ? 1 : 0)); draw(); };\n\n    const size = () => {\n      const dpr = Math.min(2, window.devicePixelRatio || 1), wpx = Math.max(200, box.clientWidth);\n      cv.width = Math.round(wpx * dpr); cv.height = Math.round((wpx * dpr * H) / W); k = cv.width / W;\n      build(); draw();\n    };\n    size();\n    const ro = new ResizeObserver(size); ro.observe(box);\n\n    api.current = { go: (i) => { touched = performance.now(); const q = point(i); s.gx = q.x; s.gy = q.y; if (reduce) settle(); else wake(); } };\n    if (reduce) settle(); else wake();\n\n    const at = (e: PointerEvent) => { const r = pad.getBoundingClientRect(); return { x: ((e.clientX - r.left) / r.width) * W - CX, y: ((e.clientY - r.top) / r.height) * H - CY }; };\n    let grab = { x: 0, y: 0 }, moved = 0, down = { x: 0, y: 0 };\n    const aim = (q: { x: number; y: number }) => { const g = clampToGate(q.x - grab.x, q.y - grab.y); s.gx = g.x; s.gy = g.y; };\n    const onDown = (e: PointerEvent) => {\n      const q = at(e); touched = performance.now(); moved = 0; down = q;\n      const onKnob = Math.hypot(q.x - s.kx, q.y - s.ky) < 50;\n      grab = onKnob ? { x: q.x - s.x, y: q.y - s.y } : { x: 0, y: 0 };\n      s.drag = onKnob; pad.setPointerCapture(e.pointerId); e.preventDefault(); wake();\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!pad.hasPointerCapture(e.pointerId)) return;\n      const q = at(e); moved = Math.max(moved, Math.hypot(q.x - down.x, q.y - down.y)); touched = performance.now();\n      if (!s.drag && moved > 6) { s.drag = true; grab = { x: 0, y: 0 }; }\n      if (s.drag) { aim(q); if (reduce) settle(); }\n    };\n    const onUp = (e: PointerEvent) => {\n      if (!pad.hasPointerCapture(e.pointerId)) return;\n      pad.releasePointerCapture(e.pointerId); touched = performance.now();\n      const q = at(e);\n      let n: number;\n      if (moved <= 6) { n = nearestGear(q.x, q.y * 0.86, 52); if (n < 0) n = gearRef.current; } // a tap on a numeral or slot end goes there\n      else { n = NEUTRAL; gears.forEach((g, i) => { if (g.row !== 0 && Math.abs(s.gx - COLS[g.col]) < 2 && s.gy * g.row > THROW * 0.42) n = i; }); }\n      s.drag = false; const g = point(n); s.gx = g.x; s.gy = g.y;\n      if (reduce) settle();\n    };\n    pad.addEventListener(\"pointerdown\", onDown); pad.addEventListener(\"pointermove\", onMove);\n    pad.addEventListener(\"pointerup\", onUp); pad.addEventListener(\"pointercancel\", onUp);\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; ro.disconnect(); io.disconnect();\n      pad.removeEventListener(\"pointerdown\", onDown); pad.removeEventListener(\"pointermove\", onMove);\n      pad.removeEventListener(\"pointerup\", onUp); pad.removeEventListener(\"pointercancel\", onUp);\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const choose = (i: number) => { api.current?.go(i); };\n  const onKey = (e: ReactKeyboardEvent, i: number) => {\n    const next = e.key === \"ArrowRight\" || e.key === \"ArrowDown\" ? (i + 1) % gears.length : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\" ? (i + gears.length - 1) % gears.length : e.key === \"Home\" ? 0 : e.key === \"End\" ? gears.length - 1 : -1;\n    if (next < 0) return;\n    e.preventDefault(); choose(next); radios.current[next]?.focus();\n  };\n\n  return (\n    <div className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#120c09] bg-[radial-gradient(110%_80%_at_50%_35%,#2a1b13_0%,#120c09_64%)] px-4 py-10 sm:min-h-[760px]\">\n      <div ref={wrap} className=\"relative aspect-[4/5] w-full max-w-[340px] sm:max-w-[468px]\">\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-[4%] cursor-grab touch-none active:cursor-grabbing\" />\n        <div role=\"radiogroup\" aria-label=\"Gear\" className=\"pointer-events-none absolute inset-0\">\n          {gears.map((g, i) => (\n            <button\n              key={g.id}\n              ref={(el) => { radios.current[i] = el; }}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={gear === i}\n              aria-label={g.name}\n              tabIndex={gear === i ? 0 : -1}\n              onClick={() => choose(i)}\n              onKeyDown={(e) => onKey(e, i)}\n              style={{ left: `${((CX + COLS[g.col]) / W) * 100}%`, top: `${((CY + g.row * (THROW + SLOT / 2 + 21)) / H) * 100}%` }}\n              className=\"absolute h-11 w-11 -translate-x-1/2 -translate-y-1/2 rounded-full outline-none focus-visible:ring-2 focus-visible:ring-[#ff6a1a] focus-visible:ring-offset-2 focus-visible:ring-offset-black/40\"\n            />\n          ))}\n        </div>\n      </div>\n      <p aria-live=\"polite\" className=\"mt-7 min-h-[20px] max-w-[320px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.16em] text-[#b79d8a]\">\n        <span className=\"text-[#f3e6d8]\">{gears[gear].name}</span>\n        <span className=\"mx-2 text-[#6f5646]\">/</span>\n        {gears[gear].note}\n      </p>\n    </div>\n  );\n}\n"}]}
