Navigation banner across the top (the next maneuver's arrow by Valhalla's type, distance, street, then…, lanes, the street being driven; maneuvers gain type/street/exit); the driving view (tilted to 3D) hides route numbers and minor roads' names, road labels further apart
CI / check (push) Successful in 22s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-30 20:06:44 +01:00
1 parent e62eb90dc7
commit 27313b3101
7 files changed
+144 -22

No files matched your search

+66 -12
View File
@@ -31,7 +31,8 @@ const REROUTE_EVERY_MS = 15000;
const ROAD_EVERY_MS = 4000; // /api/road at most this often, only while moving
const TRACE_STEP_M = 10; // fixes kept for /api/road at least this far apart
const LANES_AHEAD_M = 1000; // the next maneuver's lanes from this close
const DEFAULT_WIDGETS = { inclinometer: true, speed: true, navigation: true };
const DEFAULT_WIDGETS = { inclinometer: true, speed: true, navigation: true, banner: true };
const THEN_M = 300; // the maneuver after the next one shown with it when it follows this closely
const DEFAULT_OPACITY = 0.45; // the card's background, the map shows through
const load = () => {
@@ -43,6 +44,31 @@ const RAD = Math.PI / 180;
// meters between two [lon, lat] (equirectangular: fine at these distances)
const meters = ([lon1, lat1], [lon2, lat2]) => Math.hypot((lon2 - lon1) * Math.cos(((lat1 + lat2) / 2) * RAD), lat2 - lat1) * 111320;
const distance = (m) => (m >= 1000 ? `${(m / 1000).toFixed(1)} km` : `${Math.round(m / 10) * 10} m`);
const esc = (text) => String(text ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
// An arrow from the bottom center turning by `angle` (° from straight on),
// in a 24 × 32 box: a lane's, and the banner's maneuver.
function arrowPath(angle, cls) {
const a = (angle * Math.PI) / 180;
const [x, y] = [12 + 9 * Math.sin(a), 15 - 9 * Math.cos(a)];
const back = (side) => `${(x - 5 * Math.sin(a + side)).toFixed(1)} ${(y + 5 * Math.cos(a + side)).toFixed(1)}`;
return `<path class="${cls}" d="M12 30 V15 L${x.toFixed(1)} ${y.toFixed(1)} M${back(0.6)} L${x.toFixed(1)} ${y.toFixed(1)} L${back(-0.6)}"/>`;
}
const uturnPath = (cls) => `<path class="${cls}" d="M14 30 V14 A5 5 0 0 0 4 14 V20 M1 17 L4 21 L7 17"/>`;
// Valhalla's maneuver types (its API docs) → the banner's arrow angle;
// "uturn", "arrive", "roundabout" drawn their own way.
const MANEUVER_ANGLES = { 1: 0, 2: 45, 3: -45, 7: 0, 8: 0, 9: 45, 10: 90, 11: 135, 14: -135, 15: -90, 16: -45, 17: 0, 18: 45, 19: -45,
20: 45, 21: -45, 22: 0, 23: 30, 24: -30, 25: 0, 37: 30, 38: -30 };
function maneuverSvg(type) {
let body;
if (type === 12 || type === 13) body = uturnPath("dash-banner__arrow");
else if (type >= 4 && type <= 6) body = `<path class="dash-banner__arrow" d="M8 30 V4 M8 5 H19 L16 9 L19 13 H8"/>`; // a flag: arriving
else if (type === 26 || type === 27) body = `<circle class="dash-banner__arrow" cx="12" cy="14" r="6"/><path class="dash-banner__arrow" d="M12 30 V20 M12 8 V2 M9 5 L12 2 L15 5"/>`;
else body = arrowPath(MANEUVER_ANGLES[type] ?? 0, "dash-banner__arrow");
return `<svg viewBox="0 0 24 32" class="dash-banner__icon">${body}</svg>`;
}
// Valhalla's lane direction bits → the arrow's angle (° from straight on)
const LANE_ANGLES = { 2: 0, 16: -45, 8: -90, 4: -135, 32: 45, 64: 90, 128: 135, 512: -25, 1024: 25 };
const LANE_REVERSE = 256;
@@ -50,12 +76,8 @@ const LANE_REVERSE = 256;
// directions to take, a bitmask too) drawn bold.
function laneSvg({ directions, active = 0 }) {
const arrow = (bit) => {
const bold = active & bit ? " dash-lane__take" : "";
if (bit === LANE_REVERSE) return `<path class="dash-lane__arrow${bold}" d="M14 30 V14 A5 5 0 0 0 4 14 V20 M1 17 L4 21 L7 17"/>`;
const a = (LANE_ANGLES[bit] * Math.PI) / 180;
const [x, y] = [12 + 9 * Math.sin(a), 15 - 9 * Math.cos(a)];
const head = (side) => `L${(x - 5 * Math.sin(a + side)).toFixed(1)} ${(y + 5 * Math.cos(a + side)).toFixed(1)}`;
return `<path class="dash-lane__arrow${bold}" d="M12 30 V15 L${x.toFixed(1)} ${y.toFixed(1)} M${(x - 5 * Math.sin(a + 0.6)).toFixed(1)} ${(y + 5 * Math.cos(a + 0.6)).toFixed(1)} L${x.toFixed(1)} ${y.toFixed(1)} ${head(-0.6)}"/>`;
const cls = `dash-lane__arrow${active & bit ? " dash-lane__take" : ""}`;
return bit === LANE_REVERSE ? uturnPath(cls) : arrowPath(LANE_ANGLES[bit], cls);
};
const bits = [2, 16, 8, 4, 32, 64, 128, 512, 1024, LANE_REVERSE].filter((bit) => directions & bit);
return `<svg viewBox="0 0 24 32" class="dash-lane${active ? " dash-lane--go" : ""}">${(bits.length ? bits : [2]).map(arrow).join("")}</svg>`;
@@ -159,17 +181,22 @@ export class DashboardControl {
const current = limits.findLast(([km]) => km <= at);
const next = limits.find(([km, limit]) => km > at && limit !== current?.[1]);
const signal = signals.find(([, , km]) => km > at);
const maneuver = maneuvers.find((m) => m.at > at + 0.005);
const index = maneuvers.findIndex((m) => m.at > at + 0.005);
const maneuver = maneuvers[index];
const then = maneuvers[index + 1];
const onStreet = maneuvers[index - 1]?.street; // the one being driven: the last maneuver's
if (off <= ON_ROUTE_M) this._lastAt = at;
this._nav = {
limit: current?.[1] ?? null,
next: next && { limit: next[1], m: (next[0] - at) * 1000 },
signal: signal && (signal[2] - at) * 1000,
maneuver: maneuver && { text: maneuver.text, m: (maneuver.at - at) * 1000, lanes: maneuver.lanes },
maneuver: maneuver && { ...maneuver, m: (maneuver.at - at) * 1000 },
then: maneuver && then && then.at - maneuver.at <= THEN_M / 1000 ? then : null,
street: onStreet,
};
return;
}
this._nav = { limit: this._road?.speed_limit ?? null, offRoute: !!this._route };
this._nav = { limit: this._road?.speed_limit ?? null, offRoute: !!this._route, street: this._road?.name };
if (this._route && (speed ?? 0) >= MOVING_MS && Date.now() - this._rerouteAt >= REROUTE_EVERY_MS) {
this._rerouteAt = Date.now();
requestReroute(position, this._route.viaKm.filter((km) => km <= this._lastAt).length);
@@ -211,6 +238,7 @@ export class DashboardControl {
<label class="live-tracker__row"><input type="checkbox" data-widget="inclinometer"> Inclinometer &amp; compass</label>
<label class="live-tracker__row"><input type="checkbox" data-widget="speed"> Speed</label>
<label class="live-tracker__row"><input type="checkbox" data-widget="navigation"> Speed limit &amp; ahead (route)</label>
<label class="live-tracker__row"><input type="checkbox" data-widget="banner"> Navigation banner (next turn)</label>
<label class="live-tracker__row">Background <input type="range" class="dash-opacity" min="0" max="1" step="0.05"></label>
<button type="button" class="dash-sim">Simulate driving from a random place</button>
<p class="live-panel__status dash-sim-status" hidden></p>
@@ -281,6 +309,11 @@ export class DashboardControl {
this._card.style.setProperty("--dash-alpha", this._settings.opacity ?? DEFAULT_OPACITY);
this._card.innerHTML = `<div class="dash-nav"><div class="dash-limit"></div><div class="dash-ahead"></div></div>${INCLINOMETER}<div class="dash-speed"><span></span> km/h</div>`;
this._map.getContainer().appendChild(this._card);
// the next turn across the top, as in a car navigation
this._banner = document.createElement("div");
this._banner.className = "dash-banner";
this._banner.hidden = true;
this._map.getContainer().appendChild(this._banner);
this._status.hidden = false;
this._status.textContent = "Connecting…";
this._poll();
@@ -291,6 +324,8 @@ export class DashboardControl {
this._timer = null;
this._card?.remove();
this._card = null;
this._banner?.remove();
this._banner = null;
this._status.hidden = true;
}
@@ -330,6 +365,7 @@ export class DashboardControl {
nav.style.display = widgets.navigation ? "" : "none";
this._card.hidden = !widgets.inclinometer && !widgets.speed && !widgets.navigation;
this._renderNav(nav);
this._renderBanner();
const severity = (key, value) => (value == null ? "" : Math.abs(value) >= WARN[key] * 1.5 ? "dash-bad" : Math.abs(value) >= WARN[key] ? "dash-warn" : "");
for (const key of ["roll", "pitch"]) {
@@ -363,13 +399,31 @@ export class DashboardControl {
if (n?.offRoute) rows.push(`<div class="dash-offroute">Off the route — rerouting</div>`);
if (n?.next) rows.push(`<div><span class="dash-limit dash-limit--small${n.next.limit === "unlimited" ? " dash-limit--none" : ""}">${typeof n.next.limit === "number" ? n.next.limit : n.next.limit ? "" : "?"}</span> in ${distance(n.next.m)}</div>`);
if (n?.signal != null) rows.push(`<div><span class="dash-signal"></span> ${distance(n.signal)}</div>`);
if (n?.maneuver) {
if (n?.maneuver && !this._settings.widgets.banner) {
const { text, m, lanes } = n.maneuver;
rows.push(`<div class="dash-maneuver">${distance(m)} · ${text.replace(/[&<>]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" })[c])}</div>`);
rows.push(`<div class="dash-maneuver">${distance(m)} · ${esc(text)}</div>`);
if (lanes && m <= LANES_AHEAD_M) {
rows.push(`<div class="dash-lanes">${lanes.map(laneSvg).join("")}</div>`);
}
}
nav.querySelector(".dash-ahead").innerHTML = rows.join("");
}
// Across the top: the next maneuver's arrow, distance and street (its
// instruction when it has no street), "then …" when the one after follows
// closely, its lanes from LANES_AHEAD_M, and the street being driven.
_renderBanner() {
const n = this._nav;
const show = this._settings.widgets.banner && !!n?.maneuver;
this._banner.hidden = !show;
if (!show) return;
this._banner.style.setProperty("--dash-alpha", Math.max(0.8, this._settings.opacity ?? DEFAULT_OPACITY));
const { m, type, street, text, exit, lanes } = n.maneuver;
const what = street ? `${exit ? `${exit}. exit · ` : ""}${esc(street)}` : esc(text);
const then = n.then ? `<div class="dash-banner__then">then ${maneuverSvg(n.then.type)} ${esc(n.then.street || n.then.text)}</div>` : "";
const lanesRow = lanes && m <= LANES_AHEAD_M ? `<div class="dash-lanes dash-banner__lanes">${lanes.map(laneSvg).join("")}</div>` : "";
const onStreet = n.street ? `<div class="dash-banner__on">on ${esc(n.street)}</div>` : "";
this._banner.innerHTML = `<div class="dash-banner__main">${maneuverSvg(type)}<div><div class="dash-banner__distance">${distance(m)}</div>` +
`<div class="dash-banner__street">${what}</div></div></div>${lanesRow}${then}${onStreet}`;
}
}
+2 -1
View File
@@ -24,7 +24,7 @@ import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "
import { isLoggedIn } from "/js/auth.js";
import { DebugControl } from "/js/debug.js";
import { setupUpdate } from "/js/update.js";
import { patchOnlineStyle, patternImage, PATTERN_IDS, NightMap } from "/js/map_themes.js";
import { patchOnlineStyle, patternImage, PATTERN_IDS, NightMap, declutterWhenDriving } 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";
@@ -485,6 +485,7 @@ 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 }) => {
+29 -5
View File
@@ -8,19 +8,43 @@
// map.js styles the map with it and /symbols.html draws its legend from the
// same patched style, so the two can't drift apart.
// 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.
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();
});
}
export function patchOnlineStyle(style) {
return insertTrackOnlyLayer(protectedAreasAndTextures(roadLabelSpacing(parkTilesStyle(style))));
}
// Road names and route numbers repeat every symbol-spacing *pixels*, so
// zoomed in close (z22) a street got its name every few meters. Past
// z16 the spacing grows with the zoom: the same distance on the ground
// (~400 m here) at every zoom.
const ROAD_LABEL_FIXED_ZOOM = 16;
// zoomed in close (z22) a street got its name every few meters. 1.5×
// Liberty's spacing, and past z15 it grows with the zoom: the same
// distance on the ground (~900 m) at every zoom.
const ROAD_LABEL_FIXED_ZOOM = 15;
const ROAD_LABEL_SPACING = 1.5;
function roadLabelSpacing(style) {
for (const layer of style.layers) {
if (layer.type !== "symbol" || layer["source-layer"] !== "transportation_name") continue;
const spacing = layer.layout?.["symbol-spacing"] ?? 250; // the style spec's default
const spacing = (layer.layout?.["symbol-spacing"] ?? 250) * ROAD_LABEL_SPACING; // 250: the style spec's default
layer.layout = { ...layer.layout, "symbol-spacing": ["interpolate", ["exponential", 2], ["zoom"], ROAD_LABEL_FIXED_ZOOM, spacing, 22, spacing * 64] };
}
return style;