Skip to content

Commit 2208b0f

Browse files
authored
fix: pause packet animation progress (#44)
Closes #42
1 parent 2dc7f92 commit 2208b0f

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
selectElement,
2222
} from "./types/viewportManager";
2323
import { DataGraph } from "./types/graphs/datagraph";
24-
import { packetTicker } from "./types/packet";
24+
import { Packet } from "./types/packet";
2525

2626
const WORLD_WIDTH = 10000;
2727
const WORLD_HEIGHT = 10000;
@@ -388,19 +388,18 @@ export class RightBar {
388388
pauseIcon.src = PlaySvg;
389389
pauseButton.style.backgroundColor = "#f44336";
390390
pauseButton.title = "Resume";
391-
packetTicker.stop();
391+
Packet.pauseAnimation();
392392
} else {
393393
pauseIcon.src = PauseSvg;
394394
pauseButton.style.backgroundColor = "#228b22";
395395
pauseButton.title = "Pause";
396-
packetTicker.start();
396+
Packet.unpauseAnimation();
397397
}
398398
};
399399

400400
pauseButton.onclick = () => {
401401
triggerPause();
402402
};
403-
packetTicker.start();
404403

405404
document.body.onkeyup = function (e) {
406405
if (e.key === " " || e.code === "Space") {

src/types/devices/device.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ export class Device extends Sprite {
130130
const pathEdges = pathEdgeIds.map((id) => this.viewgraph.getEdge(id));
131131

132132
const packet = new Packet(packetType, speed, this.id, destinationId);
133-
const stage = this.viewgraph.getViewport();
134-
stage.addChild(packet);
135133
packet.animateAlongPath(pathEdges, this.id);
136134
}
137135

src/types/edge.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Device } from "./devices/index"; // Import the Device class
44
import { deselectElement, selectElement } from "./viewportManager";
55
import { RightBar } from "..";
66
import { Colors, ZIndexLevels } from "../utils";
7+
import { Packet } from "./packet";
78

89
export interface Position {
910
x: number;
@@ -65,6 +66,12 @@ export class Edge extends Graphics {
6566
this.zIndex = ZIndexLevels.Edge;
6667
this.startPos = startPos;
6768
this.endPos = endPos;
69+
70+
this.children.forEach((child) => {
71+
if (child instanceof Packet) {
72+
child.updatePosition();
73+
}
74+
});
6875
}
6976

7077
select() {

src/types/packet.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { selectElement } from "./viewportManager";
99
import { circleGraphicsContext, Colors, ZIndexLevels } from "../utils";
1010
import { RightBar } from "../index";
1111

12-
export const packetTicker = new Ticker();
13-
1412
const contextPerPacketType: Record<string, GraphicsContext> = {
1513
IP: circleGraphicsContext(Colors.Green, 0, 0, 5),
1614
ICMP: circleGraphicsContext(Colors.Red, 0, 0, 5),
@@ -29,6 +27,16 @@ export class Packet extends Graphics {
2927
sourceId: number;
3028
destinationId: number;
3129

30+
static animationPaused = false;
31+
32+
static pauseAnimation() {
33+
Packet.animationPaused = true;
34+
}
35+
36+
static unpauseAnimation() {
37+
Packet.animationPaused = false;
38+
}
39+
3240
constructor(
3341
type: string,
3442
speed: number,
@@ -89,39 +97,45 @@ export class Packet extends Graphics {
8997
console.error(
9098
"No se puede animar un paquete a lo largo de un camino vacío",
9199
);
100+
this.destroy();
92101
return;
93102
}
94103
console.log(path);
95104
this.currentPath = path;
96105
this.currentEdge = this.currentPath.shift();
97106
this.currentStart = start;
98-
// TODO: use global ticker, and add "shouldProgress" flag
99-
packetTicker.add(this.updateProgress, this);
107+
// Add packet as a child of the current edge
108+
this.currentEdge.addChild(this);
109+
this.updatePosition();
110+
Ticker.shared.add(this.animationTick, this);
100111
}
101112

102-
updateProgress(ticker: Ticker) {
113+
animationTick(ticker: Ticker) {
103114
if (this.progress >= 1) {
104115
this.progress = 0;
116+
this.removeFromParent();
105117
if (this.currentPath.length == 0) {
106-
ticker.remove(this.updateProgress, this);
107-
this.removeFromParent();
118+
ticker.remove(this.animationTick, this);
119+
this.destroy();
108120
return;
109121
}
110122
this.currentStart = this.currentEdge.otherEnd(this.currentStart);
111123
this.currentEdge = this.currentPath.shift();
124+
this.currentEdge.addChild(this);
125+
}
126+
if (!Packet.animationPaused) {
127+
this.progress += (ticker.deltaMS * this.speed) / 100000;
112128
}
113-
this.progress += (ticker.deltaMS * this.speed) / 100000;
114129

130+
this.updatePosition();
131+
}
132+
133+
updatePosition() {
115134
const current = this.currentEdge;
116135
const start = this.currentStart;
117136

118-
console.log("current: ", current);
119-
console.log("start: ", start);
120-
121137
const startPos = current.nodePosition(start);
122-
console.log("startPos: ", startPos);
123138
const endPos = current.nodePosition(current.otherEnd(start));
124-
console.log("endPos: ", endPos);
125139
this.setPositionAlongEdge(startPos, endPos, this.progress);
126140
}
127141

0 commit comments

Comments
 (0)