Skip to content

Commit 13ac049

Browse files
committed
fix(AnimatedSprite): Fix ESLint error by using ref for animate function
1 parent 5e78581 commit 13ac049

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/components/AnimatedSprite.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (

src/components/ImageViewer.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export function ImageViewer({
6464
const blob = await response.blob()
6565
saveAs(blob, fileName || 'image.png')
6666
} catch (err) {
67-
// eslint-disable-next-line no-console
6867
console.error('Failed to download image', err)
6968
}
7069
}, [currentImage, fileName, repo])

0 commit comments

Comments
 (0)