-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// NOTE: this whole file is copied from @shopify/react-native-skia, see TODO | ||
import { useEffect } from "react"; | ||
|
||
import { | ||
DataSourceParam, | ||
SkImage, | ||
useAnimatedImage, | ||
} from "@shopify/react-native-skia"; | ||
const DEFAULT_FRAME_DURATION = 60; | ||
|
||
import { | ||
FrameInfo, | ||
SharedValue, | ||
useFrameCallback, | ||
useSharedValue, | ||
} from "react-native-reanimated"; | ||
|
||
export const useAnimatedImageValue = ( | ||
source: DataSourceParam, | ||
paused?: SharedValue<boolean>, | ||
) => { | ||
const defaultPaused = useSharedValue(false); | ||
const isPaused = paused ?? defaultPaused; | ||
const currentFrame = useSharedValue<null | SkImage>(null); | ||
const lastTimestamp = useSharedValue(-1); | ||
const animatedImage = useAnimatedImage( | ||
source, | ||
(err) => { | ||
console.error(err); | ||
throw new Error( | ||
`Could not load animated image - got '${err.message}'`, | ||
); | ||
}, | ||
false, | ||
); | ||
const frameDuration = | ||
animatedImage?.currentFrameDuration() || DEFAULT_FRAME_DURATION; | ||
|
||
useFrameCallback((frameInfo: FrameInfo) => { | ||
if (!animatedImage) { | ||
currentFrame.value = null; | ||
return; | ||
} | ||
if (isPaused.value && lastTimestamp.value !== -1) { | ||
return; | ||
} | ||
const { timestamp } = frameInfo; | ||
const elapsed = timestamp - lastTimestamp.value; | ||
|
||
// Check if it's time to switch frames based on GIF frame duration | ||
if (elapsed < frameDuration) { | ||
return; | ||
} | ||
|
||
// Update the current frame | ||
animatedImage.decodeNextFrame(); | ||
// TODO: this whole file is copied from @shopify/react-native-skia because | ||
// the following dispose call seems to be causing flickering in the animated | ||
// image. Will try address upstream at some point, then delete this file. | ||
//if (currentFrame.value) { | ||
// currentFrame.value.dispose(); | ||
//} | ||
currentFrame.value = animatedImage.getCurrentFrame(); | ||
|
||
// Update the last timestamp | ||
lastTimestamp.value = timestamp; | ||
}); | ||
useEffect(() => { | ||
return () => { | ||
animatedImage?.dispose(); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
return currentFrame; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters