diff --git a/public/js/gpx.js b/public/js/gpx.js index fd79fad..93c6514 100644 --- a/public/js/gpx.js +++ b/public/js/gpx.js @@ -20,6 +20,11 @@ const UPLOAD_URL = "/api/library/import"; const FOLDER_URL = "/api/library/folder"; const MOVE_URL = "/api/library/move"; const TRACK_COLOR = "#e63946"; +const TRACK_CASING = "#1d2a1f"; +const ARROW_IMAGE = "track-arrow"; +// Tracks without a color of their own get one of these, fixed per file, +// so several shown together are told apart. +const TRACK_PALETTE = ["#5a55e0", "#7cc957", "#e0a32e", "#9058d6", "#e63946", "#2aa7a0"]; const POI_ICON_KEYS = Object.keys(POI_ICONS); const POI_ICON_IMAGE_SIZE = 48; // rasterized once at this size, rendered much smaller by icon-size below const POI_ICON_SIZE = 0.3; // 48 * 0.3 = ~14px, sized to sit inside the marker badge below @@ -608,15 +613,42 @@ export class GpxPanelControl { // Excludes category:"Note" — a saved Measure-tool distance line gets // its own layer below (popup-on-click, not this hover-summary/profile- // panel track behavior), not this one. + // Tracks: a dark casing under the colored line, direction arrows along + // it and a dot at each end. + const trackFilter = ["all", ["!=", ["get", "category"], "Note"], ["!=", ["geometry-type"], "Polygon"]]; // also true without a category (null) + this._map.addLayer({ + id: `${linesId}-casing`, + type: "line", + source: linesId, + filter: trackFilter, + layout: { "line-cap": "round", "line-join": "round" }, + paint: { "line-color": TRACK_CASING, "line-width": ["interpolate", ["linear"], ["zoom"], 8, 4, 14, 8] }, + }); this._map.addLayer({ id: `${linesId}-line`, type: "line", source: linesId, - filter: ["!=", ["get", "category"], "Note"], // also true without a category (null) + filter: trackFilter, layout: { "line-cap": "round", "line-join": "round" }, - paint: { "line-color": ["coalesce", ["get", "color"], TRACK_COLOR], "line-width": 3 }, + paint: { "line-color": ["coalesce", ["get", "color"], defaultTrackColor(path)], "line-width": ["interpolate", ["linear"], ["zoom"], 8, 2, 14, 5] }, }); this._registerLineInteractivity(`${linesId}-line`); + if (!this._map.hasImage(ARROW_IMAGE)) this._map.addImage(ARROW_IMAGE, arrowImage(), { pixelRatio: 2 }); + this._map.addLayer({ + id: `${linesId}-arrows`, + type: "symbol", + source: linesId, + filter: trackFilter, + minzoom: 10, + layout: { "symbol-placement": "line", "symbol-spacing": 160, "icon-image": ARROW_IMAGE, "icon-rotation-alignment": "map", "icon-allow-overlap": true, "icon-ignore-placement": true }, + }); + this._map.addSource(`${linesId}-ends`, { type: "geojson", data: lineEnds(lines) }); + this._map.addLayer({ + id: `${linesId}-ends`, + type: "circle", + source: `${linesId}-ends`, + paint: { "circle-radius": ["interpolate", ["linear"], ["zoom"], 8, 4, 14, 7], "circle-color": TRACK_CASING, "circle-stroke-color": "#fff", "circle-stroke-width": 2 }, + }); // Polygon/line notes (see notes.js and measure.js's "Save as note") — // the same "lines" source already only renders LineString geometry @@ -908,7 +940,10 @@ export class GpxPanelControl { const linesId = linesSourceId(path); const pointsId = pointsSourceId(path); [ + `${linesId}-casing`, `${linesId}-line`, + `${linesId}-arrows`, + `${linesId}-ends`, `${linesId}-polygon-fill`, `${linesId}-polygon-outline`, `${linesId}-note-line`, @@ -920,6 +955,7 @@ export class GpxPanelControl { if (this._map.getLayer(layerId)) this._map.removeLayer(layerId); }); if (this._map.getSource(linesId)) this._map.removeSource(linesId); + if (this._map.getSource(`${linesId}-ends`)) this._map.removeSource(`${linesId}-ends`); if (this._map.getSource(pointsId)) this._map.removeSource(pointsId); this._pointsByPath.delete(path); @@ -1157,7 +1193,7 @@ export class GpxPanelControl { this._colorInput.addEventListener("change", () => this._patchNode(this._colorInput.dataset.path, { color: this._colorInput.value })); } this._colorInput.dataset.path = file.path; - this._colorInput.value = file.color || TRACK_COLOR; + this._colorInput.value = file.color || defaultTrackColor(file.path); this._colorInput.click(); } @@ -1241,6 +1277,48 @@ function uploadFile(formData, onUploadComplete) { }); } +function defaultTrackColor(path) { + let hash = 0; + for (const ch of String(path)) hash = (hash * 31 + ch.charCodeAt(0)) | 0; + return TRACK_PALETTE[Math.abs(hash) % TRACK_PALETTE.length]; +} + +// Start and end of every track/route line, for the end dots. +function lineEnds(lines) { + const features = []; + for (const f of lines.features) { + if (f.properties?.category === "Note") continue; + const parts = f.geometry.type === "LineString" ? [f.geometry.coordinates] : f.geometry.type === "MultiLineString" ? f.geometry.coordinates : []; + const coords = parts.flat(); + if (coords.length < 2) continue; + for (const c of [coords[0], coords.at(-1)]) features.push({ type: "Feature", geometry: { type: "Point", coordinates: c.slice(0, 2) }, properties: {} }); + } + return { type: "FeatureCollection", features }; +} + +// A white pill with a chevron pointing along the line (drawn at 2x). +function arrowImage() { + const [w, h] = [36, 22]; + const ctx = Object.assign(document.createElement("canvas"), { width: w, height: h }).getContext("2d"); + ctx.beginPath(); + ctx.roundRect(1.5, 1.5, w - 3, h - 3, (h - 3) / 2); + ctx.fillStyle = "#fff"; + ctx.fill(); + ctx.lineWidth = 2.5; + ctx.strokeStyle = TRACK_CASING; + ctx.stroke(); + ctx.beginPath(); + ctx.moveTo(12, 11); + ctx.lineTo(24, 11); + ctx.moveTo(19, 6); + ctx.lineTo(24, 11); + ctx.lineTo(19, 16); + ctx.lineCap = ctx.lineJoin = "round"; + ctx.lineWidth = 2.5; + ctx.stroke(); + return ctx.getImageData(0, 0, w, h); +} + // Rasterizes a hand-authored SVG icon to ImageData for map.addImage(), at a // fixed square size (the source SVGs have no explicit width/height, just a // viewBox, so one is injected — otherwise an falls back to the CSS