Skip to content

Instantly share code, notes, and snippets.

@aneelkkhatri
Created March 6, 2021 08:13
Show Gist options
  • Save aneelkkhatri/3a38b0a7dd14978ff8224e9bafc173f9 to your computer and use it in GitHub Desktop.
Save aneelkkhatri/3a38b0a7dd14978ff8224e9bafc173f9 to your computer and use it in GitHub Desktop.
Trailing Letters
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
let content = "ANEEL KUMAR";
let movingEls = content.split('').map(c => {
let d = document.createElement('div')
d.innerHTML = c;
d.style.position = "absolute";
return d;
})
movingEls.forEach(div => {
document.body.appendChild(div);
})
function move(div, params) {
div.style.left = (params.x)+'px';
div.style.top = params.y+'px';
}
const SPACE = 20;
// using approach 2
approach2();
function approach1() {
document.addEventListener('mousemove', e => {
let gap = 60;
for (var i = 0; i < movingEls.length; i++) {
setTimeout((i, SPACE) => {
move(movingEls[i], {
x: (SPACE+e.pageX),
y: e.pageY
})
}, gap * i, i, SPACE * i)
}
})
}
function approach2() {
const QUEUE_SIZE = 4*movingEls.length;
let pos = [];
for (var i = 0; i < QUEUE_SIZE; ++i) {
pos[i] = {x:0, x:0}
}
setInterval(() => {
for (var i = 0; i < movingEls.length; i++) {
move(movingEls[i], {
x: (i*SPACE+pos[QUEUE_SIZE - 4*i - 1].x),
y: pos[QUEUE_SIZE - 4*i - 1].y
})
}
pos.push(currentPos)
pos.shift();
},10)
let currentPos = {
x: 0,
y: 0
};
document.addEventListener('mousemove', e => {
currentPos = {
x: e.pageX,
y: e.pageY
};
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment