Skip to content

Instantly share code, notes, and snippets.

@IntegralPilot
Created May 14, 2025 12:14
Show Gist options
  • Select an option

  • Save IntegralPilot/103860b7296c9d47ae82394979aeafa0 to your computer and use it in GitHub Desktop.

Select an option

Save IntegralPilot/103860b7296c9d47ae82394979aeafa0 to your computer and use it in GitHub Desktop.
Hacked Text Effect - JavaScript
// Makes a H1 element have a "hacked" effect on hover.
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let interval = null;
document.querySelector("h1").onmouseover = event => {
let iteration = 0;
const targetValue = event.target.dataset.value;
clearInterval(interval);
interval = setInterval(() => {
const currentTextInDOM = event.target.innerText;
const processingLength = Math.max(currentTextInDOM.length, targetValue.length);
event.target.innerText = Array.from({ length: processingLength })
.map((_, index) => {
if (index < iteration) {
return targetValue[index] || "";
}
return letters[Math.floor(Math.random() * 26)];
})
.join("");
if (iteration >= targetValue.length) {
clearInterval(interval);
event.target.innerText = targetValue;
}
iteration += 1 / 3;
}, 30);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment