-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (148 loc) · 5.44 KB
/
Copy pathscript.js
File metadata and controls
166 lines (148 loc) · 5.44 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
/* ============================================================
THE LIFE & DEATH OF A STAR — interactions
============================================================ */
/* ---------- 1. Animated starfield ---------- */
(function starfield() {
const canvas = document.getElementById('starfield');
const ctx = canvas.getContext('2d');
let w, h, stars, shootTimer = 0;
function resize() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
const count = Math.min(260, Math.floor((w * h) / 6000));
stars = Array.from({ length: count }, () => ({
x: Math.random() * w,
y: Math.random() * h,
r: Math.random() * 1.4 + 0.3,
tw: Math.random() * Math.PI * 2,
sp: Math.random() * 0.015 + 0.004,
z: Math.random() * 0.5 + 0.5
}));
}
let shooters = [];
function spawnShooter() {
shooters.push({
x: Math.random() * w,
y: Math.random() * h * 0.5,
len: Math.random() * 120 + 80,
sp: Math.random() * 6 + 6,
life: 1
});
}
function draw() {
ctx.clearRect(0, 0, w, h);
for (const s of stars) {
s.tw += s.sp;
const a = 0.45 + Math.sin(s.tw) * 0.4;
ctx.beginPath();
ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255,255,255,${a * s.z})`;
ctx.fill();
}
// occasional shooting star
shootTimer++;
if (shootTimer > 180 && Math.random() < 0.02) { spawnShooter(); shootTimer = 0; }
shooters = shooters.filter(sh => sh.life > 0);
for (const sh of shooters) {
const dx = sh.sp, dy = sh.sp * 0.5;
const grad = ctx.createLinearGradient(sh.x, sh.y, sh.x - sh.len, sh.y - sh.len * 0.5);
grad.addColorStop(0, `rgba(255,255,255,${sh.life})`);
grad.addColorStop(1, 'rgba(255,255,255,0)');
ctx.strokeStyle = grad;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(sh.x, sh.y);
ctx.lineTo(sh.x - sh.len, sh.y - sh.len * 0.5);
ctx.stroke();
sh.x += dx; sh.y += dy; sh.life -= 0.012;
}
requestAnimationFrame(draw);
}
window.addEventListener('resize', resize);
resize();
draw();
})();
/* ---------- 2. Scroll progress bar ---------- */
const progressBar = document.getElementById('progress-bar');
window.addEventListener('scroll', () => {
const scrolled = document.documentElement.scrollTop;
const max = document.documentElement.scrollHeight - window.innerHeight;
progressBar.style.width = (scrolled / max) * 100 + '%';
}, { passive: true });
/* ---------- 3. Reveal-on-scroll + accent theming ---------- */
const root = document.documentElement;
const sections = document.querySelectorAll('.stage, .outro');
const revealObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('reveal');
const accent = entry.target.getAttribute('data-accent');
if (accent) root.style.setProperty('--accent', accent);
}
});
}, { threshold: 0.25 });
sections.forEach(s => revealObserver.observe(s));
/* ---------- 4. Active timeline dot ---------- */
const navLinks = document.querySelectorAll('#timeline-nav a');
const stageMap = {};
navLinks.forEach(link => {
const id = link.getAttribute('href').slice(1);
stageMap[id] = link;
});
const navObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
navLinks.forEach(l => l.classList.remove('active'));
const link = stageMap[entry.target.id];
if (link) link.classList.add('active');
}
});
}, { threshold: 0.5 });
document.querySelectorAll('section[id^="stage"]').forEach(s => navObserver.observe(s));
/* ---------- 5. Supernova "detonate" button ---------- */
const detonateBtn = document.getElementById('detonate');
if (detonateBtn) {
// create overlay element once
const overlay = document.createElement('div');
overlay.id = 'flash-overlay';
document.body.appendChild(overlay);
detonateBtn.addEventListener('click', () => {
overlay.classList.remove('boom');
// force reflow so the animation can restart
void overlay.offsetWidth;
overlay.classList.add('boom');
spawnExplosionParticles();
});
function spawnExplosionParticles() {
const N = 40;
for (let i = 0; i < N; i++) {
const p = document.createElement('div');
const angle = (i / N) * Math.PI * 2;
const dist = 120 + Math.random() * 220;
const size = Math.random() * 6 + 2;
p.style.cssText = `
position:fixed; left:50%; top:50%; z-index:201;
width:${size}px; height:${size}px; border-radius:50%;
background:hsl(${Math.random() * 50 + 10},100%,${60 + Math.random() * 30}%);
box-shadow:0 0 8px currentColor; pointer-events:none;
transition:transform 1.4s cubic-bezier(.1,.7,.3,1), opacity 1.4s ease;
`;
document.body.appendChild(p);
requestAnimationFrame(() => {
p.style.transform = `translate(${Math.cos(angle) * dist}px, ${Math.sin(angle) * dist}px)`;
p.style.opacity = '0';
});
setTimeout(() => p.remove(), 1500);
}
}
}
/* ---------- 6. Hero parallax on the star ---------- */
const heroStar = document.querySelector('.hero-star');
window.addEventListener('scroll', () => {
if (!heroStar) return;
const y = window.scrollY;
if (y < window.innerHeight) {
heroStar.style.transform = `translateY(${y * 0.3}px) scale(${1 - y / 2400})`;
heroStar.style.opacity = String(1 - y / (window.innerHeight * 0.8));
}
}, { passive: true });