<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anuj Gupta</title>
    <description>The latest articles on Forem by Anuj Gupta (@aj019).</description>
    <link>https://forem.com/aj019</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F349275%2F31636463-4092-4555-921a-78c6f9fb6022.png</url>
      <title>Forem: Anuj Gupta</title>
      <link>https://forem.com/aj019</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aj019"/>
    <language>en</language>
    <item>
      <title>How to animate background image in react native</title>
      <dc:creator>Anuj Gupta</dc:creator>
      <pubDate>Mon, 21 Nov 2022 21:26:18 +0000</pubDate>
      <link>https://forem.com/aj019/how-to-animate-background-image-in-react-native-1k37</link>
      <guid>https://forem.com/aj019/how-to-animate-background-image-in-react-native-1k37</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyuzzeyucfmzbwhasrq6w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyuzzeyucfmzbwhasrq6w.gif" alt="Image description" width="296" height="633"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wanted to add an animated background for my game Catchphrase as the main play screen had a lot of white space and now you can add it as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Constants
&lt;/h2&gt;

&lt;p&gt;Let’s start by defining some constants for our animation. Create constants.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const INPUT_RANGE_START = 0;
export const INPUT_RANGE_END = 1;
export const OUTPUT_RANGE_START = -281;
export const OUTPUT_RANGE_END = 0;
export const ANIMATION_TO_VALUE = 1;
export const ANIMATION_DURATION = 25000;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can modify the values according to your need.&lt;/p&gt;

&lt;p&gt;We need these values for interpolation . If you want to learn more about interpolation. Here is a great tutorial for it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://eveningkid.medium.com/interpolation-with-react-native-animations-853e467fe5c1" rel="noopener noreferrer"&gt;Interpolation with React Native Animations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need an image that we want to animate. I am using the following image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fekhylwotuowb1t4l043n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fekhylwotuowb1t4l043n.png" alt="Image description" width="720" height="691"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If it was not clear what we are doing exactly in the above gif. Here is a simple explaination. We have an image and we are moving it down at a 45 degree angle using animation.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz67cuf10f9sj4vw1rg05.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz67cuf10f9sj4vw1rg05.gif" alt="Image description" width="291" height="633"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Styling the image
&lt;/h2&gt;

&lt;p&gt;Okay let’s make the big enough to cover the screen. Create styles.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {StyleSheet} from 'react-native'

const styles = StyleSheet.create({    

    background: {
        position: 'absolute',
        width: 1200,
        height: 1200,
        top: 0,
        opacity: 0.2,
        transform: [
          {
            translateX: 0,
          },
          {
            translateY: 0,
          },
        ],      
      }, 
  });

export default styles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now finally lets create the component to animate background :- BackgroundAnimation&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Animating the ImageBackground Component
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect,useRef } from 'react';
import { Animated, Easing, ImageBackground } from 'react-native';

import backgroundImage from '../../assets/background.png';
import styles from './styles';
import {
  INPUT_RANGE_START,
  INPUT_RANGE_END,
  OUTPUT_RANGE_START,
  OUTPUT_RANGE_END,
  ANIMATION_TO_VALUE,
  ANIMATION_DURATION,
} from '../../utils/constants';


export default function BackgroundAnimation() {
  const initialValue = 0;
  const translateValue = useRef(new Animated.Value(initialValue)).current;

  useEffect(() =&amp;gt; {
    const translate = () =&amp;gt; {
      translateValue.setValue(initialValue);
      Animated.timing(translateValue, {
        toValue: ANIMATION_TO_VALUE,
        duration: ANIMATION_DURATION,
        easing: Easing.linear,
        useNativeDriver: true,
      }).start(() =&amp;gt; translate());
    };

    translate();
  }, [translateValue]);

  const translateAnimation = translateValue.interpolate({
    inputRange: [INPUT_RANGE_START, INPUT_RANGE_END],
    outputRange: [OUTPUT_RANGE_START, OUTPUT_RANGE_END],
  });

  const AnimetedImage = Animated.createAnimatedComponent(ImageBackground);

  return (

        &amp;lt;AnimetedImage 
            resizeMode="repeat" 
            style={[styles.background,{
                transform: [
                    {
                      translateX: translateAnimation,
                    },
                    {
                      translateY: translateAnimation,
                    },
                  ],
            }]}
            source={backgroundImage} /&amp;gt;

  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can simply import our BackgroundAnimation in any of our screen and we will get an animated background automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9fb1m8cltz9g327vv1sx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9fb1m8cltz9g327vv1sx.gif" alt="Image description" width="296" height="633"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading this article. Be sure to like/recommend as much as you can and also share with your friends. It means a lot to me.&lt;/p&gt;

&lt;p&gt;If you want to check out the game in this blog . The game is available both for &lt;a href="https://bit.ly/3QD7s2a" rel="noopener noreferrer"&gt;Android&lt;/a&gt; and &lt;a href="https://apple.co/3SC4I5o" rel="noopener noreferrer"&gt;iOS&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>coding</category>
      <category>learning</category>
    </item>
    <item>
      <title>First Post : Lets connect on Github</title>
      <dc:creator>Anuj Gupta</dc:creator>
      <pubDate>Fri, 03 Jun 2022 11:41:28 +0000</pubDate>
      <link>https://forem.com/aj019/first-post-lets-connect-on-github-53i5</link>
      <guid>https://forem.com/aj019/first-post-lets-connect-on-github-53i5</guid>
      <description>&lt;p&gt;Just wanted to say Hi and connect with fellow developers. If you would like to connect please share your github/gitlab link in the comments.&lt;/p&gt;

</description>
      <category>github</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
