w3ps1/src/lib/helpers.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-02-16 13:26:53 +01:00
import removeMd from 'remove-markdown'
2023-02-16 12:29:17 +01:00
export function rand(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
2023-02-16 13:26:53 +01:00
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
2023-02-16 12:29:17 +01:00
}
return result;
}
export function animateText (ev) {
if (!ev.target.getAttribute('data-text')) {
ev.target.setAttribute('data-text', ev.target.innerHTML)
}
if (ev.target.getAttribute('data-animate') === "1") {
return;
}
ev.target.setAttribute('data-animate', "1")
2023-02-16 13:26:53 +01:00
const orig = removeMd(ev.target.getAttribute('data-text'))
2023-02-16 12:29:17 +01:00
const steps = orig.length
2023-02-16 12:58:21 +01:00
const genRand = (pos = 0, len = null) => orig.substring(pos, len).split(' ').map(x => rand(x.length)).join(' ')
const random = genRand(0, orig.length)
2023-02-16 12:29:17 +01:00
ev.target.innerHTML = random
for (let i = 0; i <= steps; i++) {
setTimeout(() => {
2023-02-16 12:58:21 +01:00
ev.target.innerHTML = orig.substring(0, i) + genRand(i, orig.length)
2023-02-16 13:10:11 +01:00
//console.log(ev.target.innerHTML)
2023-02-16 12:29:17 +01:00
if (i === steps) {
ev.target.setAttribute('data-animate', "0")
}
}, 50*i)
}
}