Layout · 21 of 23

Confetti Burst

Physics-based confetti from button

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<button class="trigger-btn" id="confettiBtn">Celebrate &#127881;</button>

CSS

.trigger-btn {
  padding: 0.6rem 1.2rem;
  background: #7c5cff;
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}

.confetti-piece {
  position: fixed;
  width: 8px;
  height: 12px;
  /* Centre on the launch point via margin, not transform — the WAAPI keyframes
     own `transform` and would replace any base value set here */
  margin: -6px 0 0 -4px;
  border-radius: 1px;
  pointer-events: none;
  z-index: 5000;
  will-change: transform, opacity;
}

JavaScript

// Projectile motion with linear air drag, integrated into explicit keyframes and
// played back with easing:'linear' — so the sampled physics *is* the motion.
// (A single cubic-bezier can't express "decelerate horizontally while accelerating
// downward"; an ease-out curve front-loads everything and the burst reads as a blink.)
//
//   dv/dt = g - k*v   ->   v(t) = (v0 - g/k)e^(-kt) + g/k
//   x(t) = (v0x/k)(1 - e^(-kt))
//   y(t) = ((v0y - g/k)/k)(1 - e^(-kt)) + (g/k)t
//
var confettiBtn = document.getElementById('confettiBtn');
var COLORS = ['#7c5cff', '#a78bfa', '#ff006e', '#00f5d4', '#ffd60a'];
var COUNT = 60;

var G = 1600;      // px/s^2 — effective gravity
var K = 2.2;       // 1/s   — drag; terminal fall speed = G/K ~ 727 px/s
var K_ROT = 1.2;   // 1/s   — rotational drag
var VT = G / K;
var STEPS = 26;    // keyframes sampled per piece

confettiBtn.addEventListener('click', function () {
  var rect = confettiBtn.getBoundingClientRect();
  var cx = rect.left + rect.width / 2;
  var cy = rect.top + rect.height / 2;

  for (var i = 0; i < COUNT; i++) {
    var piece = document.createElement('div');
    piece.className = 'confetti-piece';
    piece.style.left = cx + 'px';
    piece.style.top = cy + 'px';
    piece.style.background = COLORS[i % COLORS.length];
    document.body.appendChild(piece);

    // Launch in an upward cone: theta = -PI/2 + s, so v0y is always negative.
    var s = (Math.random() * 2 - 1) * 0.95;
    var v0 = 700 + Math.random() * 600;
    var v0x = v0 * Math.sin(s);
    var v0y = -v0 * Math.cos(s);

    var durS = 2.4 + Math.random() * 0.8;
    var w0 = (Math.random() < 0.5 ? -1 : 1) * (720 + Math.random() * 1440); // deg/s
    // ~2-6 edge-on flips over the piece's life; faster than this strobes
    var flutter0 = (Math.random() < 0.5 ? -1 : 1) * (300 + Math.random() * 500);
    var flutterPhase = Math.random() * Math.PI * 2;

    var frames = [];
    for (var n = 0; n < STEPS; n++) {
      var p = n / (STEPS - 1);
      var t = p * durS;
      var decay = 1 - Math.exp(-K * t);

      var x = (v0x / K) * decay;
      var y = ((v0y - VT) / K) * decay + VT * t;

      var rot = (w0 / K_ROT) * (1 - Math.exp(-K_ROT * t));
      // Edge-on flip, so pieces flash thin like real paper
      var flip = flutter0 * t + Math.sin(t * 6 + flutterPhase) * 30;

      frames.push({
        offset: p,
        transform: 'translate3d(' + x.toFixed(1) + 'px, ' + y.toFixed(1) + 'px, 0) ' +
                   'rotate(' + rot.toFixed(1) + 'deg) rotateY(' + flip.toFixed(1) + 'deg)',
        opacity: p < 0.7 ? 1 : Math.max(0, 1 - (p - 0.7) / 0.3)
      });
    }

    piece.animate(frames, {
      duration: durS * 1000,
      easing: 'linear'   // the physics is already baked into the offsets
    }).onfinish = function () { this.effect.target.remove(); };
  }
});