Dev.to WebDev 🛠 Dev 👁 2 📖 5 min read

How I Built a 60fps Frosted-Glass Shatter & Crystal Sound Effect with Pure CSS + Web Audio API (0 KB Audio Files)

When I launched the interactive landing page preview for HabitReveal — an ad-free 21-day visual habit tracker for Android and Chrome — I wanted visitors to feel the core product hook in under 5 seconds without installing

When I launched the interactive landing page preview for HabitReveal — an ad-free 21-day visual habit tracker for Android and Chrome — I wanted visitors to feel the core product hook in under 5 seconds without installing anything:

Every daily habit check-in shatters one frosted-glass tile to reveal a hidden piece of artwork (or your own personal Dream Photo).

My first attempt was a tiny CSS opacity fade. Unsurprisingly, early testers on Reddit told me honestly: "I didn't see a tile shatter — it just disappeared."

I didn't want to bloat a static SEO landing page with Three.js, Lottie, or a 150 KB .mp3 sound pack that would hurt Core Web Vitals (LCP/INP). So I built a real 60fps frosted-glass shatter + synthesized crystal chime using ~2 KB of vanilla CSS, JS, and the Web Audio API — zero external libraries and 0 KB of audio files.

You can test the live 3-tile interactive demo (turn your sound on!) right on habitreveal.app. Here is the exact architecture behind it, plus a subtle mobile CSS bug a Reddit tester caught for me.

1. GPU-Only Flying Polygon Shards (clip-path + CSS Variables)

To keep frame times rock-solid on mid-range Android phones, every shard animates only transform (translate3d, rotate, scale) and opacity — zero layout thrashing.

Each broken tile spawns 12 translucent polygonal fragments using clip-path: polygon(...), driven by per-shard CSS custom properties (--tx, --ty, --rot):

.glass-flash {
  position: absolute;
  pointer-events: none;
  z-index: 25;
  border-radius: 50%;
  background: radial-gradient(
    circle,
    rgba(255, 255, 255, 0.95) 0%,
    rgba(97, 249, 206, 0.65) 40%,
    rgba(97, 249, 206, 0) 75%
  );
  animation: glassFlash 0.36s ease-out forwards;
}

@keyframes glassFlash {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(0.2); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.9); }
}

.glass-shard {
  position: absolute;
  pointer-events: none;
  z-index: 26;
  background: linear-gradient(
    135deg,
    rgba(240, 255, 253, 0.95) 0%,
    rgba(97, 249, 206, 0.62) 45%,
    rgba(12, 46, 66, 0.92) 100%
  );
  box-shadow: 0 4px 14px rgba(97, 249, 206, 0.4);
  will-change: transform, opacity;
  animation: shardFly 0.68s cubic-bezier(0.16, 0.84, 0.32, 1) forwards;
}

@keyframes shardFly {
  0% { opacity: 1; transform: translate3d(0, 0, 0) rotate(0deg) scale(1); }
  25% { opacity: 1; }
  100% {
    opacity: 0;
    transform: translate3d(var(--tx), var(--ty), 0) rotate(var(--rot)) scale(0.35);
  }
}

Adding Gravity & Crystal Dust Sparkles in Vanilla JS

Instead of radiating shards in a flat circle, adding a downward gravity bias (+34px to +58px on --ty) plus 10 tiny glowing dust motes (.glass-sparkle) makes the break feel physical rather than flat:

function spawnGlassShatter(board, tileBtn, step) {
  playGlassSound(step);
  const bRect = board.getBoundingClientRect();
  const tRect = tileBtn.getBoundingClientRect();
  const cx = tRect.left - bRect.left + tRect.width / 2;
  const cy = tRect.top - bRect.top + tRect.height / 2;

  const polys = [
    "polygon(0% 0%, 100% 15%, 45% 100%)",
    "polygon(20% 0%, 100% 0%, 80% 100%, 0% 70%)",
    "polygon(0% 25%, 90% 0%, 100% 85%, 25% 100%)",
    "polygon(50% 0%, 100% 60%, 30% 100%, 0% 40%)",
    "polygon(10% 0%, 100% 35%, 65% 100%, 0% 80%)",
    "polygon(0% 0%, 85% 20%, 100% 100%, 15% 75%)"
  ];

  for (let i = 0; i < 12; i++) {
    const shard = document.createElement("span");
    shard.className = "glass-shard";
    const angle = (Math.PI * 2 * i) / 12 + (Math.random() - 0.5) * 0.45;
    const dist = 34 + Math.random() * 56;
    const tx = Math.cos(angle) * dist;
    const ty = Math.sin(angle) * dist + 34 + Math.random() * 24; // downward gravity bias
    const rot = (Math.random() > 0.5 ? 1 : -1) * (95 + Math.random() * 210);
    const w = Math.max(10, tRect.width * (0.24 + Math.random() * 0.24));
    const h = Math.max(10, tRect.height * (0.24 + Math.random() * 0.24));

    shard.style.width = `${w}px`;
    shard.style.height = `${h}px`;
    shard.style.left = `${cx - w / 2 + (Math.random() - 0.5) * tRect.width * 0.55}px`;
    shard.style.top = `${cy - h / 2 + (Math.random() - 0.5) * tRect.height * 0.55}px`;
    shard.style.clipPath = polys[i % polys.length];
    shard.style.setProperty("--tx", `${tx.toFixed(1)}px`);
    shard.style.setProperty("--ty", `${ty.toFixed(1)}px`);
    shard.style.setProperty("--rot", `${rot.toFixed(0)}deg`);
    board.appendChild(shard);
    setTimeout(() => shard.remove(), 720);
  }

  // Subtle haptic tap on supported Android browsers
  if (navigator.vibrate) {
    try { navigator.vibrate(25); } catch (_) {}
  }
}

