-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (127 loc) · 3.9 KB
/
Copy pathscript.js
File metadata and controls
149 lines (127 loc) · 3.9 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
const canvas = document.getElementById('galaxy');
const ctx = canvas.getContext('2d');
let width = canvas.width = window.innerWidth;
let height = canvas.height = window.innerHeight;
// Resize canvas on window resize
window.addEventListener('resize', () => {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
});
// Mouse position
let mouseX = width / 2;
let mouseY = height / 2;
window.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
});
window.addEventListener('click', (e) => {
createShootingStar(e.clientX, e.clientY);
});
// Star class
class Star {
constructor(layer) {
this.x = Math.random() * width;
this.y = Math.random() * height;
this.size = Math.random() * 2 * (3 - layer);
this.opacity = Math.random() * 0.8 + 0.2;
this.twinkleSpeed = Math.random() * 0.02 + 0.005;
this.twinkleOffset = Math.random() * Math.PI * 2;
this.layer = layer; // 1 = far, 2 = mid, 3 = near
}
update() {
// Parallax effect based on mouse and layer
const dx = (mouseX - width / 2) * (this.layer * 0.05);
const dy = (mouseY - height / 2) * (this.layer * 0.05);
this.x += (this.x - width / 2 + dx) * 0.0001 * this.layer;
this.y += (this.y - height / 2 + dy) * 0.0001 * this.layer;
// Wrap around screen
if (this.x < 0) this.x = width;
if (this.x > width) this.x = 0;
if (this.y < 0) this.y = height;
if (this.y > height) this.y = 0;
}
draw() {
const alpha = (Math.sin(Date.now() * this.twinkleSpeed + this.twinkleOffset) + 1) * 0.5 * this.opacity;
ctx.globalAlpha = alpha;
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.globalAlpha = 1;
}
}
// Shooting Star
function createShootingStar(x, y) {
const star = {
x,
y,
length: 0,
speed: 15,
angle: Math.random() * 0.2 + 0.1,
direction: Math.random() > 0.5 ? 1 : -1,
life: 30
};
const animate = () => {
ctx.globalCompositeOperation = 'lighter';
ctx.strokeStyle = 'rgba(255, 255, 255, 0.8)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(star.x, star.y);
const nx = star.x + Math.cos(star.angle) * star.direction * star.length;
const ny = star.y + Math.sin(star.angle) * star.direction * star.length;
ctx.lineTo(nx, ny);
ctx.stroke();
ctx.globalCompositeOperation = 'source-over';
star.length += star.speed;
star.life--;
if (star.life > 0) {
requestAnimationFrame(animate);
}
};
animate();
}
// Create stars
const stars = [];
for (let layer = 1; layer <= 3; layer++) {
const count = layer === 1 ? 200 : layer === 2 ? 100 : 50;
for (let i = 0; i < count; i++) {
stars.push(new Star(layer));
}
}
// Draw constellations
const constellations = [
[{ x: 0.1, y: 0.2 }, { x: 0.2, y: 0.5 }, { x: 0.3, y: 0.3 }, { x: 0.4, y: 0.7 }],
[{ x: 0.7, y: 0.1 }, { x: 0.8, y: 0.4 }, { x: 0.9, y: 0.2 }],
[{ x: 0.5, y: 0.8 }, { x: 0.6, y: 0.6 }, { x: 0.75, y: 0.9 }, { x: 0.9, y: 0.7 }]
];
function drawConstellations() {
ctx.strokeStyle = 'rgba(255, 255, 255, 0.2)';
ctx.lineWidth = 1;
constellations.forEach(stars => {
ctx.beginPath();
stars.forEach((point, i) => {
const x = point.x * width + (mouseX - width / 2) * 0.02;
const y = point.y * height + (mouseY - height / 2) * 0.02;
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.stroke();
});
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, width, height);
// Draw deep space gradient background
const gradient = ctx.createRadialGradient(width/2, height/2, 0, width/2, height/2, width);
gradient.addColorStop(0, '#111');
gradient.addColorStop(1, '#000');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
stars.forEach(star => {
star.update();
star.draw();
});
drawConstellations();
requestAnimationFrame(animate);
}
animate();