-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstars.js
170 lines (146 loc) · 4.43 KB
/
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
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
// Stars and Meteors Animation Code
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let stars = [];
let meteors = [];
const starCount = 300;
const minSpeed = 10;
const maxSpeed = 30;
const minDelay = 1200;
const maxDelay = 4200;
const starColor = "#9E00FF";
const trailColor = "#2EB9DF";
const starWidth = 10;
const starHeight = 1;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function createStars() {
for (let i = 0; i < starCount; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: Math.random() * 1.5,
brightness: Math.random()
});
}
}
function drawStars() {
stars.forEach(star => {
star.brightness = 0.5 + 0.5 * Math.sin(Date.now() * 0.002 + star.y * 0.01);
ctx.fillStyle = `rgba(255, 255, 255, ${star.brightness})`;
ctx.beginPath();
ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2);
ctx.fill();
});
}
function getRandomStartPoint() {
const side = Math.floor(Math.random() * 4);
let x, y, angle;
const offsetX = Math.random() * canvas.width;
const offsetY = Math.random() * canvas.height;
switch (side) {
case 0: // top
x = offsetX;
y = 0;
angle = Math.random() * 90 + 45; // Random angle between 45 and 135
break;
case 1: // right
x = canvas.width;
y = offsetY;
angle = Math.random() * 90 + 135; // Random angle between 135 and 225
break;
case 2: // bottom
x = offsetX;
y = canvas.height;
angle = Math.random() * 90 + 225; // Random angle between 225 and 315
break;
case 3: // left
x = 0;
y = offsetY;
angle = Math.random() * 90 + 315; // Random angle between 315 and 405
break;
default:
x = 0;
y = 0;
angle = 45;
}
return { x, y, angle };
}
function createMeteor() {
const { x, y, angle } = getRandomStartPoint();
const speed = Math.random() * (maxSpeed - minSpeed) + minSpeed;
const meteor = {
x: x,
y: y,
speed: speed,
angle: angle * (Math.PI / 180), // Convert degrees to radians
distance: 0,
scale: 1,
id: Date.now()
};
meteors.push(meteor);
}
function drawMeteors() {
meteors.forEach((meteor, index) => {
meteor.x += meteor.speed * Math.cos(meteor.angle);
meteor.y += meteor.speed * Math.sin(meteor.angle);
meteor.distance += meteor.speed;
meteor.scale = 1 + meteor.distance / 100;
// Remove meteor if it's off-screen
if (
meteor.x < -50 ||
meteor.x > canvas.width + 50 ||
meteor.y < -50 ||
meteor.y > canvas.height + 50
) {
meteors.splice(index, 1);
return;
}
// Save context
ctx.save();
// Move to meteor's position
ctx.translate(meteor.x, meteor.y);
// Rotate context
ctx.rotate(meteor.angle);
// Create gradient
const width = starWidth * meteor.scale;
const height = starHeight;
const gradient = ctx.createLinearGradient(0, 0, width, 0);
gradient.addColorStop(0, `rgba(${hexToRgb(trailColor)}, 0)`);
gradient.addColorStop(1, `rgba(${hexToRgb(starColor)}, 1)`);
ctx.fillStyle = gradient;
// Draw rectangle (meteor trail)
ctx.fillRect(0, -height / 2, width, height);
// Restore context
ctx.restore();
});
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawStars();
drawMeteors();
requestAnimationFrame(animate);
}
function scheduleMeteor() {
const randomDelay = Math.random() * (maxDelay - minDelay) + minDelay;
setTimeout(() => {
createMeteor();
scheduleMeteor();
}, randomDelay);
}
function hexToRgb(hex) {
// Convert hex color code to RGB values
let bigint = parseInt(hex.replace("#", ""), 16);
let r = (bigint >> 16) & 255;
let g = (bigint >> 8) & 255;
let b = bigint & 255;
return `${r}, ${g}, ${b}`;
}
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Start the stars and meteors animation
createStars();
animate();
scheduleMeteor();