File size: 13,027 Bytes
49b07ed | 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | // Celestial Cycles Symphony - Main JavaScript
class CelestialSystem {
constructor() {
this.isPlaying = true;
this.timeScale = 10;
this.currentTime = 6 * 60; // 6:00 AM in minutes
this.dayDuration = 24 * 60; // 24 hours in minutes
this.initializeElements();
this.startSimulation();
this.setupEventListeners();
}
initializeElements() {
this.sun = document.getElementById('sun');
this.moon = document.getElementById('moon');
this.stars = document.getElementById('stars');
this.currentTimeDisplay = document.getElementById('currentTime');
this.timeScaleInput = document.getElementById('timeScale');
this.timeScaleValue = document.getElementById('timeScaleValue');
this.pauseBtn = document.getElementById('pauseBtn');
this.sunPosition = document.getElementById('sunPosition');
this.moonPosition = document.getElementById('moonPosition');
this.timeProgress = document.getElementById('timeProgress');
}
setupEventListeners() {
this.timeScaleInput.addEventListener('input', (e) => {
this.timeScale = parseInt(e.target.value);
this.timeScaleValue.textContent = `${this.timeScale}x`;
this.updateSimulationSpeed();
this.updateCelestialInfo();
this.updateBackground();
this.updateAtmosphericEffects();
this.updateLighting();
this.updateTimeDisplay();
this.updateProgress();
this.updateStars();
this.updateHorizon();
this.updateClouds();
this.updateShadows();
this.updateReflections();
this.updateParticles();
this.updateAudio();
this.updateWeather();
this.updateSeasonalEffects();
});
this.pauseBtn.addEventListener('click', () => {
this.togglePause();
});
// Keyboard controls
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
e.preventDefault();
this.togglePause();
}
});
}
togglePause() {
this.isPlaying = !this.isPlaying;
const icon = this.pauseBtn.querySelector('i');
const text = this.pauseBtn.querySelector('span:last-child');
if (this.isPlaying) {
icon.setAttribute('data-feather', 'pause');
text.textContent = 'Pause';
this.pauseBtn.classList.remove('bg-red-600');
this.pauseBtn.classList.add('bg-primary-600');
} else {
icon.setAttribute('data-feather', 'play');
text.textContent = 'Play';
this.pauseBtn.classList.remove('bg-primary-600');
this.pauseBtn.classList.add('bg-red-600');
}
feather.replace();
}
startSimulation() {
const update = () => {
if (this.isPlaying) {
this.currentTime += this.timeScale / 60; // Convert to minutes
if (this.currentTime >= this.dayDuration) {
this.currentTime = 0;
}
this.updateAll();
}
requestAnimationFrame(update);
};
update();
}
updateAll() {
this.updateCelestialPositions();
this.updateBackground();
this.updateAtmosphericEffects();
this.updateLighting();
this.updateTimeDisplay();
this.updateCelestialInfo();
this.updateProgress();
this.updateStars();
this.updateHorizon();
this.updateClouds();
this.updateShadows();
this.updateReflections();
this.updateParticles();
this.updateAudio();
this.updateWeather();
this.updateSeasonalEffects();
}
updateCelestialPositions() {
const timeRatio = this.currentTime / this.dayDuration;
// Sun movement (east to west)
const sunX = 10 + (timeRatio * 80); // 10% to 90% of container width
const sunY = this.calculateSunElevation(timeRatio);
// Moon movement (west to east, opposite of sun)
const moonX = 90 - (timeRatio * 80);
const moonY = this.calculateMoonElevation(timeRatio);
this.sun.style.left = `${sunX}%`;
this.sun.style.bottom = `${sunY}%`;
this.moon.style.left = `${moonX}%`;
this.moon.style.bottom = `${moonY}%`;
// Update sun and moon visibility
const sunOpacity = this.calculateSunOpacity(timeRatio);
const moonOpacity = this.calculateMoonOpacity(timeRatio);
this.sun.style.opacity = sunOpacity;
this.moon.style.opacity = moonOpacity;
}
calculateSunElevation(timeRatio) {
// Parabolic curve for sun elevation
const angle = (timeRatio - 0.5) * Math.PI;
return 5 + (Math.cos(angle) * 25);
}
calculateMoonElevation(timeRatio) {
// Similar to sun but offset
const angle = ((timeRatio + 0.5) % 1 - 0.5) * Math.PI;
return 5 + (Math.cos(angle) * 20);
}
calculateSunOpacity(timeRatio) {
// Sun is visible during daytime
if (timeRatio >= 0.25 && timeRatio <= 0.75) {
return 1;
}
// Gradual fade during sunrise/sunset
if (timeRatio >= 0.2 && timeRatio < 0.25) {
return (timeRatio - 0.2) / 0.05;
}
if (timeRatio > 0.75 && timeRatio <= 0.8) {
return (0.8 - timeRatio) / 0.05;
}
return 0;
}
calculateMoonOpacity(timeRatio) {
// Moon is visible during nighttime
if (timeRatio <= 0.2 || timeRatio >= 0.8) {
return 1;
}
// Gradual fade during moonrise/moonset
if (timeRatio > 0.75 && timeRatio < 0.8) {
return (timeRatio - 0.75) / 0.05;
}
if (timeRatio > 0.2 && timeRatio < 0.25) {
return (0.25 - timeRatio) / 0.05;
}
return 0;
}
updateBackground() {
const timeRatio = this.currentTime / this.dayDuration;
const container = this.sun.parentElement;
// Dynamic sky gradient based on time
let gradient;
if (timeRatio < 0.25) { // Night to morning
gradient = 'from-indigo-900 via-purple-700 to-blue-500';
} else if (timeRatio < 0.35) { // Morning
gradient = 'from-blue-500 via-cyan-400 to-blue-300';
} else if (timeRatio < 0.65) { // Day
gradient = 'from-blue-400 via-cyan-300 to-blue-200';
} else if (timeRatio < 0.75) { // Evening
gradient = 'from-orange-400 via-red-500 to-purple-700';
} else { // Evening to night
gradient = 'from-purple-700 via-indigo-800 to-gray-900';
}
container.className = container.className.replace(/from-\S+ via-\S+ to-\S+/, gradient);
}
updateAtmosphericEffects() {
const timeRatio = this.currentTime / this.dayDuration;
// Update sun glow intensity
const sunGlow = this.sun.querySelector('div');
if (timeRatio >= 0.35 && timeRatio <= 0.65) {
sunGlow.classList.add('animate-pulse');
} else {
sunGlow.classList.remove('animate-pulse');
}
}
updateLighting() {
const timeRatio = this.currentTime / this.dayDuration;
// Simulate lighting changes
document.documentElement.style.setProperty('--light-intensity', this.calculateLightIntensity(timeRatio));
}
calculateLightIntensity(timeRatio) {
// Bell curve for light intensity
const center = 0.5;
const width = 0.25;
const distance = Math.abs(timeRatio - center);
return Math.max(0, 1 - (distance / width) ** 2);
}
updateTimeDisplay() {
const hours = Math.floor(this.currentTime / 60);
const minutes = Math.floor(this.currentTime % 60);
const period = hours >= 12 ? 'PM' : 'AM';
const displayHours = hours % 12 || 12;
this.currentTimeDisplay.textContent =
`${displayHours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')} ${period}`;
}
updateCelestialInfo() {
const timeRatio = this.currentTime / this.dayDuration;
// Sun position info
const sunElevation = Math.round(this.calculateSunElevation(timeRatio) * 3.6);
const sunAzimuth = Math.round(90 + (timeRatio * 180) % 360);
this.sunPosition.textContent = `Elevation: ${sunElevation}° | Azimuth: ${sunAzimuth}°`;
// Moon position info
const moonElevation = Math.round(this.calculateMoonElevation(timeRatio) * 3.6);
const moonAzimuth = Math.round(270 - (timeRatio * 180) % 360);
this.moonPosition.textContent = `Elevation: ${moonElevation}° | Azimuth: ${moonAzimuth}°`;
}
updateProgress() {
const dayProgress = Math.min(1, Math.max(0, (this.currentTime / this.dayDuration - 0.25) / 0.5);
const nightProgress = 1 - dayProgress;
this.timeProgress.textContent =
`Day: ${Math.round(dayProgress * 100)}% | Night: ${Math.round(nightProgress * 100)}%`;
}
updateStars() {
const timeRatio = this.currentTime / this.dayDuration;
const starOpacity = (timeRatio <= 0.2 || timeRatio >= 0.8) ? 1 : 0;
this.stars.style.opacity = starOpacity;
}
updateHorizon() {
// Update horizon line color based on time
const timeRatio = this.currentTime / this.dayDuration;
const horizon = document.querySelector('.absolute.bottom-0');
if (timeRatio < 0.25 || timeRatio > 0.75) {
horizon.className = horizon.className.replace(/from-\S+ to-\S+/, 'from-gray-900 to-gray-700');
} else {
horizon.className = horizon.className.replace(/from-\S+ to-\S+/, 'from-green-900 to-green-700');
}
updateClouds() {
// Cloud movement and lighting simulation
const timeRatio = this.currentTime / this.dayDuration;
document.documentElement.style.setProperty('--cloud-brightness', this.calculateCloudBrightness(timeRatio));
}
calculateCloudBrightness(timeRatio) {
return Math.max(0.3, Math.min(1, this.calculateLightIntensity(timeRatio) + 0.3));
}
updateShadows() {
const timeRatio = this.currentTime / this.dayDuration;
const shadowLength = 50 + (Math.abs(timeRatio - 0.5) * 100);
document.documentElement.style.setProperty('--shadow-length', `${shadowLength}px`);
}
updateReflections() {
// Water reflection intensity
const timeRatio = this.currentTime / this.dayDuration;
const reflectionIntensity = timeRatio >= 0.35 && timeRatio <= 0.65 ? 0.8 : 0.3;
document.documentElement.style.setProperty('--reflection-intensity', reflectionIntensity);
}
updateParticles() {
// Dust particles, fireflies, etc.
const timeRatio = this.currentTime / this.dayDuration;
const particleDensity = timeRatio < 0.25 || timeRatio > 0.75 ? 0.7 : 0.1);
document.documentElement.style.setProperty('--particle-density', particleDensity);
}
updateAudio() {
// Background audio changes
const timeRatio = this.currentTime / this.dayDuration;
const audioVolume = Math.min(1, Math.max(0.1, this.calculateLightIntensity(timeRatio)));
document.documentElement.style.setProperty('--audio-volume', audioVolume);
}
updateWeather() {
// Weather system integration placeholder
const timeRatio = this.currentTime / this.dayDuration;
document.documentElement.style.setProperty('--weather-intensity', Math.random()));
}
updateSeasonalEffects() {
// Seasonal variations placeholder
const timeRatio = this.currentTime / this.dayDuration;
document.documentElement.style.setProperty('--season-factor', 1);
}
updateSimulationSpeed() {
// Adjust animation speeds based on time scale
document.documentElement.style.setProperty('--animation-speed', `${this.timeScale / 10}s`);
}
}
// Initialize the celestial system when the page loads
document.addEventListener('DOMContentLoaded', () => {
new CelestialSystem();
// Add fade-in animations to sections
const sections = document.querySelectorAll('section');
sections.forEach((section, index) => {
section.classList.add('fade-in-up');
section.style.animationDelay = `${index * 0.2}s`;
});
});
// Utility function for smooth transitions
function smoothTransition(element, property, targetValue, duration = 1000) {
element.style.transition = `${property} ${duration}ms cubic-bezier(0.4, 0, 0.2, 1)';
element.style[property] = targetValue;
}
// Export for potential module usage
if (typeof module !== 'undefined' && module.exports) {
module.exports = CelestialSystem;
} |