-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstar-background.js
More file actions
47 lines (37 loc) · 1.06 KB
/
star-background.js
File metadata and controls
47 lines (37 loc) · 1.06 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
const canvas = document.getElementById("starfield");
const ctx = canvas.getContext("2d");
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener("resize", resize);
resize();
const STAR_COUNT = 100;
const STAR_OPACITY = 1;
const stars = [];
for (let i = 0; i < STAR_COUNT; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * 1 + 0.2,
speed: Math.random() * 0.3 + 0.05,
brightness: Math.random() * 0.4 + 0.3,
});
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let s of stars) {
s.y += s.speed;
if (s.y > canvas.height) {
s.y = 0;
s.x = Math.random() * canvas.width;
}
ctx.beginPath();
ctx.arc(s.x, s.y, s.size, 0, Math.PI * 2);
const gray = Math.floor(s.brightness * 255);
ctx.fillStyle = `rgba(${gray}, ${gray}, ${gray}, ${STAR_OPACITY})`;
ctx.fill();
}
requestAnimationFrame(animate);
}
animate();