Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
283 lines (244 loc) · 10.6 KB
/
Copy pathscript.js
File metadata and controls
283 lines (244 loc) · 10.6 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Get references to the elements
const mainContainer = document.getElementById('mainContainer');
const initialScreen = document.getElementById('initialScreen');
const successScreen = document.getElementById('successScreen');
const goodbyeContainer = document.getElementById('goodbyeContainer'); // New container
const goodbyeMessageText = document.getElementById('goodbyeMessageText'); // New text element
const character = document.getElementById('character');
const yesButton = document.getElementById('yesButton');
const noButton = document.getElementById('noButton');
const bgMusic = document.getElementById('bgMusic');
// Image sources
const initialCharSrc = 'images/char-initial.png';
const confusedCharSrc = 'images/char-confused.png';
const angryCharSrc = 'images/char-angry.png';
const sadCharSrc = 'images/char-sad.png';
const happyCharSrc = 'images/char-happy.png';
// Scaling parameters
let yesScale = 1.0;
let noScale = 1.0;
const scaleIncrement = 0.2;
const shrinkDecrement = 0.15;
let targetYesWidth = null;
let originalYesWidth = null;
// Flags and State
let scalingActive = true;
let isNoButtonClicked = false;
let musicStarted = false;
let goodbyeTimeoutId = null; // <<<--- ID for the goodbye timeout
// --- Initialization ---
document.addEventListener('DOMContentLoaded', () => {
console.log("DOM Loaded. Initializing...");
if (!mainContainer || !initialScreen || !successScreen || !goodbyeContainer || !goodbyeMessageText || !character || !yesButton || !noButton) {
console.error("One or more essential elements not found!");
return; // Stop if critical elements are missing
}
if (!bgMusic) console.warn("Audio element #bgMusic not found.");
// Calculate button widths
originalYesWidth = yesButton.offsetWidth;
const containerPaddingLeft = parseFloat(getComputedStyle(mainContainer).paddingLeft);
const containerPaddingRight = parseFloat(getComputedStyle(mainContainer).paddingRight);
const availableWidth = mainContainer.clientWidth - containerPaddingLeft - containerPaddingRight;
targetYesWidth = availableWidth * 0.75; // Target 75% width
console.log(`Original Yes Width: ${originalYesWidth}, Target Yes Width: ${targetYesWidth}`);
// Reset visual state
resetVisualState();
// Reset flags
scalingActive = true;
isNoButtonClicked = false;
yesScale = 1.0;
noScale = 1.0;
if (goodbyeTimeoutId) { // Clear any leftover timeout from previous state
clearTimeout(goodbyeTimeoutId);
goodbyeTimeoutId = null;
}
// Add event listeners
removeNoButtonListeners(); // Clear first
addNoButtonListeners();
yesButton.removeEventListener('click', handleYesClick);
yesButton.addEventListener('click', handleYesClick);
console.log("Initialization complete.");
// Attempt Autoplay Music
attemptAutoplay();
});
// --- Reset Visual State Function ---
function resetVisualState() {
if (character) character.src = initialCharSrc;
if (yesButton) {
yesButton.style.transform = 'scale(1)';
yesButton.style.opacity = '1';
yesButton.style.pointerEvents = 'auto';
}
if (noButton) {
noButton.style.transform = 'scale(1)';
noButton.style.opacity = '1';
noButton.classList.remove('hidden-final');
noButton.style.pointerEvents = 'auto';
noButton.style.display = 'inline-block'; // Ensure it's visible
}
if (initialScreen) {
initialScreen.style.opacity = '1';
initialScreen.style.visibility = 'visible';
initialScreen.classList.remove('hidden-final'); // Remove hiding class
initialScreen.style.display = 'flex'; // Ensure it's visible
}
if (successScreen) {
successScreen.classList.add('hidden');
successScreen.classList.remove('visible');
}
if (goodbyeContainer) { // Reset goodbye screen too
goodbyeContainer.classList.add('hidden');
goodbyeContainer.classList.remove('visible');
}
if (goodbyeMessageText) {
goodbyeMessageText.textContent = ''; // Clear text
}
console.log("Visual state reset.");
}
// --- Autoplay Music ---
function attemptAutoplay() {
if (bgMusic) {
console.log("Attempting to autoplay background music...");
const playPromise = bgMusic.play();
if (playPromise !== undefined) {
playPromise.then(() => {
console.log("Background music autoplay started successfully.");
musicStarted = true;
}).catch(error => {
console.error("Background music autoplay failed:", error);
musicStarted = false;
});
}
}
}
// --- Play Music Manually (Fallback) ---
function playMusicManually() {
if (!musicStarted && bgMusic) {
console.log("Attempting manual audio play...");
bgMusic.play().then(() => {
musicStarted = true;
console.log("Manual play successful.");
}).catch(e => console.error("Manual play failed:", e));
}
}
// --- "不要" Button Interaction Logic ---
function handleNoMouseOver() {
if (scalingActive && !isNoButtonClicked && character) {
character.src = confusedCharSrc;
}
}
function handleNoMouseOut() {
if (scalingActive && !isNoButtonClicked && character) {
character.src = initialCharSrc;
}
}
function handleNoClick() {
playMusicManually(); // Try to play music on interaction
if (!scalingActive || !originalYesWidth || !yesButton || !noButton || !character) return;
if (!isNoButtonClicked) {
isNoButtonClicked = true;
character.src = angryCharSrc;
}
const currentYesWidth = originalYesWidth * yesScale;
const yesIsLargeEnough = currentYesWidth >= targetYesWidth;
const noIsTooSmall = noScale <= shrinkDecrement; // Check if scale would go to 0 or less
if (yesIsLargeEnough || noIsTooSmall) {
// --- Final "No" click ---
console.log("Condition met: Hiding No button.");
yesScale = targetYesWidth / originalYesWidth; // Set final size for Yes
noScale = 0; // Ensure No button scale is 0
yesButton.style.transform = `scale(${yesScale})`;
noButton.style.transform = 'scale(0)';
noButton.classList.add('hidden-final'); // Apply class for potential transition/hiding
noButton.style.pointerEvents = 'none';
scalingActive = false; // Stop further scaling
removeNoButtonListeners(); // Remove listeners from No button
character.src = sadCharSrc;
console.log("No button hidden. Scaling deactivated. Character sad.");
// --- Start the Goodbye Timeout ---
if (goodbyeTimeoutId) clearTimeout(goodbyeTimeoutId); // Clear previous just in case
goodbyeTimeoutId = setTimeout(showGoodbyeMessage, 5000); // 5 seconds
console.log(`Goodbye timeout started (ID: ${goodbyeTimeoutId}). Will trigger in 3s.`);
// --------------------------------
} else {
// --- Intermediate "No" click ---
yesScale += scaleIncrement;
noScale -= shrinkDecrement;
noScale = Math.max(0, noScale); // Don't go below 0
yesButton.style.transform = `scale(${yesScale})`;
noButton.style.transform = `scale(${noScale})`;
// Change to sad character earlier if No button gets small
if (noScale < 0.7 && character.src !== sadCharSrc && isNoButtonClicked) {
character.src = sadCharSrc;
console.log("No button shrinking. Character set to sad.");
}
}
}
function addNoButtonListeners() {
if (!noButton) return;
noButton.addEventListener('mouseover', handleNoMouseOver);
noButton.addEventListener('mouseout', handleNoMouseOut);
noButton.addEventListener('click', handleNoClick);
}
function removeNoButtonListeners() {
if (!noButton) return;
noButton.removeEventListener('mouseover', handleNoMouseOver);
noButton.removeEventListener('mouseout', handleNoMouseOut);
noButton.removeEventListener('click', handleNoClick);
}
// --- "可以" Button Logic ---
function handleYesClick() {
playMusicManually(); // Try to play music on interaction
// --- Cancel the Goodbye Timeout if it's running ---
if (goodbyeTimeoutId) {
clearTimeout(goodbyeTimeoutId);
goodbyeTimeoutId = null;
console.log("Goodbye timeout cancelled because 'Yes' was clicked.");
}
// -------------------------------------------------
console.log("Yes button clicked.");
if (!initialScreen || !successScreen || !character) return;
scalingActive = false; // Stop any scaling
isNoButtonClicked = false; // Reset flag
removeNoButtonListeners(); // Remove listeners from No button
character.src = happyCharSrc; // Show happy character immediately
// Start transition to success screen
setTimeout(() => {
console.log("Fading out initial screen for success.");
initialScreen.classList.add('hidden-final'); // Use class to fade out
if (noButton) noButton.style.display = 'none'; // Hide No button completely
successScreen.classList.remove('hidden'); // Remove display: none
setTimeout(() => { // Short delay for display change to register
successScreen.classList.add('visible'); // Start fade-in
console.log("Success screen made visible.");
}, 50);
}, 150); // Small delay after click
}
// --- Goodbye Message Logic ---
function showGoodbyeMessage() {
console.log("Timeout expired. Showing goodbye message sequence.");
goodbyeTimeoutId = null; // Mark timeout as finished
if (!initialScreen || !goodbyeContainer || !goodbyeMessageText) {
console.error("Cannot show goodbye message - elements missing.");
return;
}
// 1. Hide the initial screen content immediately
initialScreen.style.transition = 'none'; // Disable transitions for immediate hide
initialScreen.style.display = 'none';
console.log("Initial screen hidden for goodbye message.");
// 2. Prepare and show "好吧"
goodbyeMessageText.textContent = '好吧😂';
goodbyeContainer.classList.remove('hidden'); // Remove display: none
setTimeout(() => { // Short delay for display change
goodbyeContainer.classList.add('visible'); // Start fade-in
console.log("Displayed '好吧'.");
// 3. Schedule change to "祝你幸福"
setTimeout(() => {
if (goodbyeMessageText) { // Check if element still exists
goodbyeMessageText.textContent = '祝你幸福😅';
console.log("Changed text to '祝你幸福'.");
}
}, 1500); // Wait 1.5 seconds before changing text
}, 50); // Delay for display change
}
// --- End of script ---