File size: 20,283 Bytes
3f7abee
3a074cf
 
 
 
 
 
 
 
 
 
 
 
 
3f7abee
 
 
3a074cf
 
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
 
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
 
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
3a074cf
3f7abee
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
3a074cf
 
 
 
 
3f7abee
3a074cf
 
 
 
 
3f7abee
3a074cf
 
3f7abee
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
3a074cf
 
 
 
 
3f7abee
3a074cf
 
 
 
 
3f7abee
3a074cf
 
3f7abee
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
 
3f7abee
3a074cf
 
 
 
 
3f7abee
3a074cf
 
3f7abee
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
 
 
 
3a074cf
 
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
 
 
 
3a074cf
 
 
 
 
 
 
 
 
 
 
 
7a50ff7
3f7abee
3a074cf
3f7abee
3a074cf
7a50ff7
3a074cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f7abee
3a074cf
 
3f7abee
3a074cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f7abee
3a074cf
 
3f7abee
3a074cf
 
 
 
3f7abee
3a074cf
3f7abee
3a074cf
 
 
 
 
 
 
 
 
 
 
 
 
7a50ff7
 
 
 
 
3f7abee
3a074cf
 
 
 
3f7abee
 
 
 
 
 
 
 
 
 
3a074cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f7abee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a074cf
 
 
 
 
 
3f7abee
 
 
 
3a074cf
3f7abee
 
 
 
 
 
 
 
 
 
3a074cf
3f7abee
7a50ff7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52fb00f
 
3a074cf
52fb00f
3a074cf
 
52fb00f
 
3a074cf
52fb00f
3a074cf
 
52fb00f
3f7abee
 
 
 
3a074cf
3f7abee
3a074cf
 
 
3f7abee
3a074cf
 
 
 
 
 
 
 
 
 
 
3f7abee
52fb00f
3a074cf
 
 
 
 
 
 
3f7abee
 
3a074cf
 
 
 
 
 
 
 
 
 
3f7abee
52fb00f
 
 
 
 
 
3f7abee
 
3a074cf
52fb00f
3a074cf
 
 
 
 
 
 
52fb00f
 
3a074cf
 
 
 
 
 
 
 
 
 
3f7abee
 
3a074cf
3f7abee
52fb00f
 
 
3a074cf
 
 
 
 
 
 
 
 
 
 
3f7abee
 
 
3a074cf
 
 
 
 
52fb00f
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542

