-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistanceCalculations.html
225 lines (193 loc) · 7.75 KB
/
distanceCalculations.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Solar System in 2D</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #000;
color: white;
flex-direction: column;
}
canvas {
border: 1px solid white;
}
#coordinates {
position: absolute;
top: 20px;
left: 20px;
font-size: 16px;
}
#dateInput {
margin-top: 20px;
display: flex;
align-items: center;
}
#dateInput label {
margin-right: 10px;
}
#playPauseBtn, #speedControl {
margin-top: 20px;
}
#currentDate {
position: absolute;
bottom: 20px;
font-size: 18px;
}
</style>
</head>
<body>
<canvas id="orbitCanvas" width="900" height="900"></canvas>
<div id="coordinates"></div>
<div id="dateInput">
<label for="date">Enter a Date (YYYY-MM-DD): </label>
<input type="text" id="date" class="datepicker">
<button id="setDateBtn">Set Date</button>
</div>
<div id="playPauseBtn">
<button onclick="toggleAnimation()">Play</button>
</div>
<div id="speedControl">
<button onclick="changeSpeed(1)">1x Speed</button>
<button onclick="changeSpeed(8)">8x Speed</button>
<button onclick="changeSpeed(32)">32x Speed</button>
<button onclick="changeSpeed(128)">128x Speed</button>
</div>
<div id="currentDate"></div>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
const canvas = document.getElementById('orbitCanvas');
const ctx = canvas.getContext('2d');
const playPauseButton = document.querySelector('#playPauseBtn button');
const currentDateDisplay = document.getElementById('currentDate');
const setDateButton = document.getElementById('setDateBtn');
const dateInput = document.getElementById('date');
const sunX = canvas.width / 2;
const sunY = canvas.height / 2;
const orbitalPeriods = {
Mercury: 0.24,
Venus: 0.615,
Earth: 1,
Mars: 1.88,
Jupiter: 11.86,
Saturn: 29.46,
Uranus: 84,
Neptune: 164
};
const planets = [
{ name: 'Mercury', radius: 50 },
{ name: 'Venus', radius: 80 },
{ name: 'Earth', radius: 120 },
{ name: 'Mars', radius: 160 },
{ name: 'Jupiter', radius: 220 },
{ name: 'Saturn', radius: 280 },
{ name: 'Uranus', radius: 340 },
{ name: 'Neptune', radius: 400 }
];
// Set the reference date to May 5, 2000
const referenceDate = new Date('2000-05-05T00:00:00Z');
let animationFrameId;
let isAnimating = false;
let daysSinceReference = 0;
let speedFactor = 0.01; // Default speed factor (slower animation)
function getDaysBetweenDates(startDate, endDate) {
const timeDiff = endDate - startDate; // Time in milliseconds
return timeDiff / (1000 * 60 * 60 * 24); // Convert to days
}
function startOrbit() {
const userDate = dateInput.value;
const selectedDate = new Date(userDate + "T00:00:00Z"); // Get selected date at midnight UTC
console.log('Selected Date:', selectedDate);
if (isNaN(selectedDate.getTime())) {
alert('Please enter a valid date!');
return;
}
// Calculate the days since the reference date
daysSinceReference = getDaysBetweenDates(referenceDate, selectedDate);
console.log('Days since reference:', daysSinceReference);
update(daysSinceReference); // Update the orbits based on the user-provided date
}
function drawOrbit(x, y, radius) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.strokeStyle = 'white';
ctx.stroke();
}
function drawPlanet(x, y, name) {
ctx.beginPath();
ctx.arc(x, y, 6, 0, Math.PI * 2); // Planet circle with size 6
ctx.fillStyle = 'white';
ctx.fill();
// Draw the planet name next to it
ctx.font = '12px Arial';
ctx.fillStyle = 'white';
ctx.fillText(name, x + 10, y); // Adjust the position of the name next to the planet
}
function update(daysSinceReference) {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear canvas
// Draw the sun in yellow and smaller (radius 15)
ctx.beginPath();
ctx.arc(sunX, sunY, 15, 0, Math.PI * 2); // Sun circle with size 15
ctx.fillStyle = 'yellow';
ctx.fill();
let coordinatesText = '';
planets.forEach(planet => {
drawOrbit(sunX, sunY, planet.radius); // Draw the orbit
const orbitalPeriodInDays = orbitalPeriods[planet.name] * 365.25; // Convert orbital period to days
const angle = 2 * Math.PI * (daysSinceReference / orbitalPeriodInDays); // Calculate angle
const x = sunX + planet.radius * Math.cos(angle); // X position
const y = sunY + planet.radius * Math.sin(angle); // Y position
drawPlanet(x, y, planet.name); // Draw the planet with its name
coordinatesText += `${planet.name}: (${x.toFixed(2)}, ${y.toFixed(2)})<br>`;
});
document.getElementById('coordinates').innerHTML = coordinatesText;
// Display the current date based on daysSinceReference
const currentDate = new Date(referenceDate);
currentDate.setDate(referenceDate.getDate() + daysSinceReference); // Add days to the reference date
currentDateDisplay.textContent = `Current Date: ${currentDate.toISOString().split('T')[0]}`;
}
function animate() {
// Slow down the animation by incrementing fewer days per frame
daysSinceReference += speedFactor; // Adjust increment based on speedFactor
update(daysSinceReference);
animationFrameId = requestAnimationFrame(animate);
}
function toggleAnimation() {
if (isAnimating) {
cancelAnimationFrame(animationFrameId);
playPauseButton.textContent = 'Play';
} else {
animate();
playPauseButton.textContent = 'Stop';
}
isAnimating = !isAnimating;
}
// Change speed factor when user selects a speed
function changeSpeed(factor) {
if (factor === 1) {
speedFactor = 0.01; // 1x speed (normal speed)
} else {
speedFactor = 0.01 * factor; // Multiply by the selected speed factor (32x, 8x, 128x)
}
if (isAnimating) {
// Restart the animation with the new speed
cancelAnimationFrame(animationFrameId);
animate();
}
}
// Handle the Set Date button click event
setDateButton.addEventListener('click', startOrbit);
// Initialize Flatpickr for date input
flatpickr("#date", {
dateFormat: "Y-m-d", // Format it as YYYY-MM-DD
});
</script>
</body>
</html>