-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (55 loc) · 1.58 KB
/
script.js
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
//Zowie!
const canvas = document.getElementById('bubbles');
const gl = canvas.getContext('2d');
const randomRange = (min, max) => {
return Math.random() * (max - min) + min;
};
let bubbles = [];
for (let bubbleIndex = 0; bubbleIndex < 128; bubbleIndex++) {
bubbles.push([
bubbleIndex / 128,
randomRange(0, 1),
randomRange(0.05, 0.1),
randomRange(0.00125, 0.0025),
0,
]);
}
let mouseX = 0.5;
let mouseY = 0.5;
canvas.onmousemove = event => {
mouseX = event.clientX / window.innerWidth;
mouseY = event.clientY / window.innerHeight;
};
setInterval(() => {
const width = window.innerWidth;
const height = window.innerHeight;
const lesser = width < height ? width : height;
canvas.width = width;
canvas.height = height;
gl.fillStyle = '#291a10';
gl.fillRect(0, 0, width, height);
//gl.fillStyle = "#ffffff";
bubbles.forEach(bubble => {
if (bubble[1] > 1 + bubble[2]) {
bubble[2] = randomRange(0.05, 0.1);
bubble[3] = randomRange(0.00125, 0.0025);
bubble[0] = randomRange(0, 1);
bubble[4] = 0;
bubble[1] = -bubble[2];
}
if (Math.abs(mouseY - bubble[1]) < bubble[2] * 1.1) {
if (Math.abs(mouseX - bubble[0]) < bubble[2] * 1.1) {
bubble[4] -= (mouseX - bubble[0]) / 100;
}
}
const offset = (lesser * bubble[2]) / 2;
bubble[0] += bubble[4];
bubble[4] *= 0.975;
bubble[1] += bubble[3];
//Draw the bubble with some detail
gl.fillStyle = '#302117';
gl.beginPath();
gl.arc(bubble[0] * width, bubble[1] * height, offset * 2, 0, 2 * Math.PI);
gl.fill();
});
}, 16);