Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (40 loc) · 1.42 KB
/
Copy pathscript.js
File metadata and controls
48 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Builds split-flap style character cells for elements with [data-flap],
// then runs a short mechanical-looking flip through random glyphs before
// settling on the real text. Skips the animation for reduced-motion users.
(function () {
const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789>-";
const prefersReduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function randomChar() {
return CHARSET[Math.floor(Math.random() * CHARSET.length)];
}
function buildRow(el) {
const text = el.getAttribute("data-flap") || "";
const chars = text.split("");
el.innerHTML = "";
const cells = chars.map((ch) => {
const span = document.createElement("span");
span.className = "flap" + (ch === " " ? " is-space" : "");
span.textContent = prefersReduced ? ch : " ";
el.appendChild(span);
return { span, ch };
});
if (prefersReduced) return;
cells.forEach(({ span, ch }, i) => {
const delay = i * 45;
const ticks = 5 + Math.floor(Math.random() * 4);
let tick = 0;
window.setTimeout(function spin() {
if (ch === " ") {
span.textContent = " ";
return;
}
tick++;
span.textContent = tick >= ticks ? ch : randomChar();
if (tick < ticks) {
window.setTimeout(spin, 55);
}
}, delay);
});
}
document.querySelectorAll("[data-flap]").forEach(buildRow);
})();