All files / geography/maps/src vehicleLayer.ts

91.53% Statements 173/189
87.05% Branches 121/139
92.59% Functions 25/27
95.51% Lines 149/156

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335              13x 13x 13x 13x                                               80x 80x 80x 80x 80x 80x 16x 16x 16x 16x 16x 16x 16x 16x 16x   64x 64x 64x 64x 64x   80x       44x 132x 132x   44x                 15x   15x 15x 15x   15x       15x     45x 22x 22x 22x 3x 3x   19x 19x   45x 45x                             20x 8x 8x 8x 8x 8x       1x     2x 1x         64x     44x 44x 44x                                 48x 48x 48x       52x 52x       36x 36x 36x 36x 9x 9x   46x 15x 21x   1x         2x 2x 2x 2x 2x         1x       16x 16x 16x 32x 64x 64x     16x 16x 16x 16x             16x                 16x               16x                       16x 16x               64x 16x 80x       1x 1x 1x       8x 8x 8x       8x     15x             15x 15x 15x 15x 15x 15x 15x   15x     13x 13x 2x 2x 2x   11x 11x 11x 11x 11x 11x     31x 31x 31x 41x 41x 39x 39x 39x   41x   41x   31x 31x 31x     18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x        
import { faBus, faTrain } from "@fortawesome/free-solid-svg-icons";
import { easeOutCubic, vehiclePoint } from "@overworld/routing";
import { FeatureCollection, Point } from "geojson";
import { CameraOptions, GeoJSONSource, Map as MapboxMap, MapboxEvent } from "mapbox-gl";
import type { VehiclePosition } from "@overworld/trimet-client";
import type { createVehicleTooltips } from "./vehicleTooltip";
 
const SOURCE = "nearby-vehicles";
const LAYERS = ["nearby-vehicle-badges", "nearby-vehicle-heading", "nearby-vehicle-icons", "nearby-vehicle-labels"];
const IMAGES = ["nearby-vehicle-bus-light", "nearby-vehicle-rail-light", "nearby-vehicle-bus-dark", "nearby-vehicle-rail-dark", "nearby-vehicle-arrow"];
const DURATION_MS = 1000;
 
export type VehicleFollowView = "north-up" | "chase";
 
export type VehicleTooltipFactory = (
  map: MapboxMap,
  layer: string,
  find: (id: string) => { vehicle: VehiclePosition; point: [number, number] } | undefined,
  onFollow: (vehicle: VehiclePosition) => void
) => ReturnType<typeof createVehicleTooltips>;
 
export interface VehicleLayerOptions {
  routeDisplay: Readonly<Record<number, string>>;
  createTooltips: VehicleTooltipFactory;
}
 
interface MovingVehicle {
  vehicle: VehiclePosition;
  from: [number, number];
  to: [number, number];
  started: number;
}
 
function vehicleImage(kind: "bus" | "rail" | "arrow", dark = false) {
  const canvas = document.createElement("canvas");
  canvas.width = canvas.height = 64;
  const context = canvas.getContext("2d");
  Iif (!context) throw new Error("Vehicle symbols could not be drawn.");
  context.fillStyle = dark ? "#000000" : "#ffffff";
  if (kind === "arrow") {
    context.strokeStyle = "#173a54";
    context.lineWidth = 2;
    context.beginPath();
    context.moveTo(32, 0);
    context.lineTo(39, 12);
    context.lineTo(25, 12);
    context.closePath();
    context.fill();
    context.stroke();
  } else {
    const [width, height, , , paths] = (kind === "bus" ? faBus : faTrain).icon;
    const scale = 36 / Math.max(width, height);
    context.translate((64 - width * scale) / 2, (64 - height * scale) / 2);
    context.scale(scale, scale);
    for (const path of typeof paths === "string" ? [paths] : paths) context.fill(new Path2D(path));
  }
  return context.getImageData(0, 0, 64, 64);
}
 
function glyphTone(color: string) {
  const channels = [1, 3, 5].map(offset => {
    const channel = parseInt(color.slice(offset, offset + 2), 16) / 255;
    return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
  });
  return channels[0] * 0.2126 + channels[1] * 0.7152 + channels[2] * 0.0722 > 0.179 ? "dark" : "light";
}
 
export function createVehicleLayer(
  map: MapboxMap,
  onError: (failed: boolean) => void,
  onFollow: (vehicle: VehiclePosition) => void,
  { routeDisplay, createTooltips }: VehicleLayerOptions
) {
  let positions = new Map<string, MovingVehicle>();
  let frame: number | undefined;
  let lastFrame = 0;
  let removed = false;
  let disposed = false;
  let following: Pick<VehiclePosition, "id" | "tripId"> | undefined;
  let followView: VehicleFollowView = "north-up";
  let savedCamera: CameraOptions | undefined;
  let northUpZoom: number;
  let lastHeading: number | undefined;
  const motion = window.matchMedia("(prefers-reduced-motion: reduce)");
 
  function trackCamera() {
    if (!following || removed || disposed) return;
    try {
      const target = positions.get(following.id);
      if (!target || !following.tripId || target.vehicle.tripId !== following.tripId || document.hidden) {
        map.stop();
        return;
      }
      const bearing = map.getBearing();
      lastHeading = target.vehicle.bearing ?? lastHeading ?? bearing;
      // Take the short turn across north rather than spinning through 360 degrees.
      const turn = (lastHeading - bearing + 540) % 360 - 180;
      map.easeTo({
        center: target.to,
        bearing: followView === "chase" ? bearing + turn : 0,
        pitch: followView === "chase" ? 60 : 0,
        zoom: followView === "chase" ? 17 : northUpZoom,
        offset: [0, followView === "chase" ? map.getContainer().clientHeight * 0.18 : 0],
        duration: motion.matches ? 0 : DURATION_MS,
        easing: easeOutCubic,
      });
    } catch {
      onError(true);
    }
  }
 
  function restoreCamera() {
    if (!savedCamera) return;
    const camera = savedCamera;
    savedCamera = undefined;
    lastHeading = undefined;
    Eif (!removed) {
      try { map.stop(); map.jumpTo(camera); } catch { onError(true); }
    }
  }
 
  function resized() { trackCamera(); }
 
  function userZoomed(event: MapboxEvent) {
    if (following && followView === "north-up" && "originalEvent" in event && event.originalEvent) {
      northUpZoom = map.getZoom();
    }
  }
 
  function data(now: number): FeatureCollection<Point> {
    return {
      type: "FeatureCollection",
      features: Array.from(positions.values()).map(moving => {
        const color = moving.vehicle.color || "#1559d1";
        const tone = glyphTone(color);
        return {
        type: "Feature", id: moving.vehicle.id,
        geometry: { type: "Point", coordinates: vehiclePoint(moving, now, DURATION_MS) },
        properties: {
          mode: moving.vehicle.type,
          ...(moving.vehicle.bearing === undefined ? {} : { bearing: moving.vehicle.bearing }),
          route: moving.vehicle.routeNumber
            ? routeDisplay[moving.vehicle.routeNumber] || String(moving.vehicle.routeNumber) : "",
          color,
          glyph: `nearby-vehicle-${moving.vehicle.type}-${tone}`,
          foreground: tone === "dark" ? "#000000" : "#ffffff",
        },
      }; }),
    };
  }
 
  function draw(now: number) {
    const source = map.getSource(SOURCE) as GeoJSONSource | undefined;
    source?.setData(data(now));
    tooltip.refresh();
  }
 
  function stop() {
    if (frame !== undefined) cancelAnimationFrame(frame);
    frame = undefined;
  }
 
  function animate(now: number) {
    frame = undefined;
    Iif (disposed || removed || document.hidden) return;
    try {
      if (now - lastFrame >= 1000 / 30) {
        draw(now);
        lastFrame = now;
      }
      if (Array.from(positions.values()).some(position => now - position.started < DURATION_MS)) {
        frame = requestAnimationFrame(animate);
      } else draw(now);
    } catch {
      onError(true);
    }
  }
 
  function settle() {
    stop();
    positions.forEach(moving => { moving.from = moving.to; });
    Eif (!removed && !disposed) {
      try { draw(performance.now()); } catch { onError(true); }
      trackCamera();
    }
  }
 
  function visibilityChanged() {
    Eif (document.hidden) { tooltip.hide(); settle(); }
  }
 
  function install() {
    Iif (removed || disposed) return;
    try {
      for (const kind of ["bus", "rail"] as const) {
        for (const tone of ["light", "dark"] as const) {
          const id = `nearby-vehicle-${kind}-${tone}`;
          Eif (!map.hasImage(id)) map.addImage(id, vehicleImage(kind, tone === "dark"), { pixelRatio: 2 });
        }
      }
      Eif (!map.hasImage(IMAGES[4])) map.addImage(IMAGES[4], vehicleImage("arrow"), { pixelRatio: 2 });
      Eif (!map.getSource(SOURCE)) map.addSource(SOURCE, { type: "geojson", data: data(performance.now()) });
      Eif (!map.getLayer(LAYERS[0])) {
        map.addLayer({
          id: LAYERS[0], type: "circle", source: SOURCE, slot: "top",
          paint: {
            "circle-radius": 12, "circle-color": ["get", "color"],
            "circle-stroke-color": "#ffffff", "circle-stroke-width": 2, "circle-emissive-strength": 1,
          },
        });
        map.addLayer({
          id: LAYERS[1], type: "symbol", source: SOURCE, slot: "top",
          filter: ["has", "bearing"],
          layout: {
            "icon-image": IMAGES[4], "icon-rotate": ["get", "bearing"],
            "icon-rotation-alignment": "map", "icon-allow-overlap": true, "icon-ignore-placement": true,
          },
          paint: { "icon-emissive-strength": 1 },
        });
        map.addLayer({
          id: LAYERS[2], type: "symbol", source: SOURCE, slot: "top",
          layout: {
            "icon-image": ["get", "glyph"],
            "icon-allow-overlap": true, "icon-ignore-placement": true,
          },
          paint: { "icon-emissive-strength": 1 },
        });
        map.addLayer({
          id: LAYERS[3], type: "symbol", source: SOURCE, slot: "top", minzoom: 13,
          layout: {
            "text-field": ["get", "route"], "text-size": 12, "text-anchor": "top",
            "text-offset": [0, 1.4], "text-padding": 5,
          },
          paint: {
            "text-color": ["get", "color"], "text-halo-color": ["get", "foreground"],
            "text-halo-width": 2, "text-emissive-strength": 1,
          },
        });
      }
      draw(performance.now());
      onError(false);
    } catch {
      clearLayers();
      onError(true);
    }
  }
 
  function clearLayers() {
    for (const layer of [...LAYERS].reverse()) if (map.getLayer(layer)) map.removeLayer(layer);
    if (map.getSource(SOURCE)) map.removeSource(SOURCE);
    for (const image of IMAGES) if (map.hasImage(image)) map.removeImage(image);
  }
 
  function mapRemoved() {
    tooltip.dispose();
    removed = true;
    stop();
  }
 
  function captureCamera() {
    Iif (savedCamera) return;
    const center = map.getCenter();
    savedCamera = {
      center: [center.lng, center.lat], bearing: map.getBearing(),
      pitch: map.getPitch(), zoom: map.getZoom(),
    };
    northUpZoom = map.getZoom();
  }
 
  const tooltip = createTooltips(map, LAYERS[0], id => {
    const moving = positions.get(id);
    return moving && { vehicle: moving.vehicle, point: vehiclePoint(moving, performance.now(), DURATION_MS) };
  }, vehicle => {
    captureCamera();
    onFollow(vehicle);
  });
  map.on("style.load", install);
  map.on("remove", mapRemoved);
  map.on("resize", resized);
  map.on("zoomend", userZoomed);
  document.addEventListener("visibilitychange", visibilityChanged);
  motion.addEventListener("change", settle);
  install();
 
  return {
    openDetails: tooltip.openDetails,
    follow(selection?: Pick<VehiclePosition, "id" | "tripId">, view: VehicleFollowView = "north-up") {
      Iif (removed || disposed) return;
      if (!selection) {
        following = undefined;
        restoreCamera();
        return;
      }
      if (!following) captureCamera();
      if (following?.id !== selection.id || following?.tripId !== selection.tripId) lastHeading = undefined;
      if (view === "chase" && followView !== "chase") northUpZoom = map.getZoom();
      following = selection;
      followView = view;
      trackCamera();
    },
    update(vehicles: VehiclePosition[]) {
      Iif (disposed || removed) return;
      const now = performance.now();
      positions = new Map<string, MovingVehicle>(vehicles.map(vehicle => {
        const previous = positions.get(vehicle.id);
        if (previous && previous.vehicle.time > vehicle.time) return [vehicle.id, previous];
        Iif (previous && previous.vehicle.time === vehicle.time) return [vehicle.id, { ...previous, vehicle }];
        const to: [number, number] = [vehicle.longitude, vehicle.latitude];
        const from = previous ? vehiclePoint(previous, now, DURATION_MS) : to;
        // Large jumps and resumed tabs must not suggest a route we have not observed.
        const snap = !previous || motion.matches || document.hidden ||
          Math.hypot((from[0] - to[0]) * Math.cos(to[1] * Math.PI / 180), from[1] - to[1]) > 0.01;
        return [vehicle.id, { vehicle, from: snap ? to : from, to, started: snap ? now - DURATION_MS : now }];
      }));
      stop();
      animate(now);
      trackCamera();
    },
    dispose() {
      disposed = true;
      tooltip.dispose();
      stop();
      restoreCamera();
      positions.clear();
      map.off("style.load", install);
      map.off("remove", mapRemoved);
      map.off("resize", resized);
      map.off("zoomend", userZoomed);
      document.removeEventListener("visibilitychange", visibilityChanged);
      motion.removeEventListener("change", settle);
      if (!removed) clearLayers();
    },
  };
}