// Solar System Simulation
class SolarSystem {
    constructor() {
        this.canvas = document.getElementById('solarCanvas');
        this.ctx = this.canvas.getContext('2d');
        this.centerX = 0;
        this.centerY = 0;
        this.zoom = 1;
        this.speed = 1;
        this.isPaused = false;
        this.time = 0;
        this.fps = 60;
        this.lastFrameTime = performance.now();
        this.startDate = new Date();
        this.currentDate = new Date();
        this.timeLapseSpeed = 1; // days per frame
        
        this.planets = [
            {
                name: 'Mercury',
                type: 'Terrestrial',
                radius: 4,
                distance: 60,
                orbitalPeriod: 87.97, // days
                color: '#9CA3AF',
                info: {
                    diameter: '4,879 km',
                    moons: 0,
                    orbitalPeriod: '87.97 days'
                },
                eccentricity: 0.2056,
                inclination: 7.0
            },
            {
                name: 'Venus',
                type: 'Terrestrial',
                radius: 7,
                distance: 85,
                orbitalPeriod: 224.70,
                color: '#FEF3C7',
                info: {
                    diameter: '12,104 km',
                    moons: 0,
                    orbitalPeriod: '224.70 days'
                },
                eccentricity: 0.0067,
                inclination: 3.39
            },
            {
                name: 'Earth',
                type: 'Terrestrial',
                radius: 8,
                distance: 110,
                orbitalPeriod: 365.26,
                color: '#3B82F6',
                info: {
                    diameter: '12,742 km',
                    moons: 1,
                    orbitalPeriod: '365.26 days'
                },
                moons: [{
                    radius: 2,
                    distance: 15,
                    orbitalPeriod: 27.32, // days
                    color: '#E5E7EB'
                }],
                eccentricity: 0.0167,
                inclination: 0.0
            },
            {
                name: 'Mars',
                type: 'Terrestrial',
                radius: 5,
                distance: 140,
                orbitalPeriod: 686.98,
                color: '#EF4444',
                info: {
                    diameter: '6,779 km',
                    moons: 2,
                    orbitalPeriod: '686.98 days'
                },
                moons: [
                    {
                        radius: 1,
                        distance: 10,
                        orbitalPeriod: 0.32, // days
                        color: '#F3F4F6'
                    },
                    {
                        radius: 1,
                        distance: 14,
                        orbitalPeriod: 1.26, // days
                        color: '#D1D5DB'
                    }
                ],
                eccentricity: 0.0935,
                inclination: 1.85
            },
            {
                name: 'Jupiter',
                type: 'Gas Giant',
                radius: 20,
                distance: 200,
                orbitalPeriod: 4332.59,
                color: '#FB923C',
                info: {
                    diameter: '139,820 km',
                    moons: 79,
                    orbitalPeriod: '11.86 years'
                },
                moons: [
                    {
                        radius: 2,
                        distance: 28,
                        orbitalPeriod: 1.77, // days
                        color: '#FCD34D'
                    },
                    {
                        radius: 2,
                        distance: 35,
                        orbitalPeriod: 3.55, // days
                        color: '#FDE68A'
                    }
                ],
                eccentricity: 0.0489,
                inclination: 1.31
            },
            {
                name: 'Saturn',
                type: 'Gas Giant',
                radius: 17,
                distance: 260,
                orbitalPeriod: 10759.22,
                color: '#F59E0B',
                hasRings: true,
                info: {
                    diameter: '116,460 km',
                    moons: 82,
                    orbitalPeriod: '29.46 years'
                },
                moons: [
                    {
                        radius: 3,
                        distance: 30,
                        orbitalPeriod: 2.74, // days
                        color: '#FBBF24'
                    }
                ],
                eccentricity: 0.0565,
                inclination: 2.49
            },
            {
                name: 'Uranus',
                type: 'Ice Giant',
                radius: 12,
                distance: 320,
                orbitalPeriod: 30688.5,
                color: '#06B6D4',
                info: {
                    diameter: '50,724 km',
                    moons: 27,
                    orbitalPeriod: '84.01 years'
                },
                eccentricity: 0.0457,
                inclination: 0.77
            },
            {
                name: 'Neptune',
                type: 'Ice Giant',
                radius: 11,
                distance: 370,
                orbitalPeriod: 60182,
                color: '#1E40AF',
                info: {
                    diameter: '49,244 km',
                    moons: 14,
                    orbitalPeriod: '164.79 years'
                },
                eccentricity: 0.0113,
                inclination: 1.77
            }
        ];
        
        this.init();
    }
    init() {
        this.resize();
        window.addEventListener('resize', () => this.resize());
        this.setupEventListeners();
        this.createStars();
        this.animate();
        this.updateDate();
        this.updateLocation();
        this.updateTimeDisplay();
        setInterval(() => this.updateDate(), 1000);
        setInterval(() => this.updateLocation(), 30000);
    }
resize() {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
        this.centerX = this.canvas.width / 2;
        this.centerY = this.canvas.height / 2;
    }
    
    createStars() {
        const starsContainer = document.getElementById('starsContainer');
        for (let i = 0; i < 200; i++) {
            const star = document.createElement('div');
            star.className = 'star';
            star.style.width = Math.random() * 3 + 'px';
            star.style.height = star.style.width;
            star.style.left = Math.random() * 100 + '%';
            star.style.top = Math.random() * 100 + '%';
            star.style.animationDelay = Math.random() * 3 + 's';
            starsContainer.appendChild(star);
        }
    }
    
