-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
361 lines (309 loc) · 11.1 KB
/
Copy pathscript.js
File metadata and controls
361 lines (309 loc) · 11.1 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
let width = window.innerWidth;
let height = window.innerHeight;
let scale = Math.min(width, height) / 2 - 10;
const COUNTRY_FILL = "#90EE9005";
const SPHERE_FILL = "rgba(0, 0, 0, 0.1)";
const COUNTRY_BORDER_STROKE = "#444444";
// Cache DOM elements
const globeCanvas = d3.select("#globe");
const context = globeCanvas.node().getContext("2d");
const metadataElement = d3.select("#metadata");
const colorbarContainer = document.getElementById('colorbar-container');
const showLocationCheckbox = document.getElementById('show-location');
const fadeColorbarCheckbox = document.getElementById('fade-colorbar');
const fadeInfoCheckbox = document.getElementById('fade-info');
const autoRefreshCheckbox = document.getElementById('auto-refresh');
let projection, path;
let world, auroraData;
let userLocation = null;
const colorScale = d3.scaleSequential(d3.interpolatePlasma)
.domain([0, 100]);
let animationFrameId = null;
let needsRender = true;
let isDataLoaded = false;
let isInitialized = false;
let autoRefreshInterval;
const autoRefreshTime = 600000/10;
function loadAuroraData() {
clearInterval(autoRefreshInterval);
return d3.json("https://services.swpc.noaa.gov/json/ovation_aurora_latest.json")
.then(function(aurora) {
auroraData = aurora;
updateMetadata();
requestRender();
if (autoRefreshCheckbox.checked) {
autoRefreshInterval = setInterval(loadAuroraData, autoRefreshTime); // 10 minutes
}
})
.catch(function(error) {
console.error("Error loading aurora data:", error);
});
}
function loadData() {
d3.json("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json")
.then(function(worldData) {
world = worldData;
isDataLoaded = true;
getUserLocation();
drawColorbar();
initializeGlobe();
resizeMap();
return loadAuroraData();
})
.catch(function(error) {
console.error("Error loading world data:", error);
});
}
function initializeGlobe() {
if (!isDataLoaded) return;
projection = d3.geoOrthographic()
.scale(scale)
.translate([width / 2, height / 2]);
path = d3.geoPath().projection(projection).context(context);
globeCanvas.call(d3.drag()
.on("start", handleInteractionStart)
.on("drag", handleInteractionMove)
.on("end", handleInteractionEnd));
globeCanvas.node().addEventListener('touchstart', handleInteractionStart, { passive: false });
globeCanvas.node().addEventListener('touchmove', handleInteractionMove, { passive: false });
globeCanvas.node().addEventListener('touchend', handleInteractionEnd, { passive: false });
isInitialized = true;
requestRender();
}
function getUserLocation() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function (position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
userLocation = [longitude, latitude];
projection.rotate([-longitude, -latitude]);
requestRender();
}, function (error) {
console.error("Error getting user location:", error);
});
} else {
console.log("Geolocation is not available");
}
}
function render() {
if (!needsRender || !isDataLoaded || !isInitialized) return;
context.clearRect(0, 0, width, height);
// Draw the globe
context.beginPath();
path({ type: "Sphere" });
context.fillStyle = SPHERE_FILL;
context.fill();
// Add white outline to the globe
context.strokeStyle = "white";
context.lineWidth = 1;
context.stroke();
// Draw graticules
const graticule = d3.geoGraticule()
.stepMajor([90, 90])
.stepMinor([45, 45]);
context.beginPath();
path(graticule());
context.lineWidth = 0.5;
context.strokeStyle = "rgba(255, 255, 255, 1)";
context.stroke();
// Pre-calculate center point
const [cx, cy] = projection.invert([width / 2, height / 2]);
// Draw aurora
if (auroraData && auroraData.coordinates) {
auroraData.coordinates.forEach(d => {
const [lon, lat] = d.slice(0, 2);
const geoDistance = d3.geoDistance([lon, lat], [cx, cy]);
if (d[2] > 0 && geoDistance < Math.PI / 2) {
const [x, y] = projection([lon, lat]);
if (x !== null && y !== null) {
drawAuroraPoint(x, y, d[2]);
}
}
});
}
// Draw countries
if (world && world.objects && world.objects.countries) {
context.beginPath();
path(topojson.feature(world, world.objects.countries));
context.fillStyle = COUNTRY_FILL;
context.fill();
// Draw country boundaries
context.beginPath();
path(topojson.mesh(world, world.objects.countries, (a, b) => a !== b));
context.strokeStyle = COUNTRY_BORDER_STROKE;
context.lineWidth = 0.5;
context.stroke();
}
// Draw coastlines
if (world && world.objects && world.objects.land) {
context.beginPath();
path(topojson.feature(world, world.objects.land));
context.strokeStyle = "white";
context.lineWidth = 0.5;
context.stroke();
}
// Draw user location if checkbox is checked
if (showLocationCheckbox.checked && userLocation) {
const [x, y] = projection(userLocation);
context.beginPath();
context.arc(x, y, 5, 0, 2 * Math.PI);
context.fillStyle = 'red';
context.fill();
context.strokeStyle = 'white';
context.lineWidth = 2;
context.stroke();
}
needsRender = false;
}
function drawAuroraPoint(x, y, intensity) {
context.beginPath();
const size = Math.min(width, height)/100;
context.arc(x, y, size, 0, 2 * Math.PI);
context.fillStyle = colorScale(intensity);
context.globalAlpha = intensity / 100;
context.fill();
context.globalAlpha = 1;
}
function drawColorbar() {
const colorbarCanvas = d3.select("#colorbar");
const colorbarWidth = document.getElementById('colorbar-container').clientWidth;
colorbarCanvas.attr("width", colorbarWidth).attr("height", 50);
const colorbarContext = colorbarCanvas.node().getContext("2d");
const gradient = colorbarContext.createLinearGradient(0, 0, colorbarWidth, 0);
for (let i = 0; i <= 1; i += 0.01) {
gradient.addColorStop(i, colorScale(i * 100));
}
colorbarContext.fillStyle = gradient;
colorbarContext.fillRect(0, 0, colorbarWidth, 20);
// Add labels
colorbarContext.fillStyle = "white";
colorbarContext.font = "12px Arial";
colorbarContext.textAlign = "center";
colorbarContext.fillText("0%", 10, 45);
colorbarContext.fillText("25%", colorbarWidth * 0.25, 45);
colorbarContext.fillText("50%", colorbarWidth * 0.5, 45);
colorbarContext.fillText("75%", colorbarWidth * 0.75, 45);
colorbarContext.fillText("100%", colorbarWidth - 10, 45);
}
function updateMetadata() {
metadataElement.html(`
Aurora Forecast Map<br>
Orthographic projection<br>
Observation Time: ${auroraData["Observation Time"]}<br>
Forecast Time: ${auroraData["Forecast Time"]}<br>
Render Time: ${new Date().toISOString()}<br>
`);
}
function resizeMap() {
const container = document.getElementById('container');
width = container.clientWidth;
height = document.getElementById('map').clientHeight;
scale = Math.min(width, height) / 2 - 10;
globeCanvas
.attr("width", width)
.attr("height", height);
if (isInitialized) {
projection
.scale(scale)
.translate([width / 2, height / 2]);
path = d3.geoPath().projection(projection).context(context);
requestRender();
drawColorbar();
}
requestRender();
}
function animate() {
render();
animationFrameId = requestAnimationFrame(animate);
}
function startAnimation() {
if (!animationFrameId) {
animate();
}
}
function stopAnimation() {
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
}
function requestRender() {
needsRender = true;
startAnimation();
}
// Consolidated touch and drag handling
let v0, r0, q0;
let touchZoomStart, touchZoomScale;
function handleInteractionStart(event) {
if (event.type === 'touchstart') {
event.preventDefault();
const touches = event.touches;
if (touches.length === 2) {
touchZoomStart = pinchDistance(touches);
touchZoomScale = projection.scale();
}
} else {
const p = d3.pointers(event, globeCanvas.node())[0];
v0 = versor.cartesian(projection.invert(p));
r0 = projection.rotate();
q0 = versor(r0);
}
startAnimation();
}
function handleInteractionMove(event) {
if (event.type === 'touchmove') {
event.preventDefault();
const touches = event.touches;
if (touches.length === 2) {
const touchZoom = pinchDistance(touches);
const newScale = touchZoomScale * touchZoom / touchZoomStart;
projection.scale(Math.min(Math.max(newScale, 100), 5000));
} else if (touches.length === 1) {
const p = [touches[0].clientX, touches[0].clientY];
const v1 = versor.cartesian(projection.rotate(r0).invert(p));
const q1 = versor.multiply(q0, versor.delta(v0, v1));
projection.rotate(versor.rotation(q1));
}
} else {
const p = d3.pointers(event, globeCanvas.node())[0];
const v1 = versor.cartesian(projection.rotate(r0).invert(p));
const q1 = versor.multiply(q0, versor.delta(v0, v1));
projection.rotate(versor.rotation(q1));
}
requestRender();
}
function handleInteractionEnd() {
stopAnimation();
}
function pinchDistance(touches) {
const dx = touches[0].clientX - touches[1].clientX;
const dy = touches[0].clientY - touches[1].clientY;
return Math.sqrt(dx * dx + dy * dy);
}
function handleMouseOver() {
document.getElementById('controls').classList.remove('fade');
document.getElementById('info-section').classList.remove('fade');
}
function handleMouseOut() {
document.getElementById('controls').classList.add('fade');
if (fadeColorbarCheckbox.checked || fadeInfoCheckbox.checked) {
document.getElementById('info-section').classList.add('fade');
}
}
function toggleAutoRefresh() {
if (autoRefreshCheckbox.checked) {
loadAuroraData();
} else {
clearInterval(autoRefreshInterval);
}
}
// Event listeners
window.addEventListener('resize', resizeMap);
document.body.addEventListener('mouseover', handleMouseOver);
document.body.addEventListener('mouseout', handleMouseOut);
showLocationCheckbox.addEventListener('change', requestRender);
fadeColorbarCheckbox.addEventListener('change', handleMouseOver);
fadeInfoCheckbox.addEventListener('change', handleMouseOver);
autoRefreshCheckbox.addEventListener('change', toggleAutoRefresh);
// Initialize
loadData();