Skip to content

Commit

Permalink
feat: Add animation coordinator to manage and pause concurrent animat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
sanity committed Nov 27, 2024
1 parent 13f84fd commit c9b9b37
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions hugo-site/layouts/shortcodes/small-world-comparison.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ <h5 class="has-text-centered is-size-5 mb-2">Random Network</h5>
</div>
</div>
</div>
<script src="/js/animation-coordinator.js"></script>
<script src="/js/small-world-comparison.js"></script>
1 change: 1 addition & 0 deletions hugo-site/layouts/shortcodes/small-world-routing.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<canvas id="networkCanvas2" width="400" height="400" style="max-width: 70%; height: auto;"></canvas>
</div>
</div>
<script src="/js/animation-coordinator.js"></script>
<script src="/js/small-world-routing.js"></script>
1 change: 1 addition & 0 deletions hugo-site/layouts/shortcodes/small-world-scale.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
</div>
<div style="margin-bottom: 2rem;"></div>
</div>
<script src="/js/animation-coordinator.js"></script>
<script src="/js/small-world-scale.js"></script>
15 changes: 15 additions & 0 deletions hugo-site/static/js/animation-coordinator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Global animation coordinator
const AnimationCoordinator = {
activeAnimation: null,

setActive(name) {
if (this.activeAnimation && this.activeAnimation !== name) {
// Pause other animations
const event = new CustomEvent('pause-other-animations', {
detail: { except: name }
});
document.dispatchEvent(event);
}
this.activeAnimation = name;
}
};
17 changes: 17 additions & 0 deletions hugo-site/static/js/small-world-comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,29 @@ waitForD3().then(() => {
}
}

// Listen for pause events from other animations
document.addEventListener('pause-other-animations', (event) => {
if (event.detail.except !== 'comparison' && isPlaying) {
isPlaying = false;
const btn = document.getElementById('comparisonPlayPauseBtn');
if (btn) {
const icon = btn.querySelector('i');
if (icon) icon.className = 'fas fa-play';
}
if (animationFrame) cancelAnimationFrame(animationFrame);
}
});

function togglePlayPause() {
isPlaying = !isPlaying;
const btn = document.getElementById('comparisonPlayPauseBtn');
const icon = btn.querySelector('i');
icon.className = isPlaying ? 'fas fa-pause' : 'fas fa-play';

if (isPlaying) {
AnimationCoordinator.setActive('comparison');
}

if (isPlaying) {
startTime = Date.now();
simulateRouting();
Expand Down
17 changes: 17 additions & 0 deletions hugo-site/static/js/small-world-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,19 @@ async function initVisualization() {
draw();
}

// Listen for pause events from other animations
document.addEventListener('pause-other-animations', (event) => {
if (event.detail.except !== 'routing' && isPlaying) {
isPlaying = false;
const btn = document.getElementById('routingPlayPauseBtn');
if (btn) {
const icon = btn.querySelector('i');
if (icon) icon.className = 'fas fa-play';
}
if (routeTimeout) clearTimeout(routeTimeout);
}
});

function togglePlayPause() {
isPlaying = !isPlaying;
const btn = document.getElementById('routingPlayPauseBtn');
Expand All @@ -378,6 +391,10 @@ async function initVisualization() {
return;
}

if (isPlaying) {
AnimationCoordinator.setActive('routing');
}

const icon = btn.querySelector('i');
if (!icon) {
console.error('Button icon not found');
Expand Down
14 changes: 14 additions & 0 deletions hugo-site/static/js/small-world-scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,26 @@ waitForD3().then(() => {

}

// Listen for pause events from other animations
document.addEventListener('pause-other-animations', (event) => {
if (event.detail.except !== 'scale' && isSimulating) {
isSimulating = false;
const btn = document.getElementById('scalePlayPauseBtn');
if (btn) {
const icon = btn.querySelector('i');
if (icon) icon.className = 'fas fa-play';
}
if (animationFrame) cancelAnimationFrame(animationFrame);
}
});

function simulate() {
if (!isSimulating) {
isSimulating = true;
const btn = document.getElementById('scalePlayPauseBtn');
const icon = btn.querySelector('i');
icon.className = 'fas fa-pause';
AnimationCoordinator.setActive('scale');

async function step() {
if (!isSimulating) return;
Expand Down

0 comments on commit c9b9b37

Please sign in to comment.