    setupEventListeners() {
        // Speed control
        const speedSlider = document.getElementById('speedSlider');
        speedSlider.addEventListener('input', (e) => {
            this.speed = parseFloat(e.target.value);
            this.timeLapseSpeed = this.speed * 0.1; // Convert to days per frame
            document.getElementById('speedValue').textContent = this.speed.toFixed(1) + 'x';
        });
// Zoom control
        const zoomSlider = document.getElementById('zoomSlider');
        zoomSlider.addEventListener('input', (e) => {
            this.zoom = parseFloat(e.target.value);
            document.getElementById('zoomValue').textContent = this.zoom.toFixed(1) + 'x';
        });
        
        // Pause button
        const pauseBtn = document.getElementById('pauseBtn');
        pauseBtn.addEventListener('click', () => {
            this.isPaused = !this.isPaused;
            pauseBtn.innerHTML = this.isPaused ? 
                '<i data-feather="play" class="w-4 h-4"></i><span>Play</span>' : 
                '<i data-feather="pause" class="w-4 h-4"></i><span>Pause</span>';
            feather.replace();
        });
        // Reset button
        document.getElementById('resetBtn').addEventListener('click', () => {
            this.time = 0;
            this.currentDate = new Date();
            this.zoom = 1;
            this.speed = 1;
            this.timeLapseSpeed = 0.1;
            document.getElementById('zoomSlider').value = 1;
            document.getElementById('speedSlider').value = 1;
            document.getElementById('zoomValue').textContent = '1.0x';
            document.getElementById('speedValue').textContent = '1.0x';
            this.updateTimeDisplay();
        });
// Fullscreen button
        document.getElementById('fullscreenBtn').addEventListener('click', () => {
            if (!document.fullscreenElement) {
                document.documentElement.requestFullscreen();
            } else {
                document.exitFullscreen();
            }
        });
        // Info button
        document.getElementById('infoBtn').addEventListener('click', () => {
            const planetInfo = document.getElementById('planetInfo');
            planetInfo.classList.toggle('hidden');
        });
        
        // Location refresh on click
        document.getElementById('locationDisplay').addEventListener('click', () => {
            document.getElementById('locationDisplay').textContent = 'Updating...';
            this.updateLocation();
        });
        // Close info panel
        document.getElementById('closeInfo').addEventListener('click', () => {
            document.getElementById('planetInfo').classList.add('hidden');
        });
        
        // Time jump controls
        document.querySelectorAll('.time-unit').forEach(unit => {
            unit.addEventListener('click', (e) => {
                const jumpDays = parseInt(e.target.dataset.time);
                this.time += jumpDays / this.timeLapseSpeed;
                this.updateTimeDisplay();
                this.updateDate();
            });
        });
// Canvas click for planet info
        this.canvas.addEventListener('click', (e) => {
            const rect = this.canvas.getBoundingClientRect();
            const x = e.clientX - rect.left - this.centerX;
            const y = e.clientY - rect.top - this.centerY;
            
            for (const planet of this.planets) {
                const angle = this.time * planet.speed * this.speed * 0.01;
                const px = Math.cos(angle) * planet.distance * this.zoom;
                const py = Math.sin(angle) * planet.distance * this.zoom;
                
                const distance = Math.sqrt((x - px) ** 2 + (y - py) ** 2);
                if (distance < planet.radius * this.zoom + 5) {
                    this.showPlanetInfo(planet);
                    break;
                }
            }
        });
    }
    showPlanetInfo(planet) {
        const info = document.getElementById('planetInfo');
        info.classList.remove('hidden');
        document.getElementById('planetName').textContent = planet.name;
        document.getElementById('planetType').textContent = planet.type;
        document.getElementById('planetDistance').textContent = planet.distance + ' AU';
        document.getElementById('planetPeriod').textContent = planet.info.orbitalPeriod;
        document.getElementById('planetDiameter').textContent = planet.info.diameter;
        document.getElementById('planetMoons').textContent = planet.info.moons || 'None';
    }
    
