-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
99 lines (89 loc) · 2.75 KB
/
background.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// From https://codepen.io/tahazsh/pen/gOqNZyw
const MIN_SPEED = 0.5;
const MAX_SPEED = 0.7;
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
const container = document.querySelector(".bouncing-blobs");
// create blue blobs
const colors = ["1C66BF", "144D91", "17447A", "4D80BD", "4674AB"];
const am = Math.floor(randomNumber(4, 6));
for (let i = 0; i < am; i++) {
const el = document.createElement("div");
el.classList.add("bouncing-blob");
el.style.background =
"#" + colors[Math.floor(randomNumber(0, colors.length - 1))];
console.log(`I am a ${el.style.background} blob ! I'm blob no ${i}`);
container.appendChild(el);
}
// create gray blobs
const am2 = Math.floor(randomNumber(2, 3));
for (let i = 0; i < am2; i++) {
const el2 = document.createElement("div");
el2.classList.add("bouncing-blob", "bouncing-blob--gray");
container.appendChild(el2);
}
console.log(`Created ${am} blue blobs and ${am2} gray blobs`);
let paused = false;
function pauseResumeBg(p) {
console.log("Background", p ? "paused" : "resumed");
paused = p;
if (!paused) {
document.querySelector(".bouncing-blobs-container").style.display = "";
requestAnimationFrame(update);
} else {
document.querySelector(".bouncing-blobs-container").style.display = "none";
}
}
class Blob {
constructor(el) {
this.el = el;
const boundingRect = this.el.getBoundingClientRect();
this.size = boundingRect.width;
this.initialX = randomNumber(0, window.innerWidth - this.size);
this.initialY = randomNumber(0, window.innerHeight - this.size);
this.el.style.top = `${this.initialY}px`;
this.el.style.left = `${this.initialX}px`;
this.vx =
randomNumber(MIN_SPEED, MAX_SPEED) * (Math.random() > 0.5 ? 1 : -1);
this.vy =
randomNumber(MIN_SPEED, MAX_SPEED) * (Math.random() > 0.5 ? 1 : -1);
this.x = this.initialX;
this.y = this.initialY;
}
update() {
this.x += this.vx;
this.y += this.vy;
if (this.x >= window.innerWidth - this.size) {
this.x = window.innerWidth - this.size;
this.vx *= -1;
}
if (this.y >= window.innerHeight - this.size) {
this.y = window.innerHeight - this.size;
this.vy *= -1;
}
if (this.x <= 0) {
this.x = 0;
this.vx *= -1;
}
if (this.y <= 0) {
this.y = 0;
this.vy *= -1;
}
}
move() {
this.el.style.transform = `translate(${this.x - this.initialX}px, ${
this.y - this.initialY
}px)`;
}
}
const blobEls = document.querySelectorAll(".bouncing-blob");
const blobs = Array.from(blobEls).map((blobEl) => new Blob(blobEl));
function update() {
if (!paused) requestAnimationFrame(update);
blobs.forEach((blob) => {
blob.update();
blob.move();
});
}
requestAnimationFrame(update);