@@ -56,6 +56,7 @@ const AnimatedSprite = memo(function AnimatedSprite({
5656 const animationStateRef = useRef < AnimationState | null > ( null )
5757 const animationFrameRef = useRef < number > ( 0 )
5858 const lastTimeRef = useRef < number > ( 0 )
59+ const animateRef = useRef < ( ( currentTime : number ) => void ) | null > ( null )
5960
6061 const [ loading , setLoading ] = useState ( true )
6162 const [ error , setError ] = useState ( false )
@@ -158,11 +159,18 @@ const AnimatedSprite = memo(function AnimatedSprite({
158159 drawFrame ( ctx , imageRef . current , frameIndex )
159160
160161 // Continue animation loop
161- animationFrameRef . current = requestAnimationFrame ( animate )
162+ if ( animateRef . current ) {
163+ animationFrameRef . current = requestAnimationFrame ( animateRef . current )
164+ }
162165 } ,
163166 [ paused , speed , drawFrame ] ,
164167 )
165168
169+ // Store animate function in ref
170+ useEffect ( ( ) => {
171+ animateRef . current = animate
172+ } , [ animate ] )
173+
166174 // Initialize animation
167175 const initAnimation = useCallback (
168176 async ( image : HTMLImageElement ) => {
@@ -225,9 +233,11 @@ const AnimatedSprite = memo(function AnimatedSprite({
225233
226234 // Start animation
227235 lastTimeRef . current = 0
228- animationFrameRef . current = requestAnimationFrame ( animate )
236+ if ( animateRef . current ) {
237+ animationFrameRef . current = requestAnimationFrame ( animateRef . current )
238+ }
229239 } ,
230- [ mcmetaUrl , preloadedMcmetaData , animate ] ,
240+ [ mcmetaUrl , preloadedMcmetaData ] ,
231241 )
232242
233243 // Load image
@@ -262,9 +272,9 @@ const AnimatedSprite = memo(function AnimatedSprite({
262272
263273 // Handle pause/resume
264274 useEffect ( ( ) => {
265- if ( ! paused && animationStateRef . current ) {
275+ if ( ! paused && animationStateRef . current && animateRef . current ) {
266276 lastTimeRef . current = 0
267- animationFrameRef . current = requestAnimationFrame ( animate )
277+ animationFrameRef . current = requestAnimationFrame ( animateRef . current )
268278 } else if ( paused && animationFrameRef . current ) {
269279 cancelAnimationFrame ( animationFrameRef . current )
270280 }
@@ -274,7 +284,7 @@ const AnimatedSprite = memo(function AnimatedSprite({
274284 cancelAnimationFrame ( animationFrameRef . current )
275285 }
276286 }
277- } , [ paused , animate ] )
287+ } , [ paused ] )
278288
279289 if ( error ) {
280290 return (
0 commit comments