@@ -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