summaryrefslogtreecommitdiff
path: root/src/js/randomblob.js
blob: 312c9612d1df0e5135fe69498d84fecb4e1d035a (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
let isFirstTime = true;
var mainInterval;

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function setRandomBlob() {
    blob.style.width = getRandomInt(50, 100) + '%';
    blob.style.height = getRandomInt(50, 100) + '%';
    blob.style.rotate = getRandomInt(0, 360) + 'deg';
}

function randomPosition(element) {
    var x = getRandomInt(0, window.innerWidth);
    var y = getRandomInt(0, window.innerHeight);
    element.style.position = 'absolute';
    element.style.left = x + 'px';
    element.style.top = y + 'px';
}

function resetBlob() {
    setRandomBlob();
    blob.style.background = generateGradient();
    randomPosition(blob);
    // fadeIn
    blob.animate([
        // keyframes
        { opacity: 0 }, 
        { opacity: .40 }
    ], { 
        // timing options
        duration: 3000,
        iterations: 1,
        fill: 'forwards'
    });
}


function main() {
    if (!isFirstTime) {
        // fadeOut
        let fadeOutAnimation = blob.animate([
            // keyframes
            { opacity: .40 }, 
            { opacity: 0, scale: .8}
        ], { 
            // timing options
            duration: 3000,
            iterations: 1,
            fill: 'forwards'
        });
        // resetBlob
        fadeOutAnimation.onfinish = () => {
            resetBlob()
        };
    } else {
        // resetBlob
        resetBlob()
        isFirstTime = false;
    }
    mainInterval = setTimeout(main, getRandomInt(7500, 30000));
}

blob.addEventListener('click', function() {
    clearTimeout(mainInterval)
    main(getRandomInt(7500, 30000));
});

main();