w3ps1/src/lib/helpers.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-02-23 14:29:23 +01:00
import removeMd from 'remove-markdown';
2023-02-16 13:26:53 +01:00
2023-02-16 12:29:17 +01:00
export function rand(length) {
2023-02-23 14:29:23 +01:00
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
export function animateText(ev, interval = 50) {
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');
const orig = removeMd(ev.target.getAttribute('data-text')).replace('&amp;', '&');
const steps = orig.length;
const genRand = (pos = 0, len = null) =>
orig
.substring(pos, len)
.split(' ')
.map((x) => rand(x.length))
.join(' ');
const random = genRand(0, orig.length);
ev.target.innerHTML = random;
for (let i = 0; i <= steps; i++) {
setTimeout(() => {
ev.target.innerHTML = orig.substring(0, i) + genRand(i, orig.length);
//console.log(ev.target.innerHTML)
if (i === steps) {
ev.target.setAttribute('data-animate', '0');
}
}, interval * i);
}
2023-02-16 12:29:17 +01:00
}
2023-02-23 14:29:23 +01:00
export async function handleAnchorClick(event) {
event.preventDefault();
const link = event.currentTarget;
const anchorId = new URL(link.href).hash.replace('#', '');
const anchor = document.getElementById(anchorId || 'intro');
return window.scrollTo({
top: anchor.offsetTop,
behavior: 'smooth'
});
2023-02-21 12:51:42 +01:00
}
2023-02-23 14:29:23 +01:00
export function animateSection(interval = 50) {
return (el) => {
for (const e of el.target.getElementsByClassName('animate-section')) {
animateText({ target: e }, interval);
}
};
}