Skip to content
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

fix: withSpring color properties flickering #6821

Merged

Conversation

patrycjakalinska
Copy link
Contributor

Summary

This PR addresses an issue where color-based properties (backgroundColor, boxShadow) were causing flickering when used with withSpring animations. The root cause of the flickering was RGBA values going below 0, resulting in NaN values.

I introduced clampRGBA function that guards values within given limits.

Before:

Screen.Recording.2024-12-16.at.11.14.39.mov

After:

Screen.Recording.2024-12-16.at.11.15.03.mov

Test plan

Paste this code to EmptyExample - it should work on both Paper and Fabric:

EmptyExample code
import { Text, StyleSheet, View, Pressable } from 'react-native';
import React from 'react';
import Animated, {
  useSharedValue,
  withSpring,
  useAnimatedStyle,
} from 'react-native-reanimated';

export default function EmptyExample() {
  const pressed = useSharedValue(false);
  const animatedStyle = useAnimatedStyle(() => {
    return {
      backgroundColor: withSpring(pressed.value ? 'blue' : 'red'),
    };
  });

  return (
    <View>
      <Animated.View style={[styles.box, animatedStyle]} />
      <Pressable
        onPress={() => {
          pressed.value = !pressed.value;
        }}>
        <Text>Press me</Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  box: {
    width: 50,
    height: 50,
  },
});

@patrycjakalinska patrycjakalinska added this pull request to the merge queue Jan 29, 2025
Merged via the queue into main with commit ff2ca92 Jan 29, 2025
9 checks passed
@patrycjakalinska patrycjakalinska deleted the @patrycjakalinska/withSpring-color-flickering-fix branch January 29, 2025 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants