-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·376 lines (328 loc) · 11.7 KB
/
script.js
File metadata and controls
executable file
·376 lines (328 loc) · 11.7 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
// --- CONFIGURATION ---
const NASA_URL = "nasa-data.csv"; // Local copy, no CORS proxy needed
const STORAGE_KEY = "nasa_temp_data_v2";
let tempData = [];
let dataLoaded = false;
let time = 0;
let speed = 0.75; // Doubled from 0.375
let paused = false;
let particles = [];
let lastSpawnedIndex = -1;
let gradient;
// SAFETY WRAPPER - Run immediately or wait for DOM
function initVisualization() {
console.log("initVisualization() called");
// 1. Get Elements
const cv = document.getElementById("c");
if (!cv) {
console.error("Canvas element not found!");
return;
}
console.log("Canvas found:", cv);
const ctx = cv.getContext("2d");
const yearEl = document.getElementById("year");
const monthEl = document.getElementById("month");
const mainDisplay = document.getElementById("mainDisplay");
const tempEl = document.getElementById("temp");
// Phase Element Removed
const liveText = document.getElementById("liveText");
const liveDot = document.getElementById("liveDot");
// Modal & Control Elements
const modal = document.getElementById("modal");
const infoBtn = document.getElementById("infoBtn");
const closeBtn = document.getElementById("closeBtn");
const speedRange = document.getElementById("speedRange");
const playPauseBtn = document.getElementById("playPauseBtn");
const resetBtn = document.getElementById("resetBtn");
const monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
// --- MODAL & CONTROLS LOGIC ---
if(infoBtn && modal && closeBtn) {
infoBtn.addEventListener("click", () => {
modal.classList.remove("hidden");
});
closeBtn.addEventListener("click", () => {
modal.classList.add("hidden");
});
// Close on clicking outside box
modal.addEventListener("click", (e) => {
if(e.target === modal) modal.classList.add("hidden");
});
}
if(speedRange) {
speedRange.addEventListener("input", (e) => {
speed = parseFloat(e.target.value);
});
}
// Play/Pause Toggle
if(playPauseBtn) {
playPauseBtn.addEventListener("click", () => {
paused = !paused;
playPauseBtn.innerHTML = paused ? '<i class="fas fa-play"></i>' : '<i class="fas fa-pause"></i>';
});
}
// Reset Button
if(resetBtn) {
resetBtn.addEventListener("click", () => {
time = 0;
lastSpawnedIndex = -1;
particles = [];
paused = false;
if(playPauseBtn) playPauseBtn.innerHTML = '<i class="fas fa-pause"></i>';
});
}
function resize() {
cv.width = window.innerWidth;
cv.height = window.innerHeight;
// Create Gradient ONCE
const centerY = cv.height / 2;
const scaleY = 225; // Increased by 50% (was 150)
gradient = ctx.createLinearGradient(0, centerY - scaleY, 0, centerY + scaleY);
gradient.addColorStop(0, "rgba(255, 50, 0, 0.4)"); // Top (Hot)
gradient.addColorStop(0.45, "rgba(255, 50, 0, 0.05)"); // Fade
gradient.addColorStop(0.5, "rgba(255, 255, 255, 0)"); // Axis
gradient.addColorStop(0.55, "rgba(0, 200, 255, 0.05)"); // Fade
gradient.addColorStop(1, "rgba(0, 200, 255, 0.4)"); // Bottom (Cold)
}
resize();
window.addEventListener("resize", resize);
// --- DATA LOADING ---
function processCSV(csvText) {
console.log("processCSV() called, data length:", csvText.length);
const lines = csvText.trim().split("\n");
tempData = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line || line.startsWith("Land") || line.startsWith("Year") || line.startsWith("Global")) continue;
const parts = line.split(",");
const year = parseInt(parts[0]);
if (isNaN(year)) continue;
if (parts.length >= 13) {
for (let month = 1; month <= 12; month++) {
const val = parts[month];
if (val && val !== "***") {
const anomaly = parseFloat(val);
if (!isNaN(anomaly)) {
tempData.push({ year, month, anomaly });
}
}
}
}
}
console.log("Processed data points:", tempData.length);
if (liveText) liveText.textContent = `BUFFER: ${tempData.length} POINTS`;
dataLoaded = true;
console.log("dataLoaded set to true");
}
function loadFallbackData() {
const annualData = [
{ year: 1880, anomaly: -0.16 }, { year: 1900, anomaly: -0.08 },
{ year: 1920, anomaly: -0.27 }, { year: 1940, anomaly: 0.13 },
{ year: 1960, anomaly: -0.03 }, { year: 1980, anomaly: 0.26 },
{ year: 2000, anomaly: 0.39 }, { year: 2024, anomaly: 1.31 }
];
tempData = [];
let prev = annualData[0];
for (let y = 1880; y <= 2025; y++) {
const match = annualData.find((d) => d.year === y);
const val = match ? match.anomaly : prev.anomaly;
if (match) prev = match;
for (let m = 1; m <= 12; m++) tempData.push({ year: y, month: m, anomaly: val });
}
dataLoaded = true;
}
const cachedData = localStorage.getItem(STORAGE_KEY);
console.log("Cached data exists:", !!cachedData);
if (cachedData) {
console.log("Using cached data");
processCSV(cachedData);
} else {
console.log("Fetching fresh data from NASA...");
if (liveText) liveText.textContent = "FETCHING...";
fetch(NASA_URL)
.then((res) => {
if (!res.ok) throw new Error("Network");
return res.text();
})
.then((text) => {
localStorage.setItem(STORAGE_KEY, text);
processCSV(text);
})
.catch((e) => {
console.error("Fetch failed:", e);
console.log("Loading fallback data");
loadFallbackData();
});
}
// --- VISUALIZATION ---
class PillarParticle {
constructor(x, axisY, dataY, temp) {
this.x = x;
this.axisY = axisY;
this.dataY = dataY;
this.temp = temp;
this.size = Math.random() * 1.5 + 0.5;
// Fill Volume
this.y = axisY + Math.random() * (dataY - axisY);
// Color Logic
const t = Math.max(-1, Math.min(2, temp));
if (t < -0.2) {
this.r = 0; this.g = 200; this.b = 255; this.type = "ice";
} else if (t < 0) {
this.r = 150; this.g = 220; this.b = 255; this.type = "ice";
} else if (t < 0.5) {
this.r = 255; this.g = 200; this.b = 100; this.type = "fire";
} else if (t < 1.0) {
this.r = 255; this.g = 100; this.b = 50; this.type = "fire";
} else {
this.r = 255; this.g = 50; this.b = 0; this.type = "fire";
}
// Physics
const absT = Math.abs(t);
this.speed = 0.5 + Math.random() * (1 + absT);
this.vy = this.temp > 0 ? -this.speed : this.speed;
}
update() {
this.y += this.vy;
if (this.temp > 0) {
if (this.y < this.dataY) {
this.y = this.axisY;
this.x += (Math.random() - 0.5) * 2;
}
} else {
if (this.y > this.dataY) {
this.y = this.axisY;
this.x += (Math.random() - 0.5) * 2;
}
}
}
draw() {
const totalDist = Math.abs(this.dataY - this.axisY);
const currentDist = Math.abs(this.y - this.axisY);
let alpha = 1 - (currentDist / (totalDist + 0.1));
alpha = Math.max(0, Math.min(1, alpha));
ctx.fillStyle = `rgba(${this.r},${this.g},${this.b},${alpha})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
function updateUI(data) {
if (!yearEl || !monthEl) return;
yearEl.textContent = data.year;
monthEl.textContent = monthNames[data.month - 1];
let color, shadowColor, shadowSize;
if (data.anomaly < 0) { // ICE
color = "rgba(200, 240, 255, 1)";
shadowColor = "rgba(0, 200, 255, 0.8)";
shadowSize = 20 + Math.abs(data.anomaly) * 30;
if (liveDot) { liveDot.style.backgroundColor = "#0cf"; liveDot.style.boxShadow = "0 0 10px #0cf"; }
} else if (data.anomaly < 0.5) { // WARM
color = "rgba(255, 240, 200, 1)";
shadowColor = "rgba(255, 180, 50, 0.6)";
shadowSize = 10;
if (liveDot) { liveDot.style.backgroundColor = "#fa0"; liveDot.style.boxShadow = "none"; }
} else { // FIRE
color = "rgba(255, 255, 255, 1)";
shadowColor = "rgba(255, 50, 0, 0.9)";
shadowSize = 30 + data.anomaly * 40;
if (liveDot) { liveDot.style.backgroundColor = "#f00"; liveDot.style.boxShadow = "0 0 15px #f00"; }
}
// Keep year display neutral (no color effect)
if (mainDisplay) {
mainDisplay.style.color = "rgba(255, 255, 255, 0.25)";
mainDisplay.style.textShadow = "none";
}
// APPLY FULL COLOR EFFECT TO TEMP DISPLAY
if (tempEl) {
tempEl.textContent = `${data.anomaly > 0 ? "+" : ""}${data.anomaly.toFixed(2)}°C`;
tempEl.style.color = color;
tempEl.style.textShadow = `0 0 ${shadowSize}px ${shadowColor}, 0 0 10px ${shadowColor}`;
}
if (liveText) liveText.innerHTML = `SCAN: ${data.year}.${String(data.month).padStart(2, "0")} [${data.anomaly.toFixed(3)}]`;
}
function animate() {
if (!dataLoaded || tempData.length === 0) {
requestAnimationFrame(animate);
return;
}
// console.log("Animating frame, time:", time);
// Only increment time if not paused
if (!paused) {
time += speed;
}
if (time >= tempData.length) {
time = 0;
lastSpawnedIndex = -1;
particles = [];
}
ctx.clearRect(0, 0, cv.width, cv.height);
const paddingX = 0; // Edge to edge
const graphWidth = cv.width - paddingX * 2;
const scaleY = 225; // Increased by 50% (was 150)
const centerY = cv.height / 2;
const currentIndex = Math.floor(time);
// 1. Draw Trend Line
ctx.beginPath();
ctx.moveTo(paddingX, centerY);
let finalX = paddingX;
for (let i = 0; i <= currentIndex; i++) {
const d = tempData[i];
const lx = paddingX + (i / tempData.length) * graphWidth;
const ly = centerY - d.anomaly * scaleY;
ctx.lineTo(lx, ly);
finalX = lx;
}
ctx.lineTo(finalX, centerY);
ctx.closePath();
ctx.fillStyle = gradient;
ctx.fill();
// 2. Spawn History (only when not paused)
if (!paused && currentIndex > lastSpawnedIndex) {
for (let i = lastSpawnedIndex + 1; i <= currentIndex; i++) {
const data = tempData[i];
if (!data) continue;
const px = paddingX + (i / tempData.length) * graphWidth;
const py = centerY - data.anomaly * scaleY;
const intensity = Math.max(2, Math.abs(data.anomaly) * 5);
const count = 2 + Math.floor(Math.random() * intensity);
for (let p = 0; p < count; p++) {
particles.push(new PillarParticle(px, centerY, py, data.anomaly));
}
}
lastSpawnedIndex = currentIndex;
}
// 3. Update Particles (always animate existing particles)
for (let i = 0; i < particles.length; i++) {
if (!paused) {
particles[i].update();
}
particles[i].draw();
}
// 4. Draw Head Dot
const data = tempData[currentIndex];
if (data) {
const hx = paddingX + (currentIndex / tempData.length) * graphWidth;
const hy = centerY - data.anomaly * scaleY;
ctx.fillStyle = "#fff";
ctx.shadowBlur = 10;
ctx.shadowColor = "#fff";
ctx.beginPath();
ctx.arc(hx, hy, 3, 0, Math.PI * 2);
ctx.fill();
ctx.shadowBlur = 0;
updateUI(data);
}
requestAnimationFrame(animate);
}
console.log("Starting animation loop...");
animate();
}
// Run immediately if DOM is ready, otherwise wait
console.log("Script loaded, readyState:", document.readyState);
if (document.readyState === 'loading') {
console.log("DOM still loading, adding event listener");
document.addEventListener('DOMContentLoaded', initVisualization);
} else {
console.log("DOM already loaded, running immediately");
initVisualization();
}