Created
March 6, 2021 08:13
-
-
Save aneelkkhatri/3a38b0a7dd14978ff8224e9bafc173f9 to your computer and use it in GitHub Desktop.
Trailing Letters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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