-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (161 loc) · 5.27 KB
/
script.js
File metadata and controls
187 lines (161 loc) · 5.27 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { drawBackGround } from "./background.js";
const canvas = document.getElementById("starShower");
const ctx = canvas.getContext("2d");
const btnIncreaseSpeed = document.getElementById("btnIncreaseSpeed")
canvas.width = innerWidth;
canvas.height = innerHeight;
addEventListener("resize", () => {
canvas.width = innerWidth;
canvas.height = innerHeight;
})
class Star {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.targetRadius = radius;
this.radius = radius;
this.color = color;
this.velocity = velocity;
this.gravity = 0.5;
this.friction = 0.72;
this.shrinkSpeed = 0.8;
}
draw() {
ctx.save();
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.shadowColor = this.color;
ctx.shadowBlur = 20;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
ctx.restore();
}
update() {
this.draw();
this.x += this.velocity.x;
this.y += this.velocity.y;
if (this.targetRadius < this.radius) {
this.radius = this.radius - this.shrinkSpeed < this.targetRadius ? this.targetRadius : this.radius - this.shrinkSpeed;
}
}
}
class MiniStar {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
this.friction = 0.88;
this.gravity = 0.5;
this.timeToLive = 1;
}
draw() {
ctx.save()
ctx.globalAlpha = this.timeToLive;
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.shadowColor = this.color;
ctx.shadowBlur = 20;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
ctx.restore();
}
update() {
this.draw();
this.x += this.velocity.x;
this.y += this.velocity.y;
this.timeToLive -= 0.008;
}
}
let stars = [];
let miniStars = [];
let timer = 1;
let spawnRate = 150;
let animationID;
function spawnStars() {
let radius = Math.floor((Math.random() * (20 - 10 + 1))) + 10;
let x = Math.floor(Math.random() * ((canvas.width - 25) - 25 + 1)) + 25;
let y = -100;
let color = "#E3EAEF";
let velocity = {
x: Math.random() > 0.5 ? (Math.random() * (10 - 2 + 1)) + 2 : (Math.random() * ((-10) - (-2) + 1)) + (-2),
y: 2
};
stars.push(new Star(x, y, radius, color, velocity))
}
spawnStars()
function animate() {
animationID = requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBackGround();
// Draw stars
for (let i = 0; i < stars.length; i++) {
stars[i].update();
// Collision between star and wall
if (stars[i].x + stars[i].velocity.x - stars[i].radius < 0 || stars[i].x + stars[i].velocity.x + stars[i].radius > canvas.width) {
stars[i].velocity.x = -stars[i].velocity.x * stars[i].friction;
// Explosion
for (let j = 0; j < stars[i].radius * 1.2; j++) {
miniStars.push(new MiniStar(stars[i].x, stars[i].y, 2, stars[i].color, { x: (Math.random() * (5 - (-5) + 1) + (-5)), y: (Math.random() * (25 - (-25) + 1) + (-25)) }))
}
// Shrink and remove star
if (stars[i].radius - 3 > 3) {
stars[i].targetRadius -= 3;
} else {
stars.splice(i, 1);
i--;
continue;
}
}
// Collsion between star and ground
if (stars[i].y + stars[i].radius + stars[i].velocity.y > canvas.height - 100) {
stars[i].velocity.y = -stars[i].velocity.y * stars[i].friction;
// Explosion
for (let j = 0; j < stars[i].radius * 1.2; j++) {
miniStars.push(new MiniStar(stars[i].x, stars[i].y, 2, stars[i].color, { x: (Math.random() * (5 - (-5) + 1) + (-5)), y: (Math.random() * (25 - (-25) + 1) + (-25)) }))
}
// Shrink and remove star
if (stars[i].radius - 3 > 3) {
stars[i].targetRadius -= 3;
} else {
stars.splice(i, 1);
i--;
continue;
}
} else {
stars[i].velocity.y += stars[i].gravity;
}
if(stars[i].radius < 0) {
stars.splice(i, 1);
i--;
}
}
// Draw miniStars
for (let i = 0; i < miniStars.length; i++) {
miniStars[i].update();
// Collsion between miniStar and ground
if (miniStars[i].y + miniStars[i].radius + miniStars[i].velocity.y > canvas.height - 100) {
miniStars[i].velocity.y = -miniStars[i].velocity.y * miniStars[i].friction
} else {
miniStars[i].velocity.y += miniStars[i].gravity;
}
// Remove miniStar
if (miniStars[i].timeToLive <= 0) {
miniStars.splice(i, 1);
i--;
}
}
timer++;
if (timer % spawnRate == 0 && spawnRate > 1) {
spawnStars();
timer = 1;
}
}
animate();
btnIncreaseSpeed.addEventListener("click", () => {
spawnRate = Math.floor(spawnRate / 1.14);
timer = 1;
})