2. Synthesizing a Crisp Glass Crack + Ascending Chime with Web Audio API (0 KB Audio Assets)

Loading an .mp3 on a static page adds network latency and cold-start delay on the very first click. With the Web Audio API, we lazily initialize a single AudioContext inside the click handler (100% autoplay-policy safe) and layer two tiny sounds:

  1. The "Crack" (55ms high-pass filtered white noise with exponential decay) — cuts everything below 2600 Hz so it sounds like crisp frosted glass instead of harsh static.
  2. The "Crystal Chime" (two harmonic oscillators) — ascends in pitch across Day 1 (D6 = 1174.66 Hz) → Day 2 (F6 = 1396.91 Hz) → Day 3 (A6 = 1760 Hz), creating an unmistakable D-minor/major sparkle progression:
let audioCtx = null;

function playGlassSound(step) {
  const Ctx = window.AudioContext || window.webkitAudioContext;
  if (!Ctx) return;
  if (!audioCtx) audioCtx = new Ctx();
  if (audioCtx.state === "suspended") audioCtx.resume();

  const now = audioCtx.currentTime;

  // 1. Crisp 55ms high-pass filtered noise burst (the glass crack)
  const bufLen = Math.floor(audioCtx.sampleRate * 0.055);
  const buf = audioCtx.createBuffer(1, bufLen, audioCtx.sampleRate);
  const data = buf.getChannelData(0);
  for (let i = 0; i < bufLen; i++) {
    data[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / bufLen, 2.2);
  }

  const noise = audioCtx.createBufferSource();
  noise.buffer = buf;
  const hp = audioCtx.createBiquadFilter();
  hp.type = "highpass";
  hp.frequency.value = 2600;

  const nGain = audioCtx.createGain();
  nGain.gain.setValueAtTime(0.22, now);
  nGain.gain.exponentialRampToValueAtTime(0.001, now + 0.055);

  noise.connect(hp);
  hp.connect(nGain);
  nGain.connect(audioCtx.destination);
  noise.start(now);

  // 2. Ascending harmonic crystal chime (Day 1 -> Day 2 -> Day 3)
  const freqs = [1174.66, 1396.91, 1760.0];
  const f0 = freqs[Math.min(2, Math.max(0, (step || 1) - 1))];
  [f0, f0 * 1.5].forEach((freq, idx) => {
    const osc = audioCtx.createOscillator();
    const gain = audioCtx.createGain();
    osc.type = idx === 0 ? "triangle" : "sine";
    osc.frequency.setValueAtTime(freq, now + idx * 0.025);
    gain.gain.setValueAtTime(idx === 0 ? 0.11 : 0.06, now + idx * 0.025);
    gain.gain.exponentialRampToValueAtTime(0.0008, now + 0.28 + idx * 0.06);
    osc.connect(gain);
    gain.connect(audioCtx.destination);
    osc.start(now + idx * 0.025);
    osc.stop(now + 0.36 + idx * 0.06);
  });
}

Because nothing runs until the user taps a tile, Lighthouse Performance and Core Web Vitals stay completely untouched.

3. The Sneaky Mobile CSS Bug a Reddit Tester Caught

On desktop Chrome everything looked great — until a Reddit user tested the page on a phone and reported:

"I tapped Day 1, then Day 2. The counter went from 0/21 to 2/21... but both tiles still read TAP after the count changed."

Why did that happen only on mobile touchscreens?

Because .p-tile:hover had color: #fff (matching the specificity of .p-tile.revealed { color: transparent; }), and mobile browsers keep :hover stuck on a <button> after a tap! Even though the frosted background turned transparent, the sticky hover rule kept the child <span>Tap!</span> bright white right on top of the revealed artwork.

The bulletproof 3-line fix:

/* 1. Never apply hover styling on touch-only screens */
@media (hover: hover) {
  .p-tile:hover { transform: scale(0.97); border-color: var(--accent-bright); color: #fff; }
}

/* 2. Explicitly hide all child labels inside revealed tiles */
.p-tile.revealed {
  background: transparent !important;
  box-shadow: none !important;
  color: transparent !important;
  pointer-events: none;
}
.p-tile.revealed span {
  opacity: 0 !important;
  visibility: hidden !important;
}

And in JS, always remove any highlight class (btn.classList.remove("try-glow")) the moment a tile unlocks.

Try the Live Demo (Sound On!)

If you want to hear the Web Audio crystal shatter and test the 3-tile preview (with a ↺ Replay button), I put it live across the homepage and all 71 free 21-day habit guides:

Have you ever used Web Audio API synthesis to replace MP3 micro-interaction sounds on the web? I'd love to see any cool sound recipes you've tried in the comments!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.