// Synchronised MapLibre maps playing hourly frames, one panel per entry of `panels` in the payload. // Runs as gr.HTML js_on_load: `element` is the component, `props.value` the JSON payload from Python. const STYLE = "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"; const state = { maps: {}, ctx: {}, images: {}, labels: [], frame: 0, n: 0, timer: null, playing: true, key: null, token: 0 }; const $ = (sel) => element.querySelector(sel); const names = () => Array.from(element.querySelectorAll(".rm-map")).map((d) => d.dataset.map); function whenMaplibre() { return new Promise((resolve) => { const check = () => (window.maplibregl ? resolve(window.maplibregl) : setTimeout(check, 50)); check(); }); } function loadImages(urls) { return Promise.all(urls.map((url) => new Promise((resolve, reject) => { const img = new Image(); img.onload = () => resolve(img); img.onerror = reject; img.src = url; }))); } async function createMap(name, view) { const maplibregl = await whenMaplibre(); const container = $(`.rm-map[data-map="${name}"]`); container.innerHTML = ""; const map = new maplibregl.Map({ container, style: STYLE, center: view.center, zoom: view.zoom, attributionControl: { compact: true }, dragRotate: false, pitchWithRotate: false }); map.touchZoomRotate.disableRotation(); map.addControl(new maplibregl.NavigationControl({ showCompass: false }), "top-right"); await new Promise((resolve) => map.on("load", resolve)); return map; } // (Re)create a panel's rain layer for frames of a given size: a canvas source must not be resized after it // is created (its texture keeps the first size and renders opaque). function rainLayer(name, width, height, corners) { const map = state.maps[name]; const ctx = state.ctx[name]; if (ctx && ctx.canvas.width === width && ctx.canvas.height === height && map.getSource("rain")) { map.getSource("rain").setCoordinates(corners); return; } if (map.getLayer("rain")) map.removeLayer("rain"); if (map.getSource("rain")) map.removeSource("rain"); const canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; state.ctx[name] = canvas.getContext("2d"); map.addSource("rain", { type: "canvas", canvas, animate: true, coordinates: corners }); // Under the place names, so labels stay readable. const firstLabel = map.getStyle().layers.find((l) => l.type === "symbol"); map.addLayer({ id: "rain", type: "raster", source: "rain", paint: { "raster-opacity": 0.88, "raster-resampling": "nearest", "raster-fade-duration": 0 } }, firstLabel ? firstLabel.id : undefined); } function syncAll(list) { let busy = false; for (const from of list) { from.on("move", () => { if (busy) return; busy = true; for (const to of list) if (to !== from) to.jumpTo({ center: from.getCenter(), zoom: from.getZoom() }); busy = false; }); } } async function ensureMaps(view) { const alive = (name) => state.maps[name] && $(`.rm-map[data-map="${name}"] canvas.maplibregl-canvas`); if (names().every(alive)) return; state.ctx = {}; for (const name of names()) state.maps[name] = await createMap(name, view); syncAll(names().map((n) => state.maps[n])); } function draw(i) { state.frame = i; for (const name of names()) { const img = (state.images[name] || [])[i]; const ctx = state.ctx[name]; if (!img || !ctx) continue; ctx.clearRect(0, 0, img.width, img.height); ctx.drawImage(img, 0, 0); } $(".rm-slider").value = i; $(".rm-time").textContent = state.labels[i] || ""; } function play(on) { state.playing = on && state.n > 1; $(".rm-play").textContent = state.playing ? "❚❚" : "▶"; $(".rm-play").setAttribute("aria-label", state.playing ? "Pause" : "Play"); clearInterval(state.timer); if (state.playing) state.timer = setInterval(() => draw((state.frame + 1) % state.n), Number($(".rm-speed").value)); } function legends(specs) { for (const spec of specs) { const box = $(`.rm-legend[data-for="${spec.target}"]`); if (!box) continue; if (spec.style === "bar") { const n = spec.colors.length; const swatches = spec.colors.map((c) => `
`).join(""); const ticks = spec.ticks.map((t, i) => `${t}`).join(""); const [left, mid, right] = spec.ends; box.innerHTML = `