-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: Make withDecay
compatible with withRepeat
#6772
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
Merged
patrycjakalinska
merged 9 commits into
main
from
@patrycjakalinska/withdecay-withrepeat-compatibility
Jan 30, 2025
Merged
fix: Make withDecay
compatible with withRepeat
#6772
patrycjakalinska
merged 9 commits into
main
from
@patrycjakalinska/withdecay-withrepeat-compatibility
Jan 30, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tjzel
reviewed
Dec 24, 2024
piaskowyk
requested changes
Jan 2, 2025
piaskowyk
approved these changes
Jan 29, 2025
tjzel
pushed a commit
that referenced
this pull request
Feb 17, 2025
## 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: https://github.com/user-attachments/assets/98b64f3e-8a08-4bed-8480-856735e6dc68 After: https://github.com/user-attachments/assets/354c0842-3bab-457c-b75a-d1eea9df782f ## Test plan Run provided code snipped on Fabric example <details> <summary>Code snipped provided in issue</summary> ```ts 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, }, }); ``` </details>
tjzel
pushed a commit
that referenced
this pull request
Feb 17, 2025
## 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: https://github.com/user-attachments/assets/98b64f3e-8a08-4bed-8480-856735e6dc68 After: https://github.com/user-attachments/assets/354c0842-3bab-457c-b75a-d1eea9df782f ## Test plan Run provided code snipped on Fabric example <details> <summary>Code snipped provided in issue</summary> ```ts 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, }, }); ``` </details>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
As it was discovered in #6757,
withRepeat
run along withwithDecay
triggered repeat only once, even if the number of repeats was greater.withDecay
relies on the value ofvelocity
. TheinitialVelocity
is the velocity provided (or default), and during the animation, thevelocity
value decreases, causing the animated object to slowly stop. The problem was, after first repeat thevelocity
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 resetingvelocity
to initial value, iffinished
istrue
.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