Coder Social home page Coder Social logo

rn-img-animated's Introduction

Code Overview ImageSequence Component

The following ReactNative Component fetches images from Firestore and animates them using the Animated API:

import React, { useEffect, useRef, useState } from 'react'; import { View, Animated, Dimensions, ActivityIndicator } from 'react-native'; import { db } from './firebaseConfig';

const { width, height } = Dimensions.get('window');

// Reduced dimensions (70% of the original size) const imagedWidth = width * 0.7; const imageHeight = height * 0.7;

const fetchImagesFromFirestore = async () => { try { const images = []; const snapshot = await db.collection('images').orderBy('index').get(); snapshot.forEach(doc => { images.push(doc.data().url); }); return images; } catch (error) { console.error('Error fetching images:', error); return []; } };

const ImageSequence = ({ duration }) => { const [images, setImages] = useState([]); const animatedValue = useRef(new Animated.Value(0)).current;

useEffect(() => { const getImages = async () => { const fetchedImages = await fetchImagesFromFirestore(); setImages(fetchedImages); }; getImages(); }, []);

useEffect(() => { if (images.length > 0) { Animated.loop( Animated.timing(animatedValue, { toValue: images.length - 1, duration: duration * images.length, useNativeDriver: false }) ).start(); }

// Cleanup function to stop the animation when component unmounts
return () => animatedValue.stopAnimation();

}, [animatedValue, images.length, duration]);

const interpolatedIndex = animatedValue.interpolate({ inputRange: Array.from({ length: images.length }, (_, i) => i), outputRange: images, });

if (images.length === 0) { return ; }

return ( <Animated.Image source={{ uri: interpolatedIndex }} style={{ width: imagedWidth, height: imageHeight }} /> ); };

const App = () => { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> ); };

export default App;

Explanation

Firebase Configuration: The firebaseConfig.js file initializes Firebase with your project's credentials and exports the Firestore instance (db).

Fetching and Sorting Images: The fetchImagesFromFirestore function fetches the images from Firestore, ensuring they are ordered by the index field.

State Management and Animation Setup:
    The useState hook manages the images array.
    The useEffect hook fetches the images and sets the images state.
    The second useEffect hook starts the animation only after the images are fetched and available.
    Animated.loop and Animated.timing create the animation.

Image Interpolation:
    The interpolatedIndex interpolates the animatedValue to map to the URLs of the images.

Loading Indicator:
    An ActivityIndicator is displayed while the images are being fetched, providing visual feedback to the user.

License This project is licensed under the MIT License - see the LICENSE file for details.

rn-img-animated's People

Contributors

zinopro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.