Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does react native animated native driver support color/backgroundColor?

i'm trying to animate color of Text, something like this:

const animateTest = scrollY.interpolate({
                                    inputRange: [0, 100],
                                    outputRange: ['rgba(255,0,0,1)', 'rgba(0,255,0,1)']                                    
                                });



      return (<View>      
    <Animated.Text style={{ position:'absolute',                                          
                              color: animateTest
                          }} >blah blah blah</Animated.Text>
    <Animated.ScrollView
              scrollEventThrottle={16}                
              onScroll={Animated.event(
                [
                    {
                        nativeEvent: {contentOffset: {y: scrollY}},
                    },
                ],
                {
                    useNativeDriver: true,
                }
            )}      
          >       

but i'm getting this error:

Style property 'color' is not supported by native animated module

using ReactNative 0.44.0

according to this blog post it's supposed to work because they say:

Not everything you can do with Animated is currently supported in Native Animated. The main limitation is that you can only animate non-layout properties, things like transform, opacity and backgroundColor will work but flexbox and position properties won't.

but i see there is a whitelist for styles in the code that are supported: link to relevant code there is a very limited whitelist:

const STYLES_WHITELIST = {
  opacity: true,
  transform: true,
  /* legacy android transform properties */
  scaleX: true,
  scaleY: true,
  translateX: true,
  translateY: true,
};

which does not include color/backgroundColor

can anybody help me out here - is it supposed to be supported or not?

like image 462
Zohar Levin Avatar asked May 23 '17 11:05

Zohar Levin


People also ask

How do you animate a background color in React Native?

For animating between colors we need to interpolate between two values of colors. Now, as the state 'animation' is of type Animated. Value, there is a method called interpolate(), which takes a configuration object with keys.

How do you animate text color in React Native?

Put two text components (showing the same text) above each other (using absolute layout). You can then useNativeDriver and change the opacity of each text component. 2. Use react native reanimated v2 to change the color and archive native performance.

Which of the following is a correct React Native animation type?

React Native provides two complementary animation systems: Animated for granular and interactive control of specific values, and LayoutAnimation for animated global layout transactions.

How do you animate ScrollView in React Native?

import { ScrollView, Text, View, Animated } from 'react-native'; import DATA from './data'; Next, modify the contents of the App component. The important prop to note below in the ScrollView component is the onScroll prop. Mapping gestures like scrolling directly to an animated value can be done by using Animated.


2 Answers

The error is from trying to change color while using the native driver (i.e. useNativeDriver: true).

color and backgroundColor aren't supported by the native driver at the moment (https://github.com/facebook/react-native/issues/14178).

If you want to keep using the native driver, you could try using opacity as a work around to change the color of the text e.g. by placing two text elements with different colors and different starting opacities on top of each other. Then use the onScroll event to change the opacity of each element so that the element that's visible at the start isn't visible at the end (and vice versa):

    const redTextOpacity = scrollY.interpolate({
      inputRange: [0, 100],
      outputRange: [1, 0],
      extrapolate: 'clamp',
    });
    const greenTextOpacity = scrollY.interpolate({
      inputRange: [0, 100],
      outputRange: [0, 1],
      extrapolate: 'clamp',
    });

    return (
      <View>
          <Animated.Text style={{
            position: 'absolute',
            opacity: redTextOpacity,
            color: 'rgba(255,0,0,1)',
          }} >blah blah blah</Animated.Text>
          <Animated.Text style={{
            position: 'absolute',
            opacity: greenTextOpacity,
            color: 'rgba(0,255,0,1)',
          }} >blah blah blah</Animated.Text>

  ...

I had the same problem today and didn't want to set useNativeDriver: false for performance reasons. But using opacity was an ok work-around for me. Hope this helps!

like image 120
alex-blair Avatar answered Nov 15 '22 10:11

alex-blair


Make useNativeDriver:false then the color property will work as expected.

I dont find any difference in 2D animation using or not using native driver.

like image 40
chetan Avatar answered Nov 15 '22 12:11

chetan