@@ -147,6 +147,18 @@ const redraw = () => {
147
147
} ;
148
148
drawDebug ( ) ;
149
149
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
+
150
162
// Cell-specific callback for providing an animation painter.
151
163
const animate = ( painter : AnimationPainter ) => {
152
164
if ( ! animating ) return ;
@@ -157,22 +169,22 @@ const redraw = () => {
157
169
158
170
const drawFrame = ( ) => {
159
171
// 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 ) ;
173
186
}
174
187
175
- painter ( frameTime ) ;
176
188
requestAnimationFrame ( drawFrame ) ;
177
189
} ;
178
190
drawFrame ( ) ;
0 commit comments