diff --git a/public/js/map.js b/public/js/map.js index 7c3e0b0..047a66a 100644 --- a/public/js/map.js +++ b/public/js/map.js @@ -23,7 +23,7 @@ import { getPosition } from "/vendor/suncalc/suncalc.js"; import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "/js/panels.js"; import { isLoggedIn } from "/js/auth.js"; import { DebugControl } from "/js/debug.js"; -import { patchOnlineStyle, patternImage, NightMap } from "/js/map_themes.js"; +import { patchOnlineStyle, patternImage, PATTERN_IDS, NightMap } from "/js/map_themes.js"; import { offerSharedFiles } from "/js/share_import.js"; import HomeButton from "/vendor/npmap-home-button/home-button.esm.js"; import OverviewMap from "/vendor/npmap-overview/overview.esm.js"; @@ -473,8 +473,20 @@ setInterval(updateSun, 60000); const NIGHT_MODES = { auto: ["◐", "Night mode: automatic (after sunset)"], on: ["☾", "Night mode: on"], off: ["☀", "Night mode: off"] }; const nightButton = document.querySelector(".topbar__night"); const nightMap = new NightMap(map); -// The base style's texture patterns (forest, grass, military hatch) are -// drawn on demand rather than shipped in a sprite sheet. +// The base style's texture patterns (grass, scrub, military hatch) and the +// peak triangle are drawn at runtime rather than shipped in a sprite +// sheet, added with every style before its tiles ask for them: since +// MapLibre 6 an image added on styleimagemissing (or from the missing-image +// resolver) misses the tiles that asked for it. +const addPatterns = () => { + for (const id of PATTERN_IDS) { + const pattern = !map.hasImage(id) && patternImage(id); + if (pattern) map.addImage(id, pattern.image, pattern.options); + } +}; +map.on("style.load", addPatterns); +if (map.isStyleLoaded()) addPatterns(); +// Still the fallback for one asked for before that. map.on("styleimagemissing", ({ id }) => { const pattern = !map.hasImage(id) && patternImage(id); if (pattern) map.addImage(id, pattern.image, pattern.options); @@ -592,7 +604,7 @@ fetch("/api/terrain-tiles") fetch("/api/hiking-trails") .then((response) => response.json()) - .then(({ countries }) => overlaysControl.setHikingTrailsOfflineCountries(countries)) + .then(({ countries, bboxes }) => overlaysControl.setHikingTrailsOfflineCountries(countries, bboxes)) .catch(() => {}); fetch("/api/kiwix") diff --git a/public/js/map_themes.js b/public/js/map_themes.js index 97d41d1..95e6b4a 100644 --- a/public/js/map_themes.js +++ b/public/js/map_themes.js @@ -103,7 +103,7 @@ const NATIONAL_PARK = ["any", ...["nation", "národn", "narodn", "nemzeti", "naz // texture from zoom 13, scrub / dwarf pine its own bushes from 11 (no forest texture: OSM rarely says what kind, and // trees everywhere were too busy). // The pattern images are drawn at runtime (patternImage below, on -// MapLibre's styleimagemissing), so there's no sprite sheet to extend and +// map.js adds them with every style), so there's no sprite sheet to extend and // they work offline alike. // ponytail: same substring trick as NATIONAL_PARK, a word per language. const STRICT_RESERVE = ["any", ...["rezerv", "reserv", "wilderness", "wildnis", "pamiatk", "památk", "monument", "denkmal"].map((word) => ["in", word, ["coalesce", ["get", "class"], ""]])]; @@ -230,6 +230,8 @@ const PATTERNS = { military: { day: "rgba(192, 57, 43, 0.4)", night: "rgba(192, 57, 43, 0.35)" }, }; +export const PATTERN_IDS = Object.keys(PATTERNS).flatMap((name) => [`maptool-pattern-${name}`, `maptool-pattern-${name}-night`]); + // "maptool-pattern-[-night]" → { width, height, data } for // map.addImage, drawn on a canvas at 2× (pixelRatio 2); null for other ids. export function patternImage(id) {