all repos — retro-website @ main

res/js/slow_load.js (view raw)

 1const loadAudio = [
 2    new Audio(url = window.location.origin + "/res/sfx/os/loadelement1.ogg"),
 3    new Audio(url = window.location.origin + "/res/sfx/os/loadelement2.ogg"),
 4    new Audio(url = window.location.origin + "/res/sfx/os/loadelement3.ogg"),
 5    new Audio(url = window.location.origin + "/res/sfx/os/loadelement4.ogg")
 6]
 7const not_allowed = ["AUDIO", "META", "TITLE", "LINK", "SOURCE", "SCRIPT", "BR"];
 8
 9function getRandomInt(min, max) {
10    min = Math.ceil(min);
11    return Math.floor(Math.random() * (Math.floor(max) - min + 1)) + min;
12}
13
14function playAudio(audio){
15    audio.volume = 0.1;
16    audio.currentTime = 0;
17    audio.play().catch(()=>{});
18}
19
20const all = document.getElementsByTagName("*");
21let total = 0;
22
23function hide_all() {
24    total = 0;
25    for (i in all) {
26        const node = all[i];
27        if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
28            total++;
29            node.setAttribute("data-opacity", node.style.opacity);
30            node.style.opacity = "0";
31        }
32    }
33}
34
35function start_loading(speed, not_allowed=[], callback) {
36
37    let counter = 0;
38    let i = 0;
39
40    function onNodeLoaded(node){
41        node.style.transition = "opacity .3s ease-out";
42        node.style.opacity = node.getAttribute("data-opacity");
43        
44        counter++;
45        const ratio = counter / total * 100;
46        
47        playAudio(loadAudio[getRandomInt(0, loadAudio.length - 1)]);
48        
49        if (ratio >= 100 && callback)
50            callback()
51    }
52    
53    for (element in all) {
54        const node = all[element];
55        if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
56            i += getRandomInt(100 / speed, 550 / speed);
57            setTimeout(() => { onNodeLoaded(node);}, i);
58        }
59    }
60}