Skip to content

Commit 2ced8a8

Browse files
committed
feat: improve piano page layout and fix router caching
1 parent 8714c25 commit 2ced8a8

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

app.js

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ function createPcgRandom(seed = 1) {
3131
const pcg = createPcgRandom(Date.now());
3232

3333
const app = document.getElementById("app");
34-
let currentPage = null; // To track the current page animation
34+
const pageCache = {}; // To cache page content and scripts
35+
let currentPage = null; // To track the current page
3536

3637
const router = async () => {
37-
// 1. Stop any running animation from the previous page
38+
const path = location.hash.slice(1) || "/";
39+
const pageName = path.substring(1) || "home";
40+
const route = routes[path];
41+
42+
// Stop any running animation from the previous page
3843
if (
3944
currentPage &&
4045
window.pageAnimations &&
@@ -44,30 +49,50 @@ const router = async () => {
4449
window.pageAnimations[currentPage].stop();
4550
}
4651

47-
// 2. Remove previously loaded dynamic scripts
48-
document
49-
.querySelectorAll("script[data-dynamic-script]")
50-
.forEach((s) => s.remove());
51-
52-
const path = location.hash.slice(1) || "/";
53-
const route = routes[path];
54-
currentPage = path.substring(1) || "home"; // Update current page
52+
// Hide all page containers
53+
Object.values(pageCache).forEach((p) => {
54+
if (p.container) {
55+
p.container.classList.add("page-hidden");
56+
}
57+
});
5558

56-
if (route) {
59+
if (pageCache[pageName]) {
60+
// Page is in cache, just show it
61+
pageCache[pageName].container.classList.remove("page-hidden");
62+
currentPage = pageName;
63+
// Restart animation if available
64+
if (
65+
window.pageAnimations &&
66+
window.pageAnimations[currentPage] &&
67+
typeof window.pageAnimations[currentPage].start === "function"
68+
) {
69+
window.pageAnimations[currentPage].start();
70+
}
71+
} else if (route) {
72+
// Page not in cache, load it
5773
try {
5874
const response = await fetch(route);
5975
if (!response.ok) throw new Error(`Page not found: ${route}`);
6076
const html = await response.text();
61-
app.innerHTML = html;
6277

63-
// Automatically load the corresponding script
78+
// Create a new container for the page
79+
const container = document.createElement("div");
80+
container.id = `${pageName}-container`;
81+
container.innerHTML = html;
82+
app.appendChild(container);
83+
84+
pageCache[pageName] = { container };
85+
currentPage = pageName;
86+
87+
// Load the corresponding script
6488
const scriptPath = route.replace(".html", ".js");
6589
const script = document.createElement("script");
6690
script.src = scriptPath;
67-
script.setAttribute("data-dynamic-script", "true");
91+
script.dataset.page = pageName; // Associate script with page
6892
document.body.appendChild(script);
6993

7094
script.onload = () => {
95+
// Start animation if available
7196
if (
7297
window.pageAnimations &&
7398
window.pageAnimations[currentPage] &&
@@ -215,8 +240,8 @@ const matrixEffect = (canvasId, containerSelector) => {
215240
particles.push({
216241
x: pcg() * canvas.width,
217242
y: pcg() * canvas.height,
218-
radius: pcg() * 170 + 25,
219-
speed: pcg() * 6,
243+
radius: pcg() * 220 + 30,
244+
speed: pcg() * 10 + 1,
220245
dir: { x: pcg() - 0.5, y: pcg() - 0.5 },
221246
});
222247
}

style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ select:hover {
408408
transform: rotate(-45deg) translate(7px, -6px);
409409
}
410410

411+
.page-hidden {
412+
display: none;
413+
}
414+
411415
/* Responsive Design */
412416
@media (max-width: 768px) {
413417
.menu-toggle {

0 commit comments

Comments
 (0)