-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
495 lines (419 loc) · 13.7 KB
/
script.js
File metadata and controls
495 lines (419 loc) · 13.7 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
494
495
// const bgAudio = new Audio("audio/bg.mp3");
// bgAudio.loop = true;
// bgAudio.play();
const books = document.querySelectorAll(".book");
const magicContainer = document.getElementById("magicContainer");
const magicContent = magicContainer.querySelector(".magic-content");
const container = document.querySelector(".butterfly-container");
const bookContainer = document.querySelector(".bookContainer");
const sizeChanger = document.querySelector(".sizeChanger");
let currentLanguage = "hi"; // Default: Hindi
let closingFromCloseBtn = false;
const butterflyImages = [
"assets/butterfly1.webp",
"assets/butterfly2.webp",
"assets/butterfly3.webp",
"assets/butterfly4.webp",
];
// ✅ Reusable close button creator
function createCloseButton(onClickHandler) {
const closeBtn = document.createElement("button");
closeBtn.innerText = "✖";
closeBtn.classList.add("close-btn");
closeBtn.addEventListener("click", onClickHandler);
return closeBtn;
}
// ✅ Show Magic Container when book is clicked
books.forEach((book) => {
book.addEventListener("click", () => {
magicContent.innerHTML = "";
// Add close button to magic container
const closeBtn = createCloseButton(closeMagic);
magicContent.appendChild(closeBtn);
// Add book image
const img = document.createElement("img");
img.src = book.src;
img.alt = "Book Image";
img.classList.add("SelectedBook");
magicContent.appendChild(img);
// Show magic container
magicContainer.classList.remove("hide");
magicContainer.classList.add("show");
magicContainer.style.display = "flex";
});
});
// ✅ Magic Container Close Function
function closeMagic() {
magicContainer.classList.remove("show");
magicContainer.classList.add("hide");
setTimeout(() => {
magicContainer.style.display = "none";
}, 500);
}
// ✅ SelectedBook click toggles bookContainer
document.addEventListener("click", function (e) {
if (e.target.classList.contains("SelectedBook")) {
fullScreen();
loadBook(currentBookName);
console.log("SelectedBook clicked");
sizeChanger.style.display = "block";
if (bookContainer) {
// Pehle se existing close button hata do (agar hai)
// Agar pehle se close button exist karta hai to dobara create mat karo
let existingCloseBtn = bookContainer.querySelector(".close-btn");
if (!existingCloseBtn) {
const closeBtn = createCloseButton(() => {
closingFromCloseBtn = true;
bookContainer.classList.remove("visible");
sizeChanger.style.display = "none";
audio.pause();
audio.currentTime = 0;
currentScale = 1;
bookContainer.style.transform = `scale(${currentScale})`;
currentMode = "voice";
soundIcon.className = "fas fa-volume-up";
currentLanguage = "hi";
languageToggleBtn.textContent = "अA";
currentPage = 0;
loadPage(currentPage);
// ✅ Reset flag back after short delay
setTimeout(() => {
closingFromCloseBtn = false;
}, 100); // small delay so loadPage runs with flag
});
bookContainer.prepend(closeBtn);
}
// Show the container
bookContainer.classList.add("visible");
// ✅ Play audio only now
if (currentMode === "voice") {
loadAudio(currentBookName, currentPage);
}
}
}
});
// ✅ Butterfly Logic
function createButterfly() {
const butterfly = document.createElement("img");
const randomIndex = Math.floor(Math.random() * butterflyImages.length);
butterfly.src = butterflyImages[randomIndex];
butterfly.classList.add("butterfly");
const left = Math.random() * 90 + 5;
butterfly.style.left = `${left}vw`;
const scale = 0.5 + Math.random() * 0.5;
const rotate = Math.random() * 20 - 10;
butterfly.style.transform = `scale(${scale}) rotate(${rotate}deg)`;
container.appendChild(butterfly);
setTimeout(() => {
butterfly.remove();
}, 30000);
}
for (let i = 0; i < 3; i++) {
createButterfly();
}
setInterval(() => {
createButterfly();
}, 2000);
// ========== BOOK LOGIC ==========
let currentBookName = "book1";
const soundIcon = document.getElementById("soundIcon");
const rightPage = document.querySelector(".rightPage");
const leftPage = document.querySelector(".leftPage");
const nextBtn = document.querySelector(".NextBtn");
const prevBtn = document.querySelector(".PrevBtn");
const scenePrevBtn = document.getElementById("scenePrevBtn");
const sceneNextBtn = document.getElementById("sceneNextBtn");
// const SceneToggle = document.querySelectorAll(".SceneToggle");
const SceneToggle = document.querySelectorAll(".SceneToggle");
const modeToggleBtn = document.getElementById("modeToggleBtn");
let currentPage = 0;
let totalPages = 0;
let bookData = null;
let currentSceneIndex = 0;
let audio = new Audio();
let sceneInterval = null;
let currentMode = "voice";
modeToggleBtn.addEventListener("click", () => {
if (currentMode === "manual") {
currentMode = "voice";
soundIcon.className = "fas fa-volume-up";
startVoiceMode();
} else {
currentMode = "manual";
soundIcon.className = "fas fa-volume-mute";
stopVoiceMode();
}
// ✅ Re-run page load to trigger updated SceneToggle visibility
loadPage(currentPage);
});
function startVoiceMode() {
loadAudio(currentBookName, currentPage);
}
function stopVoiceMode() {
if (audio) {
audio.pause();
audio.currentTime = 0;
}
if (sceneInterval) {
clearInterval(sceneInterval);
sceneInterval = null;
}
}
function loadBook(bookName) {
currentBookName = bookName;
fetch(`data/${bookName}.json`)
.then((res) => res.json())
.then((data) => {
bookData = data.pages;
totalPages = bookData.length;
currentPage = 0;
loadPage(currentPage);
})
.catch((err) => console.error("Error loading book:", err));
}
function loadAudio(bookName, pageIndex) {
if (audio) {
audio.pause();
audio.currentTime = 0;
}
// ✅ Naya audio banaye
audio = new Audio(
`audio/${currentLanguage}/${bookName}page${pageIndex + 1}.mp3`
);
// ✅ Play safely with catch
audio.play().catch((err) => {
console.warn("Audio play failed:", err);
});
// ✅ Agar pehle se interval chal raha hai to clear karo
if (sceneInterval) clearInterval(sceneInterval);
// ✅ Scene change track karne ke liye interval
sceneInterval = setInterval(() => {
handleSceneChangeByAudio();
}, 500);
// ✅ Jab audio khatam ho → voice mode me next page auto flip
audio.onended = () => {
console.log("Audio finished for page:", pageIndex);
if (currentMode === "voice" && currentPage < totalPages - 1) {
nextBtn.click(); // simulate button click → page flip
}
};
}
function loadPage(index) {
if (!bookData || index >= totalPages) return;
const page = bookData[index];
currentSceneIndex = 0;
const textClass = currentLanguage === "hi" ? "hindi-text" : "english-text";
rightPage.innerHTML = `<p class="${textClass}">${
page.rightText[currentLanguage] || "Translation not available"
}</p>`;
leftPage.innerHTML = "";
if (page.scenes && page.scenes.length > 0) {
leftPage.insertAdjacentHTML(
"afterbegin",
`<img src="${page.scenes[0].image}" alt="Scene Image" />`
);
}
// ✅ Show/hide scene toggle buttons based on manual mode and scenes
SceneToggle.forEach((btn) => {
if (currentMode === "manual" && page.scenes && page.scenes.length > 1) {
btn.style.opacity = "1";
} else {
btn.style.opacity = "0";
}
});
if (currentMode === "voice" && !closingFromCloseBtn) {
loadAudio(currentBookName, currentPage); // ✅ Only play audio if not closing
}
}
const languageToggleBtn = document.getElementById("languageToggleBtn");
languageToggleBtn.addEventListener("click", () => {
currentLanguage = currentLanguage === "en" ? "hi" : "en";
languageToggleBtn.textContent = currentLanguage === "en" ? "Aअ" : "अA";
loadPage(currentPage); // Reload current page with new language
});
function updateSceneImage(imagePath) {
let imgEl = leftPage.querySelector("img");
// Agar image already wahi hai to kuch mat karo
if (imgEl && imgEl.src.includes(imagePath)) return;
if (!imgEl) {
// First image
leftPage.insertAdjacentHTML(
"afterbegin",
`<img src="${imagePath}" alt="Scene Image" />`
);
return;
}
// Existing image → simply change src without transition
imgEl.src = imagePath;
}
function handleSceneChangeByAudio() {
const page = bookData[currentPage];
if (!page || !page.scenes) return;
const currentTime = audio.currentTime;
let newSceneIndex = currentSceneIndex;
for (let i = 0; i < page.scenes.length; i++) {
if (currentTime >= page.scenes[i].time) {
newSceneIndex = i;
} else {
break;
}
}
if (newSceneIndex !== currentSceneIndex) {
currentSceneIndex = newSceneIndex;
updateSceneImage(page.scenes[currentSceneIndex].image);
}
}
scenePrevBtn.addEventListener("click", () => {
if (currentMode !== "manual") return;
const page = bookData[currentPage];
if (!page || !page.scenes || currentSceneIndex <= 0) return;
currentSceneIndex--;
updateSceneImage(page.scenes[currentSceneIndex].image);
});
sceneNextBtn.addEventListener("click", () => {
if (currentMode !== "manual") return;
const page = bookData[currentPage];
if (!page || !page.scenes || currentSceneIndex >= page.scenes.length - 1)
return;
currentSceneIndex++;
updateSceneImage(page.scenes[currentSceneIndex].image);
});
document.querySelectorAll(".book").forEach((bookImg) => {
bookImg.addEventListener("click", () => {
const bookName = bookImg.dataset.book;
currentBookName = bookName;
// loadBook(bookName);
});
});
nextBtn.addEventListener("click", () => {
if (currentPage < totalPages - 1) {
// Clear LEFT PAGE before animation starts
leftPage.style.transition = "opacity 0.4s ease";
leftPage.style.opacity = "0";
setTimeout(() => {
leftPage.innerHTML = "";
leftPage.style.transition = "none";
leftPage.style.opacity = "1";
}, 400); // Match this with opacity transition duration
rightPage.style.transition = "transform 1s";
rightPage.style.transform = "rotateY(-180deg)";
setTimeout(() => {
rightPage.style.transition = "none";
rightPage.style.transform = "rotateY(0deg)";
currentPage++;
loadPage(currentPage);
// Voice mode support
if (currentMode === "voice") {
loadAudio(currentBookName, currentPage);
} else {
stopVoiceMode();
}
}, 600);
}
});
prevBtn.addEventListener("click", () => {
if (currentPage > 0) {
// Clear RIGHT PAGE before animation starts
rightPage.style.transition = "opacity 0.4s ease";
rightPage.style.opacity = "0";
setTimeout(() => {
rightPage.innerHTML = "";
rightPage.style.transition = "none";
rightPage.style.opacity = "1";
}, 400); // Match this with opacity transition duration
leftPage.style.transition = "transform 1s";
leftPage.style.transform = "rotateY(180deg)";
setTimeout(() => {
leftPage.style.transition = "none";
leftPage.style.transform = "rotateY(0deg)";
currentPage--;
loadPage(currentPage);
// Voice mode support
if (currentMode === "voice") {
loadAudio(currentBookName, currentPage);
} else {
stopVoiceMode();
}
}, 600);
}
});
const largeBtn = document.querySelector(".Large");
const smallBtn = document.querySelector(".Small");
// Starting scale
let currentScale = 1;
// Maximum and minimum scale limit
const maxScale = 2.5;
const minScale = 0.5;
const scaleStep = 0.2;
largeBtn.addEventListener("click", () => {
if (currentScale < maxScale) {
currentScale += scaleStep;
bookContainer.style.transform = `scale(${currentScale})`;
}
});
smallBtn.addEventListener("click", () => {
if (currentScale > minScale) {
currentScale -= scaleStep;
bookContainer.style.transform = `scale(${currentScale})`;
}
});
//fullScree
function fullScreen() {
const elem = document.documentElement;
const icon = document.getElementById("fs-icon");
if (
!document.fullscreenElement &&
!document.webkitFullscreenElement &&
!document.msFullscreenElement
) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
}
icon.classList.remove("fa-expand");
icon.classList.add("fa-compress");
}
}
function toggleFullScreenFromButton() {
const icon = document.getElementById("fs-icon");
if (
!document.fullscreenElement &&
!document.webkitFullscreenElement &&
!document.msFullscreenElement
) {
fullScreen();
} else {
// Exit fullscreen only from the button
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
icon.classList.remove("fa-compress");
icon.classList.add("fa-expand");
}
}
// ===== Preloader Logic =====
document.addEventListener("DOMContentLoaded", () => {
let progressText = document.getElementById("progress-text");
let bookContent = document.getElementById("book-content");
let preloader = document.getElementById("preloader");
let progress = 0;
let fakeLoading = setInterval(() => {
progress += Math.floor(Math.random() * 10) + 5; // 5–15% increase
if (progress > 100) progress = 100;
progressText.textContent = `Loading your library... ${progress}%`;
if (progress === 100) {
clearInterval(fakeLoading);
setTimeout(() => {
preloader.style.display = "none";
bookContent.style.display = "block";
}, 600);
}
}, 300);
});