{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"savings-chart-of-minted-coin-stacks-where-each-deposit-drops-a-coin-that-wobbles-and-settles","type":"registry:component","title":"Savings Chart of Minted Coin Stacks Where Each Deposit Drops a Coin That Wobbles and Settles","description":"Savings chart on a canvas: stacks of gold coins, one per month, where each deposit drops a coin that wobbles and settles. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/savings-chart-of-minted-coin-stacks-where-each-deposit-drops-a-coin-that-wobbles-and-settles.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// One stack per month. `coins` is how many deposits were made that month.\nconst months = [\n  { label: \"Jun\", coins: 5 },\n  { label: \"Jul\", coins: 9 },\n  { label: \"Aug\", coins: 7 },\n  { label: \"Sep\", coins: 14 },\n  { label: \"Oct\", coins: 4 },\n];\nconst COIN = 50; // what one coin is worth\nconst UNIT = \"vale\"; // an invented currency: rename it to yours\nconst MAX = 20; // the tallest a stack may grow\nconst FUND = \"Rainy-day fund\";\n\ntype Coin = { v: number; jx: number; y: number; vy: number; wob: number; wv: number; rest: boolean; wait: number; gone: boolean; a: number; dx: number };\n\nconst STOPS: [number, number, number][] = [[70, 40, 8], [140, 92, 22], [203, 150, 48], [240, 203, 106], [255, 246, 208]];\nconst gold = (t: number, tone = 1) => {\n  const p = Math.max(0, Math.min(0.9999, t)) * (STOPS.length - 1), i = Math.floor(p), f = p - i, a = STOPS[i], b = STOPS[i + 1];\n  return `rgb(${Math.min(255, (a[0] + (b[0] - a[0]) * f) * tone) | 0},${Math.min(255, (a[1] + (b[1] - a[1]) * f) * tone) | 0},${Math.min(255, (a[2] + (b[2] - a[2]) * f) * (2 - tone)) | 0})`;\n};\n\nexport default function MintedCoinStacks() {\n  const wrap = useRef<HTMLDivElement>(null);\n  const grid = useRef<HTMLUListElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [counts, setCounts] = useState(months.map((m) => m.coins));\n  const [shown, setShown] = useState(months.reduce((n, m) => n + m.coins, 0) * COIN);\n  const [last, setLast] = useState<number[]>([]);\n  const api = useRef<{ add: (i: number, user: boolean) => void; take: (i: number, user: boolean) => 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    const n = months.length;\n    let W = 0, H = 0, side = 0, rx = 0, ry = 0, th = 0, ground = 0, sprites: HTMLCanvasElement[] = [], pad = 6;\n    let seed = 11;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n    const make = (wait: number, fall: boolean): Coin => ({ v: Math.floor(rand() * 6), jx: (rand() - 0.5) * 0.09, y: fall ? 1 : 0, vy: 0, wob: 0, wv: 0, rest: !fall, wait, gone: false, a: 1, dx: 0 });\n    // Opening: every stack already stands at its value. Coins only fall for deposits made after that.\n    const stacks: Coin[][] = months.map((m) => Array.from({ length: m.coins }, () => make(0, false)));\n    const leaving: { c: Coin; x: number; y: number; vx: number }[] = [];\n\n    // One coin, drawn once per variant: reeded edge below, struck face above.\n    const strike = (v: number) => {\n      const sp = document.createElement(\"canvas\");\n      const w = rx * 2 + pad * 2, h = ry * 2 + th + pad * 2;\n      sp.width = Math.ceil(w * dpr); sp.height = Math.ceil(h * dpr);\n      const c = sp.getContext(\"2d\");\n      if (!c) return sp;\n      c.scale(dpr, dpr);\n      const cx = w / 2, cy = pad + ry, tone = 0.93 + (v % 3) * 0.05, turn = v * 1.13 + 0.4;\n      // Edge: a band between two half ellipses, lit from the upper left.\n      c.beginPath(); c.moveTo(cx - rx, cy); c.lineTo(cx - rx, cy + th); c.ellipse(cx, cy + th, rx, ry, 0, Math.PI, 0, true); c.lineTo(cx + rx, cy); c.ellipse(cx, cy, rx, ry, 0, 0, Math.PI, false); c.closePath();\n      const eg = c.createLinearGradient(cx - rx, 0, cx + rx, 0);\n      ([[0, 0.16], [0.07, 0.4], [0.2, 0.7], [0.29, 1], [0.36, 0.78], [0.5, 0.52], [0.66, 0.36], [0.8, 0.6], [0.9, 0.36], [1, 0.1]] as [number, number][]).forEach(([p, t]) => eg.addColorStop(p, gold(t, tone)));\n      c.fillStyle = eg; c.fill();\n      c.save(); c.clip();\n      for (let d = -88; d <= 88; d += 2.6) {\n        const a = (d * Math.PI) / 180, x = cx + rx * Math.sin(a), y = cy + ry * Math.cos(a);\n        c.beginPath(); c.moveTo(x, y); c.lineTo(x, y + th + 1); c.lineWidth = Math.max(0.5, rx * 0.014); c.strokeStyle = \"rgba(50,26,0,0.3)\"; c.stroke();\n        c.beginPath(); c.moveTo(x + rx * 0.014, y); c.lineTo(x + rx * 0.014, y + th + 1); c.lineWidth = Math.max(0.4, rx * 0.01); c.strokeStyle = \"rgba(255,240,190,0.3)\"; c.stroke();\n      }\n      const shade = c.createLinearGradient(0, cy, 0, cy + th + ry);\n      shade.addColorStop(0, \"rgba(255,244,200,0.28)\"); shade.addColorStop(0.25, \"rgba(0,0,0,0)\"); shade.addColorStop(1, \"rgba(40,18,0,0.36)\");\n      c.fillStyle = shade; c.fillRect(0, 0, w, h);\n      c.restore();\n      // Face, drawn as a circle and squashed into perspective.\n      c.save(); c.translate(cx, cy); c.scale(1, ry / rx);\n      c.beginPath(); c.arc(0, 0, rx, 0, Math.PI * 2);\n      if (typeof c.createConicGradient === \"function\") {\n        const g = c.createConicGradient(-2.3, 0, 0);\n        ([[0, 0.98], [0.06, 0.8], [0.15, 0.58], [0.26, 0.44], [0.37, 0.6], [0.47, 0.8], [0.52, 0.7], [0.62, 0.5], [0.74, 0.4], [0.86, 0.56], [0.95, 0.84], [1, 0.98]] as [number, number][]).forEach(([p, t]) => g.addColorStop(p, gold(t, tone)));\n        c.fillStyle = g;\n      } else c.fillStyle = gold(0.6, tone);\n      c.fill();\n      // Raised rim with a bright inner shoulder.\n      const rim = c.createLinearGradient(-rx, -rx, rx, rx);\n      rim.addColorStop(0, gold(1, tone)); rim.addColorStop(0.45, gold(0.62, tone)); rim.addColorStop(1, gold(0.2, tone));\n      c.lineWidth = rx * 0.085; c.strokeStyle = rim; c.beginPath(); c.arc(0, 0, rx * 0.955, 0, Math.PI * 2); c.stroke();\n      c.lineWidth = Math.max(0.6, rx * 0.016); c.strokeStyle = \"rgba(60,30,0,0.55)\"; c.beginPath(); c.arc(0, 0, rx * 0.905, 0, Math.PI * 2); c.stroke();\n      c.strokeStyle = \"rgba(255,246,208,0.5)\"; c.beginPath(); c.arc(0, 0, rx * 0.89, 0.6, 3.6); c.stroke();\n      c.rotate(turn);\n      // A ring of beads, each with its own highlight and shadow.\n      for (let i = 0; i < 44; i++) {\n        const a = (i / 44) * Math.PI * 2, bx = Math.cos(a) * rx * 0.8, by = Math.sin(a) * rx * 0.8, br = rx * 0.024;\n        c.beginPath(); c.arc(bx + br * 0.5, by + br * 0.6, br, 0, Math.PI * 2); c.fillStyle = \"rgba(60,30,0,0.5)\"; c.fill();\n        c.beginPath(); c.arc(bx, by, br, 0, Math.PI * 2); c.fillStyle = gold(0.86, tone); c.fill();\n      }\n      // The struck numeral: shadow below, highlight above, metal between.\n      c.textAlign = \"center\"; c.textBaseline = \"middle\";\n      const emboss = (text: string, y: number, font: string, d: number) => {\n        c.font = font;\n        c.fillStyle = \"rgba(58,28,0,0.62)\"; c.fillText(text, d, y + d * 1.3);\n        c.fillStyle = \"rgba(255,248,214,0.9)\"; c.fillText(text, -d * 0.8, y - d);\n        c.fillStyle = gold(0.7, tone); c.fillText(text, 0, y);\n      };\n      emboss(String(COIN), -rx * 0.07, `700 ${rx * 0.78}px Georgia, \"Times New Roman\", serif`, rx * 0.022);\n      emboss(UNIT.toUpperCase().split(\"\").join(\" \"), rx * 0.47, `700 ${rx * 0.13}px ui-sans-serif, system-ui, sans-serif`, rx * 0.012);\n      c.restore();\n      // One soft band of sky across the face ties every coin to the same light.\n      c.save(); c.beginPath(); c.ellipse(cx, cy, rx, ry, 0, 0, Math.PI * 2); c.clip();\n      const band = c.createLinearGradient(cx - rx, cy - ry, cx + rx * 0.4, cy + ry);\n      band.addColorStop(0, \"rgba(255,255,255,0.5)\"); band.addColorStop(0.32, \"rgba(255,255,255,0.06)\"); band.addColorStop(1, \"rgba(60,26,0,0.22)\");\n      c.fillStyle = band; c.fillRect(0, 0, w, h); c.restore();\n      c.lineWidth = 0.8; c.strokeStyle = \"rgba(60,30,0,0.5)\"; c.beginPath(); c.ellipse(cx, cy, rx - 0.3, ry - 0.3, 0, 0, Math.PI * 2); c.stroke();\n      return sp;\n    };\n\n    const layout = () => {\n      W = Math.max(280, box.clientWidth); H = W < 600 ? 330 : 520; side = W < 600 ? 4 : 72;\n      rx = Math.round(Math.min(78, ((W - side * 2) / n) * 0.45)); ry = rx * 0.36; th = Math.max(5, Math.round(rx * 0.215)); ground = H - (W < 600 ? 36 : 54) - th;\n      if (grid.current) { grid.current.style.paddingLeft = `${side}px`; grid.current.style.paddingRight = `${side}px`; }\n      cv.width = W * dpr; cv.height = H * dpr; cv.style.height = `${H}px`;\n      sprites = Array.from({ length: 6 }, (_, v) => strike(v));\n    };\n    const colX = (i: number) => side + ((W - side * 2) * (i + 0.5)) / n;\n    const blob = (x: number, y: number, sx: number, sy: number, rgb: string, alpha: number) => {\n      ctx.save(); ctx.translate(x, y); ctx.scale(sx, sy);\n      const g = ctx.createRadialGradient(0, 0, 0, 0, 0, 1);\n      g.addColorStop(0, `rgba(${rgb},${alpha})`); g.addColorStop(0.55, `rgba(${rgb},${alpha * 0.45})`); g.addColorStop(1, `rgba(${rgb},0)`);\n      ctx.fillStyle = g; ctx.beginPath(); ctx.arc(0, 0, 1, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n    };\n    const put = (c: Coin, x: number, yTop: number) => {\n      const sp = sprites[c.v], w = rx * 2 + pad * 2, h = ry * 2 + th + pad * 2;\n      ctx.save(); ctx.globalAlpha = c.a * (c.rest ? 1 : Math.max(0, Math.min(1, (yTop - H * 0.14) / (H * 0.1)))); ctx.translate(x, yTop + th / 2); ctx.rotate(c.wob);\n      ctx.drawImage(sp, -w / 2, -th / 2 - ry - pad, w, h); ctx.restore();\n    };\n\n    const draw = () => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, W, H);\n      const base = ground + th;\n      stacks.forEach((st, i) => {\n        const x = colX(i), down = st.filter((c) => c.rest || c.y < 4).length, tall = down * th;\n        // Warm light bounced off the gold, then the long shadow, then the tight one where metal meets table.\n        blob(x, base + ry * 0.3, rx * 1.5, ry * 1.7, \"214,150,40\", 0.1);\n        blob(x + rx * 0.45 + tall * 0.22, base + ry * 0.2 + tall * 0.03, rx * 1.1 + tall * 0.26, ry * 1.25 + tall * 0.03, \"60,40,20\", 0.24);\n        blob(x + rx * 0.05, base + ry * 0.1, rx * 1.16, ry * 1.3, \"40,24,8\", 0.62);\n        blob(x + rx * 0.02, base + ry * 0.02, rx * 1.04, ry * 1.08, \"30,16,4\", 0.5);\n      });\n      stacks.forEach((st, i) => {\n        const x = colX(i);\n        st.forEach((c, j) => {\n          if (c.wait > 0) return;\n          const yTop = ground - j * th - c.y;\n          if (!c.rest && c.y > 2 && c.y < 220) {\n            // A falling coin darkens what is under it, tighter the closer it gets.\n            const f = 1 - c.y / 220;\n            ctx.save(); ctx.beginPath(); ctx.ellipse(x + c.y * 0.1, ground - j * th + th - (j ? 0 : -ry * 0.1), rx * (0.92 + (1 - f) * 0.2), ry * (0.92 + (1 - f) * 0.2), 0, 0, Math.PI * 2); ctx.fillStyle = `rgba(50,26,0,${0.34 * f * f})`; ctx.fill(); ctx.restore();\n          }\n          put(c, x + c.jx * rx + c.dx, yTop);\n        });\n      });\n      leaving.forEach((l) => put(l.c, l.x, l.y));\n    };\n\n    let raf = 0, prev = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 4200, demoDir = 1, shownNow = months.reduce((s, m) => s + m.coins, 0) * COIN, shownSent = shownNow;\n    const total = () => stacks.reduce((s, st) => s + st.length, 0) * COIN;\n    const sync = () => setCounts(stacks.map((st) => st.length));\n\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; prev = now; }\n      const dt = Math.min(0.032, (now - prev) / 1000); prev = now;\n      const t = now - t0;\n      stacks.forEach((st) => st.forEach((c, j) => {\n        if (c.wait > 0) { c.wait -= dt; if (c.wait <= 0) { c.y = ground - j * th + ry + th + 30; c.vy = -260; } return; }\n        if (!c.rest) {\n          c.vy -= 3400 * dt; c.y += c.vy * dt;\n          if (c.y <= 0) {\n            c.y = 0;\n            const hit = -c.vy;\n            if (hit > 260) { c.vy = hit * 0.26; c.wv += (j % 2 ? 1 : -1) * Math.min(4.2, hit * 0.004); for (let b = 0; b < j; b++) { st[b].wv += (rand() - 0.5) * 0.9; st[b].dx += (rand() - 0.5) * 0.5; } }\n            else { c.vy = 0; c.rest = true; }\n          }\n        }\n        // The rock a coin makes as it settles flat.\n        c.wv += (-520 * c.wob - 13 * c.wv) * dt; c.wob += c.wv * dt;\n        c.wob = Math.max(-0.11, Math.min(0.11, c.wob)); c.dx *= 1 - Math.min(1, dt * 6);\n      }));\n      for (let i = leaving.length - 1; i >= 0; i--) {\n        const l = leaving[i];\n        l.c.vy += 2200 * dt; l.y += l.c.vy * dt; l.x += l.vx * dt; l.c.wob += l.vx * 0.004 * dt * 8; l.c.a -= dt * 3.2;\n        if (l.c.a <= 0) leaving.splice(i, 1); else l.c.a = Math.max(0, l.c.a);\n      }\n      // Left alone, the newest month keeps saving, then spends back down.\n      if (t > nextDemo && now - touched > 6000) {\n        const st = stacks[n - 1];\n        if (demoDir > 0 && st.length >= Math.min(MAX, months[n - 1].coins + 7)) demoDir = -1;\n        if (demoDir < 0 && st.length <= months[n - 1].coins) demoDir = 1;\n        if (demoDir > 0) drop(n - 1); else lift(n - 1);\n        nextDemo = t + (demoDir > 0 ? 1150 : 520);\n      }\n      const goal = total();\n      if (shownNow !== goal) { shownNow += (goal - shownNow) * Math.min(1, dt * 12); if (Math.abs(goal - shownNow) < 0.6) shownNow = goal; }\n      if (Math.round(shownNow) !== shownSent) { shownSent = Math.round(shownNow); setShown(shownSent); }\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { prev = performance.now(); raf = requestAnimationFrame(frame); } };\n    const still = () => { stacks.forEach((st) => st.forEach((c) => { c.wait = 0; c.y = 0; c.rest = true; c.wob = 0; })); leaving.length = 0; setShown(total()); draw(); };\n\n    const drop = (i: number) => {\n      const st = stacks[i];\n      if (st.length >= MAX) return;\n      const c = make(0, true); c.y = Math.max(160, ground - st.length * th + ry + th + 30); c.vy = -200;\n      st.push(c); sync();\n    };\n    const lift = (i: number) => {\n      const st = stacks[i];\n      if (!st.length) return;\n      const c = st.pop() as Coin;\n      leaving.push({ c: { ...c, vy: -520, wv: 0 }, x: colX(i) + c.jx * rx, y: ground - st.length * th - c.y, vx: 150 });\n      sync();\n    };\n    api.current = {\n      add: (i, user) => { if (user) touched = performance.now(); drop(i); if (reduce) still(); else wake(); },\n      take: (i, user) => { if (user) touched = performance.now(); lift(i); if (reduce) still(); else wake(); },\n    };\n\n    layout();\n    if (reduce) still(); else wake();\n    const ro = new ResizeObserver(() => { const w = Math.max(280, box.clientWidth); if (w !== W) { layout(); if (reduce || !raf) draw(); } });\n    ro.observe(box);\n    const io = new IntersectionObserver(([e]) => { seen = e.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => { cancelAnimationFrame(raf); ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis); api.current = null; };\n  }, []);\n\n  const add = (i: number) => { if (counts[i] >= MAX) return; api.current?.add(i, true); setLast((l) => [...l, i]); };\n  const undo = () => {\n    const i = last.length ? last[last.length - 1] : counts.reduce((best, c, j) => (c > counts[best] ? j : best), 0);\n    if (!counts[i]) return;\n    api.current?.take(i, true); setLast((l) => l.slice(0, -1));\n  };\n  const coins = counts.reduce((a, b) => a + b, 0);\n\n  return (\n    <div className=\"flex min-h-[580px] w-full flex-col items-center justify-center bg-[#ece5d8] bg-[radial-gradient(90%_70%_at_38%_30%,#f8f3ea_0%,#ece5d8_55%,#ddd3c2_100%)] px-4 py-12 sm:min-h-[780px]\">\n      <div className=\"w-full max-w-[1040px]\">\n        <div className=\"relative z-10 flex items-end justify-between gap-4 px-1 sm:px-[72px]\">\n          <div>\n            <p className=\"font-mono text-[11px] font-semibold uppercase tracking-[0.2em] text-stone-600\">{FUND}</p>\n            <p aria-live=\"polite\" aria-atomic=\"true\" className=\"mt-1 text-[44px] font-semibold leading-none tracking-tight text-stone-900 [font-variant-numeric:tabular-nums] sm:text-[68px]\">\n              {shown.toLocaleString(\"en-US\")}\n              <span className=\"ml-2 align-baseline font-mono text-[12px] font-semibold uppercase tracking-[0.2em] text-stone-600 sm:text-[13px]\">{UNIT}</span>\n            </p>\n          </div>\n          <button\n            type=\"button\"\n            onClick={undo}\n            disabled={!coins}\n            className=\"mb-1 h-10 shrink-0 rounded-full border border-stone-900/20 px-4 font-mono text-[11px] font-semibold uppercase tracking-[0.14em] text-stone-800 transition-colors hover:bg-stone-900 hover:text-stone-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-700 focus-visible:ring-offset-2 focus-visible:ring-offset-[#ece5d8] disabled:opacity-40 motion-reduce:transition-none\"\n          >\n            Take one back\n          </button>\n        </div>\n        <div ref={wrap} className=\"relative -mt-14 w-full sm:-mt-24\">\n          <canvas ref={canvas} aria-hidden=\"true\" className=\"block h-[330px] w-full sm:h-[520px]\" />\n          <ul ref={grid} className=\"absolute inset-0 grid\" style={{ gridTemplateColumns: `repeat(${months.length}, minmax(0, 1fr))` }}>\n            {months.map((m, i) => (\n              <li key={m.label} className=\"flex\">\n                <button\n                  type=\"button\"\n                  onClick={() => add(i)}\n                  aria-disabled={counts[i] >= MAX}\n                  aria-label={`${m.label}: ${(counts[i] * COIN).toLocaleString(\"en-US\")} ${UNIT} in ${counts[i]} coins. Add a ${COIN} ${UNIT} coin.`}\n                  className=\"group flex w-full cursor-pointer flex-col items-center justify-end rounded-2xl pb-0 outline-none focus-visible:ring-2 focus-visible:ring-amber-700\"\n                >\n                  <span className=\"font-mono text-[10px] font-semibold uppercase tracking-[0.18em] text-stone-600 transition-colors group-hover:text-stone-900 motion-reduce:transition-none sm:text-[11px]\">\n                    {m.label}\n                    <span className=\"ml-1.5 text-stone-900 [font-variant-numeric:tabular-nums]\">{(counts[i] * COIN).toLocaleString(\"en-US\")}</span>\n                  </span>\n                </button>\n              </li>\n            ))}\n          </ul>\n        </div>\n        <p className=\"mt-5 text-center font-mono text-[11px] uppercase tracking-[0.16em] text-stone-600\">\n          Tap a month to drop a {COIN} {UNIT} coin <span className=\"hidden sm:inline\"><span className=\"mx-1.5 text-stone-400\">/</span> {coins} coins saved</span>\n        </p>\n      </div>\n    </div>\n  );\n}\n"}]}
