-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (71 loc) · 1.98 KB
/
script.js
File metadata and controls
84 lines (71 loc) · 1.98 KB
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
const gravity = 0.1;
const gravitySpeed = 0.95;
const colorArr = [
"#ffb3ff",
"#b3d9ff",
"#ffffcc",
"#c2f0c2",
"#ffb3b3",
"#dfbf9f",
"#b3b3cc"
];
class Circle {
constructor(x, y, dx, dy, radius, color) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.gravity = gravity;
this.radius = radius;
this.color = colorArr[Math.floor(Math.random() * colorArr.length)];
}
draw (context) {
context.beginPath();
context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
context.strokeStyle = this.color;
context.fillStyle = this.color;
context.fill();
context.stroke();
context.closePath();
}
update () {
if (this.y + this.radius + this.dy > window.innerHeight) {
this.dy = -this.dy;
this.dy = this.dy * gravitySpeed;
this.dx = this.dx * gravitySpeed;
} else {
this.dy += gravity;
}
if (this.x + this.radius >= window.innerWidth || this.x - this.radius <= 0) {
this.dx = -this.dx * gravitySpeed;
}
this.x += this.dx;
this.y += this.dy;
}
}
let canvas = document.querySelector("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let ctx = canvas.getContext("2d");
let circleArray = [];
window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
for (let i = 0; i < 300; i++) {
const x = Math.random() * window.innerWidth;
const y = Math.random() * window.innerHeight;
const dx = (Math.random() - 0.5) * 5;
const dy = (Math.random() - 0.5) * 5;
const radius = 20;
circleArray.push(new Circle(x, y, dx, dy, radius))
}
function animate() {
ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
window.requestAnimationFrame(animate);
for (let i = 0; i < circleArray.length; i++) {
circleArray[i].draw(ctx);
circleArray[i].update(ctx);
}
}
animate();