-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
162 lines (132 loc) · 5.19 KB
/
script.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
let skittles = Array(64).fill(null); // Skittles start as null, meaning they haven't been flipped yet.
let shakeCount = 0;
const skittlesContainer = document.getElementById("skittles-container");
const headsCountElem = document.getElementById("heads-count");
const tailsCountElem = document.getElementById("tails-count");
const darkModeToggle = document.getElementById("dark-mode-toggle");
const startOverButton = document.getElementById("start-over");
const skittleColors = ["#e60000", "#ff9900", "#ffff00", "#00ff00", "#660066"]; // List of hex codes for different Skittle colors
function createSkittles() {
skittlesContainer.innerHTML = "";
skittles.forEach((state) => {
const skittle = document.createElement("div");
skittle.classList.add("skittle");
if (state === true) {
// For heads (showing "s"), pick a random color
const randomColor =
skittleColors[Math.floor(Math.random() * skittleColors.length)];
skittle.style.backgroundColor = randomColor;
skittle.textContent = "s";
} else if (state === false) {
// For tails, pick a random color but without showing "s"
const randomColor =
skittleColors[Math.floor(Math.random() * skittleColors.length)];
skittle.style.backgroundColor = randomColor;
skittle.textContent = "";
} else {
// Initial state is gray
skittle.classList.add("initial"); // Add class for initial gray state
skittle.textContent = "";
}
skittlesContainer.appendChild(skittle);
});
updateCounts();
}
function shakeSkittles() {
// Shake sound
document.getElementById("shake-sound").play();
// Increment the shake counter
shakeCount++;
document.getElementById("shake-count").textContent = shakeCount;
// Generate a random shake intensity between 5 and 15 pixels
const randomShake = Math.floor(Math.random() * 10) + 5;
skittlesContainer.style.setProperty("--shake-x", `${randomShake}px`);
// Add shake animation class to the container
skittlesContainer.classList.add("shake");
// Enable the "Start Over" button again because Skittles have now been changed
startOverButton.disabled = false;
startOverButton.classList.remove("disabled");
// Remove the shake class after the animation completes
setTimeout(() => {
skittlesContainer.classList.remove("shake");
performShake();
}, 500); // Match the duration of the shake animation (0.5s)
}
function performShake() {
let allTails = true;
// Iterate over the Skittles and update their state
skittles = skittles.map((state, index) => {
if (state === null || state === true) {
const newSide = Math.random() < 0.5; // 50% chance for heads (true) or tails (false)
if (newSide) allTails = false;
// Apply the flip animation for this Skittle
applyFlipAnimation(index);
// Return the new state
return newSide;
}
return state; // Keep the existing state if it's already tails
});
createSkittles();
if (allTails) {
// Launch confetti when all Skittles turn to tails
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 },
});
alert("All Skittles are now tails!");
}
}
function applyFlipAnimation(index) {
// Get the corresponding skittle element
const skittleElement = skittlesContainer.children[index];
// Add the flip animation class
skittleElement.classList.add("flip");
// Remove the flip class after the animation completes (600ms matches the CSS)
setTimeout(() => {
skittleElement.classList.remove("flip");
}, 600);
}
function startOver() {
// Check if all skittles are already in their initial state
const isAllGray = skittles.every((state) => state === null);
if (isAllGray) {
return; // Do nothing if they are already reset
}
// Reset the skittles to their initial state.
skittles = Array(64).fill(null);
createSkittles();
updateCounts();
// Reset the shake counter
shakeCount = 0;
document.getElementById("shake-count").textContent = shakeCount;
// Disable the "Start Over" button after resetting
startOverButton.disabled = true;
startOverButton.classList.add("disabled");
}
function updateCounts() {
const headsCount = skittles.filter((state) => state === true).length;
const tailsCount = skittles.filter((state) => state === false).length;
headsCountElem.textContent = headsCount;
tailsCountElem.textContent = tailsCount;
const progressPercentage = (tailsCount / skittles.length) * 100;
document.getElementById("progress").style.width = `${progressPercentage}%`;
}
// Disable the 'Start Over' button initially since Skittles start in their initial state
startOverButton.disabled = true;
startOverButton.classList.add("disabled");
createSkittles(); // Initialize Skittles on page load but keep counts at zero.
// Load user's previous theme preference if it exists
if (localStorage.getItem("dark-mode") === "enabled") {
document.body.classList.add("dark-mode");
darkModeToggle.checked = true;
}
darkModeToggle.addEventListener("change", () => {
if (darkModeToggle.checked) {
document.body.classList.add("dark-mode");
localStorage.setItem("dark-mode", "enabled");
} else {
document.body.classList.remove("dark-mode");
localStorage.setItem("dark-mode", "disabled");
}
});