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: Make withDecay compatible with withRepeat #6772

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

patrycjakalinska
Copy link
Contributor

@patrycjakalinska patrycjakalinska commented Nov 28, 2024

Summary

As it was discovered in #6757, withRepeat run along with withDecay triggered repeat only once, even if the number of repeats was greater. withDecay relies on the value of velocity. The initialVelocity is the velocity provided (or default), and during the animation, the velocity value decreases, causing the animated object to slowly stop. The problem was, after first repeat the velocity was already decreased to almost zero and the animation could not run, as it had no momentum.
I was able to fix that by adjusting finished prop, and reseting velocity to initial value, if finished is true.

Fixes #6757

Before:

Simulator.Screen.Recording.-.iPhone.16.Pro.-.2024-11-25.at.12.07.07.mp4

After:

Screen.Recording.2024-11-28.at.14.18.54.mov

Test plan

Run provided code snipped on Fabric example

Code snipped provided in issue
import React from 'react';
import { View, Pressable, Text, StyleSheet } from 'react-native';
import Animated, {
  useAnimatedStyle,
  useSharedValue,
  withDecay,
  withRepeat,
} from 'react-native-reanimated';
import {
  GestureHandlerRootView,
  GestureDetector,
  Gesture,
} from 'react-native-gesture-handler';

const ballSize = 100;

export default function EmptyExample() {
  const position = useSharedValue(1);

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [{ translateY: position.value }],
  }));

  const tap = Gesture.Tap().onStart(() => {
    position.value = withRepeat(
      withDecay({ velocity: 200, clamp: [-50, 100] }),
      5
    );
    // position.value = withDecay({ velocity: 200, clamp: [-500, 500] });
  });

  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <View
        style={{ flex: 1, justifyContent: 'center', backgroundColor: 'red' }}>
        <GestureDetector gesture={tap}>
          <View
            style={{
              flex: 1,
              alignItems: 'center',
              justifyContent: 'center',
              flexDirection: 'column',
            }}>
            <Animated.View style={[styles.ball, animatedStyle]} />
          </View>
        </GestureDetector>
      </View>
    </GestureHandlerRootView>
  );
}
const styles = StyleSheet.create({
  ball: {
    width: ballSize,
    height: ballSize,
    borderRadius: ballSize / 2,
    backgroundColor: 'black',
    margin: 30,
  },
});

@patrycjakalinska patrycjakalinska changed the base branch from main to @patrycjakalinska/with-decay-never-start-issue November 28, 2024 12:59
@patrycjakalinska patrycjakalinska marked this pull request as ready for review November 28, 2024 13:03
@patrycjakalinska patrycjakalinska changed the base branch from @patrycjakalinska/with-decay-never-start-issue to main December 16, 2024 10:35
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.

withRepeat not working with withDecay
1 participant