[Lab 01]AWAIS MALIK

Scramble text

Random glyphs settle into the real word, left to right. Used on the navigation and footer links.

  • Vanilla JS
  • No dependencies

Jump to the code ↓01 / 04

Live — hover me

index.html2 lines
<!-- A monospace font keeps the word from changing width while it scrambles -->
<a href="#" data-scramble>Hover me</a>
scramble.js31 lines
const GLYPHS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#%&*+/<>";
const STEP = 70; // ms before each next letter locks in
const FLICKER = 50; // ms between random-glyph shuffles

document.querySelectorAll("[data-scramble]").forEach((el) => {
  const original = el.textContent;
  let raf = 0;

  el.addEventListener("pointerenter", () => {
    cancelAnimationFrame(raf);
    // Time-based, so it runs at the same speed on 60Hz and 144Hz screens.
    const start = performance.now();
    let lastShuffle = -Infinity;

    const tick = (now) => {
      const locked = Math.floor((now - start) / STEP);
      if (locked >= original.length) {
        el.textContent = original;
        return;
      }
      if (now - lastShuffle >= FLICKER) {
        lastShuffle = now;
        el.textContent = [...original]
          .map((ch, i) => (ch === " " || i < locked ? ch : GLYPHS[Math.floor(Math.random() * GLYPHS.length)]))
          .join("");
      }
      raf = requestAnimationFrame(tick);
    };
    tick(start);
  });
});

Next experiment

Pointer mask →