drone-path-engine 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
1
  import type { LeafletApi, Scene, SceneData } from './scene.js';
2
+ import type { TrackPickHandlers } from './trackpicker.js';
2
3
  export declare function bootScene(host: HTMLElement, data: SceneData, L: LeafletApi, opts?: {
3
4
  hudHost?: HTMLElement;
5
+ trackPick?: TrackPickHandlers;
4
6
  }): Scene;
@@ -1,6 +1,6 @@
1
1
  import { createScene } from './scene.js';
2
2
  export function bootScene(host, data, L, opts) {
3
- const scene = createScene(host, data, { L, hudHost: opts?.hudHost });
3
+ const scene = createScene(host, data, { L, hudHost: opts?.hudHost, trackPick: opts?.trackPick });
4
4
  let disposed = false;
5
5
  let rafId;
6
6
  const baseDispose = scene.dispose.bind(scene);
@@ -3,6 +3,7 @@ import type { LatLon } from 'drone-path-core/geo';
3
3
  import type { StripLabels } from './dom.js';
4
4
  import type * as Leaflet from 'leaflet';
5
5
  import type { FrameChannels } from 'drone-path-core/telemetry-math';
6
+ import type { TrackPickHandlers } from './trackpicker.js';
6
7
  export type LeafletApi = typeof Leaflet;
7
8
  export interface SceneData extends FrameChannels {
8
9
  style: MapStyle;
@@ -33,4 +34,5 @@ export interface Scene {
33
34
  export declare function createScene(host: HTMLElement, firstData: SceneData, deps: {
34
35
  L: LeafletApi;
35
36
  hudHost?: HTMLElement;
37
+ trackPick?: TrackPickHandlers;
36
38
  }): Scene;
@@ -3,6 +3,7 @@ import { droneMarkerMarkup, el, hudMarkup, mapMarkup, quietAttributionLinks, sce
3
3
  import { computeScale, metersPerPixel, scaleBarMeters } from './framemath.js';
4
4
  import { themeFor } from './theme.js';
5
5
  import { createTrailController } from './trail.js';
6
+ import { TrackPicker } from './trackpicker.js';
6
7
  function hudEq(a, b) {
7
8
  return (a.heading === b.heading && a.alt === b.alt && a.speed === b.speed);
8
9
  }
@@ -66,7 +67,8 @@ class SceneState {
66
67
  externalHud;
67
68
  curIdx = 0;
68
69
  disposed = false;
69
- constructor(host, firstData, L, hudHost) {
70
+ picker = null;
71
+ constructor(host, firstData, L, hudHost, trackPick) {
70
72
  this.host = host;
71
73
  this.data = firstData;
72
74
  this.L = L;
@@ -110,6 +112,17 @@ class SceneState {
110
112
  this.layoutMap();
111
113
  this.decor = this.buildDecor();
112
114
  this.rebuildHud();
115
+ if (trackPick) {
116
+ this.picker = new TrackPicker({
117
+ L,
118
+ map: this.map,
119
+ surface: this.frameEl,
120
+ handlers: trackPick,
121
+ latlngs: () => this.latlngs,
122
+ playhead: () => this.curIdx,
123
+ look: () => this.trackPickLook(),
124
+ });
125
+ }
113
126
  this.ready = new Promise((resolve) => {
114
127
  const markReady = () => {
115
128
  clearTimeout(this.readyTimer);
@@ -124,6 +137,15 @@ class SceneState {
124
137
  scale() {
125
138
  return computeScale(this.data.width, this.data.height);
126
139
  }
140
+ trackPickLook() {
141
+ const s = this.scale();
142
+ return {
143
+ hitRadius: Math.max(10, Math.round(12 * s)),
144
+ markerRadius: Math.round(6 * s),
145
+ markerWeight: Math.max(2, Math.round(3 * s)),
146
+ color: themeFor(this.data.style, this.data.trailColor).core,
147
+ };
148
+ }
127
149
  makeTileLayer(tileUrl, style) {
128
150
  return this.L.tileLayer(tileUrl, {
129
151
  maxZoom: TILE_SOURCES[style].maxZoom,
@@ -168,6 +190,7 @@ class SceneState {
168
190
  this.scalelineEl.style.width = Math.round(bestM / mPerPx) + 'px';
169
191
  this.scaletextEl.textContent = bestM >= 1000 ? bestM / 1000 + ' km' : bestM + ' m';
170
192
  this.layout = { vw, vh, bigW, bigH, c0, bestM, mPerPx };
193
+ this.picker?.invalidate();
171
194
  }
172
195
  buildGhostLayers(theme, ghostLatlngs, ghostDash) {
173
196
  const s = this.scale();
@@ -278,6 +301,7 @@ class SceneState {
278
301
  for (const layer of this.decor.layers)
279
302
  layer.remove();
280
303
  this.decor = this.buildDecor();
304
+ this.picker?.invalidate();
281
305
  }
282
306
  rebuildHud() {
283
307
  // Scoped to the chip container so an external strip and a second scene
@@ -361,6 +385,7 @@ class SceneState {
361
385
  this.decor.drone.setLatLng(this.latlngs[i]);
362
386
  this.updateTransform(i);
363
387
  this.updateHud(i);
388
+ this.picker?.refresh();
364
389
  // Canvas painting is deferred to requestAnimationFrame
365
390
  return new Promise((resolve) => {
366
391
  requestAnimationFrame(() => {
@@ -415,11 +440,12 @@ class SceneState {
415
440
  clearTimeout(this.readyTimer);
416
441
  this.readyTimer = undefined;
417
442
  this.settleReady();
443
+ this.picker?.dispose();
418
444
  this.map.remove();
419
445
  this.styleEl.remove();
420
446
  this.host.classList.remove(this.scopeClass);
421
447
  }
422
448
  }
423
449
  export function createScene(host, firstData, deps) {
424
- return new SceneState(host, firstData, deps.L, deps.hudHost);
450
+ return new SceneState(host, firstData, deps.L, deps.hudHost, deps.trackPick);
425
451
  }
@@ -0,0 +1,13 @@
1
+ export interface ScreenPoint {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ /**
6
+ * The frame a pointer at `target` picks on a track drawn through `points` (one
7
+ * per frame, joined by straight segments), or null when the track does not come
8
+ * within `radius`. Each pass of the flight near the pointer picks its nearest
9
+ * frame, the earliest on ties. Where the path crosses itself, the passes about as
10
+ * near as the nearest one resolve to the one closest in time to `playhead`, the
11
+ * earlier on ties.
12
+ */
13
+ export declare function pickTrackFrame(points: readonly ScreenPoint[], target: ScreenPoint, radius: number, playhead: number): number | null;
@@ -0,0 +1,72 @@
1
+ /** Distance differences below this are treated as noise: a pass must move this much
2
+ away and back to end, and passes this close to the nearest one count as the same
3
+ hit, so a crossing resolves by time rather than by a sub-pixel difference. */
4
+ const PASS_SLACK_PX = 3;
5
+ /** The nearest spot on the segment from frame `i` to `i + 1`, attributed to the nearer frame. */
6
+ function segmentSample(points, i, target) {
7
+ const a = points[i];
8
+ const b = points[i + 1];
9
+ const vx = b.x - a.x;
10
+ const vy = b.y - a.y;
11
+ const len2 = vx * vx + vy * vy;
12
+ const t = len2 === 0 ? 0 : Math.min(1, Math.max(0, ((target.x - a.x) * vx + (target.y - a.y) * vy) / len2));
13
+ return { frame: t <= 0.5 ? i : i + 1, dist: Math.hypot(a.x + t * vx - target.x, a.y + t * vy - target.y) };
14
+ }
15
+ /** The track as seen from `target`: each frame, then the segment drawn to the next one. */
16
+ function* samples(points, target) {
17
+ for (let i = 0; i < points.length; i++) {
18
+ yield { frame: i, dist: Math.hypot(points[i].x - target.x, points[i].y - target.y) };
19
+ if (i + 1 < points.length)
20
+ yield segmentSample(points, i, target);
21
+ }
22
+ }
23
+ /** Splits the track into passes near `target`: a pass ends when the track leaves the
24
+ radius, or clearly moves away and clearly comes back without leaving it (a tight
25
+ loop). Sub-pixel wobble along a straight trail never splits a pass. */
26
+ function passesWithin(points, target, radius) {
27
+ const passes = [];
28
+ let current = null;
29
+ // The farthest the track has strayed since the current pass's nearest sample.
30
+ let peak = 0;
31
+ for (const { frame, dist } of samples(points, target)) {
32
+ if (dist > radius) {
33
+ current = null;
34
+ }
35
+ else if (current === null || (peak > current.dist + PASS_SLACK_PX && dist < peak - PASS_SLACK_PX)) {
36
+ current = { frame, dist };
37
+ passes.push(current);
38
+ peak = dist;
39
+ }
40
+ else if (dist < current.dist) {
41
+ current.frame = frame;
42
+ current.dist = dist;
43
+ peak = dist;
44
+ }
45
+ else {
46
+ peak = Math.max(peak, dist);
47
+ }
48
+ }
49
+ return passes;
50
+ }
51
+ /**
52
+ * The frame a pointer at `target` picks on a track drawn through `points` (one
53
+ * per frame, joined by straight segments), or null when the track does not come
54
+ * within `radius`. Each pass of the flight near the pointer picks its nearest
55
+ * frame, the earliest on ties. Where the path crosses itself, the passes about as
56
+ * near as the nearest one resolve to the one closest in time to `playhead`, the
57
+ * earlier on ties.
58
+ */
59
+ export function pickTrackFrame(points, target, radius, playhead) {
60
+ const passes = passesWithin(points, target, radius);
61
+ if (passes.length === 0)
62
+ return null;
63
+ const nearest = Math.min(...passes.map((pass) => pass.dist));
64
+ let best = null;
65
+ for (const pass of passes) {
66
+ if (pass.dist > nearest + PASS_SLACK_PX)
67
+ continue;
68
+ if (best === null || Math.abs(pass.frame - playhead) < Math.abs(best.frame - playhead))
69
+ best = pass;
70
+ }
71
+ return best === null ? null : best.frame;
72
+ }
@@ -0,0 +1,43 @@
1
+ import type * as Leaflet from 'leaflet';
2
+ export interface TrackPickHandlers {
3
+ /** The playback time shown beside the hovered point. */
4
+ label(frame: number): string;
5
+ seek(frame: number): void;
6
+ }
7
+ export interface TrackPickLook {
8
+ /** Pointer distance, in CSS pixels, within which the track counts as hit. */
9
+ hitRadius: number;
10
+ markerRadius: number;
11
+ markerWeight: number;
12
+ color: string;
13
+ }
14
+ export interface TrackPickerDeps {
15
+ L: typeof Leaflet;
16
+ map: Leaflet.Map;
17
+ /** The element that receives pointer input over the visible map. */
18
+ surface: HTMLElement;
19
+ handlers: TrackPickHandlers;
20
+ latlngs: () => readonly Leaflet.LatLngTuple[];
21
+ playhead: () => number;
22
+ look: () => TrackPickLook;
23
+ }
24
+ export declare class TrackPicker {
25
+ private readonly deps;
26
+ private points;
27
+ private pointer;
28
+ private press;
29
+ private hovered;
30
+ private marker;
31
+ constructor(deps: TrackPickerDeps);
32
+ /** Drops what depends on the map view or look; call after a re-layout or restyle. */
33
+ invalidate(): void;
34
+ /** Re-picks under a resting pointer once the map has moved beneath it. */
35
+ refresh(): void;
36
+ dispose(): void;
37
+ private readonly onMove;
38
+ private readonly onLeave;
39
+ private readonly onDown;
40
+ private readonly onClick;
41
+ private pickAt;
42
+ private show;
43
+ }
@@ -0,0 +1,101 @@
1
+ import { pickTrackFrame } from './trackpick.js';
2
+ /** A press that travels farther than this is a drag, not a click-to-seek. */
3
+ const CLICK_SLOP_PX = 4;
4
+ export class TrackPicker {
5
+ deps;
6
+ points = null;
7
+ pointer = null;
8
+ press = null;
9
+ hovered = null;
10
+ marker = null;
11
+ constructor(deps) {
12
+ this.deps = deps;
13
+ const { surface } = deps;
14
+ surface.addEventListener('pointermove', this.onMove);
15
+ surface.addEventListener('pointerleave', this.onLeave);
16
+ surface.addEventListener('pointerdown', this.onDown);
17
+ surface.addEventListener('click', this.onClick);
18
+ }
19
+ /** Drops what depends on the map view or look; call after a re-layout or restyle. */
20
+ invalidate() {
21
+ this.points = null;
22
+ this.marker?.remove();
23
+ this.marker = null;
24
+ this.hovered = null;
25
+ }
26
+ /** Re-picks under a resting pointer once the map has moved beneath it. */
27
+ refresh() {
28
+ if (this.pointer)
29
+ this.show(this.pickAt(this.pointer));
30
+ }
31
+ dispose() {
32
+ const { surface } = this.deps;
33
+ surface.removeEventListener('pointermove', this.onMove);
34
+ surface.removeEventListener('pointerleave', this.onLeave);
35
+ surface.removeEventListener('pointerdown', this.onDown);
36
+ surface.removeEventListener('click', this.onClick);
37
+ surface.style.cursor = '';
38
+ this.invalidate();
39
+ }
40
+ onMove = (event) => {
41
+ this.pointer = event;
42
+ this.show(this.pickAt(event));
43
+ };
44
+ onLeave = () => {
45
+ this.pointer = null;
46
+ this.press = null;
47
+ this.show(null);
48
+ };
49
+ onDown = (event) => {
50
+ this.press = event.button === 0 ? { x: event.clientX, y: event.clientY } : null;
51
+ };
52
+ onClick = (event) => {
53
+ const press = this.press;
54
+ this.press = null;
55
+ if (!press || Math.hypot(event.clientX - press.x, event.clientY - press.y) > CLICK_SLOP_PX)
56
+ return;
57
+ const frame = this.pickAt(event);
58
+ if (frame !== null)
59
+ this.deps.handlers.seek(frame);
60
+ };
61
+ pickAt(event) {
62
+ const { map, latlngs, playhead, look } = this.deps;
63
+ this.points ??= latlngs().map((ll) => map.latLngToContainerPoint(ll));
64
+ return pickTrackFrame(this.points, map.mouseEventToContainerPoint(event), look().hitRadius, playhead());
65
+ }
66
+ show(frame) {
67
+ const { surface } = this.deps;
68
+ surface.style.cursor = frame === null ? '' : 'pointer';
69
+ if (frame === this.hovered)
70
+ return;
71
+ this.hovered = frame;
72
+ if (frame === null) {
73
+ this.marker?.remove();
74
+ this.marker = null;
75
+ return;
76
+ }
77
+ const at = this.deps.latlngs()[frame];
78
+ const text = this.deps.handlers.label(frame);
79
+ if (this.marker) {
80
+ this.marker.setLatLng(at).setTooltipContent(text);
81
+ return;
82
+ }
83
+ const { L, map, look } = this.deps;
84
+ const { markerRadius, markerWeight, color } = look();
85
+ this.marker = L.circleMarker(at, {
86
+ radius: markerRadius,
87
+ color,
88
+ weight: markerWeight,
89
+ fillColor: '#ffffff',
90
+ fillOpacity: 1,
91
+ interactive: false,
92
+ })
93
+ .bindTooltip(text, {
94
+ permanent: true,
95
+ direction: 'top',
96
+ offset: [0, -(markerRadius + markerWeight)],
97
+ className: 'track-pick-time',
98
+ })
99
+ .addTo(map);
100
+ }
101
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drone-path-engine",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Plan and render flight-path map overlays from drone telemetry.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@types/leaflet": "^1.9.23",
44
- "drone-path-core": "^2.0.1",
44
+ "drone-path-core": "^2.1.0",
45
45
  "leaflet": "^1.9.4",
46
46
  "p-queue": "^10.0.0",
47
47
  "playwright-core": "^1.65.0",