UNPKG

@shopify/react-native-skia

Version:

High-performance React Native Graphics using Skia

51 lines (48 loc) 1.76 kB
import { useEffect } from "react"; import { useAnimatedImage } from "../../skia/core/AnimatedImage"; import Rea from "./ReanimatedProxy"; const DEFAULT_FRAME_DURATION = 60; export const useAnimatedImageValue = (source, paused) => { const defaultPaused = Rea.useSharedValue(false); const isPaused = paused !== null && paused !== void 0 ? paused : defaultPaused; const currentFrame = Rea.useSharedValue(null); const lastTimestamp = Rea.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 === null || animatedImage === void 0 ? void 0 : animatedImage.currentFrameDuration()) || DEFAULT_FRAME_DURATION; Rea.useFrameCallback(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(); if (currentFrame.value) { currentFrame.value.dispose(); } currentFrame.value = animatedImage.getCurrentFrame(); // Update the last timestamp lastTimestamp.value = timestamp; }); useEffect(() => { return () => { animatedImage === null || animatedImage === void 0 || animatedImage.dispose(); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return currentFrame; }; //# sourceMappingURL=useAnimatedImageValue.js.map