diff --git a/public/js/map.js b/public/js/map.js index 7dbed48..da46df6 100644 --- a/public/js/map.js +++ b/public/js/map.js @@ -24,7 +24,7 @@ import { addRoutePoint, setupSidePanelModes } from "/js/panels.js"; import { isLoggedIn } from "/js/auth.js"; import { DebugControl } from "/js/debug.js"; import { setupUpdate } from "/js/update.js"; -import { patchOnlineStyle, patternImage, PATTERN_IDS, NightMap, declutterWhenDriving } 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"; @@ -438,7 +438,6 @@ const addPatterns = () => { } }; map.on("style.load", addPatterns); -declutterWhenDriving(map); if (map.isStyleLoaded()) addPatterns(); // Still the fallback for one asked for before that. map.on("styleimagemissing", ({ id }) => { diff --git a/public/js/map_themes.js b/public/js/map_themes.js index 716143b..1fd3441 100644 --- a/public/js/map_themes.js +++ b/public/js/map_themes.js @@ -11,28 +11,22 @@ // The driving view (the map tilted to 3D, as following does): route // numbers and minor roads' / paths' names hidden, the main roads' names // left — what's needed at a glance. Back as soon as the map is flat. +// A ["pitch"] in the layers' filters (MapLibre ≥ 6.21), so they go already +// while tilting, and come back with every style without a listener. const DRIVE_HIDDEN_LABELS = /^(highway-shield|road_shield|highway-name-(minor|path))/; const DRIVE_PITCH = 30; -export function declutterWhenDriving(map) { - let hidden = null; - const apply = () => { - const hide = map.getPitch() >= DRIVE_PITCH; - if (hide === hidden) return; - hidden = hide; - for (const layer of map.getStyle().layers) { - if (DRIVE_HIDDEN_LABELS.test(layer.id)) map.setLayoutProperty(layer.id, "visibility", hide ? "none" : "visible"); - } - }; - map.on("pitchend", apply); - map.on("style.load", () => { - hidden = null; - apply(); - }); +function declutterWhenDriving(style) { + for (const layer of style.layers) { + if (!DRIVE_HIDDEN_LABELS.test(layer.id)) continue; + const flat = ["<", ["pitch"], DRIVE_PITCH]; + layer.filter = layer.filter ? ["all", flat, layer.filter] : flat; + } + return style; } export function patchOnlineStyle(style) { - return insertTrackOnlyLayer(protectedAreasAndTextures(roadLabelSpacing(parkTilesStyle(style)))); + return insertTrackOnlyLayer(protectedAreasAndTextures(roadLabelSpacing(declutterWhenDriving(parkTilesStyle(style))))); } // Road names and route numbers repeat every symbol-spacing *pixels*, so