-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (160 loc) · 5.23 KB
/
Copy pathscript.js
File metadata and controls
181 lines (160 loc) · 5.23 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
/**
* Toggles dark mode on the body and saves the preference to localStorage.
* This is now the default mode.
*/
function myFunction() {
const element = document.body;
element.classList.toggle("dark-mode");
// Save the user's preference in localStorage
if (element.classList.contains("dark-mode")) {
localStorage.setItem("darkMode", "enabled");
} else {
localStorage.setItem("darkMode", "disabled");
}
}
/**
* Applies the saved dark mode, font, high contrast, and theme from localStorage when a page loads.
* This function ensures the settings are consistent across all pages.
*/
function applyRememberedTheme() {
// Always apply dark mode as it's the default
document.body.classList.add("dark-mode");
// Apply font setting
const font = localStorage.getItem("siteFont") || "default";
document.body.classList.remove("font-large", "font-small");
if (font !== "default") {
document.body.classList.add(font);
}
// Apply high contrast setting
const highContrast = localStorage.getItem("highContrast") === "enabled";
if (highContrast) {
document.body.classList.add("high-contrast");
} else {
document.body.classList.remove("high-contrast");
}
// Apply the site theme (e.g., 'moonlight', 'sandy', or 'light')
const theme = localStorage.getItem("siteTheme") || "default";
document.body.classList.remove("moonlight", "sandy", "coffee", "forest", "light");
if (theme === "moonlight") {
document.body.classList.add("moonlight");
// Create the canvas for the stars background if it doesn't exist
if (!document.getElementById('stars-bg')) {
startStars();
}
} else if (theme === "light") {
document.body.classList.remove("dark-mode");
document.body.classList.add("light");
} else {
// For any other theme, remove the stars canvas if it exists
document.body.classList.add(theme);
const stars = document.getElementById('stars-bg');
if (stars) stars.remove();
}
}
// ... (rest of the script.js code) ...
// Theme select logic
const themeSelect = document.getElementById('theme-select');
if (themeSelect) {
const applyTheme = (theme) => {
document.body.classList.remove('moonlight', 'sandy', 'coffee', 'forest', 'light');
document.body.classList.remove('dark-mode');
if (theme === 'moonlight') {
document.body.classList.add('moonlight');
document.body.classList.add('dark-mode');
} else if (theme === 'sandy') {
document.body.classList.add('sandy');
document.body.classList.add('dark-mode');
} else if (theme === 'coffee') {
document.body.classList.add('coffee');
document.body.classList.add('dark-mode');
} else if (theme === 'forest') {
document.body.classList.add('forest');
document.body.classList.add('dark-mode');
} else if (theme === 'light') {
document.body.classList.add('light');
}
localStorage.setItem('siteTheme', theme);
};
themeSelect.addEventListener('change', function() {
applyTheme(this.value);
});
// Initialize on load
const currentTheme = localStorage.getItem('siteTheme') || 'default';
themeSelect.value = currentTheme;
applyTheme(currentTheme);
}
/**
* Creates and animates the moving stars background for the 'moonlight' theme.
*/
function startStars() {
if (document.getElementById('stars-bg')) return;
const canvas = document.createElement('canvas');
canvas.id = 'stars-bg';
canvas.style.cssText = 'position:fixed; top:0; left:0; z-index:-1; pointer-events:none;';
document.body.prepend(canvas);
let w = window.innerWidth,
h = window.innerHeight;
canvas.width = w;
canvas.height = h;
const ctx = canvas.getContext('2d');
const stars = Array.from({
length: 120
}, () => ({
x: Math.random() * w,
y: Math.random() * h,
r: Math.random() * 1.2 + 0.5,
dx: (Math.random() - 0.5) * 0.08,
dy: (Math.random() - 0.5) * 0.08
}));
function draw() {
ctx.clearRect(0, 0, w, h);
ctx.save();
ctx.globalAlpha = 0.8;
ctx.fillStyle = '#fff';
for (const s of stars) {
ctx.beginPath();
ctx.arc(s.x, s.y, s.r, 0, 2 * Math.PI);
ctx.fill();
}
ctx.restore();
}
function animate() {
// Only run animation if the theme is active
if (!document.body.classList.contains('moonlight')) {
const starsCanvas = document.getElementById('stars-bg');
if (starsCanvas) starsCanvas.remove();
return;
}
for (const s of stars) {
s.x += s.dx;
s.y += s.dy;
if (s.x < 0) s.x = w;
if (s.x > w) s.x = 0;
if (s.y < 0) s.y = h;
if (s.y > h) s.y = 0;
}
draw();
requestAnimationFrame(animate);
}
animate();
window.addEventListener('resize', () => {
w = window.innerWidth;
h = window.innerHeight;
canvas.width = w;
canvas.height = h;
});
}
/**
* Ensures the stars animation is properly removed on page transition.
*/
window.addEventListener('beforeunload', () => {
const stars = document.getElementById('stars-bg');
if (stars) {
stars.remove();
}
});
// --- Event Listeners ---
// When the page content is loaded, apply the saved theme.
window.addEventListener("DOMContentLoaded", applyRememberedTheme);
// When another tab changes the theme, sync it here.
window.addEventListener("storage", applyRememberedTheme);