-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v2 Animate Colors #845
Comments
@browniefed, why did you close this? |
@jakub-gonet ah I was doing it wrong, should be interpolating. Although I still don't think top level coloring is handled after reviewing some stuff |
Cool, thanks for the response, if you find another issue regarding colors feel free to reopen. |
@nandorojo, please show us code that is crashing, we can't help you based only on the stack trace. |
Yup sorry about that, I'll add a code sample here. |
This code produces that error: import React, { useReducer } from 'react'
import Animated, { useAnimatedStyle, withSpring } from 'react-native-reanimated'
import { Button } from 'react-native'
export default function ColorError() {
const [backgroundColor, toggle] = useReducer(
(bg) => (bg === 'red' ? 'green' : 'red'),
'red'
)
const style = useAnimatedStyle(() => ({
backgroundColor: withSpring(backgroundColor),
}))
return (
<Animated.View
style={[
{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
style,
]}
>
<Button title="Toggle Color" onPress={toggle} color="white" />
</Animated.View>
)
} I'm using the expo reanimated starter. Here's my {
"version": "39.0.0",
"dependencies": {
"expo": "~39.0.0",
"expo-status-bar": "~1.0.2",
"react": "~16.13.0",
"react-dom": "~16.13.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.2.tar.gz",
"react-native-gesture-handler": "^1.8.0",
"react-native-reanimated": "2.0.0-alpha.6.1",
"react-native-web": "0.13.13",
"react-navigation-stack": "^2.8.4"
},
"devDependencies": {
"@babel/core": "^7.8.6",
"babel-preset-expo": "~8.1.0",
"eslint-config-nando": "^1.0.10"
},
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo web",
"eject": "expo eject"
},
"private": true
} I also get this TS error: |
|
Got it, that makes sense. I'm basically trying to animate from one color to the next, where I keep the color as a React state value. Whenever the color changes, I animate to the next one. I'm working on a component library similar to Link: https://github.com/nandorojo/redrip I’m hoping to pass If you're interested, you can see how I'm doing it in I assume Maybe I could create an animated value under the hood for every possible color style value, but it doesn't seem ideal. Do you have any suggestions @jakub-gonet? Thanks so much for your time! |
I don't maintain Rea2 and have limited knowledge about it but @zrebcu411 or @piaskowyk maybe know the answer for this one? |
I tried using import { View, Button, StyleSheet } from 'react-native'
import React from 'react'
import Animated, {
useAnimatedStyle,
useSharedValue,
withTiming,
processColor, // I tried this
} from 'react-native-reanimated'
const colors = ['rgb(5,200,3)', 'rgb(10,5,100)']
export default function ColorBug() {
const color = useSharedValue(colors[0])
const toggleColor = () => {
if (color.value === colors[0]) {
color.value = colors[1]
} else {
color.value = colors[0]
}
}
const style = useAnimatedStyle(() => ({
backgroundColor: withTiming(processColor(color.value)),
}))
return (
<View style={styles.container}>
<Animated.View style={[styles.box, style]}></Animated.View>
<Button title="toggle" onPress={toggleColor} />
</View>
)
}
const styles = StyleSheet.create({
box: {
justifyContent: 'center',
backgroundColor: 'blue',
height: 100,
width: 100,
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
},
}) I tried using hex strings, RGB strings, and color names in the Here's a video of what happens: |
@jakub-gonet would you mind re-opening this? |
Sure, but I'm afraid I won't be able to help. |
Got it, thanks for the heads up! I'll keep an eye out here. |
Hi @nandorojo! You should move withTiming(processColor(color.value)) from useAnimatedStyle to different method and use color.value instead, and it will work
|
FYI tested this case on ios, android and web Working Code:
|
@dmmaslenn Thanks for testing that, I'll try this out. I'm actually working on a library that needs to drive the animation inside of the UAS hook, so it would be great to have this bug fixed. I really appreciate you finding the source of it. Which version are you on, by the way? I wonder if this could be related to #1511? |
Hey @nandorojo I’m sorry for my late reply. You have two simple solutions: Codeimport { View, Button, StyleSheet } from 'react-native'
import React from 'react'
import Animated, {
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated'
const colors = ['rgb(5,200,3)', 'rgb(10,5,100)']
export default function ColorBug() {
const color = useSharedValue(colors[0])
const toggleColor = () => {
if (color.value === colors[0]) color.value = colors[1]
else color.value = colors[0]
}
const style = useAnimatedStyle(() => ({
backgroundColor: withTiming(color.value),
}))
return (
<View>
<Animated.View style={[styles.box, style]}></Animated.View>
<Button title="toggle" onPress={toggleColor} />
</View>
)
}
const styles = StyleSheet.create({
box: {
justifyContent: 'center',
backgroundColor: 'blue',
height: 100,
width: 100,
},
}) My recommended way is use Codeimport { View, Button, StyleSheet } from 'react-native'
import React from 'react'
import Animated, {
useAnimatedStyle,
useSharedValue,
withTiming,
interpolateColor,
Easing
} from 'react-native-reanimated'
const colors = ['rgb(5,200,3)', 'rgb(10,5,100)']
export default function ColorBug() {
const color = useSharedValue(0)
const toggleColor = () => {
if (color.value === 0) {
color.value = withTiming(1, {duration: 200, easing: Easing.linear})
} else {
color.value = withTiming(0, {duration: 200, easing: Easing.linear})
}
}
const style = useAnimatedStyle(() => ({
backgroundColor: interpolateColor(
color.value,
[0, 1],
colors,
),
}))
return (
<View>
<Animated.View style={[styles.box, style]}></Animated.View>
<Button title="toggle" onPress={toggleColor} />
</View>
)
}
const styles = StyleSheet.create({
box: {
justifyContent: 'center',
backgroundColor: 'blue',
height: 100,
width: 100,
},
}) I tested this on the latest version of Reanimated. Have you any more questions or can I close this issue? |
@piaskowyk Thanks! The only open question – I'm passing a string to |
I think that we should change the passing type in the signature of function. I think we will change this in the newer version. |
In case anyone comes across this issue...make sure to use |
DescriptionHi! I found another bug similar to the one @nandorojo found, in fact, his library is probably buggy because of this. Basically, animating colors works fine, as long as you don't use red😅. The code snippet has two arrays, buggyColors and normalColors, if you try to animate buggyColors, the weird flickering occurs, the only difference is the colors inside, the first array is using red shades and the second one is using blue shades. Something that might be useful to notice, is that using interpolateColor does work as expected, but I have a similar use case as @nandorojo where I need to update colors "on the fly", so using interpolateColor does not seem to be the best solution(also this seems to be a bug with values too close to 255). Let me know if there's anything more I can do to help! Code Example
|
@BrandonMA I'll check it again and let you know. |
Hello, I am still having this bug on the current release, where you able to reproduce it? Or am I using the library wrong? |
You should upgrade to the latest first and see if that fixes your issue. |
@nandorojo Yes, just needed to upgrade, all set now, thanks |
Description
Animating colors with
withTiming
orwithSpring
doesn't seem to work with colors values (hex or RGB).Screenshots
Steps To Reproduce
Expected behavior
Animate colors
Actual behavior
Color just changes
Snack or minimal code example
Package versions
The text was updated successfully, but these errors were encountered: