JSFiddle - React, Tailwind, and code Playground
HTML
<!doctype html>
<style>
.trail { /* className for the trail elements */
position: absolute;
height: 6px; width: 6px;
border-radius: 3px;
background: teal;
}
body {
height: 300px;
}
</style>
<script>
const text = "so long and thanks for all the fish";
const charNodes = [...text].map( char => {
let charNode = document.createElement("div");
charNode.className = "trail";
charNode.style.display = "none";
charNode.style.left = "0px";
charNode.style.top = "0px";
charNode.appendChild(document.createTextNode(char));
document.body.appendChild(charNode);
return charNode;
});
const charTimers = [...text].map(() => null);
let charIndex = 0;
let mouseTimeout = null;
function mouseTrailer(event) {
if (mouseTimeout == null) {
if (charTimers[charIndex] != null) clearTimeout(charTimers[charIndex]);
let charNode = charNodes[charIndex];
charNode.style.display = "block";
charNode.style.left = event.clientX + "px";
charNode.style.top = event.clientY + "px";
charTimers[charIndex] = setTimeout((node) => {
node.style.display = "none";
charTimers[charIndex] = null;
}, 2000, charNode);
charIndex = (charIndex + 1) % charNodes.length;
mouseTimeout = setTimeout(() => mouseTimeout = null, 10);
}
}
document.addEventListener("mousemove", mouseTrailer);
</script>