Home / Learn / How do I make a confetti burst without a library?

How do I make a confetti burst without a library?

Confetti adds a moment of joy after an important action such as sending a form or finishing a purchase. We draw it on a transparent canvas over the page, with gravity and random speeds, and no external library.

Steps

  1. Add a fixed full-screen canvas with pointer-events: none and aria-hidden="true", so it never blocks clicks and screen readers skip it.
  2. On click, create a few dozen pieces with random colors, angles and speeds, starting at the button.
  3. On every frame add a little gravity to vertical speed, slow the horizontal speed slightly (air drag), then draw each piece rotated.
  4. Remove pieces that fall below the screen and stop the loop when none are left.
  5. If the user prefers reduced motion, do not launch the burst. Announce the success in an aria-live region instead.

The code

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Confetti</title>
<style>
  body { min-height: 100vh; display: grid; place-items: center; margin: 0; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  button { padding: .7rem 1.4rem; font: inherit; cursor: pointer; }
  canvas { position: fixed; inset: 0; inline-size: 100%; block-size: 100%; pointer-events: none; }
  #status { position: fixed; inset-block-end: 1rem; inset-inline: 0; text-align: center; }
</style>
<button id="go" type="button">أرسل</button>
<canvas id="c" aria-hidden="true"></canvas>
<p id="status" role="status" aria-live="polite"></p>
<script>
  const canvas = document.getElementById("c");
  const ctx = canvas.getContext("2d");
  const status = document.getElementById("status");
  const reduce = matchMedia("(prefers-reduced-motion: reduce)");
  const colors = ["#6c5ce7", "#00b894", "#fdcb6e", "#e17055", "#0984e3"];
  let pieces = [], raf = 0, W = 0, H = 0;

  function size() {
    const dpr = window.devicePixelRatio || 1;
    W = innerWidth; H = innerHeight;
    canvas.width = W * dpr; canvas.height = H * dpr;
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  }
  size(); addEventListener("resize", size);

  function burst(x, y) {
    for (let i = 0; i < 70; i++) {
      const a = Math.random() * Math.PI * 2, s = 4 + Math.random() * 8;
      pieces.push({
        x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s - 6,
        rot: Math.random() * 6.28, vr: (Math.random() - .5) * .3,
        w: 6 + Math.random() * 6, h: 3 + Math.random() * 4,
        c: colors[(Math.random() * colors.length) | 0]
      });
    }
    if (!raf) raf = requestAnimationFrame(tick);
  }

  function tick() {
    ctx.clearRect(0, 0, W, H);
    pieces = pieces.filter((p) => p.y < H + 20);
    for (const p of pieces) {
      p.vy += .25;        // gravity
      p.vx *= .99;        // air drag
      p.x += p.vx; p.y += p.vy; p.rot += p.vr;
      ctx.save();
      ctx.translate(p.x, p.y); ctx.rotate(p.rot);
      ctx.fillStyle = p.c;
      ctx.fillRect(-p.w / 2, -p.h / 2, p.w, p.h);
      ctx.restore();
    }
    raf = pieces.length ? requestAnimationFrame(tick) : 0;
    if (!pieces.length) ctx.clearRect(0, 0, W, H);
  }

  document.getElementById("go").addEventListener("click", (e) => {
    status.textContent = "تم الإرسال بنجاح";
    if (reduce.matches) return;           // no motion: text announcement only
    const r = e.currentTarget.getBoundingClientRect();
    burst(r.left + r.width / 2, r.top + r.height / 2);
  });
</script>
</html>