-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-toggle.js
217 lines (189 loc) · 6.24 KB
/
theme-toggle.js
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
// Function to toggle theme
function toggleTheme() {
let currentTheme = document.documentElement.getAttribute("data-theme");
let newTheme = currentTheme === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", newTheme);
localStorage.setItem("theme", newTheme);
updateButtonContent(newTheme);
}
function updateButtonContent(theme) {
const toggleButton = document.getElementById("toggleTheme");
if (theme === "dark") {
toggleButton.textContent = "🌙"; // moon emoji for dark mode
} else {
toggleButton.textContent = "☀️"; // sun emoji for light mode
}
}
// Check for stored theme preference
let storedTheme = localStorage.getItem("theme");
// Check for user's system preference
const userPrefersDark =
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
if (storedTheme) {
// Use stored theme if present
document.documentElement.setAttribute("data-theme", storedTheme);
updateButtonContent(storedTheme);
} else if (userPrefersDark) {
// Otherwise fallback to system preference
document.documentElement.setAttribute("data-theme", "dark");
updateButtonContent("dark");
} else {
document.documentElement.setAttribute("data-theme", "light");
updateButtonContent("light");
}
document.getElementById("toggleTheme").addEventListener("click", toggleTheme);
//name spinner
const nameSpinner = document.getElementById("nameSpinner");
// Variables to track rotation and freewheeling effect
let rotationAngle = 0;
let isFreewheeling = false;
let freewheelVelocity = 0;
const damping = 0.9999; // Damping factor for velocity decay
const epsilon = 0.00001; // Threshold for stopping the rotation
// Add click event listener
nameSpinner.addEventListener("click", function () {
if (!isFreewheeling) {
// Single click, perform a normal spin
rotationAngle += 360;
this.style.transform = `rotate(${rotationAngle}deg)`;
} else {
// Freewheeling mode, add velocity
freewheelVelocity += 180; // Adjust this value to control the added rotation speed
}
});
// Update the rotation and freewheeling effect
function updateRotation() {
if (isFreewheeling) {
// Apply freewheeling effect
rotationAngle += freewheelVelocity;
nameSpinner.style.transform = `rotate(${rotationAngle}deg)`;
freewheelVelocity *= damping;
if (Math.abs(freewheelVelocity) < epsilon) {
isFreewheeling = false;
freewheelVelocity = 0;
}
}
requestAnimationFrame(updateRotation);
}
updateRotation();
// Add event listener for freewheeling mode
let lastClickTime = 0;
const freewheelThreshold = 1000; // Threshold for entering freewheeling mode (in milliseconds)
nameSpinner.addEventListener("click", function () {
const currentTime = new Date().getTime();
const timeSinceLastClick = currentTime - lastClickTime;
if (timeSinceLastClick < freewheelThreshold || isFreewheeling) {
isFreewheeling = true;
} else {
isFreewheeling = false;
freewheelVelocity = 0;
}
lastClickTime = currentTime;
});
// clown mode
const toggleStyleButton = document.getElementById("toggleStyle");
const body = document.body;
const youtubeVideoContainer = document.getElementById("youtubeVideoContainer");
let currentState = 0;
const maxStates = 5;
const clownEmojis = ["🤡", "🤪", "😜", "😂", "🥳"];
let player;
// Load the YouTube Embed API
function loadYouTubeAPI() {
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
// Create the YouTube player
function onYouTubeIframeAPIReady() {
player = new YT.Player("youtubeVideoContainer", {
height: "360",
width: "640",
videoId: "2XRXT8xZ62s", // Replace with the actual YouTube video ID
events: {
onReady: onPlayerReady,
},
});
}
// Autoplay the video when ready
function onPlayerReady(event) {
if (currentState === 4) {
event.target.playVideo();
}
}
let originalContents = {};
let elementCounter = 0;
toggleStyleButton.addEventListener("click", function () {
currentState = (currentState + 1) % maxStates;
if (currentState === 0) {
body.classList.remove("funStyle", "state2", "state3");
body.style.backgroundColor = "";
body.style.color = "";
restoreOriginalContents();
youtubeVideoContainer.style.display = "none";
if (player) {
player.stopVideo();
}
} else if (currentState === 1) {
body.classList.add("funStyle");
} else if (currentState === 2) {
body.classList.add("state2");
body.style.backgroundColor = "#ffccff";
body.style.color = "#333";
} else if (currentState === 3) {
body.classList.add("state3");
storeOriginalContents();
document.querySelectorAll("p, li").forEach((el) => {
randomCapsTextNodes(el);
});
} else if (currentState === 4) {
if (confirm("engage clown mode? (this will play music)")) {
youtubeVideoContainer.style.display = "block";
if (player) {
player.playVideo();
}
} else {
currentState = 0; // If the user cancels, go back to State 0
}
}
toggleStyleButton.textContent = clownEmojis[currentState];
});
function storeOriginalContents() {
document.querySelectorAll("p, li").forEach((el) => {
const elementId = `element-${elementCounter}`;
el.setAttribute("data-element-id", elementId);
originalContents[elementId] = el.innerHTML;
elementCounter++;
});
}
function restoreOriginalContents() {
document.querySelectorAll("p, li").forEach((el) => {
const elementId = el.getAttribute("data-element-id");
if (elementId && originalContents.hasOwnProperty(elementId)) {
el.innerHTML = originalContents[elementId];
}
});
originalContents = {};
elementCounter = 0;
}
function randomCaps(text) {
return text
.split("")
.map((char) =>
Math.random() < 0.5 ? char.toUpperCase() : char.toLowerCase()
)
.join("");
}
function randomCapsTextNodes(element) {
Array.from(element.childNodes).forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
node.textContent = randomCaps(node.textContent);
} else if (node.nodeType === Node.ELEMENT_NODE) {
randomCapsTextNodes(node);
}
});
}
// Load the YouTube Embed API
loadYouTubeAPI();