{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"wooden-marble-run-where-glass-marbles-roll-down-zigzag-ramps-and-ring-a-brass-bell","type":"registry:component","title":"Wooden Marble Run Where Glass Marbles Roll Down Zigzag Ramps and Ring a Brass Bell","description":"Canvas marble run: glass marbles roll down zigzag ramps, ring a hanging brass bell and gather in a tray. Drag one to drop it. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/wooden-marble-run-where-glass-marbles-roll-down-zigzag-ramps-and-ring-a-brass-bell.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// The marbles: glass tint and the colour of the twisted vane inside. Add or remove entries freely.\nconst marbles = [\n  { glass: \"36,92,214\", vane: \"#f6f1e3\" },\n  { glass: \"226,150,24\", vane: \"#b3261e\" },\n  { glass: \"22,140,98\", vane: \"#f3d34a\" },\n  { glass: \"196,38,52\", vane: \"#fbe9d0\" },\n  { glass: \"150,176,196\", vane: \"#1f4fd1\" },\n  { glass: \"118,70,196\", vane: \"#ffb84d\" },\n];\n/** Each ramp is the top edge of a beech rail, from its high end to its low end. */\nconst ramps: [number, number, number, number][] = [\n  [-176, 150, 118, 196],\n  [176, 280, -118, 326],\n  [-176, 410, 62, 447],\n];\nconst GRAVITY = 1250; // pixels a second squared\nconst BOUNCE = 0.34; // how much speed a marble keeps when it lands on wood\nconst RAD = 18; // marble radius\n/** Paint the low end of each rail is dipped in, one per rail. */\nconst DIPS = [\"#d8342c\", \"#f0b429\", \"#1f8f86\"];\nconst STAMP = \"No. 4  BEECH & BRASS\";\n\nconst WALL = 188; // inner face of the side rails\nconst FLOOR: [number, number, number, number] = [-WALL, 572, WALL, 552]; // the felt tray, tipped a little to the left\nconst PIVOT = { x: 132, y: 446 }, ARM = 42, BELL = 24;\nconst START = { x: -146, y: 104 };\n\nexport default function WoodenMarbleRun() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const api = useRef<{ drop: () => void } | null>(null);\n  const [rings, setRings] = useState(0);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = hit.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    let dpr = 1, scale = 1, ox = 0, oy = 0, box = cv.getBoundingClientRect();\n    let back: HTMLCanvasElement | null = null;\n    let seed = 3;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    // Rails as fat line segments: the centre line sits 8 below the top edge.\n    const mount = ([ax, ay, bx, by]: [number, number, number, number]) => {\n      const len = Math.hypot(bx - ax, by - ay), ux = (bx - ax) / len, uy = (by - ay) / len;\n      const nx = ux > 0 ? -uy : uy, ny = ux > 0 ? ux : -ux; // points into the wood\n      return { ax: ax + nx * 8, ay: ay + ny * 8, bx: bx + nx * 8, by: by + ny * 8, len, angle: Math.atan2(by - ay, bx - ax), tx: ax, ty: ay };\n    };\n    const rails = ramps.map(mount);\n    const tray = mount([FLOOR[2], FLOOR[3], FLOOR[0], FLOOR[1]]);\n\n    type Marble = { x: number; y: number; vx: number; vy: number; rot: number; mode: \"free\" | \"held\" | \"carried\"; q: number; fx: number; fy: number; lift: number; idle: number; i: number };\n    // Start mid-play: three on the rails, three waiting in the tray.\n    const seedSpots: [number, number, number, number][] = [[-60, 152, 150, 24], [40, 286, -210, 32], [-40, 414, 230, 36], [-168, 552, 0, 0], [-130, 550, 0, 0], [-92, 548, 0, 0]];\n    const balls: Marble[] = marbles.map((_, i) => {\n      const p = seedSpots[i % seedSpots.length];\n      return { x: p[0], y: p[1], vx: p[2], vy: p[3], rot: i * 1.3, mode: \"free\", q: 0, fx: 0, fy: 0, lift: 0, idle: i * 0.4, i };\n    });\n    const bell = { a: 0, w: 0, ring: 0 };\n    let count = 0;\n\n    const bake = (paint: (c: CanvasRenderingContext2D) => void) => {\n      const off = document.createElement(\"canvas\");\n      const k = dpr * scale;\n      off.width = Math.max(1, Math.ceil(520 * k)); off.height = Math.max(1, Math.ceil(660 * k));\n      const c = off.getContext(\"2d\");\n      if (c) { c.setTransform(k, 0, 0, k, 260 * k, 0); paint(c); }\n      return off;\n    };\n\n    // A length of timber lying along x: gradient across the grain, then the grain itself.\n    const timber = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, rad: number, light: string, dark: string, lines: number, tone: string) => {\n      c.save();\n      c.beginPath(); c.roundRect(x, y, w, h, rad); c.clip();\n      const g = c.createLinearGradient(0, y, 0, y + h);\n      g.addColorStop(0, light); g.addColorStop(1, dark);\n      c.fillStyle = g; c.fillRect(x, y, w, h);\n      for (let i = 0; i < lines; i++) {\n        const gy = y + rand() * h, wob = (rand() - 0.5) * 5;\n        c.beginPath(); c.moveTo(x, gy); c.bezierCurveTo(x + w * 0.3, gy + wob, x + w * 0.66, gy - wob, x + w, gy + wob * 0.5);\n        c.lineWidth = 0.4 + rand() * 0.9; c.strokeStyle = `rgba(${tone},${0.05 + rand() * 0.16})`; c.stroke();\n      }\n      c.restore();\n    };\n    const screw = (c: CanvasRenderingContext2D, x: number, y: number, turn: number) => {\n      c.beginPath(); c.arc(x, y + 0.8, 3.6, 0, Math.PI * 2); c.fillStyle = \"rgba(40,22,6,0.5)\"; c.fill();\n      const g = c.createRadialGradient(x - 1, y - 1.2, 0.3, x, y, 3.4);\n      g.addColorStop(0, \"#fff3c4\"); g.addColorStop(0.5, \"#cfa046\"); g.addColorStop(1, \"#7a5616\");\n      c.beginPath(); c.arc(x, y, 3.2, 0, Math.PI * 2); c.fillStyle = g; c.fill();\n      c.save(); c.translate(x, y); c.rotate(turn); c.fillStyle = \"rgba(50,30,6,0.85)\"; c.fillRect(-2.6, -0.5, 5.2, 1); c.restore();\n    };\n\n    const paintBack = (c: CanvasRenderingContext2D) => {\n      // The birch board, with the plies showing along its lower edge.\n      c.save(); c.shadowColor = \"rgba(8,18,60,0.5)\"; c.shadowBlur = 40; c.shadowOffsetX = 14 * dpr * scale; c.shadowOffsetY = 26 * dpr * scale;\n      c.beginPath(); c.roundRect(-214, 40, 428, 572, 20); c.fillStyle = \"#8a6a3c\"; c.fill(); c.restore();\n      c.save(); c.beginPath(); c.roundRect(-214, 40, 428, 572, 20); c.clip();\n      for (let i = 0; i < 6; i++) { c.fillStyle = i % 2 ? \"#c9a56a\" : \"#8d6936\"; c.fillRect(-214, 598 + i * 2.4, 428, 2.4); }\n      c.restore();\n      timber(c, -214, 40, 428, 560, 20, \"#f0dcb4\", \"#e2c794\", 0, \"0,0,0\");\n      c.save(); c.beginPath(); c.roundRect(-214, 40, 428, 560, 20); c.clip();\n      for (let i = 0; i < 170; i++) {\n        const gx = -214 + rand() * 428, wob = (rand() - 0.5) * 9;\n        c.beginPath(); c.moveTo(gx, 40); c.bezierCurveTo(gx + wob, 220, gx - wob, 400, gx + wob * 0.6, 600);\n        c.lineWidth = 0.5 + rand() * 1.5; c.strokeStyle = rand() > 0.3 ? `rgba(150,104,48,${0.04 + rand() * 0.1})` : `rgba(255,250,232,${0.08 + rand() * 0.14})`; c.stroke();\n      }\n      const sheen = c.createLinearGradient(-214, 40, 214, 600);\n      sheen.addColorStop(0, \"rgba(255,255,255,0.2)\"); sheen.addColorStop(0.5, \"rgba(255,255,255,0)\"); sheen.addColorStop(1, \"rgba(90,50,10,0.14)\");\n      c.fillStyle = sheen; c.fillRect(-214, 40, 428, 560);\n      c.restore();\n      c.lineWidth = 1.2; c.strokeStyle = \"rgba(255,252,236,0.7)\"; c.beginPath(); c.roundRect(-213.4, 40.6, 426.8, 559, 19.4); c.stroke();\n\n      // Branded into the board with a hot iron.\n      c.save(); c.font = \"700 9px ui-monospace, SFMono-Regular, Menlo, monospace\"; c.textAlign = \"right\"; c.textBaseline = \"middle\";\n      const brand = STAMP.split(\"\").join(\"\\u200A\"), bw = c.measureText(brand).width;\n      c.fillStyle = \"rgba(94,52,16,0.62)\"; c.fillText(brand, 170, 88);\n      c.beginPath(); c.arc(170 - bw - 16, 88, 7, 0, Math.PI * 2); c.lineWidth = 1.4; c.strokeStyle = \"rgba(94,52,16,0.55)\"; c.stroke();\n      c.beginPath(); c.arc(170 - bw - 16, 88, 2.4, 0, Math.PI * 2); c.fill();\n      c.restore();\n\n      // Side rails in walnut, each throwing a shadow onto the board.\n      for (const side of [-1, 1]) {\n        c.save(); c.shadowColor = \"rgba(60,30,6,0.5)\"; c.shadowBlur = 10; c.shadowOffsetX = 4 * dpr * scale; c.shadowOffsetY = 6 * dpr * scale;\n        c.beginPath(); c.roundRect(side < 0 ? -214 : WALL, 40, 26, 572, 9); c.fillStyle = \"#5a3520\"; c.fill(); c.restore();\n        c.save(); c.translate(side < 0 ? -214 : WALL, 612); c.rotate(-Math.PI / 2);\n        timber(c, 0, 0, 572, 26, 9, \"#7d4c2c\", \"#4b2a17\", 40, \"20,8,2\");\n        const edge = c.createLinearGradient(0, 0, 0, 26); edge.addColorStop(0, \"rgba(255,220,180,0.38)\"); edge.addColorStop(0.16, \"rgba(255,220,180,0)\"); edge.addColorStop(0.8, \"rgba(0,0,0,0)\"); edge.addColorStop(1, \"rgba(0,0,0,0.4)\");\n        c.fillStyle = edge; c.beginPath(); c.roundRect(0, 0, 572, 26, 9); c.fill();\n        c.restore();\n        screw(c, side * 201, 70, 0.6 * side); screw(c, side * 201, 330, 2.1); screw(c, side * 201, 584, 1.2 * side);\n      }\n\n      // The rails the marbles ride, seen a little from above: a pale routed top and a darker front face.\n      [...rails, tray].forEach((r, i) => {\n        const felt = r === tray, x0 = felt ? 0 : -10, w = felt ? r.len : r.len + 18;\n        c.save(); c.translate(r.tx, r.ty); c.rotate(r.angle);\n        if (Math.cos(r.angle) < 0) c.scale(1, -1);\n        c.save(); c.shadowColor = \"rgba(70,36,6,0.6)\"; c.shadowBlur = 14; c.shadowOffsetX = 6 * dpr * scale; c.shadowOffsetY = 11 * dpr * scale;\n        c.beginPath(); c.roundRect(x0, -4, w, 26, 5); c.fillStyle = \"#b98a52\"; c.fill(); c.restore();\n        timber(c, x0, 2, w, 20, 5, \"#e9c48a\", \"#b8854a\", 18, \"110,60,14\");\n        const face = c.createLinearGradient(0, 2, 0, 22); face.addColorStop(0, \"rgba(255,240,205,0.35)\"); face.addColorStop(0.3, \"rgba(255,240,205,0)\"); face.addColorStop(0.8, \"rgba(60,28,4,0)\"); face.addColorStop(1, \"rgba(60,28,4,0.45)\");\n        c.fillStyle = face; c.beginPath(); c.roundRect(x0, 2, w, 20, 5); c.fill();\n        // Top face.\n        c.beginPath(); c.roundRect(x0, -5, w, 9, 4);\n        if (felt) {\n          const g = c.createLinearGradient(0, -5, 0, 4); g.addColorStop(0, \"#2f7a60\"); g.addColorStop(1, \"#1c5241\"); c.fillStyle = g; c.fill();\n          c.save(); c.clip();\n          for (let n = 0; n < 500; n++) { c.fillStyle = `rgba(${rand() > 0.5 ? \"200,246,222\" : \"0,24,14\"},${rand() * 0.2})`; c.fillRect(x0 + rand() * w, -5 + rand() * 9, 1.1, 1.1); }\n          c.restore();\n        } else {\n          const g = c.createLinearGradient(0, -5, 0, 4); g.addColorStop(0, \"#f3ddb0\"); g.addColorStop(1, \"#fdeecb\"); c.fillStyle = g; c.fill();\n          c.fillStyle = \"rgba(120,70,20,0.3)\"; c.fillRect(x0 + 5, -2.2, w - 10, 2.4);\n          c.fillStyle = \"rgba(255,252,238,0.9)\"; c.fillRect(x0 + 5, 0.2, w - 10, 0.9);\n        }\n        c.fillStyle = \"rgba(255,250,232,0.8)\"; c.fillRect(x0 + 3, 3.6, w - 6, 0.9);\n        if (!felt) {\n          // The dipped end: glossy paint over the last stretch of the rail, top and front.\n          const dx = x0 + w - 30;\n          c.save(); c.beginPath(); c.roundRect(x0, -5, w, 27, 5); c.clip();\n          c.fillStyle = DIPS[i % DIPS.length]; c.fillRect(dx, -6, 31, 29);\n          const gloss = c.createLinearGradient(0, -5, 0, 22);\n          gloss.addColorStop(0, \"rgba(255,255,255,0.5)\"); gloss.addColorStop(0.2, \"rgba(255,255,255,0.12)\"); gloss.addColorStop(0.3, \"rgba(0,0,0,0.12)\"); gloss.addColorStop(0.42, \"rgba(255,255,255,0.3)\"); gloss.addColorStop(0.6, \"rgba(255,255,255,0)\"); gloss.addColorStop(1, \"rgba(0,0,0,0.4)\");\n          c.fillStyle = gloss; c.fillRect(dx, -6, 31, 29);\n          c.fillStyle = \"rgba(0,0,0,0.22)\"; c.fillRect(dx - 0.8, -6, 0.8, 29);\n          c.restore();\n        }\n        c.restore();\n        if (felt) return;\n        const along = (d: number) => ({ x: r.ax + (r.bx - r.ax) * d, y: r.ay + (r.by - r.ay) * d + 5 });\n        const p0 = along(0.06), p1 = along(0.8);\n        screw(c, p0.x, p0.y, 0.4 + i); screw(c, p1.x, p1.y, 1.7 + i);\n      });\n\n      // The arm the bell hangs from.\n      c.save(); c.shadowColor = \"rgba(70,36,6,0.5)\"; c.shadowBlur = 8; c.shadowOffsetX = 4 * dpr * scale; c.shadowOffsetY = 7 * dpr * scale;\n      const armG = c.createLinearGradient(0, PIVOT.y - 12, 0, PIVOT.y - 4); armG.addColorStop(0, \"#f7e2a0\"); armG.addColorStop(0.5, \"#c59a3c\"); armG.addColorStop(1, \"#7b5718\");\n      c.beginPath(); c.roundRect(PIVOT.x - 7, PIVOT.y - 13, WALL - PIVOT.x + 9, 9, 4.5); c.fillStyle = armG; c.fill(); c.restore();\n    };\n\n    const drawMarble = (m: Marble) => {\n      const col = marbles[m.i], r = RAD * (1 + m.lift * 0.22), off = 1 + m.lift * 2.4;\n      // Shadow on the board, with the bright spot glass focuses into it.\n      const sx = m.x + 6 * off, sy = m.y + 10 * off;\n      const sh = ctx.createRadialGradient(sx, sy, r * 0.2, sx, sy, r * (1.25 + m.lift * 0.5));\n      sh.addColorStop(0, `rgba(60,30,6,${0.5 - m.lift * 0.22})`); sh.addColorStop(0.6, `rgba(60,30,6,${0.3 - m.lift * 0.15})`); sh.addColorStop(1, \"rgba(60,30,6,0)\");\n      ctx.fillStyle = sh; ctx.fillRect(sx - r * 2, sy - r * 2, r * 4, r * 4);\n      const cz = ctx.createRadialGradient(sx + 1, sy + 1, 0, sx + 1, sy + 1, r * 0.55);\n      cz.addColorStop(0, `rgba(${col.glass},0.6)`); cz.addColorStop(0.4, \"rgba(255,240,200,0.3)\"); cz.addColorStop(1, \"rgba(255,240,200,0)\");\n      ctx.fillStyle = cz; ctx.fillRect(sx - r, sy - r, r * 2, r * 2);\n\n      ctx.save(); ctx.translate(m.x, m.y);\n      ctx.beginPath(); ctx.arc(0, 0, r, 0, Math.PI * 2);\n      const body = ctx.createRadialGradient(-r * 0.3, -r * 0.36, r * 0.05, 0, 0, r);\n      body.addColorStop(0, `rgba(${col.glass},0.42)`); body.addColorStop(0.55, `rgba(${col.glass},0.74)`); body.addColorStop(0.88, `rgba(${col.glass},0.96)`); body.addColorStop(1, \"rgba(8,10,24,0.92)\");\n      ctx.fillStyle = \"rgba(255,255,255,0.3)\"; ctx.fill(); ctx.fillStyle = body; ctx.fill();\n      ctx.save(); ctx.clip();\n      // The cat's-eye vane, turning as the marble rolls.\n      ctx.rotate(m.rot);\n      // One broad ribbon bent into an S, with a thin second colour laid along it.\n      ctx.beginPath(); ctx.moveTo(-r * 0.72, r * 0.1); ctx.bezierCurveTo(-r * 0.4, -r * 0.62, r * 0.1, -r * 0.1, r * 0.72, -r * 0.1);\n      ctx.bezierCurveTo(r * 0.4, r * 0.62, -r * 0.1, r * 0.1, -r * 0.72, r * 0.1); ctx.closePath();\n      ctx.fillStyle = col.vane; ctx.globalAlpha = 0.94; ctx.fill();\n      ctx.beginPath(); ctx.moveTo(-r * 0.6, r * 0.04); ctx.bezierCurveTo(-r * 0.3, -r * 0.3, r * 0.1, r * 0.2, r * 0.6, -r * 0.04);\n      ctx.lineWidth = r * 0.11; ctx.lineCap = \"round\"; ctx.strokeStyle = `rgb(${col.glass})`; ctx.globalAlpha = 0.75; ctx.stroke();\n      ctx.globalAlpha = 1; ctx.rotate(-m.rot);\n      // Light gathered on the far side of the glass.\n      ctx.globalCompositeOperation = \"lighter\";\n      const glow = ctx.createRadialGradient(r * 0.38, r * 0.44, 0, r * 0.38, r * 0.44, r * 0.72);\n      glow.addColorStop(0, \"rgba(255,255,255,0.62)\"); glow.addColorStop(0.5, `rgba(${col.glass},0.3)`); glow.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = glow; ctx.fillRect(-r, -r, r * 2, r * 2);\n      ctx.globalCompositeOperation = \"source-over\";\n      const shade = ctx.createRadialGradient(0, 0, r * 0.74, 0, 0, r);\n      shade.addColorStop(0, \"rgba(0,0,0,0)\"); shade.addColorStop(1, \"rgba(0,0,20,0.42)\");\n      ctx.fillStyle = shade; ctx.fillRect(-r, -r, r * 2, r * 2);\n      ctx.restore();\n      // Window reflection and a pin of light.\n      ctx.beginPath(); ctx.ellipse(-r * 0.36, -r * 0.44, r * 0.34, r * 0.2, -0.7, 0, Math.PI * 2);\n      const spec = ctx.createLinearGradient(0, -r * 0.8, 0, -r * 0.1); spec.addColorStop(0, \"rgba(255,255,255,0.98)\"); spec.addColorStop(1, \"rgba(255,255,255,0.3)\");\n      ctx.fillStyle = spec; ctx.fill();\n      ctx.beginPath(); ctx.arc(r * 0.5, r * 0.56, r * 0.07, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,255,255,0.85)\"; ctx.fill();\n      ctx.beginPath(); ctx.arc(0, 0, r - 0.5, 0.4, 1.9); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.4)\"; ctx.stroke();\n      ctx.restore();\n    };\n\n    const drawBell = () => {\n      const bx = PIVOT.x + Math.sin(bell.a) * ARM, by = PIVOT.y + Math.cos(bell.a) * ARM;\n      // Shadow first, then the leather loop, then the bell turned about its hook.\n      ctx.save(); ctx.translate(PIVOT.x + 6, PIVOT.y + 10); ctx.rotate(-bell.a);\n      const sh = ctx.createRadialGradient(0, ARM, 4, 0, ARM, 34);\n      sh.addColorStop(0, \"rgba(60,30,6,0.5)\"); sh.addColorStop(1, \"rgba(60,30,6,0)\"); ctx.fillStyle = sh; ctx.fillRect(-36, 0, 72, 76); ctx.restore();\n      if (bell.ring > 0.02) {\n        ctx.save(); ctx.lineCap = \"round\";\n        for (let i = 0; i < 3; i++) {\n          const q = ((1 - bell.ring) * 1.4 + i * 0.22) % 1, rr = BELL + 10 + q * 34;\n          ctx.globalAlpha = bell.ring * (1 - q) * 0.8; ctx.lineWidth = 2.2; ctx.strokeStyle = \"#8a5a12\";\n          ctx.beginPath(); ctx.arc(bx, by, rr, -0.55, 0.55); ctx.stroke(); ctx.beginPath(); ctx.arc(bx, by, rr, Math.PI - 0.55, Math.PI + 0.55); ctx.stroke();\n        }\n        ctx.restore();\n      }\n      ctx.save(); ctx.translate(PIVOT.x, PIVOT.y - 8); ctx.rotate(-bell.a);\n      ctx.lineWidth = 3; ctx.strokeStyle = \"#3b2414\"; ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(0, ARM - BELL + 8); ctx.stroke();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,210,170,0.35)\"; ctx.beginPath(); ctx.moveTo(-0.8, 1); ctx.lineTo(-0.8, ARM - BELL + 6); ctx.stroke();\n      const top = ARM - BELL + 4, R2 = BELL + 2;\n      // Clapper.\n      ctx.beginPath(); ctx.arc(bell.w * 1.6, top + R2 * 1.72, 4.4, 0, Math.PI * 2); ctx.fillStyle = \"#5a3d10\"; ctx.fill();\n      ctx.beginPath(); ctx.arc(bell.w * 1.6 - 1.2, top + R2 * 1.72 - 1.3, 1.4, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,240,190,0.8)\"; ctx.fill();\n      const brass = ctx.createLinearGradient(-R2 * 1.1, 0, R2 * 1.1, 0);\n      const stops: [number, string][] = [[0, \"#4d3408\"], [0.12, \"#9a7424\"], [0.28, \"#f3d98c\"], [0.35, \"#fffbe0\"], [0.44, \"#d9b352\"], [0.68, \"#8a6418\"], [0.9, \"#553a0a\"], [1, \"#b48d36\"]];\n      for (const [p, col] of stops) brass.addColorStop(p, col);\n      ctx.beginPath(); ctx.moveTo(-5, top);\n      ctx.bezierCurveTo(-R2 * 0.72, top + 2, -R2 * 0.7, top + R2 * 0.9, -R2 * 0.92, top + R2 * 1.3);\n      ctx.quadraticCurveTo(-R2 * 1.02, top + R2 * 1.52, -R2 * 1.14, top + R2 * 1.6);\n      ctx.quadraticCurveTo(0, top + R2 * 1.84, R2 * 1.14, top + R2 * 1.6);\n      ctx.quadraticCurveTo(R2 * 1.02, top + R2 * 1.52, R2 * 0.92, top + R2 * 1.3);\n      ctx.bezierCurveTo(R2 * 0.7, top + R2 * 0.9, R2 * 0.72, top + 2, 5, top); ctx.closePath();\n      ctx.fillStyle = brass; ctx.fill();\n      ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(50,30,2,0.6)\"; ctx.stroke();\n      const under = ctx.createLinearGradient(0, top + R2 * 1.2, 0, top + R2 * 1.8); under.addColorStop(0, \"rgba(40,24,2,0)\"); under.addColorStop(1, \"rgba(40,24,2,0.45)\");\n      ctx.fillStyle = under; ctx.fill();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(60,36,4,0.45)\";\n      ctx.beginPath(); ctx.moveTo(-R2 * 0.8, top + R2 * 1.02); ctx.quadraticCurveTo(0, top + R2 * 1.16, R2 * 0.8, top + R2 * 1.02); ctx.stroke();\n      ctx.strokeStyle = \"rgba(255,250,214,0.6)\";\n      ctx.beginPath(); ctx.moveTo(-R2 * 0.8, top + R2 * 1.08); ctx.quadraticCurveTo(0, top + R2 * 1.22, R2 * 0.8, top + R2 * 1.08); ctx.stroke();\n      ctx.beginPath(); ctx.arc(0, top, 5, 0, Math.PI * 2); ctx.fillStyle = \"#c59a3c\"; ctx.fill(); ctx.strokeStyle = \"rgba(50,30,2,0.6)\"; ctx.stroke();\n      ctx.restore();\n      ctx.beginPath(); ctx.arc(PIVOT.x, PIVOT.y - 8, 3, 0, Math.PI * 2); ctx.fillStyle = \"#fff0b8\"; ctx.fill(); ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(50,30,2,0.7)\"; ctx.stroke();\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, cv.width, cv.height);\n      const k = dpr * scale;\n      ctx.setTransform(k, 0, 0, k, ox * dpr, oy * dpr);\n      if (back) ctx.drawImage(back, -260, 0, 520, 660);\n      drawBell();\n      const order = [...balls].sort((a, b) => a.lift - b.lift);\n      for (const m of order) drawMarble(m);\n    };\n\n    const fit = () => {\n      box = cv.getBoundingClientRect();\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(box.width * dpr); cv.height = Math.round(box.height * dpr);\n      const bottom = box.width < 640 ? 100 : 74;\n      scale = Math.min(box.width / 470, (box.height - bottom) / 650);\n      ox = box.width / 2; oy = Math.max(0, (box.height - bottom - 650 * scale) / 2);\n      pad.style.left = `${ox - 214 * scale}px`; pad.style.top = `${oy + 40 * scale}px`; pad.style.width = `${428 * scale}px`; pad.style.height = `${572 * scale}px`;\n      seed = 3;\n      back = bake(paintBack);\n    };\n\n    const collideLine = (m: Marble, ax: number, ay: number, bx: number, by: number, thick: number, soft: boolean) => {\n      const ex = bx - ax, ey = by - ay, q = Math.max(0, Math.min(1, ((m.x - ax) * ex + (m.y - ay) * ey) / (ex * ex + ey * ey)));\n      const dx = m.x - (ax + ex * q), dy = m.y - (ay + ey * q), d = Math.hypot(dx, dy), min = RAD + thick;\n      if (d >= min || d === 0) return;\n      const nx = dx / d, ny = dy / d, vn = m.vx * nx + m.vy * ny;\n      m.x += nx * (min - d); m.y += ny * (min - d);\n      if (vn < 0) { const e = -vn > 90 ? (soft ? 0.12 : BOUNCE) : 0; m.vx -= (1 + e) * vn * nx; m.vy -= (1 + e) * vn * ny; }\n      if (soft) { m.vx *= 0.988; m.vy *= 0.988; }\n      m.rot += (m.vx * -ny + m.vy * nx) / RAD / 240;\n    };\n\n    let time = 0;\n    const step = (h: number) => {\n      time += h;\n      // The bell is a damped pendulum.\n      bell.w += (-58 * Math.sin(bell.a) - 1.5 * bell.w) * h; bell.a += bell.w * h;\n      bell.ring *= Math.exp(-h * 2.4);\n      const cb = Math.cos(bell.a), sb = Math.sin(bell.a), bx = PIVOT.x + sb * ARM, by = PIVOT.y + cb * ARM;\n      for (const m of balls) {\n        if (m.mode === \"carried\") {\n          // Lifted off the tray and carried to the top by an unseen hand.\n          m.q = Math.min(1, m.q + h / 0.8);\n          const e = m.q * m.q * (3 - 2 * m.q);\n          m.x = m.fx + (START.x - m.fx) * e; m.y = m.fy + (START.y - m.fy) * e - Math.sin(e * Math.PI) * 26;\n          m.lift = Math.sin(Math.min(1, m.q * 1.12) * Math.PI); m.rot += h * 3;\n          if (m.q >= 1) { m.mode = \"free\"; m.vx = 30; m.vy = 0; m.lift = 0; m.idle = 0; }\n          continue;\n        }\n        if (m.mode === \"held\") { m.lift += (1 - m.lift) * Math.min(1, h * 14); continue; }\n        m.lift += (0 - m.lift) * Math.min(1, h * 14);\n        m.vy += GRAVITY * h;\n        m.vx *= 1 - 0.1 * h; m.vy *= 1 - 0.1 * h;\n        m.x += m.vx * h; m.y += m.vy * h;\n        for (const r of rails) collideLine(m, r.ax, r.ay, r.bx, r.by, 8, false);\n        collideLine(m, FLOOR[0] - 40, FLOOR[1] + 2.1, FLOOR[2] + 40, FLOOR[3] - 2.1, 0, true);\n        if (m.x < -WALL + RAD) { m.x = -WALL + RAD; if (m.vx < 0) m.vx *= -0.4; }\n        if (m.x > WALL - RAD) { m.x = WALL - RAD; if (m.vx > 0) m.vx *= -0.4; }\n        if (m.y < 40 + RAD && m.vy < 0) m.vy *= -0.3;\n        // Striking the bell: the marble gives up some of its speed to the swing.\n        const dx = m.x - bx, dy = m.y - by, d = Math.hypot(dx, dy), min = RAD + BELL;\n        if (d < min && d > 0) {\n          const nx = dx / d, ny = dy / d, tn = cb * nx - sb * ny;\n          const vrel = (m.vx - ARM * bell.w * cb) * nx + (m.vy + ARM * bell.w * sb) * ny;\n          m.x += nx * (min - d); m.y += ny * (min - d);\n          if (vrel < 0) {\n            const j = (-(1 + 0.5) * vrel) / (1 + (tn * tn) / 1.5);\n            m.vx += j * nx; m.vy += j * ny; bell.w -= (j * tn) / (1.5 * ARM);\n            if (j > 170) { if (bell.ring < 0.3) { count += 1; setRings(count); } bell.ring = 1; }\n          }\n        }\n        const still = Math.hypot(m.vx, m.vy) < 26 && m.y > 520;\n        m.idle = still ? m.idle + h : 0;\n      }\n      for (let a = 0; a < balls.length; a++) for (let b = a + 1; b < balls.length; b++) {\n        const p = balls[a], q = balls[b];\n        if (p.lift > 0.3 || q.lift > 0.3) continue;\n        const dx = q.x - p.x, dy = q.y - p.y, d = Math.hypot(dx, dy);\n        if (d >= RAD * 2 || d === 0) continue;\n        const nx = dx / d, ny = dy / d, push = (RAD * 2 - d) / 2;\n        p.x -= nx * push; p.y -= ny * push; q.x += nx * push; q.y += ny * push;\n        const vn = (q.vx - p.vx) * nx + (q.vy - p.vy) * ny;\n        if (vn < 0) { const j = -(1 + 0.6) * vn / 2; p.vx -= j * nx; p.vy -= j * ny; q.vx += j * nx; q.vy += j * ny; }\n      }\n    };\n\n    const send = () => {\n      // The marble that has waited longest in the tray goes next.\n      let best: Marble | null = null;\n      for (const m of balls) if (m.mode === \"free\" && m.idle > 0.25 && (!best || m.idle > best.idle)) best = m;\n      if (!best) for (const m of balls) if (m.mode === \"free\" && m.y > 500 && (!best || m.y > best.y)) best = m;\n      if (!best) return false;\n      best.mode = \"carried\"; best.q = 0; best.fx = best.x; best.fy = best.y; best.idle = 0;\n      return true;\n    };\n\n    let raf = 0, last = 0, seen = true, touched = -1e9, nextDemo = 0.5;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      let dt = Math.min(0.05, (now - last) / 1000); last = now;\n      if (time > nextDemo && now - touched > 4000) { send(); nextDemo = time + 1.7; }\n      while (dt > 1e-5) { const h = Math.min(dt, 1 / 240); step(h); dt -= h; }\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = 0; raf = requestAnimationFrame(frame); } };\n\n    api.current = {\n      drop: () => {\n        touched = performance.now();\n        if (reduce) {\n          // Without motion: work out where everything ends up and show that.\n          if (send()) for (let i = 0; i < 240 * 9; i++) step(1 / 240);\n          draw(); return;\n        }\n        send(); wake();\n      },\n    };\n\n    const local = (e: PointerEvent) => ({ x: (e.clientX - box.left - ox) / scale, y: (e.clientY - box.top - oy) / scale });\n    let held: Marble | null = null, px = 0, py = 0, pt = 0;\n    const onDown = (e: PointerEvent) => {\n      box = cv.getBoundingClientRect();\n      const p = local(e);\n      let best: Marble | null = null, bd = RAD + 16;\n      for (const m of balls) { const d = Math.hypot(m.x - p.x, m.y - p.y); if (d < bd && m.mode !== \"carried\") { bd = d; best = m; } }\n      if (!best || reduce) return;\n      held = best; best.mode = \"held\"; best.vx = 0; best.vy = 0; px = p.x; py = p.y; pt = performance.now(); touched = pt;\n      pad.setPointerCapture(e.pointerId); e.preventDefault(); wake();\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!held) return;\n      const p = local(e), now = performance.now(), dt = Math.max(0.008, (now - pt) / 1000);\n      const x = Math.max(-WALL + RAD, Math.min(WALL - RAD, p.x)), y = Math.max(40 + RAD, Math.min(FLOOR[3] - RAD, p.y));\n      held.vx = held.vx * 0.5 + ((x - px) / dt) * 0.5; held.vy = held.vy * 0.5 + ((y - py) / dt) * 0.5;\n      held.rot += (x - px) / RAD;\n      held.x = x; held.y = y; px = x; py = y; pt = now; touched = now;\n    };\n    const onUp = () => {\n      if (!held) return;\n      const sp = Math.hypot(held.vx, held.vy), cap = 1100;\n      if (sp > cap) { held.vx *= cap / sp; held.vy *= cap / sp; }\n      if (performance.now() - pt > 90) { held.vx = 0; held.vy = 0; }\n      held.mode = \"free\"; held.idle = 0; held = null; touched = performance.now();\n    };\n    pad.addEventListener(\"pointerdown\", onDown);\n    pad.addEventListener(\"pointermove\", onMove);\n    pad.addEventListener(\"pointerup\", onUp);\n    pad.addEventListener(\"pointercancel\", onUp);\n\n    fit();\n    if (reduce) { for (let i = 0; i < 120; i++) step(1 / 240); draw(); } else wake();\n    const ro = new ResizeObserver(() => { fit(); if (reduce || !raf) draw(); });\n    ro.observe(cv);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      pad.removeEventListener(\"pointerdown\", onDown); pad.removeEventListener(\"pointermove\", onMove);\n      pad.removeEventListener(\"pointerup\", onUp); pad.removeEventListener(\"pointercancel\", onUp);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  return (\n    <div className=\"relative flex h-[620px] w-full flex-col overflow-hidden bg-[#3a5fc4] bg-[radial-gradient(100%_80%_at_42%_30%,#5c82e6_0%,#3a5fc4_55%,#2b4aa3_100%)] sm:h-[760px]\">\n      <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n      <div ref={hit} aria-hidden=\"true\" className=\"absolute cursor-grab touch-none active:cursor-grabbing\" />\n      <div className=\"pointer-events-none relative mx-auto mt-auto flex w-full max-w-[880px] flex-col items-center gap-3 px-5 pb-6 text-[13px] sm:flex-row sm:justify-between sm:pb-8\">\n        <p className=\"text-center text-blue-50 sm:text-left\">\n          <span className=\"font-semibold text-white\">Marble run</span>\n          <span className=\"mx-2 text-blue-200\">/</span>\n          pick up a marble and let go anywhere\n          <span aria-live=\"polite\" className=\"ml-3 inline-block min-w-[92px] rounded-full bg-[#213a86] px-3 py-1 text-center font-mono text-[11px] text-white\">\n            bell rung {rings}\n          </span>\n        </p>\n        <button\n          type=\"button\"\n          onClick={() => api.current?.drop()}\n          className=\"pointer-events-auto min-h-[40px] shrink-0 rounded-full bg-[#f6e3bd] px-5 font-medium text-[#3b2414] shadow-[0_12px_24px_-12px_rgba(10,20,70,0.8)] transition-transform hover:-translate-y-0.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-[#3a5fc4] motion-reduce:transition-none\"\n        >\n          Drop a marble\n        </button>\n      </div>\n    </div>\n  );\n}\n"}]}
