Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/components/map/RouteMap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@
shapeData = await shapeResponse.json();
const shapePoints = shapeData?.data?.entry?.points;
if (shapePoints && isMounted) {
await mapProvider.createPolyline(shapePoints);
await mapProvider.createPolyline(shapePoints, { animate: true });
}
}

const stopTimes = tripData?.data?.entry?.schedule?.stopTimes ?? [];
const stops = tripData?.data?.references?.stops ?? [];
// TODO: implement better way to transition to route shape
const location = calculateMidpoint(stops);

if (location) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/search/SearchPane.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
for (const polylineData of polylinesData) {
const shape = polylineData.points;
let polyline;
polyline = mapProvider.createPolyline(shape);
polyline = mapProvider.createPolyline(shape, { animate: true });
polylines.push(polyline);
}

Expand Down
29 changes: 29 additions & 0 deletions src/lib/Provider/OpenStreetMapProvider.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,26 @@ export default class OpenStreetMapProvider {
}).addTo(this.map);
}

animatePolylineOpacity(polyline, targetOpacity = 1, durationMs = 350) {
if (!browser || !this.map || !polyline) return;
if (typeof requestAnimationFrame !== 'function') return;

const startTime = performance.now();
const tick = (now) => {
// Stop animation if polyline was removed/detached.
if (!this.map || !this.map.hasLayer(polyline)) return;

const progress = Math.min((now - startTime) / durationMs, 1);
polyline.setStyle({ opacity: targetOpacity * progress });

if (progress < 1) {
requestAnimationFrame(tick);
}
};

requestAnimationFrame(tick);
}

createPolyline(points, options = {}) {
if (!browser || !this.map) return null;

Expand All @@ -470,6 +490,11 @@ export default class OpenStreetMapProvider {
weight: options.weight || 4,
opacity: options.opacity ?? 1
};
const animate = options.animate ?? false;
const targetOpacity = polylineOpts.opacity;
if (animate) {
polylineOpts.opacity = 0;
}
if (options.dashArray) {
polylineOpts.dashArray = options.dashArray;
}
Expand Down Expand Up @@ -499,6 +524,10 @@ export default class OpenStreetMapProvider {

polyline.arrowDecorator = arrowDecorator;

if (animate) {
this.animatePolylineOpacity(polyline, targetOpacity);
}

return polyline;
}

Expand Down