Last active
March 20, 2019 04:42
-
-
Save graciano/654c26f621faf8ccfbaf39ba268095cf to your computer and use it in GitHub Desktop.
efeito misturando caracteres de um elemento html
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
const CYBER_MIN_DURATION = 30; | |
const CYBER_MAX_DURATION = 70; | |
const CYBER_CHARS = [...`10!@#$%*()£¢¬{[]}^<>.;:?/|\\-_=+§`]; | |
const random = max => parseInt(Math.random() * max); | |
const randomRange = (a, b) => a + random(b - a); | |
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
const randomElem = arr => arr[random((arr.length - 1))]; | |
const range = num => [...Array(num).keys()]; | |
const changeCharFromPos = (text, char, pos) => text.substring(0, pos) + char | |
+ text.substring(pos+1, text.length); | |
const changeChar = text => changeCharFromPos( | |
text, | |
randomElem(CYBER_CHARS), | |
random(text.length) | |
); | |
const changeRandomNumberOfChars = text => range(random(5)) | |
.reduce(acc => changeChar(acc), text); | |
const cyberShuffle = async (elem, time) => { | |
let timer = 0; | |
const originalText = elem.textContent; | |
while (true) { | |
const tick = randomRange(CYBER_MIN_DURATION, CYBER_MAX_DURATION); | |
timer += tick; | |
if (timer > time) break; | |
await sleep(tick); | |
elem.textContent = changeRandomNumberOfChars(elem.textContent); | |
} | |
elem.textContent = originalText; | |
}; | |
const loopCyberShuffle = async () => { | |
const cyberNodes = Array.from(document.querySelectorAll('.cyber-shuffle')); | |
return cyberShuffle(randomElem(cyberNodes), 1000); | |
}; | |
setInterval(loopCyberShuffle, 4000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment