-
Notifications
You must be signed in to change notification settings - Fork 0
/
stars.js
51 lines (45 loc) · 931 Bytes
/
stars.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
class Star {
constructor(x) {
this.size = random(3, 6);
this.x = x;
this.y = random(0, window.innerHeight / 2);
}
draw() {
ellipse(this.x, this.y, this.size, this.size);
}
}
class Stars {
constructor() {
this.array = [];
this.seed();
this.opacity = 0;
this.fadeSpeed = 0.005;
}
newStar(x) {
this.array.push(new Star(x));
}
seed() {
for (let i = 0; i < width / 30; i++) {
this.newStar(random(0, width));
}
}
get color() {
return `rgba(255, 255, 255, ${this.opacity})`;
}
draw(isNight) {
if (isNight && frameCount % 2 === 0) {
this.opacity += this.fadeSpeed;
}
if (!isNight && frameCount % 2 === 0) {
this.opacity -= this.fadeSpeed;
}
this.opacity = constrain(this.opacity, 0, 1);
push();
fill(this.color);
noStroke();
for (let star of this.array) {
star.draw(isNight);
}
pop();
}
}