{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"steel-ball-bearing-loader-that-drops-a-ball-into-the-race-for-every-tenth-loaded","type":"registry:component","title":"Steel Ball Bearing Loader That Drops a Ball into the Race for Every Tenth Loaded","description":"Loader on a canvas: a steel bearing whose brass cage takes another ball for each tenth loaded, with the percentage in the bore. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/steel-ball-bearing-loader-that-drops-a-ball-into-the-race-for-every-tenth-loaded.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// What the loader says. The first is read while it runs, the second when it finishes.\nconst LABEL = \"Fetching your library\";\nconst DONE_LABEL = \"Library ready\";\nconst BALLS = 10; // one ball is seated for every 1 / BALLS of progress\nconst DEMO_SECONDS = 7; // length of the self-running demo\nconst SIZE = 340; // the bearing's box in CSS pixels\nconst PAD = 60; // extra canvas round the box so the shadow is never cut off\nconst STAGE = \"#2445d8\";\n\nexport default function SteelBallBearingLoader() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  // Write your own 0 to 1 value into this ref and delete the demo clock in the frame loop.\n  const progress = useRef(0);\n  const restart = useRef<(() => void) | null>(null);\n  const [pct, setPct] = useState(0);\n\n  useEffect(() => {\n    const cv = canvas.current;\n    if (!cv) 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) * 1.25;\n    const FULL = SIZE + PAD * 2;\n    cv.width = FULL * dpr; cv.height = FULL * dpr;\n    const C = FULL / 2, OUT = 152, SHOULDER_O = 113, SHOULDER_I = 89, BORE = 54, PITCH = 101, BALL = 14.5;\n    const hasConic = typeof ctx.createConicGradient === \"function\";\n    const TAU = Math.PI * 2;\n\n    // Turning marks for both rings, drawn once.\n    const marks = document.createElement(\"canvas\");\n    marks.width = cv.width; marks.height = cv.height;\n    const mx = marks.getContext(\"2d\");\n    if (mx) {\n      mx.scale(dpr, dpr);\n      let seed = 11;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let r = BORE; r < OUT; r += 0.6) {\n        const v = rand();\n        mx.beginPath(); mx.arc(C, C, r, 0, TAU);\n        mx.lineWidth = 0.5 + rand() * 0.4;\n        mx.strokeStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.34})` : `rgba(10,16,40,${(0.5 - v) * 0.24})`;\n        mx.stroke();\n      }\n    }\n\n    const s = { a: 0, v: 1.1, cage: 0.3, light: -2.25, lightTo: -2.25, drag: false, k: Array.from({ length: BALLS }, () => 0), kv: Array.from({ length: BALLS }, () => 0), seated: 0 };\n    // Steel that picks up the blue of the table in its dark bands.\n    const steel = (v: number) => { const c = Math.max(0, Math.min(255, v)); const k = Math.max(-40, 170 - c) * (c > 170 ? 0.03 : 0.16); return `rgb(${Math.max(0, c - k * 1.3) | 0},${Math.max(0, c - k * 0.3) | 0},${Math.min(255, c + k * 1.5) | 0})`; };\n    const sheen = (angle: number, lo: number, hi: number) => {\n      if (!hasConic) { const g = ctx.createLinearGradient(C - OUT, C - OUT, C + OUT, C + OUT); g.addColorStop(0, steel(hi)); g.addColorStop(0.5, steel(lo)); g.addColorStop(1, steel(hi)); return g; }\n      const g = ctx.createConicGradient(angle, C, C);\n      const mid = (lo + hi) / 2;\n      const stops: [number, number][] = [[0, hi], [0.035, hi - 6], [0.09, mid + 14], [0.17, lo + 6], [0.24, lo - 16], [0.32, mid - 18], [0.4, mid + 10], [0.455, hi - 34], [0.5, hi - 10], [0.545, hi - 40], [0.61, mid - 4], [0.7, lo - 10], [0.77, lo + 12], [0.85, mid - 12], [0.925, mid + 32], [0.97, hi - 8], [1, hi]];\n      for (const [p, v] of stops) g.addColorStop(p, steel(v));\n      return g;\n    };\n    const ring = (r0: number, r1: number) => { ctx.beginPath(); ctx.arc(C, C, r1, 0, TAU); ctx.arc(C, C, r0, 0, TAU, true); };\n    const etched = (text: string, r: number, start: number, size: number) => {\n      ctx.save(); ctx.translate(C, C); ctx.rotate(start - ((text.length - 1) * size * 0.74) / r / 2);\n      ctx.font = `600 ${size}px ui-monospace, SFMono-Regular, Menlo, monospace`; ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\";\n      for (const ch of text) {\n        ctx.fillStyle = \"rgba(255,255,255,0.55)\"; ctx.fillText(ch, 0.5, -r + 0.7);\n        ctx.fillStyle = \"rgba(24,30,52,0.72)\"; ctx.fillText(ch, 0, -r);\n        ctx.rotate((size * 0.74) / r);\n      }\n      ctx.restore();\n    };\n\n    const drawBall = (x: number, y: number, k: number, lx: number, ly: number) => {\n      const lift = Math.max(0, 1 - k), R = BALL * (1 + lift * 0.85), alpha = Math.max(0, Math.min(1, k * 7));\n      if (alpha <= 0) return;\n      ctx.save(); ctx.globalAlpha = alpha;\n      // Shadow on the race: far away while the ball is still in the air, tight once seated.\n      ctx.beginPath(); ctx.arc(x - lx * (3 + lift * 30), y - ly * (3 + lift * 30), BALL * (1 + lift * 0.3), 0, TAU);\n      ctx.fillStyle = `rgba(0,0,0,${0.5 - lift * 0.3})`; ctx.fill();\n      const g = ctx.createRadialGradient(x + lx * R * 0.42, y + ly * R * 0.42, R * 0.04, x, y, R);\n      g.addColorStop(0, \"#ffffff\"); g.addColorStop(0.2, \"#f6f8fb\"); g.addColorStop(0.48, \"#bcc3cf\"); g.addColorStop(0.74, \"#5a6070\"); g.addColorStop(0.91, \"#1d2029\"); g.addColorStop(1, \"#8f9cc0\");\n      ctx.beginPath(); ctx.arc(x, y, R, 0, TAU); ctx.fillStyle = g; ctx.fill();\n      // Brass of the cage thrown back off the far side of the ball.\n      ctx.save(); ctx.beginPath(); ctx.arc(x, y, R, 0, TAU); ctx.clip();\n      const b = ctx.createRadialGradient(x - lx * R * 0.8, y - ly * R * 0.8, 0, x - lx * R * 0.8, y - ly * R * 0.8, R * 0.85);\n      b.addColorStop(0, \"rgba(232,184,96,0.6)\"); b.addColorStop(1, \"rgba(232,184,96,0)\");\n      ctx.fillStyle = b; ctx.fillRect(x - R, y - R, R * 2, R * 2);\n      ctx.restore();\n      ctx.save(); ctx.translate(x + lx * R * 0.46, y + ly * R * 0.46); ctx.rotate(Math.atan2(ly, lx) + Math.PI / 2); ctx.scale(1, 0.55);\n      ctx.beginPath(); ctx.arc(0, 0, R * 0.2, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.95)\"; ctx.fill(); ctx.restore();\n      ctx.restore();\n    };\n\n    const draw = () => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, FULL, FULL);\n      const la = s.light, lx = Math.cos(la), ly = Math.sin(la);\n\n      // Shadow on the table: wide and soft, then tight where the steel touches.\n      ctx.save(); ctx.shadowColor = \"rgba(6,14,70,0.6)\"; ctx.shadowBlur = 34 * dpr; ctx.shadowOffsetX = -lx * 10 * dpr; ctx.shadowOffsetY = 20 * dpr;\n      ring(BORE, OUT - 4); ctx.fillStyle = \"#000\"; ctx.fill(\"evenodd\"); ctx.restore();\n      ctx.save(); ctx.shadowColor = \"rgba(4,10,56,0.8)\"; ctx.shadowBlur = 5 * dpr; ctx.shadowOffsetY = 3.5 * dpr;\n      ring(BORE, OUT - 1); ctx.fillStyle = \"#000\"; ctx.fill(\"evenodd\"); ctx.restore();\n\n      // The groove the balls run in.\n      const floor = ctx.createRadialGradient(C, C, SHOULDER_I - 2, C, C, SHOULDER_O + 2);\n      floor.addColorStop(0, \"#565c6c\"); floor.addColorStop(0.22, \"#101218\"); floor.addColorStop(0.78, \"#0d0f14\"); floor.addColorStop(1, \"#4b5162\");\n      ring(SHOULDER_I - 2, SHOULDER_O + 2); ctx.fillStyle = floor; ctx.fill(\"evenodd\");\n\n      // Pressed brass cage: a ribbon with a pocket for each ball and a rivet between pockets.\n      const ribbon = ctx.createRadialGradient(C, C, PITCH - 8, C, C, PITCH + 8);\n      ribbon.addColorStop(0, \"#5c3f12\"); ribbon.addColorStop(0.3, \"#c79a45\"); ribbon.addColorStop(0.5, \"#f1d48a\"); ribbon.addColorStop(0.72, \"#b98a38\"); ribbon.addColorStop(1, \"#553a10\");\n      ring(PITCH - 7.5, PITCH + 7.5); ctx.fillStyle = ribbon; ctx.fill(\"evenodd\");\n      for (let i = 0; i < BALLS; i++) {\n        const ang = s.cage + (i / BALLS) * TAU, x = C + Math.cos(ang) * PITCH, y = C + Math.sin(ang) * PITCH;\n        const pocket = ctx.createRadialGradient(x + lx * 5, y + ly * 5, BALL - 2, x, y, BALL + 5);\n        pocket.addColorStop(0, \"#f6dc97\"); pocket.addColorStop(0.55, \"#c0913d\"); pocket.addColorStop(1, \"#553a10\");\n        ctx.beginPath(); ctx.arc(x, y, BALL + 4.5, 0, TAU); ctx.fillStyle = pocket; ctx.fill();\n        const hole = ctx.createRadialGradient(x - lx * 4, y - ly * 4, 2, x, y, BALL + 1);\n        hole.addColorStop(0, \"#1a1c22\"); hole.addColorStop(1, \"#06070a\");\n        ctx.beginPath(); ctx.arc(x, y, BALL + 1, 0, TAU); ctx.fillStyle = hole; ctx.fill();\n        const mid = ang + Math.PI / BALLS, rx = C + Math.cos(mid) * PITCH, ry = C + Math.sin(mid) * PITCH;\n        ctx.beginPath(); ctx.arc(rx - lx * 0.8, ry - ly * 0.8, 3.1, 0, TAU); ctx.fillStyle = \"rgba(60,38,8,0.75)\"; ctx.fill();\n        const rivet = ctx.createRadialGradient(rx + lx * 1.1, ry + ly * 1.1, 0.2, rx, ry, 2.7);\n        rivet.addColorStop(0, \"#fff3c9\"); rivet.addColorStop(0.6, \"#c99b46\"); rivet.addColorStop(1, \"#6b4a17\");\n        ctx.beginPath(); ctx.arc(rx, ry, 2.6, 0, TAU); ctx.fillStyle = rivet; ctx.fill();\n      }\n      for (let i = 0; i < BALLS; i++) if (s.k[i] > 0.6) { const ang = s.cage + (i / BALLS) * TAU; drawBall(C + Math.cos(ang) * PITCH, C + Math.sin(ang) * PITCH, s.k[i], lx, ly); }\n\n      // The two rings. Their reflections hold still while the engraving turns.\n      const steelRing = (r0: number, r1: number, off: number) => {\n        ring(r0, r1); ctx.fillStyle = sheen(la + off, 104, 255); ctx.fill(\"evenodd\");\n        ctx.save(); ring(r0, r1); ctx.clip(\"evenodd\");\n        ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(marks, 0, 0); ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n        const crown = ctx.createRadialGradient(C, C, r0, C, C, r1);\n        crown.addColorStop(0, \"rgba(8,14,44,0.4)\"); crown.addColorStop(0.16, \"rgba(8,14,44,0)\"); crown.addColorStop(0.5, \"rgba(255,255,255,0.14)\"); crown.addColorStop(0.84, \"rgba(8,14,44,0)\"); crown.addColorStop(1, \"rgba(8,14,44,0.46)\");\n        ctx.fillStyle = crown; ctx.fillRect(0, 0, FULL, FULL);\n        ctx.restore();\n        // Ground chamfers on both edges, lit from the opposite side to the flat.\n        for (const [c0, c1] of [[r0, r0 + 3.4], [r1 - 3.4, r1]]) { ring(c0, c1); ctx.fillStyle = sheen(la + off + 0.62, 60, 255); ctx.fill(\"evenodd\"); }\n        ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(8,12,34,0.55)\";\n        for (const r of [r0, r0 + 3.4, r1 - 3.4, r1]) { ctx.beginPath(); ctx.arc(C, C, r, 0, TAU); ctx.stroke(); }\n      };\n      steelRing(SHOULDER_O, OUT, 0);\n      steelRing(BORE, SHOULDER_I, 0.35);\n      // A turned step on each ring, the seat a seal would snap into.\n      for (const r of [121.5, 81.5]) {\n        ctx.lineWidth = 1.3; ctx.strokeStyle = \"rgba(8,12,34,0.5)\"; ctx.beginPath(); ctx.arc(C, C, r, 0, TAU); ctx.stroke();\n        ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(255,255,255,0.75)\"; ctx.beginPath(); ctx.arc(C, C, r + (r > 100 ? 1.2 : -1.2), 0, TAU); ctx.stroke();\n      }\n      etched(\"DEEP GROOVE\", 133, -0.62, 8.4);\n      etched(`${BALLS} BALL`, 133, 0.62, 8.4);\n      etched(\"CHROME STEEL\", 133, Math.PI, 8.4);\n      etched(\"30 x 62 x 16\", 71.5, s.a, 7.6);\n      for (const off of [2.1, 4.2]) {\n        const x = C + Math.sin(s.a + off) * 71.5, y = C - Math.cos(s.a + off) * 71.5;\n        ctx.beginPath(); ctx.arc(x, y, 2.2, 0, TAU); ctx.fillStyle = \"rgba(14,18,40,0.8)\"; ctx.fill();\n        ctx.beginPath(); ctx.arc(x - lx * 0.6, y - ly * 0.6, 2.2, 0, TAU); ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(255,255,255,0.7)\"; ctx.stroke();\n      }\n      // Balls still on their way down are drawn over the rings.\n      for (let i = 0; i < BALLS; i++) if (s.k[i] <= 0.6) { const ang = s.cage + (i / BALLS) * TAU; drawBall(C + Math.cos(ang) * PITCH, C + Math.sin(ang) * PITCH, s.k[i], lx, ly); }\n\n      // The table seen through the bore, in the shadow of the inner ring.\n      ctx.save(); ctx.beginPath(); ctx.arc(C, C, BORE, 0, TAU); ctx.clip();\n      ctx.fillStyle = STAGE; ctx.fillRect(C - BORE, C - BORE, BORE * 2, BORE * 2);\n      const well = ctx.createRadialGradient(C - lx * 12, C - ly * 12 + 6, BORE * 0.5, C, C, BORE + 6);\n      well.addColorStop(0, \"rgba(5,12,64,0)\"); well.addColorStop(1, \"rgba(5,12,64,0.72)\");\n      ctx.fillStyle = well; ctx.fillRect(C - BORE, C - BORE, BORE * 2, BORE * 2);\n      ctx.restore();\n    };\n\n    let raf = 0, last = 0, seen = true, clock = 0, shown = -1, lastAngle = 0, touched = -1e9, idle = 0;\n    const HOLD = 2.4;\n    const report = (p: number) => { const n = Math.round(p * 100); if (n !== shown) { shown = n; setPct(n); } };\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n\n      // Demo clock. Delete this block when `progress.current` is fed from real work.\n      clock += dt;\n      if (clock < DEMO_SECONDS) { const t = clock / DEMO_SECONDS; progress.current = Math.min(1, t + 0.035 * Math.sin(t * Math.PI * 6) * (1 - t)); }\n      else if (clock < DEMO_SECONDS + HOLD) progress.current = 1;\n      else { clock = 0; progress.current = 0; }\n\n      const p = Math.max(0, Math.min(1, progress.current));\n      const want = Math.floor(p * BALLS + 1e-6);\n      if (want > s.seated) s.v += 0.9 * (want - s.seated); // each ball that lands nudges the bearing round\n      s.seated = want;\n      for (let i = 0; i < BALLS; i++) {\n        const to = i < want ? 1 : 0;\n        if (to === 1) { s.kv[i] += (150 * (1 - s.k[i]) - 15 * s.kv[i]) * dt; s.k[i] += s.kv[i] * dt; }\n        else { s.kv[i] = 0; s.k[i] = Math.max(0, s.k[i] - dt * 3.2); }\n      }\n      const cruise = p >= 1 ? 0.5 : 1.1 + 2.3 * p;\n      if (!s.drag) { s.v += (cruise - s.v) * Math.min(1, dt * 1.1); s.a += s.v * dt; s.cage += s.v * 0.42 * dt; }\n      idle += dt;\n      if (now - touched > 4000) s.lightTo = -2.25 + 0.3 * Math.sin(idle / 2.3);\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 6);\n      report(p);\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = 0; raf = requestAnimationFrame(frame); } };\n    const still = () => { progress.current = 1; s.k = s.k.map(() => 1); s.seated = BALLS; report(1); draw(); };\n    restart.current = () => { clock = 0; progress.current = 0; if (reduce) still(); else wake(); };\n    if (reduce) still(); else wake();\n\n    const angleAt = (e: PointerEvent) => { const b = cv.getBoundingClientRect(); return Math.atan2(e.clientY - (b.top + b.height / 2), e.clientX - (b.left + b.width / 2)); };\n    const onDown = (e: PointerEvent) => { s.drag = true; lastAngle = angleAt(e); touched = performance.now(); cv.setPointerCapture(e.pointerId); };\n    const onMove = (e: PointerEvent) => {\n      if (reduce) return;\n      if (!s.drag) { touched = performance.now() - 2600; s.lightTo = angleAt(e); return; }\n      touched = performance.now();\n      const a = angleAt(e); let d = a - lastAngle; if (d > Math.PI) d -= TAU; if (d < -Math.PI) d += TAU;\n      lastAngle = a; s.a += d; s.cage += d * 0.42; s.v = s.v * 0.6 + (d / 0.016) * 0.4; // a flick carries on after you let go\n    };\n    const onUp = () => { s.drag = false; };\n    cv.addEventListener(\"pointerdown\", onDown); cv.addEventListener(\"pointermove\", onMove);\n    cv.addEventListener(\"pointerup\", onUp); cv.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); restart.current = null;\n      cv.removeEventListener(\"pointerdown\", onDown); cv.removeEventListener(\"pointermove\", onMove);\n      cv.removeEventListener(\"pointerup\", onUp); cv.removeEventListener(\"pointercancel\", onUp);\n      io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const done = pct >= 100;\n  return (\n    <div className=\"flex min-h-[640px] w-full overflow-hidden flex-col items-center justify-center bg-[#2445d8] bg-[radial-gradient(110%_80%_at_50%_32%,#3a5cf0_0%,#2445d8_48%,#1a34b4_100%)] px-4 py-12 sm:min-h-[720px]\">\n      <div\n        role=\"progressbar\"\n        aria-label={LABEL}\n        aria-valuemin={0}\n        aria-valuemax={100}\n        aria-valuenow={pct}\n        aria-valuetext={done ? DONE_LABEL : `${pct} percent, ${Math.min(BALLS, Math.floor(pct / (100 / BALLS)))} of ${BALLS} balls seated`}\n        className=\"relative h-[340px] w-[340px] sm:my-12 sm:scale-125\"\n      >\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute -left-[60px] -top-[60px] h-[460px] w-[460px] max-w-none cursor-grab touch-none rounded-full active:cursor-grabbing\" />\n        <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 flex items-center justify-center font-mono text-[26px] font-semibold tabular-nums tracking-tight text-white\">\n          {pct}\n          <span className=\"ml-0.5 text-[13px] text-blue-100\">%</span>\n        </div>\n      </div>\n      <div className=\"mt-8 flex flex-col items-center gap-3\">\n        <p role=\"status\" className=\"font-mono text-[11px] uppercase tracking-[0.2em] text-white\">\n          {done ? DONE_LABEL : LABEL}\n        </p>\n        <button\n          type=\"button\"\n          onClick={() => restart.current?.()}\n          className=\"flex h-10 items-center rounded-full border border-white/40 px-5 font-mono text-[11px] uppercase tracking-[0.18em] text-white outline-none transition-colors hover:bg-white/10 focus-visible:ring-2 focus-visible:ring-white motion-reduce:transition-none\"\n        >\n          Run again\n        </button>\n      </div>\n    </div>\n  );\n}\n"}]}
