Skip to content

Commit 31c7c36

Browse files
committed
fix pausing logic
1 parent 56b006a commit 31c7c36

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

demo/content.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ addCanvas(
638638
);
639639

640640
addCanvas(1.8, (ctx, width, height) => {
641+
// Only animate in the most recent painter call.
642+
const animationID = Math.random();
643+
const wasReplaced = () => (ctx.canvas as any).animationID !== animationID;
644+
641645
const period = Math.PI * 1000;
642646
const center: Coord = {x: width * 0.5, y: height * 0.5};
643647
const size = Math.min(width, height) * 0.8;
@@ -657,13 +661,17 @@ addCanvas(1.8, (ctx, width, height) => {
657661
)();
658662

659663
const renderFrame = () => {
664+
if (wasReplaced()) return;
660665
ctx.clearRect(0, 0, width, height);
661666
animation.renderFrame();
662667
requestAnimationFrame(renderFrame);
663668
};
664669
requestAnimationFrame(renderFrame);
665670

666-
const loopAnimation = (): void => animation.transition(genFrame());
671+
const loopAnimation = (): void => {
672+
if (wasReplaced()) return;
673+
animation.transition(genFrame());
674+
};
667675

668676
let frameCount = -1;
669677
const genFrame = (overrides: Partial<CanvasKeyframe> = {}): CanvasKeyframe => {
@@ -684,7 +692,12 @@ addCanvas(1.8, (ctx, width, height) => {
684692

685693
animation.transition(genFrame({duration: 0}));
686694

687-
ctx.canvas.onclick = animation.playPause;
695+
ctx.canvas.onclick = () => {
696+
if (wasReplaced()) return;
697+
animation.playPause();
698+
};
699+
700+
(ctx.canvas as any).animationID = animationID;
688701

689702
return `Points can be removed at the end of animations as the target shape has been reached.
690703
However if the animation is interrupted during interpolation there is no opportunity to

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,6 @@
147147
</main>
148148
<script src="./example.ts"></script>
149149
<script src="./content.ts"></script>
150+
<!-- TODO fix unexpanded landing alignment -->
150151
</body>
151152
</html>

index.html

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

internal/animate/state.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,35 @@ export const statefulAnimationGenerator = <K extends CallbackKeyframe, T>(
1919
let callbackStore: CallbackStore = {};
2020

2121
// Keep track of paused state.
22-
// TODO fix
2322
let pausedAt = 0;
2423
let pauseOffset = 0;
24+
const getAnimationTimestamp = () => Date.now() - pauseOffset;
25+
const isPaused = () => pausedAt !== 0;
2526

2627
const play = () => {
27-
console.log("play");
28-
if (pausedAt === 0) return;
29-
pauseOffset += Date.now() - pausedAt;
28+
if (!isPaused()) return;
29+
pauseOffset += getAnimationTimestamp() - pausedAt;
3030
pausedAt = 0;
3131
};
3232

3333
const pause = () => {
34-
console.log("pause");
35-
pausedAt = Date.now();
34+
if (isPaused()) return;
35+
pausedAt = getAnimationTimestamp();
3636
};
3737

3838
const playPause = () => {
39-
if (pausedAt === 0) {
40-
pause();
41-
} else {
39+
``;
40+
if (isPaused()) {
4241
play();
42+
} else {
43+
pause();
4344
}
4445
};
4546

4647
const renderFrame = (): T => {
4748
const renderOutput = renderFramesAt({
4849
renderCache: renderCache,
49-
timestamp: pausedAt === 0 ? Date.now() - pauseOffset : pausedAt,
50+
timestamp: isPaused() ? pausedAt : getAnimationTimestamp(),
5051
currentFrames: internalFrames,
5152
});
5253

@@ -70,7 +71,7 @@ export const statefulAnimationGenerator = <K extends CallbackKeyframe, T>(
7071

7172
const transitionOutput = transitionFrames<K>({
7273
renderCache: renderCache,
73-
timestamp: Date.now() - pauseOffset,
74+
timestamp: getAnimationTimestamp(),
7475
currentFrames: internalFrames,
7576
newFrames: keyframes,
7677
shapeGenerator: generator,

0 commit comments

Comments
 (0)