summaryrefslogtreecommitdiff
path: root/assets/js/transitions.js
blob: e6d7c85608203692e766559933e7e54aa34843ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const displacement = document.getElementById("displacement");
const noise = document.getElementById("noise");


var nextFunc = () => {}
function transitionOut(callback) {
    document.body.style.filter = "url(#transition)"
    const src = new Number(displacement.getAttribute("scale"));
    noise.setAttribute("seed",Math.random() * 1000)
    if (src < 1000) {
        displacement.setAttribute("scale", src + 10 + src);
        nextFunc = () => transitionOut(callback)
        requestAnimationFrame(nextFunc)
    } else {
        setTimeout(() =>{
            document.body.style.filter = ""
        },10000);
        callback()
    }
}

function transitionIn() {
    document.body.style.filter = "url(#transition)"
    const src = new Number(displacement.getAttribute("scale"));
    noise.setAttribute("seed",Math.random() * 1000)
    if (src > 0) {
        displacement.setAttribute("scale", src - 1 - (Math.max(src,1)/4));
        nextFunc = () => transitionIn();
        requestAnimationFrame(nextFunc)
    } else {
        displacement.setAttribute("scale", 0);
        document.body.style.filter = ""
    }
}

if (noise && displacement) {
    displacement.setAttribute("scale", 200);
    transitionIn()
    const links = document.querySelectorAll("a");
    links.forEach(l => {    
        if (l.hasAttribute("no-transition")) return;
        if (l.href.startsWith(location.origin)) {
            l.addEventListener("click", (e)=>{
                e.preventDefault()
                displacement.setAttribute("scale", 0);
                transitionOut(() => {
                    location.href = l.href
                });
            })
        }
    })
} else {
    console.log("Can't do transitions!!")
}