Color animation issue
See original GitHub issueDescription
Depending on the colors you are animating, the animation might show some weird colors that should not be part of the animation. Is as easy as animating the background color from white to blue.
I added an example with normal Reanimated and with Moti to showcase the problem on a snack.
Expected behavior
The animation should go from white to blue smoothly without weird(green in this case) colors in the middle.
Actual behavior & steps to reproduce
https://user-images.githubusercontent.com/6232522/125182330-d5b93a80-e1d2-11eb-9be4-127fb8da39ed.mov
import React, { useState } from 'react';
import { Text, View, Button } from 'react-native';
import Constants from 'expo-constants';
import { MotiView } from 'moti';
import Reanimated, {
useSharedValue,
useAnimatedStyle,
withTiming,
} from 'react-native-reanimated';
const colors = ['blue', 'white'];
const between = (min, max) => Math.floor(Math.random() * (max - min) + min);
const getNewColor = () => colors[between(0, colors.length)];
export default function App() {
const [color, setColor] = useState(colors[0]);
const reanimatedBackgroundColor = useSharedValue(colors[0]);
const animatedStyles = useAnimatedStyle(() => {
return {
backgroundColor: withTiming(reanimatedBackgroundColor.value),
};
});
return (
<View>
<Reanimated.View
style={[
{
marginBottom: 10,
height: 300,
width: 300,
},
animatedStyles,
]}
/>
<MotiView
style={{ height: 300, width: 300 }}
animate={{ backgroundColor: color }}
/>
<Button
title="Change color"
onPress={() => {
let newColor = getNewColor();
while (newColor === color) {
newColor = getNewColor();
}
reanimatedBackgroundColor.value = newColor;
setColor(newColor);
}}
/>
</View>
);
}
Snack or minimal code example
https://snack.expo.io/@brandonma/color-bug-reanimated
Package versions
- React Native: 0.63.4
- React Native Reanimated: 2.2.0
- Expo: 42.0.0
Affected platforms
- Android (I did not try it out, but probably is also happening)
- iOS
- Web
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Color animation issue · Issue #87 · nandorojo/moti - GitHub
What's happening? Depending on the color value you want to animate, some weird colors appear during the animation. Screen.Recording.2021-07-10.at.21.26.39.
Read more >Color animation not working - Stack Overflow
Windows.Media.Animation.ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.
Read more >Color animation problem - Unity Forum
I have a problem when animating color values using the animation editor. Say I have a cube with a blue diffuse material.
Read more >Color Banding in Gradient Animation: 10 Quick Fixes - SVGator
Tried & true workarounds to eliminate color banding from any gradient animation & how switching to SVG animation is a safe bet for...
Read more >CSS Animations Not Working? Try These Fixes - HubSpot Blog
Learn how to fix common CSS animation error and get your animations looking how you want them.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Still reproduce in reanimated 2.4.0
With
interpolateColor
helper it works correct. May be strange intermediate colors appears because you set color as animated value and it formed byprocessColor
(for blue 4278190335, for white 4294967295) Reanimated helper fn as a number. On animation start it number changed from previous to next value as a sum of all color channels, but it must be changed by each channel independent. AndinterpolateColor
will help to fix that.Log of list of intermediate colors:
and list of processed values for each color: