Skip to content

Commit

Permalink
Merge pull request #1 from Pranav2612000/feat/pause-btn
Browse files Browse the repository at this point in the history
feat: allow pausing and resuming animations
  • Loading branch information
vasusharma7 authored Mar 26, 2024
2 parents 2580156 + 8c79979 commit 96d82f4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
54 changes: 54 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ import "./style.css";
import Network from "./src/Network";
import { CANVAS_HEIGHT, CANVAS_WIDTH } from "./src/canvas";

window.isPaused = false;
window.setInterval = (fn, duration) => {
let isRunning = true;
let currentDuration;
let currentCancelFn = () => null;
let currentResolve;

const mainLoop = async () => {
while(isRunning) {
currentDuration = duration;

while (currentDuration > 0) {
if (window.isPaused) {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1000);
});
continue;
}
await new Promise((resolve) => {
currentResolve = resolve;
currentCancelFn = setTimeout(() => {
resolve();
}, 200);
});
currentDuration = currentDuration - 200;
}
if (isRunning) {
fn();
}
}
};
mainLoop();

return () => {
clearTimeout(currentCancelFn);
currentResolve();
isRunning = false;
}
}

window.clearInterval = (intervalFn) => {
intervalFn();
}

document.querySelector("#app").innerHTML = `
<div class="main">
<h1>Raft</h1>
Expand All @@ -17,6 +63,7 @@ document.querySelector("#app").innerHTML = `
<button id="resetLeader">Reset Leader</button>
<button id="addNode">Add Node</button>
<button id="addData">Send Data</button>
<button id="pause">Play / Pause</button>
</div>
`;

Expand Down Expand Up @@ -44,3 +91,10 @@ document.getElementById("addData").addEventListener("click", () => {

network.nodes[network.leader - 1].receiveData(currentMsg++);
});
document.getElementById("pause").addEventListener("click", () => {
if (window.isPaused) {
window.isPaused = false;
} else {
window.isPaused = true;
}
});
4 changes: 2 additions & 2 deletions src/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
import { MESSAGE_TYPE } from "./types";

class Network {
static HEARTBEAT = 4000;
static HEARTBEAT = 5000;
static MAX_ELECTION_TIMEOUT = 9000;
static MIN_ELECTION_TIMEOUT = 6000;
static NETWORK_DELAY = 1000; // in milliseconds
static NETWORK_DELAY = 1500; // in milliseconds

constructor(numOfNodes) {
this.numOfNodes = numOfNodes;
Expand Down
30 changes: 28 additions & 2 deletions src/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,20 @@ export function drawNode(context, nodePosition, node) {
return;
}

window.requestAnimationFrame(animationFrame);
const recurse = async () => {
while (window.isPaused) {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1000);
});
lastFrameTimestamp += 1000;
}

window.requestAnimationFrame(animationFrame);
};

recurse();
}

window.requestAnimationFrame(animationFrame);
Expand Down Expand Up @@ -237,7 +250,20 @@ export async function showDataTransfer(
}

// Otherwise display the next animation
window.requestAnimationFrame(animationFrame);
const recurse = async () => {
while (window.isPaused) {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1000);
});
lastFrameTimestamp += 1000;
}

window.requestAnimationFrame(animationFrame);
};

recurse();
}
window.requestAnimationFrame(animationFrame);
});
Expand Down

0 comments on commit 96d82f4

Please sign in to comment.