Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
322 lines (256 loc) · 11.7 KB
/
Copy pathscript.js
File metadata and controls
322 lines (256 loc) · 11.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
const data = window.location.search;
let urlParam = new URLSearchParams(data);
let n = urlParam.get('n') || "";
// URL of the JSON file (replace with your actual JSON URL)
const poemUrl = "https://raw.githubusercontent.com/Mayborg121/poems/refs/heads/main/poems.json";
// Function to fetch JSON from the web
fetch(poemUrl)
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok " + response.statusText);
}
return response.json(); // Parse JSON data
})
.then(data => {
data.reverse();
const poemList = document.getElementById("poemList");
const contentDiv = document.getElementById("content");
const Hero = document.getElementById("hero");
const body = document.querySelector('body');
const title = document.getElementById("title");
const poem = document.getElementById("poem");
const timing = document.getElementById("timing");
const context = document.getElementById("context");
const meaning = document.getElementById("meaning");
const author = document.getElementById("author");
const poems = document.getElementById("poems");
const footer = document.querySelector("footer");
const download = document.getElementById("downloadBtn");
const container = document.getElementById("content-dnld");
if(n=='a'){
footer.style.position = "fixed";
poems.style.display = "none";
poemList.style.display = "none";
Hero.style.display = "none";
author.style.display = "flex";
}
const bg = Math.floor(Math.random() * 24) + 1;
console.log("bg = ",bg);
n = Number(n);
console.log(n);
if (!isNaN(n) && n > 0 && n <= data.length) {
// If 'n' is valid, hide the list and show nth item
poemList.style.display = "none";
Hero.style.display = "none";
container.style.backgroundImage = `url(img/${bg}.webp)`;
contentDiv.style.display = "flex";
const selectedItem = data.find(data => data.No == n);
title.textContent = `${selectedItem.Title}`;
poem.innerHTML = `${selectedItem.Poem}`;
timing.textContent = `~ ${selectedItem.Time}, Mayborg.`;
context.textContent = `${selectedItem.Context}`;
meaning.textContent = `${selectedItem.Meaning}`;
download.innerHTML = `Download Poem`;
document.getElementById('nextBtn').addEventListener('click', function() {
if (n == data.length){
window.location.href = `?n=1`;
} else{
window.location.href = `?n=${n+1}`;
}
});
}
else{
const read = document.getElementById("rp");
const rand = Math.floor(Math.random() * data.length) + 1;
const lastTime = data[0].Time;
console.log(rand);
lastupdatetime.textContent = `Last updated : ${lastTime}`;
read.href = `?n=${rand}`;
// Show list if 'n' is not present or invalid
data.forEach(item => {
const li = document.createElement("li");
const link = document.createElement("a");
link.textContent = item.Title; // Display title
li.appendChild(link);
link.href = `?n=${item.No}` || "#"; // Use the URL, or "#" if missing
poemList.appendChild(li);
});
}
})
.catch(error => console.error("Error fetching JSON:", error));
document.getElementById('downloadBtn').addEventListener('click', function() {
const element = document.getElementById('content-dnld');
if (element) {
html2canvas(element, {
backgroundColor: null, /* Ensure the background color is captured */
scale: window.devicePixelRatio, /* Use device pixel ratio for better quality */
useCORS: true /* Allow cross-origin resource sharing */
}).then(function(canvas) {
const link = document.createElement('a');
link.href = canvas.toDataURL('image/png');
link.download = 'poem.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}).catch(function(error) {
console.error('Error capturing the element:', error);
});
} else {
console.error('Element with ID "capture" not found.');
}
});
let activeFireflies = 0;
const maxFireflies = 15;
function createFirefly() {
if (activeFireflies >= maxFireflies) return;
activeFireflies++;
const heading = document.getElementById("mainheading");
const rect = heading.getBoundingClientRect();
const firefly = document.createElement("div");
firefly.classList.add("firefly");
document.body.appendChild(firefly);
const x = (rect.left + 20) + Math.random() * (rect.width - 50);
const y = (rect.top - 20) + Math.random() * (rect.height + 40);
firefly.style.left = `${x}px`;
firefly.style.top = `${y}px`;
// Assign random properties for size, glow, movement, and speed
firefly.style.setProperty('--size', Math.random() * 0.7 + 0.25);
firefly.style.setProperty('--glow', Math.random() * 0.45 + 0.28);
firefly.style.setProperty('--twinkle-time', `${Math.random() * 3 + 2}s`);
firefly.style.setProperty('--twinkle-opacity', Math.random() * 0.1 + 0.2);
firefly.style.setProperty('--speed', `${Math.random() * 4 + 14}s`);
firefly.style.setProperty('--randomX1', Math.random());
firefly.style.setProperty('--randomY1', Math.random());
firefly.style.setProperty('--randomX2', Math.random());
firefly.style.setProperty('--randomY2', Math.random());
firefly.style.setProperty('--randomX3', Math.random());
firefly.style.setProperty('--randomY3', Math.random());
firefly.style.setProperty('--randomX4', Math.random());
firefly.style.setProperty('--randomY4', Math.random());
firefly.style.animation = `moveRandom var(--speed) ease-in-out forwards, fadeInOut var(--speed) ease-in-out, twinkle var(--twinkle-time) infinite`;
setTimeout(() => {
firefly.remove();
activeFireflies--;
}, parseFloat(firefly.style.getPropertyValue('--speed')) * 1000);
}
function spawnFireflies() {
setTimeout(() => {
if (activeFireflies < maxFireflies) {
const count = Math.min(Math.floor(Math.random() * 4) + 4, maxFireflies - activeFireflies);
for (let i = 0; i < count; i++) {
setTimeout(createFirefly, Math.random() * 5000);
}
}
//Continue spawning every 4s after the first batch
setInterval(() => {
if (activeFireflies < maxFireflies) {
const count = Math.min(Math.floor(Math.random() * 4) + 4, maxFireflies - activeFireflies);
for (let i = 0; i < count; i++) {
setTimeout(createFirefly, Math.random() * 5000); // Keep the random delay
}
}
}, 4000);
}, 1200); //First batch spawns after 1 second
}
spawnFireflies();
//text selection critera for touch screens and mouse
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".selectable").forEach(function (element) {
let touchTimer;
element.addEventListener("touchstart", function (e) {
if (e.touches.length === 1) {
touchTimer = setTimeout(() => selectableTextHandler.enableSelection(element), 500);
}
});
element.addEventListener("touchend", function () {
clearTimeout(touchTimer);
});
// Disable selection initially
element.style.userSelect = "none";
});
// Reset selection if user taps elsewhere
document.addEventListener("touchstart", function (e) {
if (!e.target.classList.contains("selectable")) {
selectableTextHandler.disableSelection();
}
});
});
// Encapsulated functions for clean behavior
const selectableTextHandler = {
enableSelection: function (element) {
element.style.userSelect = "text";
},
disableSelection: function () {
document.querySelectorAll(".selectable").forEach(element => {
element.style.userSelect = "none"; // Require long press again
});
}
};
//audio settings
// Check if an audio element already exists, if not, create one
let audio = document.getElementById("audio");
if (!audio) {
audio = document.createElement("audio");
audio.id = "audio";
audio.src = "Nostalgia-C418.aac"; // Change to your actual audio file
audio.loop = true;
document.body.appendChild(audio);
}
// Select UI elements
const playPauseBtn = document.getElementById("playPauseBtn");
const progressBar = document.getElementById("progressBar");
// Load saved playback state & position
document.addEventListener("DOMContentLoaded", () => {
const savedTime = localStorage.getItem("audioTime");
const savedState = localStorage.getItem("audioState");
if (savedTime) {
audio.currentTime = parseFloat(savedTime);
}
if (savedState === "playing") {
audio.play().then(() => {
playPauseBtn.textContent = "••"; // Update button UI
}).catch(() => {
console.warn("Autoplay was blocked, user interaction needed.");
});
}
});
// Play/Pause Function
playPauseBtn.addEventListener("click", () => {
if (audio.paused) {
audio.play();
localStorage.setItem("audioState", "playing");
playPauseBtn.textContent = "••";
} else {
audio.pause();
localStorage.setItem("audioState", "paused");
playPauseBtn.textContent = "▶";
}
});
// Update Progress Bar
audio.addEventListener("timeupdate", () => {
localStorage.setItem("audioTime", audio.currentTime);
localStorage.setItem("audioState", audio.paused ? "paused" : "playing");
const progress = (audio.currentTime / audio.duration) * 100;
progressBar.value = progress;
progressBar.style.background = `linear-gradient(to right, #689e9f1b ${progress}%, #00000000 ${progress}%)`;
});
// Seek audio when progress bar is clicked/dragged
progressBar.addEventListener("input", () => {
const seekTime = (progressBar.value / 100) * audio.duration;
audio.currentTime = seekTime;
progressBar.style.background = `linear-gradient(to right, #689e9f1b ${progressBar.value}%, #00000000 ${progressBar.value}%)`;
});
// Prevent audio from pausing on navigation
window.addEventListener("beforeunload", () => {
localStorage.setItem("audioTime", audio.currentTime);
localStorage.setItem("audioState", audio.paused ? "paused" : "playing");
});
// Ensure audio persists across navigation
if (sessionStorage.getItem("audioInitialized") !== "true") {
sessionStorage.setItem("audioInitialized", "true");
document.body.appendChild(audio);
}
audio.addEventListener("ended", () => {
audio.currentTime = 0;
audio.play(); // Restart playback when finished
});