-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
198 lines (172 loc) · 6.4 KB
/
Copy pathscript.js
File metadata and controls
198 lines (172 loc) · 6.4 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
const coinContainer = document.getElementById("coinContainer");
const coinCountInput = document.getElementById("coinCount");
const headsCount = document.getElementById("headsCount");
const tailsCount = document.getElementById("tailsCount");
const totalCoinsCount = document.getElementById("totalCoinsCount");
const totalTossCount = document.getElementById("totalTossCount");
const tossStyleToggle = document.getElementById("tossStyleToggle");
const flipSound = document.getElementById("flipSound");
const spinSound = document.getElementById("spinSound");
const sidebar = document.getElementById("sidebar");
const drawerToggle = document.getElementById("drawerToggle");
let currentMaterial = "gold";
let heads = 0,
tails = 0,
totalTosses = 0,
totalCoins = 0;
drawerToggle.onclick = () => {
const isOpen = sidebar.classList.toggle("open");
drawerToggle.setAttribute("aria-expanded", isOpen);
};
document.getElementById("setGold").onclick = () => {
currentMaterial = "gold";
document.getElementById("setGold").classList.add("active");
document.getElementById("setGold").setAttribute("aria-pressed", "true");
document.getElementById("setSilver").classList.remove("active");
document.getElementById("setSilver").setAttribute("aria-pressed", "false");
};
document.getElementById("setSilver").onclick = () => {
currentMaterial = "silver";
document.getElementById("setSilver").classList.add("active");
document.getElementById("setSilver").setAttribute("aria-pressed", "true");
document.getElementById("setGold").classList.remove("active");
document.getElementById("setGold").setAttribute("aria-pressed", "false");
};
coinCountInput.onkeypress = (e) => {
if (e.key === "Enter") addCoins();
};
function addCoins() {
const count = parseInt(coinCountInput.value) || 1;
for (let i = 0; i < count; i++) createCoin();
}
function createCoin() {
const coin = document.createElement("div");
coin.className = `coin ${currentMaterial}`;
coin.setAttribute("role", "button");
coin.setAttribute("tabindex", "0");
coin.setAttribute("aria-label", "Toss this coin");
coin.innerHTML = `
<div class="side heads" aria-hidden="true">HEADS</div>
<div class="side tails" aria-hidden="true">TAILS</div>
`;
coin.onclick = () => tossCoin(coin);
coin.onkeypress = (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
tossCoin(coin);
}
};
coinContainer.appendChild(coin);
totalCoins++;
totalCoinsCount.textContent = totalCoins;
}
function tossCoin(coin) {
const result = Math.random() < 0.5 ? "heads" : "tails";
const isVertical = tossStyleToggle.checked;
const animationName = isVertical ? `toss-${result}` : `spin-${result}`;
const duration = isVertical ? "1.5s" : "5s";
const delay = isVertical ? 1500 : 5000;
isVertical ? flipSound.play() : spinSound.play();
coin.style.animation = "none";
void coin.offsetWidth;
coin.style.animation = `${animationName} ${duration} cubic-bezier(0.1, 0, 0.2, 1) forwards`;
setTimeout(() => {
if (result === "heads") {
heads++;
headsCount.textContent = heads;
} else {
tails++;
tailsCount.textContent = tails;
}
totalTosses++;
totalTossCount.textContent = totalTosses;
}, delay);
}
document.getElementById("addCoinsButton").onclick = addCoins;
document.getElementById("tossAllButton").onclick = () => {
document.querySelectorAll(".coin").forEach(tossCoin);
};
document.getElementById("tossButton").onclick = () => {
const coins = document.querySelectorAll(".coin");
if (coins.length) tossCoin(coins[coins.length - 1]);
};
createCoin();
const fullScreenBtn = document.getElementById("fullScreenIcon");
const exitFullScreenBtn = document.getElementById("exitFullScreenIcon");
const appFrame = document.getElementById("appFrame");
// Function to Enter Fullscreen
fullScreenBtn.onclick = () => {
if (appFrame.requestFullscreen) {
appFrame.requestFullscreen();
} else if (appFrame.webkitRequestFullscreen) {
/* Safari */
appFrame.webkitRequestFullscreen();
} else if (appFrame.msRequestFullscreen) {
/* IE11 */
appFrame.msRequestFullscreen();
}
};
// Function to Exit Fullscreen
exitFullScreenBtn.onclick = () => {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
/* Safari */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
/* IE11 */
document.msExitFullscreen();
}
};
// Listen for Fullscreen Change (Handles 'Esc' key)
document.addEventListener("fullscreenchange", handleFullscreenUI);
document.addEventListener("webkitfullscreenchange", handleFullscreenUI);
document.addEventListener("mozfullscreenchange", handleFullscreenUI);
document.addEventListener("MSFullscreenChange", handleFullscreenUI);
function handleFullscreenUI() {
const isFullscreen =
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement;
if (isFullscreen) {
fullScreenBtn.style.display = "none";
exitFullScreenBtn.style.display = "block";
} else {
fullScreenBtn.style.display = "block";
exitFullScreenBtn.style.display = "none";
}
}
// PWA Installation & Service Worker Logic
const installContainer = document.getElementById("installContainer");
const installAppButton = document.getElementById("installAppButton");
let deferredPrompt;
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker.register("./sw.js").then(
(registration) => console.log("ServiceWorker registration successful:", registration.scope),
(err) => console.log("ServiceWorker registration failed:", err)
);
});
}
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
deferredPrompt = e;
installContainer.style.display = "block";
});
installAppButton.addEventListener("click", async () => {
if (!deferredPrompt) return;
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
console.log(`User response to the install prompt: ${outcome}`);
deferredPrompt = null;
installContainer.style.display = "none";
});
window.addEventListener("appinstalled", () => {
installContainer.style.display = "none";
deferredPrompt = null;
console.log("PWA was installed successfully");
});
// Keep footer copyright year current
const yearEl = document.getElementById("year");
if (yearEl) yearEl.textContent = new Date().getFullYear();