-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
395 lines (332 loc) · 12.8 KB
/
script.js
File metadata and controls
395 lines (332 loc) · 12.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
// Matrix Intro Variables
let matrixCanvas, matrixCtx;
let matrixChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()";
let matrixColumns = [];
let matrixDrops = [];
// Initialize Matrix Effect
function initMatrix() {
matrixCanvas = document.getElementById('matrix-canvas');
matrixCtx = matrixCanvas.getContext('2d');
// Set canvas size
matrixCanvas.width = window.innerWidth;
matrixCanvas.height = window.innerHeight;
const fontSize = 14;
const columns = matrixCanvas.width / fontSize;
// Initialize drops array
for (let i = 0; i < columns; i++) {
matrixDrops[i] = Math.floor(Math.random() * matrixCanvas.height / fontSize);
}
drawMatrix();
}
function drawMatrix() {
// Black background with fade effect
matrixCtx.fillStyle = 'rgba(0, 0, 0, 0.04)';
matrixCtx.fillRect(0, 0, matrixCanvas.width, matrixCanvas.height);
matrixCtx.fillStyle = '#00ff00';
matrixCtx.font = '14px monospace';
for (let i = 0; i < matrixDrops.length; i++) {
const text = matrixChars[Math.floor(Math.random() * matrixChars.length)];
matrixCtx.fillText(text, i * 14, matrixDrops[i] * 14);
if (matrixDrops[i] * 14 > matrixCanvas.height && Math.random() > 0.975) {
matrixDrops[i] = 0;
}
matrixDrops[i]++;
}
}
// Matrix Text Animation
function animateMatrixText() {
const matrixText = document.getElementById('matrix-text');
const originalText = 'NOIRHYTHM';
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
let iterations = 0;
const interval = setInterval(() => {
matrixText.innerHTML = originalText
.split('')
.map((letter, index) => {
if (index < iterations) {
return originalText[index];
}
return chars[Math.floor(Math.random() * chars.length)];
})
.join('');
if (iterations >= originalText.length) {
clearInterval(interval);
}
iterations += 1 / 3;
}, 30);
}
// Start Matrix Intro
function startMatrixIntro() {
initMatrix();
// Start matrix rain animation
const matrixInterval = setInterval(() => {
drawMatrix();
}, 33);
// Start text animation after a short delay
setTimeout(() => {
animateMatrixText();
}, 500);
// Transition to main app after 3 seconds
setTimeout(() => {
const matrixIntro = document.getElementById('matrix-intro');
const mainApp = document.getElementById('main-app');
// Fade out intro
matrixIntro.classList.add('fade-out');
// Clear matrix animation
clearInterval(matrixInterval);
// Show main app after fade out
setTimeout(() => {
matrixIntro.style.display = 'none';
mainApp.style.display = 'flex';
// Trigger smooth entrance animation
setTimeout(() => {
mainApp.classList.add('show');
}, 50);
// Initialize main app
main();
}, 800);
}, 3000);
}
// Resize handler for matrix canvas
window.addEventListener('resize', () => {
if (matrixCanvas) {
matrixCanvas.width = window.innerWidth;
matrixCanvas.height = window.innerHeight;
}
});
// Original Music Player Code
let currentsong = new Audio()
let songs;
let currfolder;
function secondsToMinutesSeconds(seconds) {
if (isNaN(seconds) || seconds < 0) {
return "00:00";
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(remainingSeconds).padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
}
async function getsongs(folder){
currfolder = folder
// Use relative path instead of localhost
let a = await fetch(`./${folder}/`)
let response = await a.text();
let div = document.createElement("div")
div.innerHTML = response
let as = div.getElementsByTagName("a")
songs = []
for(let i=0;i<as.length;i++){
const element = as[i]
if(element.href.endsWith(".mp3")){
songs.push(decodeURIComponent(element.href.split(`/${folder}/`)[1]))
}
}
// Show songs in playlist
let songul = document.querySelector(".songlist").getElementsByTagName("ul")[0]
songul.innerHTML = ""
for (const song of songs) {
songul.innerHTML = songul.innerHTML+`<li>
<img class="invert" src ="svg/music.svg" alt="">
<div class="info">
<div>${decodeURIComponent(song).replace(/\.mp3$/i, "").trim()}</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img class="invert" style="width: 26px;height: 26px;" src="svg/play.svg" alt="">
</div>
</li>`
}
// Attach an event listener to each song
Array.from(document.querySelector(".songlist").getElementsByTagName("li")).forEach(e=>{
e.addEventListener("click", element=>{
playmusic(e.querySelector(".info").firstElementChild.innerHTML.trim())
// Close mobile menu after selecting a song
if (window.innerWidth <= 1199) {
closeMobileMenu();
}
})
})
return songs
}
const playmusic = (track, pause = false) => {
// Ensure it has .mp3
const rawTrack = track.trim().endsWith(".mp3") ? track.trim() : `${track.trim()}.mp3`;
// Use relative path for audio source
currentsong.src = `./${currfolder}/` + encodeURIComponent(rawTrack);
if (!pause) {
currentsong.play();
play.src = "svg/pause.svg";
}
// Update UI display
const displayName = rawTrack.replace(/\.mp3$/i, "").trim();
document.querySelector(".songinfo").innerHTML = displayName;
document.querySelector(".songtime").innerHTML = "00:00 / 00:00";
}
// Function to play the next song automatically
const playNextSong = () => {
let currentTrack = decodeURIComponent(currentsong.src.split("/").slice(-1)[0]);
let index = songs.indexOf(currentTrack);
// Check if there's a next song in the playlist
if ((index + 1) < songs.length) {
playmusic(songs[index + 1]);
} else {
// Optional: Loop back to the first song when playlist ends
// Uncomment the line below if you want the playlist to loop
// playmusic(songs[0]);
// Or just stop and reset play button
play.src = "svg/play.svg";
console.log("Playlist ended");
}
}
function openMobileMenu() {
const leftPanel = document.querySelector(".left");
leftPanel.classList.add("show");
document.body.style.overflow = 'hidden'; // Prevent background scrolling
}
function closeMobileMenu() {
const leftPanel = document.querySelector(".left");
leftPanel.classList.remove("show");
document.body.style.overflow = 'auto'; // Restore background scrolling
}
async function main(){
// Get list of all songs
try {
await getsongs("songs/hindi")
playmusic(songs[0], true)
console.log("First song loaded:", songs[0])
} catch (error) {
console.error("Error loading songs:", error)
// Fallback: Set up empty songs array and continue with other functionality
songs = []
}
// Attach eventlistener to play/pause
play.addEventListener("click",()=>{
if(currentsong.paused){
currentsong.play()
play.src = "svg/pause.svg"
}
else{
currentsong.pause()
play.src = "svg/play.svg"
}
})
// For timeupdate event
currentsong.addEventListener("timeupdate",()=>{
console.log(currentsong.currentTime, currentsong.duration)
document.querySelector(".songtime").innerHTML = `${secondsToMinutesSeconds(currentsong.currentTime)} / ${secondsToMinutesSeconds(currentsong.duration)}`
document.querySelector(".circle").style.left = (currentsong.currentTime/currentsong.duration)*100 + "%"
})
// AUTO-PLAY FEATURE: Event listener for when song ends
currentsong.addEventListener("ended", () => {
console.log("Song ended, playing next song...");
playNextSong();
});
// Event listener for seekbar
document.querySelector(".seekbar").addEventListener("click", e => {
let percent = (e.offsetX / e.target.getBoundingClientRect().width) * 100;
document.querySelector(".circle").style.left = percent + "%";
currentsong.currentTime = ((currentsong.duration) * percent) / 100
})
// Event listener for hamburger menu - TOGGLE functionality
document.querySelector(".hamburger").addEventListener("click", (e) => {
e.stopPropagation();
const leftPanel = document.querySelector(".left");
console.log("Hamburger clicked"); // Debug log
if (leftPanel.classList.contains("show")) {
closeMobileMenu();
} else {
openMobileMenu();
}
})
// Event listener for close button
document.querySelector(".close").addEventListener("click", (e) => {
e.stopPropagation();
closeMobileMenu();
})
// Close mobile menu when clicking on the overlay/background
document.addEventListener("click", (e) => {
const leftPanel = document.querySelector(".left");
const hamburger = document.querySelector(".hamburger");
// Only close if menu is open and click is outside the left panel and not on hamburger
if (window.innerWidth <= 1199 &&
leftPanel.classList.contains("show") &&
!leftPanel.contains(e.target) &&
!hamburger.contains(e.target)) {
closeMobileMenu();
}
});
// Prevent clicks inside the left panel from closing the menu
document.querySelector(".left").addEventListener("click", (e) => {
e.stopPropagation();
});
// Event listener for previous and next
previous.addEventListener("click", () => {
let currentTrack = decodeURIComponent(currentsong.src.split("/").slice(-1)[0]);
let index = songs.indexOf(currentTrack);
if ((index - 1) >= 0) {
playmusic(songs[index - 1]);
}
});
next.addEventListener("click", () => {
let currentTrack = decodeURIComponent(currentsong.src.split("/").slice(-1)[0]);
let index = songs.indexOf(currentTrack);
if ((index + 1) < songs.length) {
playmusic(songs[index + 1]);
}
});
// Event listener for volume
document.querySelector(".range").getElementsByTagName("input")[0].addEventListener("change",(e)=>{
console.log("setting volume",e.target.value)
currentsong.volume = parseInt(e.target.value)/100
})
currentsong.addEventListener("loadedmetadata", () => {
document.querySelector(".songtime").innerHTML =
`00:00 / ${secondsToMinutesSeconds(currentsong.duration)}`
});
// Load playlist whenever card is clicked
Array.from(document.getElementsByClassName("card")).forEach(e=>{
e.addEventListener("click",async item=>{
try {
songs = await getsongs(`songs/${item.currentTarget.dataset.folder}`)
playmusic(songs[0])
// Close mobile menu after selecting playlist
if (window.innerWidth <= 1199) {
closeMobileMenu();
}
} catch (error) {
console.error("Error loading playlist:", error)
}
})
})
// Add event listener to mute the track
document.querySelector(".volume>img").addEventListener("click", e=>{
if(e.target.src.includes("svg/volume.svg")){
e.target.src = e.target.src.replace("svg/volume.svg", "svg/mute.svg")
currentsong.volume = 0;
document.querySelector(".range").getElementsByTagName("input")[0].value = 0;
}
else{
e.target.src = e.target.src.replace("svg/mute.svg", "svg/volume.svg")
currentsong.volume = .10;
document.querySelector(".range").getElementsByTagName("input")[0].value = 10;
}
})
// Handle window resize - close mobile menu if screen becomes large
window.addEventListener('resize', () => {
if (window.innerWidth > 1199) {
closeMobileMenu();
}
});
// Handle escape key to close mobile menu
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && window.innerWidth <= 1199) {
closeMobileMenu();
}
});
}
// Start the matrix intro when page loads
window.addEventListener('load', () => {
startMatrixIntro();
});