-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
123 lines (106 loc) · 2.41 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = innerWidth;
canvas.height = innerHeight;
const colorArray = [
"#9139B4",
"#FE3E6D",
"#DA6D73",
"#DCB687",
"#42A6D8",
"#9b111e",
"#d8e2dc",
];
addEventListener("resize", () => {
canvas.width = innerWidth;
canvas.height = innerHeight;
init();
});
//delete all particles with space bar
document.addEventListener("keydown", (event) => {
if (event.code === "Space") {
particlesArray = [];
}
});
function getRandomInt(min, max) {
let randomInt = Math.floor(Math.random() * (max - min + 1) + min);
if (randomInt === 0) {
getRandomInt(min, max); //need to improve this
} else {
return randomInt;
}
}
function getRandomColor(colorsArray) {
return colorArray[Math.floor(Math.random() * colorArray.length)];
}
function getDistance(x1, y1, x2, y2) {
const xDististance = x2 - x1;
const yDististance = y2 - y1;
return Math.hypot(xDististance, yDististance);
}
class Particle {
constructor(x, y, directionX, directionY, radius, color) {
this.x = x;
this.y = y;
this.directionY = directionY;
this.directionX = directionX;
this.radius = radius;
this.color = color;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = this.color;
ctx.fill();
ctx.strokeStyle = this.color;
ctx.stroke();
ctx.closePath();
}
update() {
let step = getRandomInt(1, 4); //random step
if (step === 1) {
this.y += this.directionY;
}
if (step === 2) {
this.x += this.directionX;
}
if (step === 3) {
this.y -= this.directionY;
}
if (step === 4) {
this.x -= this.directionX;
}
this.draw();
}
}
// Implementation
let particlesArray;
function init() {
particlesArray = [];
for (let i = 0; i < 850; i++) {
let radius = 0.1;
let x = getRandomInt(20, canvas.width - this.radius);
let y = getRandomInt(0, canvas.height - this.radius);
let directionX = getRandomInt(-3, 3);
let directionY = getRandomInt(-3, 3);
particlesArray.push(
new Particle(
x,
y,
directionX,
directionY,
radius,
getRandomColor(colorArray)
)
);
}
}
// Animation Loop
function animate() {
requestAnimationFrame(animate);
particlesArray.forEach((ptcl) => {
ptcl.update();
});
}
init();
animate();