{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"piano-octave-key-picker-with-real-key-dip-red-felt-and-a-lacquer-fallboard-that-mirrors-the-keys","type":"registry:component","title":"Piano Octave Key Picker with Real Key Dip, Red Felt and a Lacquer Fallboard That Mirrors the Keys","description":"Piano key picker on a canvas: an octave in a lacquer case where the chosen key stays held down, over real radio buttons. Free React and HTML code to copy.","dependencies":[],"files":[{"path":"components/we-inc/piano-octave-key-picker-with-real-key-dip-red-felt-and-a-lacquer-fallboard-that-mirrors-the-keys.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// One octave, left to right. `sharp` makes a black key. Rename the labels to reuse it as any twelve-way picker.\nconst keys = [\n  { label: \"C\", sharp: false, note: \"No sharps or flats\" },\n  { label: \"D flat\", sharp: true, note: \"Five flats\" },\n  { label: \"D\", sharp: false, note: \"Two sharps\" },\n  { label: \"E flat\", sharp: true, note: \"Three flats\" },\n  { label: \"E\", sharp: false, note: \"Four sharps\" },\n  { label: \"F\", sharp: false, note: \"One flat\" },\n  { label: \"F sharp\", sharp: true, note: \"Six sharps\" },\n  { label: \"G\", sharp: false, note: \"One sharp\" },\n  { label: \"A flat\", sharp: true, note: \"Four flats\" },\n  { label: \"A\", sharp: false, note: \"Three sharps\" },\n  { label: \"B flat\", sharp: true, note: \"Two flats\" },\n  { label: \"B\", sharp: false, note: \"Five sharps\" },\n];\nconst START = 0; // the key that is held down on load\nconst DEMO = [4, 7, 10, 5, 2, 0]; // keys the idle demo walks through\nconst SOUND_ON = false; // set to true to hear a soft tone on each key you press\nconst FELT = [163, 22, 31]; // red, green, blue of the felt strip behind the keys\nconst RING = \"#ffb454\"; // keyboard focus outline\n\n// The model, in millimetres. x runs left to right, y is up, z runs from the front of the keys to the fallboard.\nconst WK = 23.5, GAP = 1.1, LEN = 150, BZ = 55, BW = 5.6, BT = 4.5, BH = 11.5, CHEEK = 23, CH = 15, BASE = -34, FALL = 76, BACK = 22, SLIPZ = -15, SLIPY = -10, PIVOT = 235, DIP = 10.5, SINK = 7.5;\nconst NUDGE = [-1.8, 1.8, -2.4, 0, 2.4]; // black keys are not centred on the gaps of a real keyboard\nconst WHITES = keys.filter((k) => !k.sharp).length, HALF = (WHITES * WK) / 2, FX = HALF + CHEEK;\nconst layout = (() => {\n  let w = 0, b = 0;\n  return keys.map((k) => (k.sharp ? { x: -HALF + w * WK + NUDGE[b++ % 5], w: w - 1 } : { x: -HALF + w * WK, w: w++ }));\n})();\n\n// A fixed camera in front of and above the keyboard, and a fit of the whole case into the canvas.\nconst CW = 720, CH2 = 640, CAM_Y = 700, CAM_Z = -790, SIN = Math.sin(0.68), COS = Math.cos(0.68);\nconst unit = (x: number, y: number, z: number) => { const dy = y - CAM_Y, dz = z - CAM_Z, d = dz * COS - dy * SIN; return [x / d, -(dy * COS + dz * SIN) / d]; };\nconst fitBox = (() => {\n  let x1 = 0, y0 = 9, y1 = -9;\n  for (const y of [BASE, FALL]) for (const z of [SLIPZ, LEN + BACK]) { const [ux, uy] = unit(FX, y, z); x1 = Math.max(x1, ux); y0 = Math.min(y0, uy); y1 = Math.max(y1, uy); }\n  const S = Math.min((CW - 150) / (2 * x1), (CH2 - 84) / (y1 - y0));\n  return { S, OX: CW / 2, OY: 26 - y0 * S };\n})();\nconst P = (x: number, y: number, z: number): [number, number] => { const [ux, uy] = unit(x, y, z); return [fitBox.OX + ux * fitBox.S, fitBox.OY + uy * fitBox.S]; };\n// Screen boxes for the real buttons that sit over each key, as fractions of the canvas.\nconst boxes = keys.map((k, i) => {\n  const g = layout[i];\n  const pts = k.sharp ? [P(g.x - BW, 0, BZ), P(g.x + BW, 0, BZ), P(g.x - BT, BH, LEN), P(g.x + BT, BH, LEN)] : [P(g.x, -20, 0), P(g.x + WK, -20, 0), P(g.x + 3, 0, BZ), P(g.x + WK - 3, 0, BZ)];\n  const xs = pts.map((p) => p[0]), ys = pts.map((p) => p[1]);\n  if (!k.sharp) { xs.push(P(g.x + 1, 0, 0)[0], P(g.x + WK - 1, 0, 0)[0]); }\n  const l = Math.min(...xs), t = Math.min(...ys);\n  return { left: l / CW, top: t / CH2, width: (Math.max(...xs) - l) / CW, height: (Math.max(...ys) - t) / CH2 };\n});\n\nlet audio: AudioContext | null = null;\nconst tone = (i: number) => {\n  if (!SOUND_ON || typeof window === \"undefined\") return;\n  audio = audio || new AudioContext();\n  const o = audio.createOscillator(), g = audio.createGain(), t = audio.currentTime;\n  o.type = \"triangle\"; o.frequency.value = 261.63 * Math.pow(2, i / 12);\n  g.gain.setValueAtTime(0.16, t); g.gain.exponentialRampToValueAtTime(0.001, t + 0.9);\n  o.connect(g); g.connect(audio.destination); o.start(t); o.stop(t + 0.9);\n};\n\ntype V = [number, number, number];\n\nexport default function PianoOctaveKeyPicker() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const wrap = useRef<HTMLDivElement>(null);\n  const radios = useRef<(HTMLButtonElement | null)[]>([]);\n  const [value, setValue] = useState(START);\n  const valueRef = useRef(START);\n  const api = useRef<{ go: (i: number) => void; focus: (i: number) => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, box = wrap.current;\n    if (!cv || !box) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(2, window.devicePixelRatio || 1);\n    let k = dpr, mirror = false;\n    const s = { p: keys.map((_, i) => (i === START ? 1 : 0)), v: keys.map(() => 0), hover: -1, drag: false, focus: -1 };\n\n    const pt = (v: V) => P(v[0], v[1], mirror ? 2 * LEN - v[2] : v[2]);\n    const path = (vs: V[]) => { ctx.beginPath(); vs.forEach((v, i) => { const [x, y] = pt(v); if (i) ctx.lineTo(x, y); else ctx.moveTo(x, y); }); ctx.closePath(); };\n    const grad = (a: V, b: V, stops: [number, string][]) => { const [x0, y0] = pt(a), [x1, y1] = pt(b); const g = ctx.createLinearGradient(x0, y0, x1, y1); for (const [o, c] of stops) g.addColorStop(o, c); return g; };\n    const quad = (vs: V[], fill: string | CanvasGradient) => { path(vs); ctx.fillStyle = fill; ctx.fill(); };\n    const line = (a: V, b: V, w: number, c: string | CanvasGradient) => { const [x0, y0] = pt(a), [x1, y1] = pt(b); ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(x1, y1); ctx.lineWidth = w; ctx.strokeStyle = c; ctx.stroke(); };\n    // A blurred shape with nothing solid under it: the shape is drawn off the canvas and only its shadow lands.\n    const soft = (vs: V[], colour: string, blur: number) => {\n      ctx.save(); ctx.shadowColor = colour; ctx.shadowBlur = blur * k; ctx.shadowOffsetX = 3000 * k; ctx.translate(-3000, 0); path(vs); ctx.fillStyle = \"#000\"; ctx.fill(); ctx.restore();\n    };\n    // How far the top of a key has dropped at a given depth. Keys pivot far behind the fallboard, so the front moves most.\n    const drop = (i: number, z: number) => Math.max(-0.04, s.p[i]) * (keys[i].sharp ? SINK * (PIVOT - z) / (PIVOT - BZ) : DIP * (PIVOT - z) / PIVOT);\n    const whiteAt = (w: number) => keys.findIndex((key, i) => !key.sharp && layout[i].w === w);\n    let seed = 11;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n    const flecks = Array.from({ length: 520 }, () => [(rand() * 2 - 1) * HALF, rand(), rand()]);\n\n    const whiteKeys = () => {\n      const order = keys.map((_, i) => i).filter((i) => !keys[i].sharp).sort((a, b) => Math.abs(layout[b].x + WK / 2) - Math.abs(layout[a].x + WK / 2));\n      for (const i of order) {\n        const p = Math.max(0, s.p[i]), xl = layout[i].x + GAP / 2, xr = xl + WK - GAP, d0 = drop(i, 0), d1 = drop(i, LEN);\n        const shade = (v: number) => `rgb(${(v - 26 * p) | 0},${(v - 3 - 28 * p) | 0},${(v - 14 - 32 * p) | 0})`;\n        if (!mirror) quad([[xl, -d0, 0], [xr, -d0, 0], [xr, -d0 - 22, 0], [xl, -d0 - 22, 0]], grad([0, -d0, 0], [0, -d0 - 22, 0], [[0, \"#f1ecdf\"], [0.3, \"#d9d3c3\"], [1, \"#958e7c\"]]));\n        // A tilted key throws the light somewhere else, so the bright band slides and the whole top dims.\n        const b = 0.34 + 0.2 * p;\n        quad([[xl, -d0, 0], [xr, -d0, 0], [xr, -d1, LEN], [xl, -d1, LEN]], grad([0, -d0, 0], [0, -d1, LEN], [[0, shade(249)], [b - 0.2, shade(240)], [b - 0.05, shade(255)], [b + 0.07, shade(255)], [b + 0.22, shade(238)], [0.8, shade(226)], [1, shade(190)]]));\n        if (mirror) continue;\n        line([xl + 0.3, -d0, 0], [xl + 0.3, -d1, LEN], 0.9, \"rgba(255,255,255,0.85)\");\n        line([xr - 0.3, -d0, 0], [xr - 0.3, -d1, LEN], 0.9, \"rgba(90,74,44,0.2)\");\n        line([xl + 0.6, -d0 - 0.3, 0], [xr - 0.6, -d0 - 0.3, 0], 1.7, \"#ffffff\");\n        line([xl + 0.6, -d0 - 1.6, 0], [xr - 0.6, -d0 - 1.6, 0], 0.9, \"rgba(120,104,70,0.35)\");\n      }\n      if (mirror) return;\n      // Where one key is lower than the next, the bare wooden side of the higher one shows.\n      for (let w = 0; w < WHITES - 1; w++) {\n        const a = whiteAt(w), b = whiteAt(w + 1), xe = -HALF + (w + 1) * WK;\n        if (Math.abs(xe) < 0.01) continue;\n        const hi = xe < 0 ? a : b, lo = xe < 0 ? b : a, x = xe < 0 ? xe - GAP / 2 : xe + GAP / 2;\n        if (drop(lo, 0) - drop(hi, 0) < 0.05) continue;\n        quad([[x, -drop(hi, 0), 0], [x, -drop(lo, 0), 0], [x, -drop(lo, LEN), LEN], [x, -drop(hi, LEN), LEN]], grad([x, 0, 0], [x, 0, LEN], [[0, \"#c79f68\"], [0.5, \"#a9824f\"], [1, \"#6d5131\"]]));\n        line([x, -drop(hi, 0), 0], [x, -drop(hi, LEN), LEN], 0.8, \"rgba(255,255,255,0.7)\");\n      }\n      // And the higher key shades the lower one.\n      for (let w = 0; w < WHITES; w++) {\n        const i = whiteAt(w), left = w > 0 ? whiteAt(w - 1) : -1, d = drop(i, 0) - (left < 0 ? 0 : drop(left, 0)), x = layout[i].x + GAP / 2;\n        if (d < 0.1) continue;\n        const reach = d * 0.62;\n        quad([[x, -drop(i, 0), 0], [x + reach, -drop(i, 0), 0], [x + reach * 0.3, -drop(i, LEN), LEN], [x, -drop(i, LEN), LEN]], grad([x, 0, 0], [x + reach, 0, 0], [[0, \"rgba(50,34,10,0.5)\"], [0.7, \"rgba(50,34,10,0.3)\"], [1, \"rgba(50,34,10,0)\"]]));\n      }\n    };\n\n    const blackKeys = () => {\n      keys.forEach((key, i) => {\n        if (!key.sharp) return;\n        const xc = layout[i].x, s0 = drop(i, BZ), s1 = drop(i, LEN), h0 = BH - s0, h1 = BH - s1, t = Math.max(0, s0 / BH);\n        const zs = BZ + 7, zb = BZ + 7 * t, wb = BW + (BT - BW) * t; // where the sloped nose meets the white keys once it has sunk\n        if (!mirror && Math.abs(xc) > 2) {\n          const sgn = xc < 0 ? 1 : -1, near = whiteAt(layout[i].w + (xc < 0 ? 1 : 0)), xb = xc + sgn * BW, xt = xc + sgn * BT;\n          quad([[xc + sgn * wb, 0, zb], [xb, -drop(near, BZ), BZ], [xb, -drop(near, LEN), LEN], [xt, h1, LEN], [xt, h0, zs]], grad([xb, 0, BZ], [xb, BH, BZ], [[0, \"#050506\"], [0.6, \"#1d1e21\"], [1, \"#3b3c42\"]]));\n        }\n        if (!mirror) quad([[xc - wb, 0, zb], [xc + wb, 0, zb], [xc + BT, h0, zs], [xc - BT, h0, zs]], grad([xc, 0, zb], [xc, h0, zs], [[0, \"#101012\"], [0.5, \"#33343a\"], [1, \"#7c7d85\"]]));\n        quad([[xc - BT, h0, zs], [xc + BT, h0, zs], [xc + BT, h1, LEN], [xc - BT, h1, LEN]], grad([xc, h0, zs], [xc, h1, LEN], [[0, \"#55565d\"], [0.05, \"#2a2b30\"], [0.3, \"#17171a\"], [0.52, \"#2e2f34\"], [0.62, \"#35363c\"], [0.8, \"#141416\"], [1, \"#060607\"]]));\n        if (mirror) return;\n        line([xc - BT + 0.5, h0, zs + 2], [xc - BT + 0.5, h1, LEN - 8], 1.1, \"rgba(255,255,255,0.32)\");\n        line([xc - BT + 0.4, h0, zs], [xc + BT - 0.4, h0, zs], 1.2, \"rgba(255,255,255,0.6)\");\n      });\n    };\n\n    const cheeks = (inner: boolean) => {\n      for (const sg of [-1, 1]) {\n        const xi = sg * HALF, xo = sg * FX;\n        if (inner) {\n          quad([[xi, -16, SLIPZ], [xi, CH, SLIPZ], [xi, CH, LEN], [xi, -16, LEN]], grad([xi, CH, 0], [xi, -4, 0], [[0, \"#0a0a0c\"], [0.55, \"#1a1a1d\"], [0.8, sg < 0 ? \"#34322d\" : \"#5b5850\"], [1, \"#2b2924\"]]));\n          continue;\n        }\n        quad([[xi, CH, SLIPZ], [xo, CH, SLIPZ], [xo, CH, LEN], [xi, CH, LEN]], grad([0, CH, SLIPZ], [0, CH, LEN], [[0, \"#3d3e44\"], [0.06, \"#1b1c1f\"], [0.3, \"#0d0d0f\"], [0.48, \"#2b2c31\"], [0.6, \"#303136\"], [0.75, \"#101012\"], [1, \"#050506\"]]));\n        if (mirror) continue;\n        quad([[xi, CH, SLIPZ], [xo, CH, SLIPZ], [xo, BASE, SLIPZ], [xi, BASE, SLIPZ]], grad([0, CH, SLIPZ], [0, BASE, SLIPZ], [[0, \"#2a2b2f\"], [0.25, \"#121214\"], [0.7, \"#0a0a0b\"], [1, \"#020202\"]]));\n        line([xi, CH, SLIPZ], [xo, CH, SLIPZ], 1.3, \"rgba(255,255,255,0.55)\");\n        line([xi, CH, SLIPZ], [xi, CH, LEN], 1, sg < 0 ? \"rgba(255,255,255,0.16)\" : \"rgba(255,255,255,0.42)\");\n      }\n    };\n\n    const draw = () => {\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      ctx.clearRect(0, 0, CW, CH2);\n      mirror = false;\n      const ZB = LEN + BACK;\n\n      // Shadows on the table: a long soft one from the tall back, a short one from the low front, and a dark seam where the case meets the surface.\n      soft([[-FX + 8, BASE, SLIPZ - 7], [FX + 12, BASE, SLIPZ - 7], [FX + 20, BASE, ZB + 8], [-FX + 20, BASE, ZB + 8]], \"rgba(48,12,2,0.45)\", 26);\n      soft([[-FX + 1, BASE, SLIPZ - 3], [FX + 3, BASE, SLIPZ - 3], [FX + 3, BASE, SLIPZ + 12], [-FX + 1, BASE, SLIPZ + 12]], \"rgba(20,4,0,0.8)\", 5);\n\n      // The back of the case: its top, and the fallboard that faces the player.\n      quad([[-FX, FALL, LEN], [FX, FALL, LEN], [FX, FALL, ZB], [-FX, FALL, ZB]], grad([0, FALL, LEN], [0, FALL, ZB], [[0, \"#4a4b52\"], [0.12, \"#1e1f22\"], [0.5, \"#2f3035\"], [1, \"#0c0c0e\"]]));\n      const board: V[] = [[-FX, FALL, LEN], [FX, FALL, LEN], [FX, BASE, LEN], [-FX, BASE, LEN]];\n      quad(board, grad([0, FALL, LEN], [0, 0, LEN], [[0, \"#2c2d32\"], [0.05, \"#131316\"], [0.55, \"#0a0a0c\"], [1, \"#050506\"]]));\n      // Black lacquer is a dark mirror: draw the keyboard again, reflected in the plane of the board.\n      ctx.save(); path(board); ctx.clip();\n      mirror = true; whiteKeys(); blackKeys(); cheeks(false); mirror = false;\n      quad(board, grad([0, 0, LEN], [0, FALL * 0.8, LEN], [[0, \"rgba(6,6,8,0.5)\"], [0.3, \"rgba(6,6,8,0.8)\"], [0.7, \"rgba(6,6,8,0.97)\"], [1, \"rgba(6,6,8,1)\"]]));\n      quad(board, grad([0, FALL, LEN], [0, FALL - 30, LEN], [[0, \"rgba(255,255,255,0.1)\"], [1, \"rgba(255,255,255,0)\"]]));\n      ctx.restore();\n      line([-FX, FALL, LEN], [FX, FALL, LEN], 1.4, \"rgba(255,255,255,0.5)\");\n\n      // Keybed, then the keys.\n      quad([[-HALF, -16, SLIPZ], [HALF, -16, SLIPZ], [HALF, -16, LEN], [-HALF, -16, LEN]], \"#0b0806\");\n      cheeks(true);\n      whiteKeys();\n      ctx.save(); path([[-HALF, 0, 0], [HALF, 0, 0], [HALF, 0, LEN], [-HALF, 0, LEN]]); ctx.clip();\n      soft([[-HALF, 0, -4], [-HALF + CH * 0.42, 0, -4], [-HALF + CH * 0.42, 0, LEN], [-HALF, 0, LEN]], \"rgba(40,26,6,0.5)\", 5);\n      keys.forEach((key, i) => {\n        if (!key.sharp) return;\n        const h = BH - drop(i, BZ), x = layout[i].x, o = h * 0.62;\n        soft([[x - BW + 1, 0, BZ], [x + BW + o, 0, BZ - h * 0.12], [x + BW + o, 0, LEN], [x - BW + 1, 0, LEN]], \"rgba(40,26,6,0.62)\", 4.5);\n      });\n      quad([[-HALF, 0, LEN - 46], [HALF, 0, LEN - 46], [HALF, 0, LEN], [-HALF, 0, LEN]], grad([0, 0, LEN - 46], [0, 0, LEN], [[0, \"rgba(26,14,2,0)\"], [1, \"rgba(26,14,2,0.5)\"]]));\n      ctx.restore();\n      // Felt strip lying across the backs of the keys.\n      const [fr, fg, fb] = FELT, FZ = LEN - 9;\n      quad([[-HALF, 4, FZ], [HALF, 4, FZ], [HALF, -2, FZ], [-HALF, -2, FZ]], `rgb(${fr * 0.45 | 0},${fg * 0.45 | 0},${fb * 0.45 | 0})`);\n      quad([[-HALF, 4, FZ], [HALF, 4, FZ], [HALF, 4, LEN], [-HALF, 4, LEN]], grad([0, 4, FZ], [0, 4, LEN], [[0, `rgb(${Math.min(255, fr * 1.2) | 0},${fg * 1.5 | 0},${fb * 1.4 | 0})`], [0.5, `rgb(${fr},${fg},${fb})`], [1, `rgb(${fr * 0.5 | 0},${fg * 0.5 | 0},${fb * 0.5 | 0})`]]));\n      for (const [fx, fz, fv] of flecks) { const [x, y] = pt([fx, 4, FZ + fz * 9]); ctx.fillStyle = fv > 0.5 ? `rgba(255,190,170,${(fv - 0.5) * 0.5})` : `rgba(30,0,0,${(0.5 - fv) * 0.8})`; ctx.fillRect(x, y, 1.5, 0.8); }\n      blackKeys();\n\n      // Cheek blocks, and the rail the key fronts drop behind.\n      cheeks(false);\n      quad([[-HALF, SLIPY, SLIPZ], [HALF, SLIPY, SLIPZ], [HALF, SLIPY, -1.2], [-HALF, SLIPY, -1.2]], grad([0, SLIPY, SLIPZ], [0, SLIPY, -1.2], [[0, \"#6a6b73\"], [0.25, \"#2b2c31\"], [0.7, \"#1a1a1d\"], [1, \"#3a3936\"]]));\n      quad([[-HALF, SLIPY, SLIPZ], [HALF, SLIPY, SLIPZ], [HALF, BASE, SLIPZ], [-HALF, BASE, SLIPZ]], grad([0, SLIPY, SLIPZ], [0, BASE, SLIPZ], [[0, \"#2f3035\"], [0.16, \"#121214\"], [0.45, \"#1c1d20\"], [0.6, \"#0d0d0f\"], [1, \"#020202\"]]));\n      const glint = ctx.createLinearGradient(pt([-HALF, 0, 0])[0], 0, pt([HALF, 0, 0])[0], 0);\n      glint.addColorStop(0, \"rgba(255,255,255,0.1)\"); glint.addColorStop(0.28, \"rgba(255,255,255,0.75)\"); glint.addColorStop(0.6, \"rgba(255,255,255,0.3)\"); glint.addColorStop(1, \"rgba(255,255,255,0.08)\");\n      line([-HALF, SLIPY, SLIPZ], [HALF, SLIPY, SLIPZ], 1.5, glint);\n      line([-FX, BASE, SLIPZ], [FX, BASE, SLIPZ], 1.2, \"rgba(0,0,0,0.9)\");\n\n      // Brass lock plate in the middle of the fall.\n      const [ex, ey] = pt([0, FALL - 22, LEN]), r = Math.abs(pt([0, FALL - 22, LEN])[1] - pt([0, FALL - 29, LEN])[1]);\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.85)\"; ctx.shadowBlur = 3 * k; ctx.shadowOffsetY = 1.5 * k; ctx.shadowOffsetX = 1 * k;\n      ctx.beginPath(); ctx.ellipse(ex, ey, r * 0.68, r, 0, 0, Math.PI * 2);\n      const br = ctx.createRadialGradient(ex - r * 0.25, ey - r * 0.45, 0.5, ex, ey, r * 1.1); br.addColorStop(0, \"#fff3c4\"); br.addColorStop(0.4, \"#d0a03c\"); br.addColorStop(1, \"#6a4810\");\n      ctx.fillStyle = br; ctx.fill(); ctx.restore();\n      ctx.fillStyle = \"#130c02\"; ctx.beginPath(); ctx.arc(ex, ey - r * 0.22, r * 0.22, 0, Math.PI * 2); ctx.fill(); ctx.fillRect(ex - r * 0.1, ey - r * 0.2, r * 0.2, r * 0.66);\n\n      if (s.focus >= 0) {\n        const i = s.focus, g = layout[i];\n        const outline: V[] = keys[i].sharp\n          ? [[g.x - BT, BH - drop(i, BZ), BZ + 7], [g.x + BT, BH - drop(i, BZ), BZ + 7], [g.x + BT, BH - drop(i, LEN), LEN - 10], [g.x - BT, BH - drop(i, LEN), LEN - 10]]\n          : [[g.x + 1.6, -drop(i, 0), 1.5], [g.x + WK - 1.6, -drop(i, 0), 1.5], [g.x + WK - 1.6, -drop(i, BZ), BZ - 4], [g.x + 1.6, -drop(i, BZ), BZ - 4]];\n        ctx.save(); ctx.shadowColor = RING; ctx.shadowBlur = 8 * k; path(outline); ctx.lineJoin = \"round\"; ctx.lineWidth = 3; ctx.strokeStyle = RING; ctx.stroke(); ctx.restore();\n      }\n    };\n\n    let raf = 0, last = 0, seen = true, touched = -1e9, nextDemo = 900, t0 = 0, step = 0;\n    const select = (i: number) => { if (valueRef.current !== i) { valueRef.current = i; setValue(i); } };\n    const settle = () => { s.p = keys.map((_, i) => (i === valueRef.current ? 1 : 0)); s.v = keys.map(() => 0); draw(); };\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 > 5000 && t > nextDemo && !s.drag) { select(DEMO[step++ % DEMO.length]); nextDemo = t + 1500; }\n      keys.forEach((_, i) => {\n        const to = i === valueRef.current ? 1 : i === s.hover ? 0.14 : 0, down = to > s.p[i];\n        // Going down a key is pushed and stops dead on its felt. Coming up it is thrown by its own weight and settles.\n        for (let n = 0; n < 3; n++) {\n          s.v[i] += ((down ? 1300 : 560) * (to - s.p[i]) - (down ? 34 : 21) * s.v[i]) * (dt / 3); s.p[i] += s.v[i] * (dt / 3);\n          if (s.p[i] > 1) { s.p[i] = 1; s.v[i] *= -0.22; }\n        }\n      });\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    api.current = {\n      go: (i) => { touched = performance.now(); select(i); tone(i); if (reduce) settle(); else wake(); },\n      focus: (i) => { s.focus = i; if (reduce) draw(); },\n    };\n\n    const fit = () => { k = (cv.clientWidth / CW) * dpr; cv.width = Math.round(CW * k); cv.height = Math.round(CH2 * k); draw(); };\n    fit();\n    if (reduce) settle(); else wake();\n    const ro = new ResizeObserver(fit); ro.observe(cv);\n\n    const inside = (x: number, y: number, vs: V[]) => {\n      let hit = false;\n      for (let a = 0, b = vs.length - 1; a < vs.length; b = a++) { const [xa, ya] = pt(vs[a]), [xb, yb] = pt(vs[b]); if (ya > y !== yb > y && x < ((xb - xa) * (y - ya)) / (yb - ya) + xa) hit = !hit; }\n      return hit;\n    };\n    const at = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect(), x = ((e.clientX - b.left) / b.width) * CW, y = ((e.clientY - b.top) / b.height) * CH2;\n      for (let i = 0; i < keys.length; i++) { const g = layout[i]; if (keys[i].sharp && inside(x, y, [[g.x - BW, 0, BZ], [g.x + BW, 0, BZ], [g.x + BT, BH, LEN], [g.x - BT, BH, LEN]])) return i; }\n      for (let i = 0; i < keys.length; i++) { const g = layout[i]; if (!keys[i].sharp && inside(x, y, [[g.x, -24, 0], [g.x + WK, -24, 0], [g.x + WK, 0, LEN], [g.x, 0, LEN]])) return i; }\n      return -1;\n    };\n    const onDown = (e: PointerEvent) => {\n      const i = at(e); if (i < 0) return;\n      s.drag = true; s.hover = -1; box.setPointerCapture(e.pointerId); api.current?.go(i);\n    };\n    const onMove = (e: PointerEvent) => {\n      const i = at(e);\n      if (s.drag) { if (i >= 0 && i !== valueRef.current) api.current?.go(i); return; } // slide across the keys like a glissando\n      if (e.pointerType === \"mouse\" && i !== s.hover) { s.hover = i; touched = Math.max(touched, performance.now() - 3500); }\n    };\n    const onUp = () => { s.drag = false; };\n    const onLeave = () => { s.hover = -1; };\n    box.addEventListener(\"pointerdown\", onDown); box.addEventListener(\"pointermove\", onMove);\n    box.addEventListener(\"pointerup\", onUp); box.addEventListener(\"pointercancel\", onUp); box.addEventListener(\"pointerleave\", onLeave);\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      box.removeEventListener(\"pointerdown\", onDown); box.removeEventListener(\"pointermove\", onMove);\n      box.removeEventListener(\"pointerup\", onUp); box.removeEventListener(\"pointercancel\", onUp); box.removeEventListener(\"pointerleave\", onLeave);\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const choose = (i: number) => { valueRef.current = i; setValue(i); api.current?.go(i); };\n  const onKey = (e: ReactKeyboardEvent, i: number) => {\n    const next = e.key === \"ArrowRight\" || e.key === \"ArrowUp\" ? (i + 1) % keys.length : e.key === \"ArrowLeft\" || e.key === \"ArrowDown\" ? (i + keys.length - 1) % keys.length : e.key === \"Home\" ? 0 : e.key === \"End\" ? keys.length - 1 : -1;\n    if (next < 0) return;\n    e.preventDefault(); choose(next); radios.current[next]?.focus();\n  };\n  const pc = (n: number) => `${n * 100}%`;\n\n  return (\n    <div className=\"flex min-h-[600px] w-full flex-col items-center justify-center overflow-hidden bg-[#a9472f] bg-[radial-gradient(110%_80%_at_50%_34%,#c25a3c_0%,#a9472f_55%,#933a25_100%)] px-2 py-10 sm:min-h-[780px]\">\n      <p className=\"mb-2 font-mono text-[11px] font-semibold uppercase tracking-[0.28em] text-[#ffe9dc] sm:mb-4\">Key signature</p>\n      <div ref={wrap} className=\"relative aspect-[600/640] w-full max-w-[590px] touch-pan-y select-none\">\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute left-[-10%] top-0 h-full w-[120%] max-w-none\" />\n        <div role=\"radiogroup\" aria-label=\"Key signature\" className=\"absolute left-[-10%] top-0 h-full w-[120%]\">\n          {keys.map((key, i) => (\n            <button\n              key={key.label}\n              ref={(el) => { radios.current[i] = el; }}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={value === i}\n              aria-label={`${key.label} major, ${key.note.toLowerCase()}`}\n              tabIndex={value === i ? 0 : -1}\n              onClick={() => choose(i)}\n              onKeyDown={(e) => onKey(e, i)}\n              onFocus={(e) => api.current?.focus(e.currentTarget.matches(\":focus-visible\") ? i : -1)}\n              onBlur={() => api.current?.focus(-1)}\n              style={{ left: pc(boxes[i].left), top: pc(boxes[i].top), width: pc(boxes[i].width), height: pc(boxes[i].height) }}\n              className={`absolute cursor-pointer bg-transparent outline-none ${key.sharp ? \"z-10\" : \"\"}`}\n            />\n          ))}\n        </div>\n      </div>\n      <p aria-live=\"polite\" className=\"mt-1 min-h-[20px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.16em] text-[#ffd9c7] sm:mt-3\">\n        <span className=\"font-semibold text-white\">{keys[value].label} major</span>\n        <span className=\"mx-2 text-[#f0a98d]\">/</span>\n        {keys[value].note}\n      </p>\n    </div>\n  );\n}\n"}]}
