Skip to content

Commit e8e4b1a

Browse files
committed
pause demo animations on click
1 parent 4b8451a commit e8e4b1a

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<link rel="shortcut icon" href="https://blobs.dev/assets/favicon.ico?v=3ewlwLn2WO">
3+
<link rel="shortcut icon" href="https://blobs.dev/assets/favicon.ico?v=3ewlwLn2WO" />
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<style>
66
html {

demo/internal/layout.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ const redraw = () => {
147147
};
148148
drawDebug();
149149

150+
// Keep track of paused state.
151+
let pausedAt = 0;
152+
let pauseOffset = 0;
153+
cell.canvas.onclick = () => {
154+
if (pausedAt === 0) {
155+
pausedAt = Date.now();
156+
} else {
157+
pauseOffset += Date.now() - pausedAt;
158+
pausedAt = 0;
159+
}
160+
};
161+
150162
// Cell-specific callback for providing an animation painter.
151163
const animate = (painter: AnimationPainter) => {
152164
if (!animating) return;
@@ -157,22 +169,22 @@ const redraw = () => {
157169

158170
const drawFrame = () => {
159171
// Stop animating if cell is redrawn.
160-
if (cell.animationID !== animationID) {
161-
return;
162-
}
163-
164-
const frameTime = Date.now() - startTime;
165-
cell.ctx.clearRect(0, 0, cellWidth, cellHeight);
166-
drawDebug();
167-
if (isDebug()) {
168-
tempStyles(
169-
cell.ctx,
170-
() => (cell.ctx.fillStyle = colors.debug),
171-
() => cell.ctx.fillText(String(frameTime), 10, 15),
172-
);
172+
if (cell.animationID !== animationID) return;
173+
174+
if (pausedAt === 0) {
175+
const frameTime = Date.now() - startTime - pauseOffset;
176+
cell.ctx.clearRect(0, 0, cellWidth, cellHeight);
177+
drawDebug();
178+
if (isDebug()) {
179+
tempStyles(
180+
cell.ctx,
181+
() => (cell.ctx.fillStyle = colors.debug),
182+
() => cell.ctx.fillText(String(frameTime), 10, 15),
183+
);
184+
}
185+
painter(frameTime);
173186
}
174187

175-
painter(frameTime);
176188
requestAnimationFrame(drawFrame);
177189
};
178190
drawFrame();

0 commit comments

Comments
 (0)