Skip to content

Commit

Permalink
fix: Prevent greedy routing by properly managing animation frames and…
Browse files Browse the repository at this point in the history
… playback state
  • Loading branch information
sanity committed Nov 27, 2024
1 parent c9b9b37 commit 1c8e968
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions hugo-site/static/js/small-world-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ async function initVisualization() {
}
return;
}

// Clear any existing animation
if (animationFrame) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}

const path = findPath(sourceNode, targetNode);
currentPath = path;
Expand Down Expand Up @@ -269,7 +275,13 @@ async function initVisualization() {
let lastSegmentStartTime = null;

function animate(currentTime) {
if (!isPlaying) return;
if (!isPlaying) {
if (animationFrame) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}
return;
}

if (!startTime) {
startTime = currentTime;
Expand All @@ -287,7 +299,12 @@ async function initVisualization() {
if (currentPathSegment >= currentPath.length - 1) {
// Animation complete - immediately start new route
if (isPlaying) {
requestAnimationFrame(() => startNewRoute());
// Use timeout instead of immediate requestAnimationFrame
routeTimeout = setTimeout(() => {
if (isPlaying) {
startNewRoute();
}
}, 500);
}
return;
}
Expand Down

0 comments on commit 1c8e968

Please sign in to comment.