    updateTimeDisplay() {
        const timeDisplay = document.getElementById('timeDisplay');
        if (timeDisplay) {
            const days = Math.floor(this.timeLapseSpeed * this.time);
            const years = Math.floor(days / 365.26);
            const remainingDays = days % 365.26;
            const months = Math.floor(remainingDays / 30.44);
            const remainingDays2 = Math.floor(remainingDays % 30.44);
            
            let timeString = '';
            if (years > 0) timeString += `${years}Y `;
            if (months > 0) timeString += `${months}M `;
            if (remainingDays2 > 0 || timeString === '') timeString += `${remainingDays2}D`;
            
            timeDisplay.textContent = `Time Lapse: ${timeString}`;
        }
    }
    
    calculatePlanetPosition(planet, elapsedTime) {
        const meanAnomaly = (2 * Math.PI * elapsedTime) / planet.orbitalPeriod;
        const eccentricity = planet.eccentricity || 0;
        
        // Solve Kepler's equation (simplified)
        let E = meanAnomaly;
        for (let i = 0; i < 5; i++) {
            E = meanAnomaly + eccentricity * Math.sin(E);
        }
        
        // True anomaly
        const trueAnomaly = 2 * Math.atan2(
            Math.sqrt(1 + eccentricity) * Math.sin(E / 2),
            Math.sqrt(1 - eccentricity) * Math.cos(E / 2)
        );
        
        // Distance from sun
        const r = planet.distance * (1 - eccentricity * Math.cos(E));
        
        // Position
        const x = r * Math.cos(trueAnomaly);
        const y = r * Math.sin(trueAnomaly);
        
        // Apply inclination
        const inclination = (planet.inclination || 0) * Math.PI / 180;
        const yInclined = y * Math.cos(inclination);
        
        return { x, y: yInclined, r };
    }
    updateDate() {
        const now = new Date();
        document.getElementById('dateDisplay').textContent = now.toLocaleDateString('en-US', { 
            weekday: 'long', 
            year: 'numeric', 
            month: 'long', 
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit'
        });
        
        // Update current date display
        const currentDateDisplay = document.getElementById('currentDateDisplay');
        if (currentDateDisplay) {
            currentDateDisplay.textContent = this.currentDate.toLocaleDateString('en-US', {
                month: 'short',
                day: 'numeric',
                year: 'numeric'
            });
        }
    }
async updateLocation() {
        const locationDisplay = document.getElementById('locationDisplay');
        
        if (!navigator.geolocation) {
            locationDisplay.textContent = 'Location not supported';
            return;
        }
        
        navigator.geolocation.getCurrentPosition(
            async (position) => {
                const { latitude, longitude } = position.coords;
                
                try {
                    // Using Nominatim reverse geocoding API
                    const response = await fetch(
                        `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}&zoom=10`,
                        {
                            headers: {
                                'User-Agent': 'CosmicExplorer/1.0'
                            }
                        }
                    );
                    
                    if (!response.ok) throw new Error('Location fetch failed');
                    
                    const data = await response.json();
                    const city = data.address?.city || data.address?.town || data.address?.village || 'Unknown';
                    const country = data.address?.country || '';
                    
                    locationDisplay.textContent = country ? `${city}, ${country}` : city;
                } catch (error) {
                    // Fallback to coordinates
                    locationDisplay.textContent = `${latitude.toFixed(2)}°, ${longitude.toFixed(2)}°`;
                }
            },
            (error) => {
                locationDisplay.textContent = 'Location denied';
            }
        );
    }
drawSun() {
        // Simplified sun for performance
        this.ctx.fillStyle = '#FBBF24';
        this.ctx.beginPath();
        this.ctx.arc(this.centerX, this.centerY, 25 * this.zoom, 0, Math.PI * 2);
        this.ctx.fill();
        
        // Simple glow effect
        this.ctx.fillStyle = 'rgba(251, 191, 36, 0.3)';
        this.ctx.beginPath();
        this.ctx.arc(this.centerX, this.centerY, 35 * this.zoom, 0, Math.PI * 2);
        this.ctx.fill();
    }
drawPlanet(planet) {
        const elapsedTime = this.timeLapseSpeed * this.time;
        const position = this.calculatePlanetPosition(planet, elapsedTime);
        const x = this.centerX + position.x * this.zoom;
        const y = this.centerY + position.y * this.zoom;
        
        // Draw orbit
        this.ctx.strokeStyle = 'rgba(147, 51, 234, 0.2)';
        this.ctx.lineWidth = 1;
        this.ctx.beginPath();
        this.ctx.ellipse(this.centerX, this.centerY, planet.distance * this.zoom, planet.distance * this.zoom, 0, 0, Math.PI * 2);
        this.ctx.stroke();
        
        // Draw rings for Saturn
        if (planet.hasRings) {
            this.ctx.strokeStyle = 'rgba(245, 158, 11, 0.5)';
            this.ctx.lineWidth = 3 * this.zoom;
            this.ctx.beginPath();
            this.ctx.ellipse(x, y, planet.radius * this.zoom * 2, planet.radius * this.zoom * 0.7, 0, 0, Math.PI * 2);
            this.ctx.stroke();
        }
        
        // Draw planet
        this.ctx.fillStyle = planet.color;
        this.ctx.beginPath();
        this.ctx.arc(x, y, planet.radius * this.zoom, 0, Math.PI * 2);
        this.ctx.fill();
        
        // Draw moons
        if (planet.moons) {
            planet.moons.forEach(moon => {
                const moonElapsedTime = this.timeLapseSpeed * this.time;
                const moonAngle = (2 * Math.PI * moonElapsedTime) / moon.orbitalPeriod;
                const moonX = x + Math.cos(moonAngle) * moon.distance * this.zoom;
                const moonY = y + Math.sin(moonAngle) * moon.distance * this.zoom;
                
                this.ctx.fillStyle = moon.color;
                this.ctx.beginPath();
                this.ctx.arc(moonX, moonY, moon.radius * this.zoom, 0, Math.PI * 2);
                this.ctx.fill();
            });
        }
        
        // Draw planet name
        if (this.zoom > 0.8) {
            this.ctx.fillStyle = 'rgba(255, 255, 255, 0.7)';
            this.ctx.font = `${Math.max(10, 10 * this.zoom)}px Arial`;
            this.ctx.textAlign = 'center';
            this.ctx.fillText(planet.name, x, y - planet.radius * this.zoom - 5);
        }
        
        return { x, y, position };
    }
adjustBrightness(color, amount) {
        const num = parseInt(color.replace('#', ''), 16);
        const r = Math.max(0, Math.min(255, (num >> 16) + amount));
        const g = Math.max(0, Math.min(255, ((num >> 8) & 0x00FF) + amount));
        const b = Math.max(0, Math.min(255, (num & 0x0000FF) + amount));
        return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, '0')}`;
    }
    animate() {
        // Clear canvas completely for better performance
        this.ctx.fillStyle = '#000';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
        
        // Draw sun
        this.drawSun();
        
        // Draw planets
        this.planets.forEach(planet => this.drawPlanet(planet));
        // Update time
        if (!this.isPaused) {
            this.time++;
            this.currentDate = new Date(this.startDate.getTime() + this.timeLapseSpeed * this.time * 24 * 60 * 60 * 1000);
            this.updateTimeDisplay();
        }
// Update stats less frequently
        if (this.time % 10 === 0) {
            this.updateStats();
        }
        
        // Calculate FPS
        const now = performance.now();
        const delta = now - this.lastFrameTime;
        this.fps = Math.round(1000 / delta);
        this.lastFrameTime = now;
        
        requestAnimationFrame(() => this.animate());
    }
    updateStats() {
        document.getElementById('fpsDisplay').textContent = this.fps;
        const totalDays = Math.floor(this.timeLapseSpeed * this.time);
        document.getElementById('daysDisplay').textContent = totalDays;
        document.getElementById('yearsDisplay').textContent = (totalDays / 365.26).toFixed(2);
    }
}
// Initialize the solar system
document.addEventListener('DOMContentLoaded', () => {
    new SolarSystem();
});