-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
285 lines (237 loc) · 9.91 KB
/
script.js
File metadata and controls
285 lines (237 loc) · 9.91 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Toast Notification
function showToast(message) {
const container = document.getElementById('toast-container');
if (!container) return;
// Create toast
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerHTML = `
<svg class="w-5 h-5 text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="width:20px; color:var(--success)">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>${message}</span>
`;
container.appendChild(toast);
// Animate out
setTimeout(() => {
toast.classList.add('toast-out');
toast.addEventListener('animationend', () => toast.remove());
}, 3000);
}
// Update Progress Bar
function updateProgress() {
const total = document.querySelectorAll('.step-item').length;
const completed = document.querySelectorAll('.step-item.completed').length;
const progress = total === 0 ? 0 : (completed / total) * 100;
const bar = document.getElementById('progressBar');
if (bar) bar.style.width = `${progress}% `;
}
// Sound Effect (Web Audio API)
function playSuccessSound() {
const AudioContext = window.AudioContext || window.webkitAudioContext;
if (!AudioContext) return;
const ctx = new AudioContext();
// Helper to create a soft tone
const playTone = (freq) => {
const osc = ctx.createOscillator();
const gainNode = ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, ctx.currentTime);
// Soft envelope: Gentle attack, smooth decay
gainNode.gain.setValueAtTime(0, ctx.currentTime);
gainNode.gain.linearRampToValueAtTime(0.15, ctx.currentTime + 0.05);
gainNode.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.5);
osc.connect(gainNode);
gainNode.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + 0.55);
};
// Play a nice C Major interval (C5 & E5) for a happy chime
playTone(523.25); // C5
setTimeout(() => playTone(659.25), 50); // E5 (staggered slightly for 'strum' feel)
}
// Particle Effect
function createParticles(x, y) {
const particleCount = 12;
const colors = ['#60a5fa', '#34d399', '#fbfb8c'];
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement('div');
particle.classList.add('particle');
// Random placement variance
const destX = (Math.random() - 0.5) * 100;
const destY = (Math.random() - 0.5) * 100;
const rot = Math.random() * 360;
const color = colors[Math.floor(Math.random() * colors.length)];
particle.style.backgroundColor = color;
particle.style.left = `${x} px`;
particle.style.top = `${y} px`;
particle.style.setProperty('--dest-x', `${destX} px`);
particle.style.setProperty('--dest-y', `${destY} px`);
particle.style.setProperty('--rot', `${rot} deg`);
document.body.appendChild(particle);
particle.addEventListener('animationend', () => particle.remove());
}
}
// --- Main Initialization ---
document.addEventListener('DOMContentLoaded', () => {
// 1. Theme Manager
const themeToggle = document.getElementById('themeToggle');
if (themeToggle) {
const sunIcon = themeToggle.querySelector('.sun-icon');
const moonIcon = themeToggle.querySelector('.moon-icon');
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
document.body.classList.add('light-mode');
sunIcon.style.display = 'none';
moonIcon.style.display = 'block';
}
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('light-mode');
const isLight = document.body.classList.contains('light-mode');
localStorage.setItem('theme', isLight ? 'light' : 'dark');
sunIcon.style.display = isLight ? 'none' : 'block';
moonIcon.style.display = isLight ? 'block' : 'none';
});
}
// 2. Initialize Progress
updateProgress();
// 3. Code Blocks & Toast Integration
const codeBlocks = document.querySelectorAll('.code-block');
codeBlocks.forEach(block => {
// Create wrapper
const wrapper = document.createElement('div');
wrapper.className = 'code-block-wrapper';
block.parentNode.insertBefore(wrapper, block);
wrapper.appendChild(block);
// Create copy button
const btn = document.createElement('button');
btn.className = 'copy-btn';
btn.textContent = 'Copy';
wrapper.appendChild(btn);
// Click handler
btn.addEventListener('click', async () => {
try {
const text = block.textContent.trim();
await navigator.clipboard.writeText(text);
// Toast Feedback
showToast('Code copied to clipboard!');
// Button feedback
const originalText = btn.textContent;
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(() => {
btn.textContent = originalText;
btn.classList.remove('copied');
}, 2000);
} catch (err) {
console.error('Failed to copy!', err);
btn.textContent = 'Error';
}
});
});
// 4. Intersection Observer for Icons & TOC
const observerOptions = {
root: null,
rootMargin: '-20% 0px -50% 0px',
threshold: 0
};
const tocList = document.querySelector('#toc');
const tocUl = document.createElement('ul');
if (tocList) tocList.appendChild(tocUl);
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('active');
document.querySelectorAll('#toc a').forEach(a => a.classList.remove('active'));
const id = entry.target.id;
const link = document.querySelector(`#toc a[href = "#${id}"]`);
if (link) link.classList.add('active');
} else {
entry.target.classList.remove('active');
}
});
}, observerOptions);
const stepItems = document.querySelectorAll('.step-item');
stepItems.forEach(item => {
observer.observe(item);
// Generate TOC Link
const h2 = item.querySelector('h2');
if (h2 && item.id) {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = `#${item.id} `;
a.textContent = h2.textContent.replace('Optional', '').trim();
a.addEventListener('click', (e) => {
e.preventDefault();
item.scrollIntoView({ behavior: 'smooth', block: 'center' });
history.pushState(null, null, `#${item.id} `);
});
li.appendChild(a);
tocUl.appendChild(li);
}
// Click to Complete (Sound + Particles + Progress)
const icon = item.querySelector('.step-icon');
if (icon) {
icon.style.cursor = 'pointer';
icon.addEventListener('click', (e) => {
// Prevent quick double-toggles
if (!item.classList.contains('animating')) {
item.classList.toggle('completed');
updateProgress(); // Update bar
if (item.classList.contains('completed')) {
playSuccessSound();
createParticles(e.clientX, e.clientY);
showToast('Step completed!');
}
}
});
}
});
// 5. Back to Top Button
const backToTopBtn = document.getElementById('backToTop');
if (backToTopBtn) {
window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
backToTopBtn.classList.add('visible');
} else {
backToTopBtn.classList.remove('visible');
}
});
backToTopBtn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
});
// --- Global Event Listeners ---
// Overscroll Effect (Pull-to-refresh style)
let overshoot = 0;
document.addEventListener('wheel', (e) => {
// Only trigger if at the very top
if (window.scrollY === 0) {
// Pulling down (deltaY negative)
if (e.deltaY < 0) {
overshoot -= e.deltaY * 0.5; // Resistance
overshoot = Math.min(overshoot, 150); // Cap
if (overshoot > 0) {
const wrapper = document.querySelector('.layout-wrapper');
if (wrapper) wrapper.style.transform = `translateY(${overshoot}px)`;
}
}
// Releasing up (deltaY positive) while overshot
else if (overshoot > 0 && e.deltaY > 0) {
overshoot -= e.deltaY;
if (overshoot < 0) overshoot = 0;
const wrapper = document.querySelector('.layout-wrapper');
if (wrapper) wrapper.style.transform = `translateY(${overshoot}px)`;
}
}
}, { passive: true });
// Decay/Snapback loop
setInterval(() => {
if (overshoot > 0) {
overshoot *= 0.9; // Decay factor
if (overshoot < 1) overshoot = 0;
const wrapper = document.querySelector('.layout-wrapper');
if (wrapper) wrapper.style.transform = `translateY(${overshoot}px)`;
}
}, 16);