[01]
Scramble text
Random glyphs settle into the real word, left to right. Used on the navigation and footer links.
- Vanilla JS
- No dependencies
View code · 2 files
index.html
<!-- A monospace font keeps the word from changing width while it scrambles -->
<a href="#" data-scramble>Hover me</a>scramble.js
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);
});
});