Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
493 lines (402 loc) Β· 14.8 KB
/
Copy pathscript.js
File metadata and controls
493 lines (402 loc) Β· 14.8 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// --- Web Audio Synthesizer (Zero External Dependencies) ---
class SoundEffects {
constructor() {
this.ctx = null;
this.isPlayingMusic = false;
this.musicTimeout = null;
this.currentNoteIndex = 0;
}
init() {
if (!this.ctx) {
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
this.ctx = new AudioContextClass();
}
if (this.ctx.state === 'suspended') {
this.ctx.resume();
}
}
// Play a single synthesized note
playTone(freq, duration = 0.3, type = 'sine', gainVal = 0.2) {
this.init();
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, this.ctx.currentTime);
gain.gain.setValueAtTime(gainVal, this.ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.0001, this.ctx.currentTime + duration);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start();
osc.stop(this.ctx.currentTime + duration);
}
// Balloon Pop Sound FX
playPop() {
this.init();
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'triangle';
osc.frequency.setValueAtTime(450, this.ctx.currentTime);
osc.frequency.exponentialRampToValueAtTime(40, this.ctx.currentTime + 0.12);
gain.gain.setValueAtTime(0.4, this.ctx.currentTime);
gain.gain.linearRampToValueAtTime(0.01, this.ctx.currentTime + 0.12);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start();
osc.stop(this.ctx.currentTime + 0.12);
}
// Candle Blow Sound FX
playBlow() {
this.init();
// Noise buffer for air whoosh
const bufferSize = this.ctx.sampleRate * 0.4;
const buffer = this.ctx.createBuffer(1, bufferSize, this.ctx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
data[i] = Math.random() * 2 - 1;
}
const noise = this.ctx.createBufferSource();
noise.buffer = buffer;
const filter = this.ctx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.setValueAtTime(1200, this.ctx.currentTime);
filter.frequency.exponentialRampToValueAtTime(200, this.ctx.currentTime + 0.4);
const gain = this.ctx.createGain();
gain.gain.setValueAtTime(0.3, this.ctx.currentTime);
gain.gain.linearRampToValueAtTime(0.01, this.ctx.currentTime + 0.4);
noise.connect(filter);
filter.connect(gain);
gain.connect(this.ctx.destination);
noise.start();
}
// Cheer / Celebration Chime
playCheerChime() {
this.init();
const notes = [523.25, 659.25, 783.99, 1046.50]; // C5, E5, G5, C6
notes.forEach((freq, idx) => {
setTimeout(() => {
this.playTone(freq, 0.5, 'triangle', 0.25);
}, idx * 120);
});
}
// Synthesized "Happy Birthday" Melody
playBirthdayMelody(onEndCallback) {
this.init();
this.isPlayingMusic = true;
// Note frequencies in Hz
const N = {
C4: 261.63, D4: 293.66, E4: 329.63, F4: 349.23,
G4: 392.00, A4: 440.00, B4: 493.88,
C5: 523.25, D5: 587.33, E5: 659.25, F5: 698.46,
G5: 783.99, A5: 880.00, Bb4: 466.16
};
// Melody: [Note, duration in beats]
const song = [
[N.G4, 0.75], [N.G4, 0.25], [N.A4, 1], [N.G4, 1], [N.C5, 1], [N.B4, 2],
[N.G4, 0.75], [N.G4, 0.25], [N.A4, 1], [N.G4, 1], [N.D5, 1], [N.C5, 2],
[N.G4, 0.75], [N.G4, 0.25], [N.G5, 1], [N.E5, 1], [N.C5, 1], [N.B4, 1], [N.A4, 1],
[N.F5, 0.75], [N.F5, 0.25], [N.E5, 1], [N.C5, 1], [N.D5, 1], [N.C5, 2.5]
];
const tempo = 150; // BPM
const beatDuration = 60 / tempo;
let totalDelay = 0;
this.songTimeouts = [];
song.forEach(([freq, beats], index) => {
const delay = totalDelay;
const duration = beats * beatDuration;
const t = setTimeout(() => {
if (!this.isPlayingMusic) return;
this.playTone(freq, duration * 0.9, 'triangle', 0.28);
}, delay * 1000);
this.songTimeouts.push(t);
totalDelay += duration;
});
const endTimeout = setTimeout(() => {
this.isPlayingMusic = false;
if (onEndCallback) onEndCallback();
}, totalDelay * 1000);
this.songTimeouts.push(endTimeout);
}
stopMusic() {
this.isPlayingMusic = false;
if (this.songTimeouts) {
this.songTimeouts.forEach(t => clearTimeout(t));
this.songTimeouts = [];
}
}
}
// Instantiate Audio
const audio = new SoundEffects();
// --- Confetti Blaster Helper ---
function fireConfettiBurst(x = 0.5, y = 0.5) {
if (typeof confetti === 'function') {
confetti({
particleCount: 80,
spread: 70,
origin: { x, y },
colors: ['#ec4899', '#8b5cf6', '#f59e0b', '#06b6d4', '#10b981', '#ffffff']
});
}
}
function fireGrandFireworks() {
if (typeof confetti !== 'function') return;
const duration = 2.5 * 1000;
const animationEnd = Date.now() + duration;
const defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 1200 };
const interval = setInterval(() => {
const timeLeft = animationEnd - Date.now();
if (timeLeft <= 0) {
return clearInterval(interval);
}
const particleCount = 40 * (timeLeft / duration);
confetti(Object.assign({}, defaults, { particleCount, origin: { x: Math.random() * 0.4 + 0.1, y: Math.random() - 0.2 } }));
confetti(Object.assign({}, defaults, { particleCount, origin: { x: Math.random() * 0.4 + 0.5, y: Math.random() - 0.2 } }));
}, 250);
}
// --- Floating Background Balloons Spawner ---
function initBackgroundBalloons() {
const sky = document.getElementById('balloon-sky');
if (!sky) return;
const colors = ['#f43f5e', '#ec4899', '#8b5cf6', '#3b82f6', '#06b6d4', '#f59e0b', '#10b981'];
for (let i = 0; i < 15; i++) {
const balloon = document.createElement('div');
balloon.className = 'sky-balloon';
const size = Math.floor(Math.random() * 35) + 35;
balloon.style.width = `${size}px`;
balloon.style.height = `${size * 1.25}px`;
balloon.style.left = `${Math.random() * 95}%`;
balloon.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
balloon.style.animationDuration = `${Math.random() * 10 + 12}s`;
balloon.style.animationDelay = `${Math.random() * 10}s`;
sky.appendChild(balloon);
}
}
// --- Surprise Unboxing Screen ---
function initSurpriseModal() {
const modal = document.getElementById('surprise-modal');
const openBtn = document.getElementById('open-gift-btn');
const giftBox = document.getElementById('gift-box');
const replayBtn = document.getElementById('replay-surprise-btn');
function openGift() {
audio.playCheerChime();
giftBox.classList.add('opened');
fireGrandFireworks();
setTimeout(() => {
modal.classList.add('hidden');
// Automatically kick off celebratory birthday tune
toggleMusic(true);
}, 1100);
}
if (openBtn) openBtn.addEventListener('click', openGift);
if (giftBox) giftBox.addEventListener('click', openGift);
if (replayBtn) {
replayBtn.addEventListener('click', () => {
giftBox.classList.remove('opened');
modal.classList.remove('hidden');
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
}
// --- Music & Party Mode Controls ---
function toggleMusic(forcePlay = null) {
const musicBtn = document.getElementById('music-btn');
const textSpan = musicBtn.querySelector('.btn-text');
const shouldPlay = forcePlay !== null ? forcePlay : !audio.isPlayingMusic;
if (shouldPlay) {
audio.playBirthdayMelody(() => {
textSpan.textContent = 'Play Song';
musicBtn.classList.remove('active');
});
textSpan.textContent = 'Pause Song';
musicBtn.classList.add('active');
} else {
audio.stopMusic();
textSpan.textContent = 'Play Song';
musicBtn.classList.remove('active');
}
}
function initActionToolbar() {
const musicBtn = document.getElementById('music-btn');
const confettiBtn = document.getElementById('confetti-btn');
const partyModeBtn = document.getElementById('party-mode-btn');
if (musicBtn) {
musicBtn.addEventListener('click', () => toggleMusic());
}
if (confettiBtn) {
confettiBtn.addEventListener('click', () => {
audio.playTone(880, 0.15, 'sine');
fireGrandFireworks();
});
}
if (partyModeBtn) {
partyModeBtn.addEventListener('click', () => {
document.body.classList.toggle('party-mode');
partyModeBtn.classList.toggle('active');
fireConfettiBurst();
});
}
}
// --- Cake & Candle Interaction ---
function initCakeInteraction() {
const blowBtn = document.getElementById('blow-candles-btn');
const cutBtn = document.getElementById('cut-cake-btn');
const cakeStatus = document.getElementById('cake-status');
const cakeSlice = document.getElementById('cake-slice');
const flames = document.querySelectorAll('.flame');
const smokes = document.querySelectorAll('.smoke');
let candlesBlown = false;
blowBtn.addEventListener('click', () => {
audio.playBlow();
flames.forEach(f => f.classList.add('extinguished'));
smokes.forEach(s => s.classList.add('active'));
candlesBlown = true;
blowBtn.disabled = true;
blowBtn.textContent = 'π¨ Candles Extinguished!';
cutBtn.disabled = false;
cakeStatus.textContent = 'Wishes made! Now cut the cake, Ankit! πβ¨';
fireConfettiBurst(0.5, 0.4);
});
cutBtn.addEventListener('click', () => {
audio.playCheerChime();
cakeSlice.classList.add('served');
cutBtn.disabled = true;
cutBtn.textContent = 'π° Cake Served!';
cakeStatus.innerHTML = 'π Happy Birthday Ankit! The first slice is yours! Enjoy the treat! π';
fireGrandFireworks();
});
}
// --- Balloon Popping Mini-Game ---
const balloonMessages = [
"π₯ Ankit's swag has no match!",
"β Always down for late-night chai & gossip!",
"π Never says no to a biryani & pizza treat!",
"πͺ The most loyal & dependable brother!",
"π§ Brain of a genius, heart of gold!",
"π Legend in the making, keep shining Ankit!"
];
const balloonColors = [
'#f43f5e', '#ec4899', '#8b5cf6', '#3b82f6', '#06b6d4', '#f59e0b'
];
function initBalloonGame() {
const grid = document.getElementById('balloon-grid');
const popCountElem = document.getElementById('pop-count');
const msgBanner = document.getElementById('game-message-banner');
const resetBtn = document.getElementById('reset-game-btn');
let poppedCount = 0;
function renderBalloons() {
grid.innerHTML = '';
poppedCount = 0;
popCountElem.textContent = '0';
msgBanner.textContent = 'Click any balloon to pop it!';
balloonMessages.forEach((msg, idx) => {
const wrapper = document.createElement('div');
wrapper.className = 'game-balloon-wrapper';
const balloon = document.createElement('div');
balloon.className = 'game-balloon';
balloon.style.backgroundColor = balloonColors[idx % balloonColors.length];
balloon.style.borderBottomColor = balloonColors[idx % balloonColors.length];
const string = document.createElement('div');
string.className = 'balloon-string';
wrapper.appendChild(balloon);
wrapper.appendChild(string);
wrapper.addEventListener('click', () => {
if (wrapper.classList.contains('popped')) return;
wrapper.classList.add('popped');
audio.playPop();
poppedCount++;
popCountElem.textContent = poppedCount;
msgBanner.textContent = msg;
// Fire mini burst at balloon position
const rect = wrapper.getBoundingClientRect();
const x = (rect.left + rect.width / 2) / window.innerWidth;
const y = (rect.top + rect.height / 2) / window.innerHeight;
fireConfettiBurst(x, y);
if (poppedCount === balloonMessages.length) {
msgBanner.innerHTML = 'π <strong>ALL POPPED! Ankit is officially 100% AWESOME!</strong> π₯³';
fireGrandFireworks();
}
});
grid.appendChild(wrapper);
});
}
resetBtn.addEventListener('click', () => {
renderBalloons();
});
renderBalloons();
}
// --- Polaroid Real Photo Upload ---
function initPhotoUpload() {
const fileInput = document.getElementById('photo-upload');
const polaroidGrid = document.getElementById('polaroid-grid');
if (!fileInput || !polaroidGrid) return;
fileInput.addEventListener('change', (e) => {
const files = Array.from(e.target.files);
if (!files.length) return;
const cards = polaroidGrid.querySelectorAll('.polaroid-card');
files.forEach((file, index) => {
if (index >= cards.length) return; // Fill available slots
const reader = new FileReader();
reader.onload = (event) => {
const imgWrapper = cards[index].querySelector('.polaroid-img-wrapper');
imgWrapper.innerHTML = `<img src="${event.target.result}" alt="Ankit memory">`;
};
reader.readAsDataURL(file);
});
fireConfettiBurst();
});
}
// --- Interactive Wish Board ---
function initWishesBoard() {
const form = document.getElementById('wish-form');
const wishesWall = document.getElementById('wishes-wall');
const authorInput = document.getElementById('wish-author');
const emojiSelect = document.getElementById('wish-emoji');
const textInput = document.getElementById('wish-text');
// Load saved custom wishes from localStorage
const savedWishes = JSON.parse(localStorage.getItem('ankit_birthday_wishes') || '[]');
savedWishes.forEach(w => prependWishCard(w.author, w.emoji, w.text, w.time, false));
function prependWishCard(author, emoji, text, time, persist = true) {
const card = document.createElement('div');
card.className = 'wish-card';
card.innerHTML = `
<div class="wish-header">
<span class="wish-author">${escapeHtml(author)}</span>
<span class="wish-badge">${emoji}</span>
</div>
<p class="wish-msg">${escapeHtml(text)}</p>
<span class="wish-time">${time}</span>
`;
wishesWall.insertBefore(card, wishesWall.firstChild);
if (persist) {
savedWishes.unshift({ author, emoji, text, time });
localStorage.setItem('ankit_birthday_wishes', JSON.stringify(savedWishes));
}
}
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
if (form) {
form.addEventListener('submit', (e) => {
e.preventDefault();
const author = authorInput.value.trim();
const emoji = emojiSelect.value;
const text = textInput.value.trim();
if (!author || !text) return;
audio.playTone(659.25, 0.2, 'sine');
prependWishCard(author, emoji, text, 'Just now', true);
fireConfettiBurst();
form.reset();
});
}
}
// --- App Initialization ---
document.addEventListener('DOMContentLoaded', () => {
initBackgroundBalloons();
initSurpriseModal();
initActionToolbar();
initCakeInteraction();
initBalloonGame();
initPhotoUpload();
initWishesBoard();
});