-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (46 loc) · 1.28 KB
/
script.js
File metadata and controls
54 lines (46 loc) · 1.28 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
const canvas = document.getElementById('stars');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let stars = [];
const numStars = 512; // Increased number of stars
class Star {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.radius = Math.random() * 2;
this.dy = Math.random() * 0.7 + 0.3; // Slightly faster movement
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
}
update() {
this.y += this.dy;
if (this.y > canvas.height) {
this.y = 0; // Reset star to the top
this.x = Math.random() * canvas.width; // Randomize X position
}
this.draw();
}
}
function init() {
stars = [];
for (let i = 0; i < numStars; i++) {
stars.push(new Star());
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
stars.forEach(star => star.update());
requestAnimationFrame(animate);
}
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init();
});
init();
animate();