-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
447 lines (409 loc) · 19.5 KB
/
Copy pathindex.html
File metadata and controls
447 lines (409 loc) · 19.5 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Global Daylight Map with Time Control</title>
<!-- Tailwind CSS CDN for styling -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom styles for the body and canvas */
body {
font-family: "Inter", sans-serif;
background-color: #1a202c; /* Dark background */
color: #e2e8f0; /* Light text color */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
overflow: hidden; /* Prevent scrollbars due to full viewport elements */
}
canvas {
background-color: #3b82f6; /* Blue for oceans */
display: block;
border: 2px solid #4a5568; /* Border around the canvas */
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); /* Subtle shadow */
max-width: 90vw; /* Responsive width */
max-height: 70vh; /* Responsive height, leaving more space for controls */
width: auto; /* Allow width to adjust based on aspect ratio */
height: auto; /* Allow height to adjust based on aspect ratio */
object-fit: contain; /* Ensure the content scales down if needed */
margin: auto; /* Center the canvas */
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 1rem;
width: 100%;
}
.info-panel {
background-color: #2d3748;
border-radius: 0.75rem;
padding: 1rem;
margin-top: 1.5rem;
text-align: center;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
max-width: 90vw;
width: 600px; /* Max width for panel */
}
.control-group {
display: flex;
justify-content: center;
align-items: center;
gap: 1rem;
flex-wrap: wrap; /* Allow wrapping on small screens */
margin-top: 1rem;
}
.control-group button, .speed-control label {
padding: 0.75rem 1.5rem;
border-radius: 0.5rem;
font-weight: 600;
cursor: pointer;
transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.control-group button:hover {
transform: translateY(-2px);
}
.control-group button:active {
transform: translateY(0);
}
.btn-blue {
background-color: #4c68d4;
color: #e2e8f0;
}
.btn-blue:hover {
background-color: #3b50a2;
}
.btn-green {
background-color: #38a169;
color: #e2e8f0;
}
.btn-green:hover {
background-color: #2f855a;
}
.speed-control {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
min-width: 200px; /* Ensure speed control has enough width */
}
.speed-control input[type="range"] {
width: 100%;
-webkit-appearance: none;
height: 8px;
background: #4a5568;
border-radius: 5px;
outline: none;
opacity: 0.7;
transition: opacity .2s;
}
.speed-control input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: #63b3ed;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.speed-control input[type="range"]::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background: #63b3ed;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-4xl sm:text-5xl font-extrabold mb-8 text-center bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-indigo-600">
Global Daylight & Cities
</h1>
<canvas id="daylightCanvas" class="rounded-xl"></canvas>
<div class="info-panel">
<div id="currentDateTime" class="text-xl sm:text-2xl font-mono text-blue-300 mb-2"></div>
<div class="text-base sm:text-lg text-gray-400">
Explore the Earth's day-night cycle. Hover over cities to see their names!
</div>
<div class="control-group">
<button id="reverseBtn" class="btn-blue"><< Reverse</button>
<button id="forwardBtn" class="btn-blue">Forward >></button>
<div class="speed-control">
<label for="speedInput" class="text-gray-300 text-sm">Speed: <span id="currentSpeed">1x</span></label>
<input type="range" id="speedInput" min="1" max="1440" value="1" step="1">
</div>
<button id="resetBtn" class="btn-green">Reset to Current Time</button>
</div>
</div>
</div>
<script>
// Get the canvas element and its 2D rendering context
const canvas = document.getElementById('daylightCanvas');
const ctx = canvas.getContext('2d');
const currentDateTimeDisplay = document.getElementById('currentDateTime');
const speedInput = document.getElementById('speedInput');
const currentSpeedDisplay = document.getElementById('currentSpeed');
const reverseBtn = document.getElementById('reverseBtn');
const forwardBtn = document.getElementById('forwardBtn');
const resetBtn = document.getElementById('resetBtn');
// Global map boundaries for Plate Carrée projection
const WORLD_WEST_LON = -180; // Westernmost longitude
const WORLD_EAST_LON = 180; // Easternmost longitude
const WORLD_NORTH_LAT = 90; // Northernmost latitude (North Pole)
const WORLD_SOUTH_LAT = -90; // Southernmost latitude (South Pole)
// Calculate the total span of longitudes and latitudes for mapping
const LONGITUDE_SPAN = WORLD_EAST_LON - WORLD_WEST_LON; // 360 degrees
const LATITUDE_SPAN = WORLD_NORTH_LAT - WORLD_SOUTH_LAT; // 180 degrees
// Simulation time variables
let simulationTime = new Date(); // The time used for the simulation
let simulationSpeed = 1; // Speed multiplier (1x, 1440x etc.)
let timeDirection = 1; // 1 for forward, -1 for reverse
let lastFrameTime = performance.now(); // For delta time calculation
// Mouse coordinates for hovering
let mouseX = 0;
let mouseY = 0;
let hoveredCity = null;
// List of major global cities (approximate coordinates)
const CITIES = [
{ name: 'London', lat: 51.5, lon: 0.1 },
{ name: 'New York', lat: 40.7, lon: -74.0 },
{ name: 'Tokyo', lat: 35.6, lon: 139.7 },
{ name: 'Sydney', lat: -33.8, lon: 151.2 },
{ name: 'Rio de Janeiro', lat: -22.9, lon: -43.2 },
{ name: 'Cairo', lat: 30.0, lon: 31.2 },
{ name: 'Mumbai', lat: 19.0, lon: 72.8 },
{ name: 'Moscow', lat: 55.7, lon: 37.6 },
{ name: 'Paris', lat: 48.8, lon: 2.3 },
{ name: 'Beijing', lat: 39.9, lon: 116.3 },
{ name: 'Cape Town', lat: -33.9, lon: 18.4 },
{ name: 'Mexico City', lat: 19.4, lon: -99.1 },
{ name: 'Berlin', lat: 52.5, lon: 13.4 },
{ name: 'Rome', lat: 41.9, lon: 12.5 },
{ name: 'Toronto', lat: 43.6, lon: -79.4 },
{ name: 'Dubai', lat: 25.2, lon: 55.2 },
{ name: 'Johannesburg', lat: -26.2, lon: 28.0 },
{ name: 'Bogota', lat: 4.6, lon: -74.0 },
{ name: 'Lagos', lat: 6.5, lon: 3.3 },
{ name: 'Washington D.C.', lat: 38.9, lon: -77.0 },
{ name: 'Los Angeles', lat: 34.05, lon: -118.25 },
{ name: 'Seattle', lat: 47.6, lon: -122.3 }, // Added for user context
{ name: 'Singapore', lat: 1.35, lon: 103.8 }
];
/**
* Maps a longitude to a canvas X coordinate using Plate Carrée projection.
* @param {number} lon - Longitude in degrees (-180 to 180).
* @param {number} canvasWidth - The width of the canvas.
* @returns {number} The X coordinate on the canvas.
*/
function mapLonToCanvasX(lon, canvasWidth) {
return (lon + 180) / LONGITUDE_SPAN * canvasWidth;
}
/**
* Maps a latitude to a canvas Y coordinate using Plate Carrée projection.
* Note: Y-axis typically goes from top (0) to bottom (canvasHeight),
* so higher latitudes (North) map to smaller Y values.
* @param {number} lat - Latitude in degrees (-90 to 90).
* @param {number} canvasHeight - The height of the canvas.
* @returns {number} The Y coordinate on the canvas.
*/
function mapLatToCanvasY(lat, canvasHeight) {
return canvasHeight - ((lat + 90) / LATITUDE_SPAN * canvasHeight);
}
/**
* Calculates the Sun's declination for a given date.
* This is a simplified approximation for visualization.
* @param {Date} date - The current Date object.
* @returns {number} Sun's declination in radians.
*/
function getSunDeclination(date) {
// Day of year (0-364 or 365)
const startOfYear = new Date(date.getFullYear(), 0, 1);
const dayOfYear = (date.getTime() - startOfYear.getTime()) / (1000 * 60 * 60 * 24);
// Solar declination approximation (in radians)
// -23.45 degrees is the axial tilt of the Earth
const obliquity = -23.45 * Math.PI / 180;
// Angle around the sun's path (approximate)
// The 81 offset makes the vernal equinox roughly March 21
const sunAngle = 2 * Math.PI * (dayOfYear - 81) / 365.25;
return Math.asin(Math.sin(obliquity) * Math.sin(sunAngle));
}
/**
* Calculates the longitude of the subsolar point.
* @param {Date} date - The current Date object.
* @returns {number} Subsolar longitude in radians.
*/
function getSubsolarLongitude(date) {
// Earth rotates 15 degrees longitude per hour.
const utcHours = date.getUTCHours();
const utcMinutes = date.getUTCMinutes();
const utcSeconds = date.getUTCSeconds();
const totalUTCHours = utcHours + utcMinutes / 60 + utcSeconds / 3600;
// Simplified: (totalUTCHours * 15) - 180 degrees
// This maps 0-24h UTC to -180 to 180 degrees longitude,
// with 12 UTC roughly at 0 longitude (Greenwich).
let subsolarLon = (totalUTCHours * 15) - 180; // Degrees
return subsolarLon * Math.PI / 180; // Convert to radians
}
/**
* Draws faint grid lines (latitude and longitude) on the global map.
* @param {CanvasRenderingContext2D} ctx - The 2D rendering context.
* @param {number} canvasWidth - The width of the canvas.
* @param {number} canvasHeight - The height of the canvas.
*/
function drawGrid(ctx, canvasWidth, canvasHeight) {
ctx.strokeStyle = 'rgba(255, 255, 255, 0.08)'; // Very faint white grid lines
ctx.lineWidth = 0.5;
// Draw longitude lines (e.g., every 30 degrees)
for (let lon = -180; lon <= 180; lon += 30) {
const x = mapLonToCanvasX(lon, canvasWidth);
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvasHeight);
ctx.stroke();
}
// Draw latitude lines (e.g., every 30 degrees)
for (let lat = -90; lat <= 90; lat += 30) {
const y = mapLatToCanvasY(lat, canvasHeight);
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvasWidth, y);
ctx.stroke();
}
}
/**
* Draws the day/night terminator and shades the night side.
* This uses the `destination-out` global composite operation to "cut out" the day region.
* @param {CanvasRenderingContext2D} ctx - The 2D rendering context.
* @param {number} sunDeclinationRad - Sun's declination in radians.
* @param {number} subsolarLonRad - Subsolar longitude in radians.
* @param {number} canvasWidth - The width of the canvas.
* @param {number} canvasHeight - The height of the canvas.
*/
function drawNightOverlay(ctx, sunDeclinationRad, subsolarLonRad, canvasWidth, canvasHeight) {
// 1. Fill the entire canvas with night color
ctx.fillStyle = 'rgba(0, 0, 50, 0.6)'; // Dark blue for night
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// 2. Set global composite operation to 'destination-out' to erase where the day polygon is drawn
ctx.globalCompositeOperation = 'destination-out';
// 3. Build the polygon path for the day side
ctx.beginPath();
// Iterate longitudes from west to east
let firstPoint = true;
for (let lon = -180; lon <= 180; lon += 1) { // 1-degree step for high resolution terminator
const lonRad = (lon * Math.PI) / 180;
const hourAngle = lonRad - subsolarLonRad;
let latTerminatorRad;
// Calculate latitude on the terminator where solar altitude is 0
// sin(lat)sin(dec) + cos(lat)cos(dec)cos(hourAngle) = 0
// Rearranged for lat: lat = atan2(-cos(dec)*cos(hourAngle), sin(dec))
latTerminatorRad = Math.atan2(-Math.cos(sunDeclinationRad) * Math.cos(hourAngle), Math.sin(sunDeclinationRad));
const latTerminator = (latTerminatorRad * 180) / Math.PI;
// Clamp latitude to ensure it stays within map bounds (-90 to 90)
const clampedLat = Math.max(-90, Math.min(90, latTerminator));
const x = mapLonToCanvasX(lon, canvasWidth);
const y = mapLatToCanvasY(clampedLat, canvasHeight);
if (firstPoint) {
ctx.moveTo(x, y);
firstPoint = false;
} else {
ctx.lineTo(x, y);
}
}
// Close the day polygon by connecting to the appropriate pole (top or bottom of the map)
// If the sun is in the Northern Hemisphere (declination >= 0), the day side extends towards the North Pole
// If the sun is in the Southern Hemisphere (declination < 0), the day side extends towards the South Pole
if (sunDeclinationRad >= 0) {
// Connect to top-right corner then to top-left corner (North Pole side)
ctx.lineTo(mapLonToCanvasX(180, canvasWidth), mapLatToCanvasY(90, canvasHeight));
ctx.lineTo(mapLonToCanvasX(-180, canvasWidth), mapLatToCanvasY(90, canvasHeight));
} else {
// Connect to bottom-right corner then to bottom-left corner (South Pole side)
ctx.lineTo(mapLonToCanvasX(180, canvasWidth), mapLatToCanvasY(-90, canvasHeight));
ctx.lineTo(mapLonToCanvasX(-180, canvasWidth), mapLatToCanvasY(-90, canvasHeight));
}
ctx.closePath();
ctx.fill();
// 4. Reset composite operation to default
ctx.globalCompositeOperation = 'source-over';
// 5. Draw the terminator line itself for clearer visibility
ctx.strokeStyle = 'rgba(255, 255, 255, 0.4)'; // A subtle line for the terminator
ctx.lineWidth = 1.5;
ctx.beginPath();
firstPoint = true; // Reset first point for drawing line
for (let lon = -180; lon <= 180; lon += 1) {
const lonRad = (lon * Math.PI) / 180;
const hourAngle = lonRad - subsolarLonRad;
let latTerminatorRad = Math.atan2(-Math.cos(sunDeclinationRad) * Math.cos(hourAngle), Math.sin(sunDeclinationRad));
const latTerminator = (latTerminatorRad * 180) / Math.PI;
const clampedLat = Math.max(-90, Math.min(90, latTerminator));
const x = mapLonToCanvasX(lon, canvasWidth);
const y = mapLatToCanvasY(clampedLat, canvasHeight);
if (firstPoint) {
ctx.moveTo(x, y);
firstPoint = false;
} else {
ctx.lineTo(x, y);
}
}
ctx.stroke();
}
/**
* Draws city markers and names on the canvas.
* @param {CanvasRenderingContext2D} ctx - The 2D rendering context.
* @param {Array<Object>} cities - Array of city objects ({name, lat, lon}).
* @param {number} canvasWidth - The width of the canvas.
* @param {number} canvasHeight - The height of the canvas.
*/
function drawCities(ctx, cities, canvasWidth, canvasHeight) {
cities.forEach(city => {
const x = mapLonToCanvasX(city.lon, canvasWidth);
const y = mapLatToCanvasY(city.lat, canvasHeight);
// Draw city dot
ctx.fillStyle = '#fca5a5'; // Light red color for city dots
ctx.beginPath();
ctx.arc(x, y, 2.5, 0, Math.PI * 2); // Small circle for city
ctx.fill();
// If this city is currently hovered, draw its name
if (hoveredCity && hoveredCity.name === city.name) {
ctx.fillStyle = '#e2e8f0'; // Light text color for hovered city name
ctx.font = 'bold 14px Inter';
ctx.textAlign = 'left';
ctx.textBaseline = 'bottom';
// Adjust text position to be slightly offset from the dot
ctx.fillText(city.name, x + 5, y - 5);
}
});
}
/**
* Resizes the canvas dynamically based on window size while maintaining aspect ratio.
*/
function resizeCanvas() {
// Global map aspect ratio (360/180 = 2)
const aspectRatio = LONGITUDE_SPAN / LATITUDE_SPAN; // Always 2 for Plate Carrée
const maxWidth = window.innerWidth * 0.9; // 90% of window width
const maxHeight = window.innerHeight * 0.6; // Reduced to 60% for more control panel space
let newWidth = maxWidth;
let newHeight = maxWidth / aspectRatio;
if (newHeight > maxHeight) {
newHeight = maxHeight;
newWidth = maxHeight * aspectRatio;
}
canvas.width = newWidth;
canvas.height = newHeight;
// Redraw immediately after resize
draw(simulationTime);
}
/**
* The main drawing function that updates the canvas content.
* @param {Date} dateToDraw - The Date object representing the time to draw.
*/
function draw(dateToDraw) {