@geolith/globe 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +80 -2
- package/dist/index.d.ts +333 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +740 -0
- package/dist/index.js.map +1 -0
- package/package.json +84 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
import { computed, defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef, watch } from "vue";
|
|
2
|
+
import { WeatherFieldLayer, WindParticleLayer, decodeGrid } from "@geoql/maplibre-gl-weather";
|
|
3
|
+
import { SliderRange, SliderRoot, SliderThumb, SliderTrack } from "reka-ui";
|
|
4
|
+
const GlobeSkeleton = defineComponent({
|
|
5
|
+
name: "GlobeSkeleton",
|
|
6
|
+
setup() {
|
|
7
|
+
return () => h("div", {
|
|
8
|
+
"data-globe-skeleton": "",
|
|
9
|
+
class: "globe-skeleton"
|
|
10
|
+
}, [h("span", {
|
|
11
|
+
class: "globe-skeleton-ring",
|
|
12
|
+
"aria-hidden": "true"
|
|
13
|
+
}), h("span", { class: "globe-skeleton-label" }, "Loading the globe")]);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
// src/utils/camera.ts
|
|
17
|
+
/**
|
|
18
|
+
* The globe opens at a fixed zoom and pitch, over one place: the visitor's
|
|
19
|
+
* country when the caller resolved it, the world otherwise.
|
|
20
|
+
*/
|
|
21
|
+
const OPEN_ZOOM = 1.8;
|
|
22
|
+
/** The globe is never tilted on open: a tilted planet hides half the field. */
|
|
23
|
+
const OPEN_PITCH = 0;
|
|
24
|
+
/**
|
|
25
|
+
* Slow rotation so a still visitor sees a planet, not a poster. One full turn
|
|
26
|
+
* takes five minutes; faster reads as a loading spinner.
|
|
27
|
+
*/
|
|
28
|
+
const ROTATE_DEGREES_PER_SECOND = 1.2;
|
|
29
|
+
/** The world view: equirectangular centre of the planet, 20° north. */
|
|
30
|
+
const WORLD_CENTRE = [0, 20];
|
|
31
|
+
/**
|
|
32
|
+
* The opening camera: the visitor's country's centroid when it is known, the
|
|
33
|
+
* world otherwise.
|
|
34
|
+
*
|
|
35
|
+
* @param centroid - `[lon, lat]` of the visitor's country, or null when unknown.
|
|
36
|
+
*/
|
|
37
|
+
function openingCamera(centroid) {
|
|
38
|
+
return {
|
|
39
|
+
center: centroid ? [centroid[0], centroid[1]] : [WORLD_CENTRE[0], WORLD_CENTRE[1]],
|
|
40
|
+
zoom: OPEN_ZOOM,
|
|
41
|
+
pitch: 0
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Whether the globe should rotate right now. Reduced motion is honoured, and the
|
|
46
|
+
* first interaction hands control to the visitor for good.
|
|
47
|
+
*/
|
|
48
|
+
function shouldRotate(reducedMotion, interacted) {
|
|
49
|
+
return !reducedMotion && !interacted;
|
|
50
|
+
}
|
|
51
|
+
// src/utils/handover.ts
|
|
52
|
+
/**
|
|
53
|
+
* Zoom range over which the field fades out and the districts fade in. They are
|
|
54
|
+
* the same two numbers on purpose: the globe is always showing one of them, and
|
|
55
|
+
* a seam between the ramps would show as a flash of bare basemap.
|
|
56
|
+
*/
|
|
57
|
+
const FIELD_FADE = [6.5, 8];
|
|
58
|
+
/** The districts' half of the same handover. */
|
|
59
|
+
const DISTRICT_FADE = [6.5, 8];
|
|
60
|
+
/** The desktop particle count the spike measured as free on a desktop GPU. */
|
|
61
|
+
const PARTICLE_CAP = 42e3;
|
|
62
|
+
const RAMP = FIELD_FADE;
|
|
63
|
+
/**
|
|
64
|
+
* Opacity of the global field at a zoom: 1 below the ramp, 0 above it, linear
|
|
65
|
+
* between.
|
|
66
|
+
*/
|
|
67
|
+
function fieldOpacity(zoom) {
|
|
68
|
+
const [from, to] = RAMP;
|
|
69
|
+
if (zoom <= from) return 1;
|
|
70
|
+
if (zoom >= to) return 0;
|
|
71
|
+
return (to - zoom) / (to - from);
|
|
72
|
+
}
|
|
73
|
+
/** Opacity of the district fill at a zoom; the field's complement. */
|
|
74
|
+
function districtOpacity(zoom) {
|
|
75
|
+
return 1 - fieldOpacity(zoom);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* How many particles to draw. A phone is given fewer because the fragment cost
|
|
79
|
+
* is per device pixel, and a visitor who prefers reduced motion gets none.
|
|
80
|
+
*
|
|
81
|
+
* @param width - The map's width in CSS pixels.
|
|
82
|
+
* @param dpr - The device pixel ratio the map draws at.
|
|
83
|
+
*/
|
|
84
|
+
function particleCount(width, dpr, reducedMotion) {
|
|
85
|
+
if (reducedMotion) return 0;
|
|
86
|
+
return Math.min(PARTICLE_CAP, Math.round(width * (width < 600 ? dpr >= 2 ? 18 : 26 : 30)));
|
|
87
|
+
}
|
|
88
|
+
// src/utils/palette.ts
|
|
89
|
+
/**
|
|
90
|
+
* The field ramps. They are data ramps, not brand colours: a shader needs plain
|
|
91
|
+
* RGB stops, and these are perceptually spaced so a 1 mm cell never looks like a
|
|
92
|
+
* 40 mm cell. A caller may pass its own stops to `WeatherFieldLayer`.
|
|
93
|
+
*/
|
|
94
|
+
const RAIN_RAMP = [
|
|
95
|
+
"#f7fbff",
|
|
96
|
+
"#deebf7",
|
|
97
|
+
"#9ecae1",
|
|
98
|
+
"#4292c6",
|
|
99
|
+
"#2171b5",
|
|
100
|
+
"#08519c",
|
|
101
|
+
"#5e3c99"
|
|
102
|
+
];
|
|
103
|
+
/** Cold to hot, through a neutral middle so 0 °C is not tinted. */
|
|
104
|
+
const TEMPERATURE_RAMP = [
|
|
105
|
+
"#2166ac",
|
|
106
|
+
"#67a9cf",
|
|
107
|
+
"#d1e5f0",
|
|
108
|
+
"#f7f7f7",
|
|
109
|
+
"#fddbc7",
|
|
110
|
+
"#f4a582",
|
|
111
|
+
"#d6604d",
|
|
112
|
+
"#b2182b"
|
|
113
|
+
];
|
|
114
|
+
/** Calm to strong, the magma ramp. */
|
|
115
|
+
const WIND_RAMP = [
|
|
116
|
+
"#3b0f70",
|
|
117
|
+
"#8c2981",
|
|
118
|
+
"#de4968",
|
|
119
|
+
"#fe9f6d",
|
|
120
|
+
"#fcfdbf"
|
|
121
|
+
];
|
|
122
|
+
/** Every ramp the globe ships, keyed by the field family it draws. */
|
|
123
|
+
const FIELD_PALETTES = {
|
|
124
|
+
rain: RAIN_RAMP,
|
|
125
|
+
temperature: TEMPERATURE_RAMP,
|
|
126
|
+
wind: WIND_RAMP
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* The ramp for a field name: rain grids take the rain ramp, both temperature
|
|
130
|
+
* fields the temperature ramp, both wind components the wind ramp.
|
|
131
|
+
*/
|
|
132
|
+
function paletteFor(field) {
|
|
133
|
+
if (field === "rain") return RAIN_RAMP;
|
|
134
|
+
if (field === "tmax" || field === "tmin") return TEMPERATURE_RAMP;
|
|
135
|
+
return WIND_RAMP;
|
|
136
|
+
}
|
|
137
|
+
// src/composables/use-globe-map.ts
|
|
138
|
+
const DEFAULT_FIELD_OPACITY = .85;
|
|
139
|
+
/**
|
|
140
|
+
* The map's lifecycle and its layers. Mounts only when its host measures non
|
|
141
|
+
* zero in both axes, so a hidden page (or a test with no layout) mounts
|
|
142
|
+
* nothing and logs nothing.
|
|
143
|
+
*/
|
|
144
|
+
function useGlobeMap(input) {
|
|
145
|
+
const map = shallowRef(null);
|
|
146
|
+
let field = null;
|
|
147
|
+
let particles = null;
|
|
148
|
+
let frame = 0;
|
|
149
|
+
let interacted = false;
|
|
150
|
+
let wired = false;
|
|
151
|
+
const stopRotation = () => {
|
|
152
|
+
if (frame !== 0) cancelAnimationFrame(frame);
|
|
153
|
+
frame = 0;
|
|
154
|
+
};
|
|
155
|
+
const rotate = () => {
|
|
156
|
+
const instance = map.value;
|
|
157
|
+
if (!instance || !shouldRotate(input.reducedMotion, interacted)) return;
|
|
158
|
+
const centre = instance.getCenter();
|
|
159
|
+
const lng = centre.lng + ROTATE_DEGREES_PER_SECOND / 60;
|
|
160
|
+
instance.setCenter([lng > 180 ? lng - 360 : lng, centre.lat]);
|
|
161
|
+
frame = requestAnimationFrame(rotate);
|
|
162
|
+
};
|
|
163
|
+
const addField = (instance) => {
|
|
164
|
+
const frames = input.frames.value;
|
|
165
|
+
if (frames.length === 0 || field) return;
|
|
166
|
+
const first = frames[0];
|
|
167
|
+
const palette = input.palette ?? paletteFor(first?.meta.field ?? "rain");
|
|
168
|
+
field = new WeatherFieldLayer({
|
|
169
|
+
frames,
|
|
170
|
+
mix: input.mix.value,
|
|
171
|
+
palette,
|
|
172
|
+
opacity: DEFAULT_FIELD_OPACITY
|
|
173
|
+
});
|
|
174
|
+
instance.addLayer(field);
|
|
175
|
+
instance.on("zoom", () => {
|
|
176
|
+
field?.setOpacity(DEFAULT_FIELD_OPACITY * fieldOpacity(instance.getZoom()));
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
const addParticles = (instance) => {
|
|
180
|
+
const wind = input.wind.value;
|
|
181
|
+
if (!wind || particles) return;
|
|
182
|
+
const width = input.host.value?.clientWidth ?? 0;
|
|
183
|
+
const dpr = globalThis.devicePixelRatio ?? 1;
|
|
184
|
+
particles = new WindParticleLayer({
|
|
185
|
+
u: wind.u,
|
|
186
|
+
v: wind.v,
|
|
187
|
+
count: particleCount(width, dpr, input.reducedMotion),
|
|
188
|
+
animated: !input.reducedMotion
|
|
189
|
+
});
|
|
190
|
+
instance.addLayer(particles);
|
|
191
|
+
};
|
|
192
|
+
const addDistricts = (instance) => {
|
|
193
|
+
const district = input.district.value;
|
|
194
|
+
if (!district) return;
|
|
195
|
+
const sourceId = `globe-districts-${district.country}`;
|
|
196
|
+
if (instance.getSource(sourceId)) return;
|
|
197
|
+
instance.addSource(sourceId, {
|
|
198
|
+
type: "vector",
|
|
199
|
+
tiles: [district.url]
|
|
200
|
+
});
|
|
201
|
+
instance.addLayer({
|
|
202
|
+
id: `${sourceId}-fill`,
|
|
203
|
+
type: "fill",
|
|
204
|
+
source: sourceId,
|
|
205
|
+
paint: {
|
|
206
|
+
"fill-color": paletteFor("rain")[3] ?? "#4292c6",
|
|
207
|
+
"fill-opacity": [
|
|
208
|
+
"interpolate",
|
|
209
|
+
["linear"],
|
|
210
|
+
["zoom"],
|
|
211
|
+
6.5,
|
|
212
|
+
0,
|
|
213
|
+
8,
|
|
214
|
+
1
|
|
215
|
+
]
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
const wire = () => {
|
|
220
|
+
const instance = map.value;
|
|
221
|
+
if (!instance || !wired) return;
|
|
222
|
+
addField(instance);
|
|
223
|
+
addParticles(instance);
|
|
224
|
+
addDistricts(instance);
|
|
225
|
+
};
|
|
226
|
+
const mount = async () => {
|
|
227
|
+
if (map.value) return;
|
|
228
|
+
const host = input.host.value;
|
|
229
|
+
if (!host || typeof window === "undefined") return;
|
|
230
|
+
const rect = host.getBoundingClientRect();
|
|
231
|
+
if (rect.width === 0 || rect.height === 0) return;
|
|
232
|
+
const { Map } = await import("maplibre-gl");
|
|
233
|
+
const camera = openingCamera(input.centroid);
|
|
234
|
+
const created = new Map({
|
|
235
|
+
container: host,
|
|
236
|
+
style: input.style,
|
|
237
|
+
center: camera.center,
|
|
238
|
+
zoom: camera.zoom,
|
|
239
|
+
pitch: camera.pitch,
|
|
240
|
+
attributionControl: false
|
|
241
|
+
});
|
|
242
|
+
created.setProjection({ type: "globe" });
|
|
243
|
+
map.value = created;
|
|
244
|
+
created.once("idle", () => {
|
|
245
|
+
wired = true;
|
|
246
|
+
wire();
|
|
247
|
+
input.onIdle();
|
|
248
|
+
});
|
|
249
|
+
const stop = () => {
|
|
250
|
+
interacted = true;
|
|
251
|
+
stopRotation();
|
|
252
|
+
};
|
|
253
|
+
const canvas = created.getCanvas();
|
|
254
|
+
canvas.addEventListener("pointerdown", stop, { once: true });
|
|
255
|
+
canvas.addEventListener("wheel", stop, { once: true });
|
|
256
|
+
canvas.addEventListener("touchstart", stop, { once: true });
|
|
257
|
+
if (shouldRotate(input.reducedMotion, interacted)) frame = requestAnimationFrame(rotate);
|
|
258
|
+
};
|
|
259
|
+
watch(input.frames, () => {
|
|
260
|
+
if (field) field.setFrames(input.frames.value, input.mix.value);
|
|
261
|
+
else wire();
|
|
262
|
+
});
|
|
263
|
+
watch(input.mix, (value) => field?.setMix(value));
|
|
264
|
+
watch(input.wind, () => wire());
|
|
265
|
+
watch(input.district, () => wire());
|
|
266
|
+
watch(input.mountWhen, (ready) => {
|
|
267
|
+
if (ready) mount();
|
|
268
|
+
});
|
|
269
|
+
const dispose = () => {
|
|
270
|
+
stopRotation();
|
|
271
|
+
field = null;
|
|
272
|
+
particles = null;
|
|
273
|
+
wired = false;
|
|
274
|
+
map.value?.remove();
|
|
275
|
+
map.value = null;
|
|
276
|
+
};
|
|
277
|
+
return {
|
|
278
|
+
map,
|
|
279
|
+
mount,
|
|
280
|
+
dispose
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
const GlobeCanvas = defineComponent({
|
|
284
|
+
name: "GlobeCanvas",
|
|
285
|
+
props: {
|
|
286
|
+
layers: {
|
|
287
|
+
type: Array,
|
|
288
|
+
required: true
|
|
289
|
+
},
|
|
290
|
+
dataSource: {
|
|
291
|
+
type: Object,
|
|
292
|
+
required: true
|
|
293
|
+
},
|
|
294
|
+
style: {
|
|
295
|
+
type: Object,
|
|
296
|
+
required: true
|
|
297
|
+
},
|
|
298
|
+
status: {
|
|
299
|
+
type: String,
|
|
300
|
+
required: true
|
|
301
|
+
},
|
|
302
|
+
centroid: {
|
|
303
|
+
type: Array,
|
|
304
|
+
default: null
|
|
305
|
+
},
|
|
306
|
+
reducedMotion: {
|
|
307
|
+
type: Boolean,
|
|
308
|
+
default: false
|
|
309
|
+
},
|
|
310
|
+
frames: {
|
|
311
|
+
type: Array,
|
|
312
|
+
default: () => []
|
|
313
|
+
},
|
|
314
|
+
mix: {
|
|
315
|
+
type: Number,
|
|
316
|
+
default: 0
|
|
317
|
+
},
|
|
318
|
+
palette: {
|
|
319
|
+
type: Array,
|
|
320
|
+
default: null
|
|
321
|
+
},
|
|
322
|
+
wind: {
|
|
323
|
+
type: Object,
|
|
324
|
+
default: null
|
|
325
|
+
},
|
|
326
|
+
district: {
|
|
327
|
+
type: Object,
|
|
328
|
+
default: null
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
emits: ["idle"],
|
|
332
|
+
setup(props, { emit }) {
|
|
333
|
+
const host = ref(null);
|
|
334
|
+
const frames = ref(props.frames);
|
|
335
|
+
const mix = ref(props.mix);
|
|
336
|
+
const wind = ref(props.wind);
|
|
337
|
+
const district = ref(props.district);
|
|
338
|
+
const mountWhen = ref(props.status === "ready");
|
|
339
|
+
const globe = useGlobeMap({
|
|
340
|
+
host,
|
|
341
|
+
style: props.style,
|
|
342
|
+
centroid: props.centroid,
|
|
343
|
+
reducedMotion: props.reducedMotion,
|
|
344
|
+
source: props.dataSource,
|
|
345
|
+
frames,
|
|
346
|
+
mix,
|
|
347
|
+
palette: props.palette,
|
|
348
|
+
wind,
|
|
349
|
+
district,
|
|
350
|
+
mountWhen,
|
|
351
|
+
onIdle: () => emit("idle")
|
|
352
|
+
});
|
|
353
|
+
watch(() => props.frames, (value) => {
|
|
354
|
+
frames.value = value;
|
|
355
|
+
});
|
|
356
|
+
watch(() => props.mix, (value) => {
|
|
357
|
+
mix.value = value;
|
|
358
|
+
});
|
|
359
|
+
watch(() => props.wind, (value) => {
|
|
360
|
+
wind.value = value;
|
|
361
|
+
});
|
|
362
|
+
watch(() => props.district, (value) => {
|
|
363
|
+
district.value = value;
|
|
364
|
+
});
|
|
365
|
+
watch(() => props.status, (value) => {
|
|
366
|
+
mountWhen.value = value === "ready";
|
|
367
|
+
});
|
|
368
|
+
onMounted(() => {
|
|
369
|
+
if (props.status === "ready") globe.mount();
|
|
370
|
+
});
|
|
371
|
+
onBeforeUnmount(() => globe.dispose());
|
|
372
|
+
return () => h("div", {
|
|
373
|
+
ref: host,
|
|
374
|
+
class: "globe-canvas",
|
|
375
|
+
"data-globe": props.status,
|
|
376
|
+
"data-globe-host": ""
|
|
377
|
+
}, props.status === "loading" ? [h(GlobeSkeleton)] : []);
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
const GlobeLock = defineComponent({
|
|
381
|
+
name: "GlobeLock",
|
|
382
|
+
props: { country: {
|
|
383
|
+
type: String,
|
|
384
|
+
required: true
|
|
385
|
+
} },
|
|
386
|
+
emits: ["unlock-request"],
|
|
387
|
+
setup(props, { emit }) {
|
|
388
|
+
return () => h("div", {
|
|
389
|
+
class: "globe-lock",
|
|
390
|
+
"data-globe-lock": props.country
|
|
391
|
+
}, [h("p", { class: "globe-lock-copy" }, "District data is not open for this country yet."), h("button", {
|
|
392
|
+
type: "button",
|
|
393
|
+
class: "globe-lock-action",
|
|
394
|
+
onClick: () => emit("unlock-request", { country: props.country })
|
|
395
|
+
}, "Unlock district data for 230 countries")]);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
const GlobeReadout = defineComponent({
|
|
399
|
+
name: "GlobeReadout",
|
|
400
|
+
props: {
|
|
401
|
+
value: {
|
|
402
|
+
type: Number,
|
|
403
|
+
default: null
|
|
404
|
+
},
|
|
405
|
+
unit: {
|
|
406
|
+
type: String,
|
|
407
|
+
required: true
|
|
408
|
+
}
|
|
409
|
+
},
|
|
410
|
+
setup(props) {
|
|
411
|
+
return () => {
|
|
412
|
+
const value = props.value;
|
|
413
|
+
if (value === null) return h("span", { class: "globe-readout" });
|
|
414
|
+
const shown = Math.round(value * 10) / 10;
|
|
415
|
+
return h("span", {
|
|
416
|
+
class: "globe-readout",
|
|
417
|
+
"data-globe-readout": ""
|
|
418
|
+
}, `${shown} ${props.unit}`);
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
const GlobeTimeSlider = defineComponent({
|
|
423
|
+
name: "GlobeTimeSlider",
|
|
424
|
+
props: {
|
|
425
|
+
labels: {
|
|
426
|
+
type: Array,
|
|
427
|
+
required: true
|
|
428
|
+
},
|
|
429
|
+
modelValue: {
|
|
430
|
+
type: Number,
|
|
431
|
+
required: true
|
|
432
|
+
},
|
|
433
|
+
playing: {
|
|
434
|
+
type: Boolean,
|
|
435
|
+
default: false
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
emits: ["update:modelValue", "toggle"],
|
|
439
|
+
setup(props, { emit }) {
|
|
440
|
+
const label = () => props.labels[props.modelValue] ?? "";
|
|
441
|
+
const update = (value) => {
|
|
442
|
+
const next = value?.[0];
|
|
443
|
+
if (typeof next === "number") emit("update:modelValue", next);
|
|
444
|
+
};
|
|
445
|
+
return () => h("div", {
|
|
446
|
+
class: "globe-slider",
|
|
447
|
+
"data-globe-slider": ""
|
|
448
|
+
}, [
|
|
449
|
+
h("button", {
|
|
450
|
+
type: "button",
|
|
451
|
+
class: "globe-slider-play",
|
|
452
|
+
"aria-label": props.playing ? "Pause" : "Play",
|
|
453
|
+
"aria-pressed": props.playing ? "true" : "false",
|
|
454
|
+
onClick: () => emit("toggle")
|
|
455
|
+
}, props.playing ? "Pause" : "Play"),
|
|
456
|
+
h(SliderRoot, {
|
|
457
|
+
class: "globe-slider-root",
|
|
458
|
+
modelValue: [props.modelValue],
|
|
459
|
+
max: Math.max(props.labels.length - 1, 0),
|
|
460
|
+
step: 1,
|
|
461
|
+
"onUpdate:modelValue": update
|
|
462
|
+
}, () => [h(SliderTrack, { class: "globe-slider-track" }, () => [h(SliderRange, { class: "globe-slider-range" })]), h(SliderThumb, {
|
|
463
|
+
class: "globe-slider-thumb",
|
|
464
|
+
"aria-valuetext": label(),
|
|
465
|
+
"aria-label": "Forecast day"
|
|
466
|
+
})]),
|
|
467
|
+
h("span", { class: "globe-slider-label" }, label())
|
|
468
|
+
]);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
// src/composables/use-globe-state.ts
|
|
472
|
+
const clamp = (value, max) => {
|
|
473
|
+
if (!Number.isFinite(value)) return 0;
|
|
474
|
+
const whole = Math.trunc(value);
|
|
475
|
+
if (whole < 0) return 0;
|
|
476
|
+
return whole > max ? max : whole;
|
|
477
|
+
};
|
|
478
|
+
/**
|
|
479
|
+
* The globe's state machine: which layer, model and day are showing, whether it
|
|
480
|
+
* plays, and which countries may show district numbers.
|
|
481
|
+
*
|
|
482
|
+
* Pure Vue reactivity — no browser API and no HTTP — so a page and a test share
|
|
483
|
+
* one behaviour. `now` is injected so a test can advance the exploration clock
|
|
484
|
+
* without sleeping.
|
|
485
|
+
*/
|
|
486
|
+
function useGlobeState(initial) {
|
|
487
|
+
const { layers, unlocked, unlockedCountries } = initial;
|
|
488
|
+
const now = initial.now ?? (() => Date.now());
|
|
489
|
+
const dayCount = clamp(initial.days, Number.MAX_SAFE_INTEGER);
|
|
490
|
+
const opening = layers[0];
|
|
491
|
+
const selection = ref({
|
|
492
|
+
layerId: opening?.id ?? "",
|
|
493
|
+
model: opening?.models[0] ?? "ecmwf-ifs",
|
|
494
|
+
dayIndex: 0
|
|
495
|
+
});
|
|
496
|
+
const playing = ref(false);
|
|
497
|
+
const explored = ref({
|
|
498
|
+
layers: new Set(opening ? [opening.id] : []),
|
|
499
|
+
startedAt: now()
|
|
500
|
+
});
|
|
501
|
+
const lastDay = computed(() => clamp(dayCount, Number.MAX_SAFE_INTEGER) - 1);
|
|
502
|
+
const layer = computed(() => layers.find((entry) => entry.id === selection.value.layerId) ?? null);
|
|
503
|
+
const remember = (layerId) => {
|
|
504
|
+
if (explored.value.layers.has(layerId)) return;
|
|
505
|
+
explored.value = {
|
|
506
|
+
layers: /* @__PURE__ */ new Set([...explored.value.layers, layerId]),
|
|
507
|
+
startedAt: explored.value.startedAt
|
|
508
|
+
};
|
|
509
|
+
};
|
|
510
|
+
const select = (layerId) => {
|
|
511
|
+
const next = layers.find((entry) => entry.id === layerId);
|
|
512
|
+
if (!next) return;
|
|
513
|
+
const model = next.models.includes(selection.value.model) ? selection.value.model : next.models[0] ?? selection.value.model;
|
|
514
|
+
selection.value = {
|
|
515
|
+
...selection.value,
|
|
516
|
+
layerId,
|
|
517
|
+
model
|
|
518
|
+
};
|
|
519
|
+
remember(layerId);
|
|
520
|
+
};
|
|
521
|
+
const setModel = (model) => {
|
|
522
|
+
if (!layer.value?.models.includes(model)) return;
|
|
523
|
+
selection.value = {
|
|
524
|
+
...selection.value,
|
|
525
|
+
model
|
|
526
|
+
};
|
|
527
|
+
};
|
|
528
|
+
const setDay = (index) => {
|
|
529
|
+
selection.value = {
|
|
530
|
+
...selection.value,
|
|
531
|
+
dayIndex: clamp(index, lastDay.value)
|
|
532
|
+
};
|
|
533
|
+
};
|
|
534
|
+
const togglePlaying = (value) => {
|
|
535
|
+
playing.value = value ?? !playing.value;
|
|
536
|
+
};
|
|
537
|
+
const lockFor = (country) => {
|
|
538
|
+
if (unlocked) return false;
|
|
539
|
+
const key = country.trim().toLowerCase();
|
|
540
|
+
return !unlockedCountries.some((entry) => entry.toLowerCase() === key);
|
|
541
|
+
};
|
|
542
|
+
return {
|
|
543
|
+
selection,
|
|
544
|
+
playing,
|
|
545
|
+
explored,
|
|
546
|
+
layer,
|
|
547
|
+
lastDay,
|
|
548
|
+
select,
|
|
549
|
+
setModel,
|
|
550
|
+
setDay,
|
|
551
|
+
togglePlaying,
|
|
552
|
+
lockFor
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
// src/composables/use-globe-fields.ts
|
|
556
|
+
const isIndex$1 = (value) => {
|
|
557
|
+
if (typeof value !== "object" || value === null) return false;
|
|
558
|
+
const candidate = value;
|
|
559
|
+
return typeof candidate.run === "string" && Array.isArray(candidate.days) && typeof candidate.width === "number" && typeof candidate.height === "number" && typeof candidate.step === "number";
|
|
560
|
+
};
|
|
561
|
+
/** A 404 is a country or run with no data, not a broken globe. */
|
|
562
|
+
const isMissing = (error) => {
|
|
563
|
+
if (error?.status === 404) return true;
|
|
564
|
+
return /404|not found/i.test(String(error));
|
|
565
|
+
};
|
|
566
|
+
/** `'forecast-rain'` → `'rain'`, for a caller that passes no field list. */
|
|
567
|
+
const fieldFromLayer = (layerId) => {
|
|
568
|
+
const tail = layerId.split("-").pop() ?? "";
|
|
569
|
+
if (tail === "rain") return "rain";
|
|
570
|
+
if (tail === "temperature" || tail === "tmax" || tail === "tmin") return "tmax";
|
|
571
|
+
return "wind-u";
|
|
572
|
+
};
|
|
573
|
+
/**
|
|
574
|
+
* Loads one field's index, then the selected day and the day after it, so the
|
|
575
|
+
* shader can blend. The composable never invents a zero field: no data is
|
|
576
|
+
* `'empty'` and a failed grid after a good index keeps the frames it had.
|
|
577
|
+
*/
|
|
578
|
+
function useGlobeFields(input) {
|
|
579
|
+
const index = ref(null);
|
|
580
|
+
const frames = ref([]);
|
|
581
|
+
const mix = ref(0);
|
|
582
|
+
const status = ref("idle");
|
|
583
|
+
const labels = ref([]);
|
|
584
|
+
let run = 0;
|
|
585
|
+
const meta = (dto, day) => ({
|
|
586
|
+
field: dto.field,
|
|
587
|
+
run: dto.run,
|
|
588
|
+
day,
|
|
589
|
+
width: dto.width,
|
|
590
|
+
height: dto.height,
|
|
591
|
+
lonMin: dto.lonMin,
|
|
592
|
+
latMax: dto.latMax,
|
|
593
|
+
step: dto.step,
|
|
594
|
+
scale: dto.scale,
|
|
595
|
+
offset: dto.offset,
|
|
596
|
+
unit: dto.unit
|
|
597
|
+
});
|
|
598
|
+
const load = async () => {
|
|
599
|
+
const ticket = ++run;
|
|
600
|
+
const { source, selection } = input;
|
|
601
|
+
const field = input.fields?.[0] ?? fieldFromLayer(selection.value.layerId);
|
|
602
|
+
const model = selection.value.model;
|
|
603
|
+
status.value = "loading";
|
|
604
|
+
let dto;
|
|
605
|
+
try {
|
|
606
|
+
const raw = await input.fetchJson(source.indexUrl(model, field));
|
|
607
|
+
if (!isIndex$1(raw)) throw new Error("field index is not an index");
|
|
608
|
+
dto = raw;
|
|
609
|
+
} catch (error) {
|
|
610
|
+
if (ticket !== run) return;
|
|
611
|
+
frames.value = [];
|
|
612
|
+
index.value = null;
|
|
613
|
+
labels.value = [];
|
|
614
|
+
mix.value = 0;
|
|
615
|
+
status.value = isMissing(error) ? "empty" : "error";
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (ticket !== run) return;
|
|
619
|
+
index.value = dto;
|
|
620
|
+
labels.value = dto.days;
|
|
621
|
+
const at = Math.min(Math.max(selection.value.dayIndex, 0), dto.days.length - 1);
|
|
622
|
+
const day = dto.days[at];
|
|
623
|
+
if (day === void 0) {
|
|
624
|
+
frames.value = [];
|
|
625
|
+
status.value = "empty";
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
const nextDay = dto.days[at + 1] ?? day;
|
|
629
|
+
try {
|
|
630
|
+
const first = await input.fetchBytes(source.gridUrl(model, field, dto.run, day));
|
|
631
|
+
const second = nextDay === day ? first : await input.fetchBytes(source.gridUrl(model, field, dto.run, nextDay));
|
|
632
|
+
if (ticket !== run) return;
|
|
633
|
+
frames.value = [decodeGrid(first, meta(dto, day)), decodeGrid(second, meta(dto, nextDay))];
|
|
634
|
+
mix.value = 0;
|
|
635
|
+
status.value = "ready";
|
|
636
|
+
} catch (error) {
|
|
637
|
+
if (ticket !== run) return;
|
|
638
|
+
status.value = isMissing(error) ? "empty" : "error";
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
return {
|
|
642
|
+
index,
|
|
643
|
+
frames,
|
|
644
|
+
mix,
|
|
645
|
+
status,
|
|
646
|
+
labels,
|
|
647
|
+
load
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
// src/composables/use-globe-particles.ts
|
|
651
|
+
const isIndex = (value) => {
|
|
652
|
+
if (typeof value !== "object" || value === null) return false;
|
|
653
|
+
const candidate = value;
|
|
654
|
+
return typeof candidate.run === "string" && Array.isArray(candidate.days) && typeof candidate.width === "number" && typeof candidate.height === "number";
|
|
655
|
+
};
|
|
656
|
+
const dayOf = (dto, index) => {
|
|
657
|
+
const at = Math.min(Math.max(index, 0), dto.days.length - 1);
|
|
658
|
+
return dto.days[at] ?? null;
|
|
659
|
+
};
|
|
660
|
+
/**
|
|
661
|
+
* Loads the 10 m wind pair (`u` east, `v` north) for the selected day. A layer
|
|
662
|
+
* with no wind fields, or a run that published none, is `'empty'` — the globe
|
|
663
|
+
* then draws the field alone, never particles advected by zero wind.
|
|
664
|
+
*/
|
|
665
|
+
function useGlobeParticles(input) {
|
|
666
|
+
const wind = ref(null);
|
|
667
|
+
const status = ref("idle");
|
|
668
|
+
let run = 0;
|
|
669
|
+
const one = async (model, field, dayIndex) => {
|
|
670
|
+
const raw = await input.fetchJson(input.source.indexUrl(model, field));
|
|
671
|
+
if (!isIndex(raw)) throw new Error("wind index is not an index");
|
|
672
|
+
const day = dayOf(raw, dayIndex);
|
|
673
|
+
if (day === null) return null;
|
|
674
|
+
const bytes = await input.fetchBytes(input.source.gridUrl(model, field, raw.run, day));
|
|
675
|
+
return decodeGrid(bytes, {
|
|
676
|
+
field,
|
|
677
|
+
run: raw.run,
|
|
678
|
+
day,
|
|
679
|
+
width: raw.width,
|
|
680
|
+
height: raw.height,
|
|
681
|
+
lonMin: raw.lonMin,
|
|
682
|
+
latMax: raw.latMax,
|
|
683
|
+
step: raw.step,
|
|
684
|
+
scale: raw.scale,
|
|
685
|
+
offset: raw.offset,
|
|
686
|
+
unit: raw.unit
|
|
687
|
+
});
|
|
688
|
+
};
|
|
689
|
+
const load = async () => {
|
|
690
|
+
const ticket = ++run;
|
|
691
|
+
status.value = "loading";
|
|
692
|
+
const { model, dayIndex } = input.selection.value;
|
|
693
|
+
try {
|
|
694
|
+
const [u, v] = await Promise.all([one(model, "wind-u", dayIndex), one(model, "wind-v", dayIndex)]);
|
|
695
|
+
if (ticket !== run) return;
|
|
696
|
+
if (!u || !v) {
|
|
697
|
+
wind.value = null;
|
|
698
|
+
status.value = "empty";
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
wind.value = {
|
|
702
|
+
u,
|
|
703
|
+
v
|
|
704
|
+
};
|
|
705
|
+
status.value = "ready";
|
|
706
|
+
} catch (error) {
|
|
707
|
+
if (ticket !== run) return;
|
|
708
|
+
const missing = error?.status === 404;
|
|
709
|
+
status.value = missing ? "empty" : "error";
|
|
710
|
+
if (missing) wind.value = null;
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
return {
|
|
714
|
+
wind,
|
|
715
|
+
status,
|
|
716
|
+
load
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
// src/composables/use-globe-districts.ts
|
|
720
|
+
/**
|
|
721
|
+
* Decides whether a country's districts may be drawn at all. A locked country
|
|
722
|
+
* gets `null`, so the component never mounts a source it may not show: no
|
|
723
|
+
* district geometry reaches a browser that may not read it.
|
|
724
|
+
*/
|
|
725
|
+
function useGlobeDistricts(input) {
|
|
726
|
+
const dataset = input.dataset ?? "boundaries";
|
|
727
|
+
const layerFor = (country) => {
|
|
728
|
+
if (!input.unlocked) return null;
|
|
729
|
+
const key = country.trim().toLowerCase();
|
|
730
|
+
if (key === "") return null;
|
|
731
|
+
return {
|
|
732
|
+
id: `globe-districts-${key}`,
|
|
733
|
+
source: input.districtUrl(dataset, key)
|
|
734
|
+
};
|
|
735
|
+
};
|
|
736
|
+
return { layerFor };
|
|
737
|
+
}
|
|
738
|
+
export { DISTRICT_FADE, FIELD_FADE, FIELD_PALETTES, GlobeCanvas, GlobeLock, GlobeReadout, GlobeSkeleton, GlobeTimeSlider, OPEN_PITCH, OPEN_ZOOM, PARTICLE_CAP, RAIN_RAMP, ROTATE_DEGREES_PER_SECOND, TEMPERATURE_RAMP, WIND_RAMP, WORLD_CENTRE, districtOpacity, fieldOpacity, openingCamera, paletteFor, particleCount, shouldRotate, useGlobeDistricts, useGlobeFields, useGlobeMap, useGlobeParticles, useGlobeState };
|
|
739
|
+
|
|
740
|
+
//# sourceMappingURL=index.js.map
|