-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
206 lines (176 loc) · 8.57 KB
/
script.js
File metadata and controls
206 lines (176 loc) · 8.57 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
// DOM Elements
const toggleAnimationBtn = document.getElementById('toggle-animation');
const changeGradientBtn = document.getElementById('change-gradient');
const toggleStarsBtn = document.getElementById('toggle-stars');
const randomGradientBtn = document.getElementById('random-gradient');
const speedBtns = document.querySelectorAll('.speed-btn');
const starsContainer = document.getElementById('stars');
const gradientCounter = document.getElementById('gradient-counter');
// Animation elements
const sunContainer = document.querySelector('.sun-container');
const moonContainer = document.querySelector('.moon-container');
const gradients = document.querySelectorAll('.sunset-gradient');
const skyElement = document.querySelector('.sky');
// Gradient names
const gradientNames = [
'Classic Orange', 'Golden Orange', 'Orange Violet', 'Ocean Blue',
'Reddish Orange', 'Golden Hour', 'Bluish Green', 'Violet Dusk',
'Deep Red', 'Purple Fusion', 'Amber Ember', 'Magenta Sky'
];
// State
let isPlaying = true;
let currentGradient = 0;
let starsVisible = false;
let animationSpeed = 1;
// Audio state
let isPlayingAudio = false;
// Audio elements
const bgAudio = document.getElementById('bgAudio');
const audioToggleBtn = document.getElementById('audioToggleBtn');
const audioIcon = document.getElementById('audioIcon');
const audioText = document.getElementById('audioText');
const audioWarning = document.getElementById('audioWarning');
// Create stars
function createStars() {
starsContainer.innerHTML = '';
for (let i = 0; i < 120; i++) {
const star = document.createElement('div');
star.classList.add('star');
star.style.left = `${Math.random() * 100}%`;
star.style.top = `${Math.random() * 70}%`;
const size = Math.random() * 2 + 1;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
star.style.animation = `twinkle ${Math.random() * 5 + 3}s infinite ${Math.random() * 5}s`;
starsContainer.appendChild(star);
}
}
// Change gradient
function changeGradient(direction = 'next') {
gradients.forEach(g => g.style.opacity = '0');
if (direction === 'next') {
currentGradient = (currentGradient + 1) % gradients.length;
}
gradients[currentGradient].style.opacity = '1';
gradientCounter.textContent = `${gradientNames[currentGradient]} (${currentGradient + 1}/${gradients.length})`;
}
// Random gradient
function randomGradient() {
gradients.forEach(g => g.style.opacity = '0');
let newGradient;
do {
newGradient = Math.floor(Math.random() * gradients.length);
} while (newGradient === currentGradient && gradients.length > 1);
currentGradient = newGradient;
gradients[currentGradient].style.opacity = '1';
gradientCounter.textContent = `${gradientNames[currentGradient]} (${currentGradient + 1}/${gradients.length})`;
}
// Toggle stars
function toggleStars() {
starsVisible = !starsVisible;
starsContainer.style.opacity = starsVisible ? '1' : '0';
toggleStarsBtn.innerHTML = starsVisible ? '✨ Hide' : '✨ Show';
}
// Toggle animation
function toggleAnimation() {
isPlaying = !isPlaying;
const playingText = toggleAnimationBtn.querySelector('.playing');
const pausedText = toggleAnimationBtn.querySelector('.paused');
[sunContainer, moonContainer].forEach(el => {
el.classList.toggle('paused', !isPlaying);
});
if (isPlaying) {
playingText.style.display = 'inline';
pausedText.style.display = 'none';
} else {
playingText.style.display = 'none';
pausedText.style.display = 'inline';
}
}
// Change speed
function changeSpeed(speed) {
animationSpeed = speed;
sunContainer.style.animationDuration = `${30 / speed}s`;
moonContainer.style.animationDuration = `${30 / speed}s`;
speedBtns.forEach(btn => {
btn.classList.toggle('active', parseFloat(btn.dataset.speed) === speed);
});
}
async function toggleAudio() {
try {
if (!isPlayingAudio) {
// Try to play
await bgAudio.play();
isPlayingAudio = true;
audioIcon.textContent = '⏸️';
audioText.textContent = 'Pause Music';
audioWarning.classList.remove('visible');
} else {
bgAudio.pause();
isPlayingAudio = false;
audioIcon.textContent = '🎵';
audioText.textContent = 'Play Music';
}
} catch (error) {
console.log('Audio error:', error);
audioWarning.textContent = '⚠️ Click again to play (browser needs interaction)';
audioWarning.classList.add('visible');
// Retry once
setTimeout(async () => {
try {
await bgAudio.play();
isPlayingAudio = true;
audioIcon.textContent = '⏸️';
audioText.textContent = 'Pause Music';
audioWarning.classList.remove('visible');
} catch (e) {
console.log('Retry failed');
}
}, 300);
}
}
// Audio event listeners
bgAudio.addEventListener('canplaythrough', () => {
audioWarning.textContent = '🎵 Click "Play Music" for ambient soundtrack';
audioWarning.classList.add('visible');
});
bgAudio.addEventListener('error', () => {
audioWarning.textContent = '⚠️ Audio unavailable. Enjoy the visual sunset.';
audioWarning.classList.add('visible');
});
// Audio toggle button
audioToggleBtn.addEventListener('click', toggleAudio);
// EVENT LISTENERS
toggleAnimationBtn.addEventListener('click', toggleAnimation);
changeGradientBtn.addEventListener('click', () => changeGradient('next'));
toggleStarsBtn.addEventListener('click', toggleStars);
randomGradientBtn.addEventListener('click', randomGradient);
speedBtns.forEach(btn => {
btn.addEventListener('click', () => changeSpeed(parseFloat(btn.dataset.speed)));
});
// Touch screen detection
let touchStartX = 0;
skyElement.addEventListener('touchstart', (e) => {
touchStartX = e.touches[0].clientX;
}, { passive: true });
skyElement.addEventListener('touchend', (e) => {
if (touchStartX === 0) return;
const touchEndX = e.changedTouches[0].clientX;
const diff = touchEndX - touchStartX;
if (Math.abs(diff) > 50) {
if (diff > 0) {
gradients.forEach(g => g.style.opacity = '0');
currentGradient = (currentGradient - 1 + gradients.length) % gradients.length;
gradients[currentGradient].style.opacity = '1';
gradientCounter.textContent = `${gradientNames[currentGradient]} (${currentGradient + 1}/${gradients.length})`;
} else {
changeGradient('next');
}
}
touchStartX = 0;
}, { passive: true });
// Initialize
createStars();
changeSpeed(1);
// Auto-cycle gradients
setInterval(() => changeGradient('next'), 15000);