-
-
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
Add support for spring duration #4179
Add support for spring duration #4179
Conversation
24b600b
to
b56f8b5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me is great! But let's wait with merge since kmagiera opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, we forgot about typescript and about adding proper types to d.ts
b56f8b5
to
bfe9455
Compare
Discussed that with @piaskowyk today but wanted to post the summary anyways;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok that we don't take velocity into account when calculating damping/mass?
c674f6d
to
6fbcf5f
Compare
3ce1ef6
to
9a7fd1c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we limit duration-based animation to underdamped case only (over-damped seem to have an easier mechanics). On top of that it looks like we are not handling the case of overriding an ongoing spring animation with duration. I'd suggest that you try and replace this line https://github.com/software-mansion/react-native-reanimated/blob/main/Example/src/AnimatedStyleUpdateExample.tsx#L20 with spring animation with duration – then when you tap the button while the animation is still running, in case of timing it will only change the destination but it will finish at the designated time. With spring animation it seem like the new animation will start from new point and on top of that velocity will be reset, so there will be no smooth transition between animation from one target to the other?
618557e
to
4beb109
Compare
Yes, but I don't think that average user will be able to see it. I've decided to set it to zero, since if we modify mass to change the duration we get this formula LINK. So unless
Type of the motion depends on
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting very close, just a couple more comments I added inline
useAnimatedStyle, | ||
Easing, | ||
withSpring, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please revert all the changes to this file. It was meant to be the most basic example of using animations inside uAS. We link to it in bunch of places. If we want, we can swap out timing with spring but don't make it any more sophisticated than that (i.e. don't add more components etc)
src/reanimated2/animation/spring.ts
Outdated
animation.current = newPosition; | ||
animation.velocity = newVelocity; | ||
|
||
if (isOvershooting || (isVelocity && isDisplacement)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd been discussed somewhere before but perhaps got lost: we want for animations with duration set to have additional stop condition such that it ends at the exact moment in time. This is needed because duration could be used to synchronize things happening on screen and we need to make sure the animation finishes exactly when requested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been thinking you mean cases when the obtained animation is too long and we want to terminate it earlier, but it looks like you want to "animate a static string" as well. IMHO both situations are very different so I've added four options to specify exactDuration
parameter, instead of a single boolean flag. 🤔
d27bf6e
to
7af0989
Compare
7af0989
to
c2e63e5
Compare
a95cdad
to
f747991
Compare
src/reanimated2/animation/spring.ts
Outdated
t, | ||
}); | ||
|
||
const { isOvershooting, isVelocity, isDisplacement } = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seem to be no point in calling this method when duration is set. We should collocate the final condition for the case when duration is set and not set to make this cleaner
src/reanimated2/animation/spring.ts
Outdated
dampingRatio: 0.5, | ||
} as const; | ||
|
||
const config = { ...defaultConfig, ...userConfig }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const config = { ...defaultConfig, ...userConfig }; | |
const config = { ...defaultConfig, ...userConfig, useDuration: userConfig.duration || userConfig.dampingRatio }; |
721aff0
to
edee401
Compare
edee401
to
2a7833b
Compare
## Summary We have decided to introduce two new config props - damping ratio and duration to make usage of `withSpring` easier. ```mermaid graph LR; A[Use<br>withSpring<br>animation] --> B; B{Did user provide<br>config with duration or<br>dampingRatio?} --YES-->C[Calculate spring movement using default or provided<br>mass and damping. Defaults:<br>damping 10, mass 1]; B--NO-->D[Calculate spring movement using default or provided<br>duration and damping ratio. Defaults:<br>duration: 2, dampingRatio: 0.5]; ``` ### Implementation When starting an animation with predefined duration we have to find a mass that would give us the expected duration. Thus we have to solve a simple numeric equation. We do it with bisection, since Newton method appears to be much slower in this case (it happens, when finding roots of functions that grow really fast 📈) <details><summary>Testing that Newton's method is much slower</summary> https://user-images.githubusercontent.com/56199675/233330812-5eb31550-f77d-4b68-8765-5131ba18c2d1.mov </details> ## Recordings In all the examples below duration is set to 5000ms <table> <tr><td>dampingRatio=1</td><td>dampingRatio=0.5</td></tr> <tr><td> https://user-images.githubusercontent.com/56199675/233336875-b2666629-f3e9-403e-b629-fd44efba0dea.mov</td><td> https://user-images.githubusercontent.com/56199675/233337127-433077d3-2017-4fde-8c1e-647c0ab52600.mov </td></tr> <tr><td>dampingRatio=1000</td><td>dampingRatio=0.1</td></tr> <tr><td> https://user-images.githubusercontent.com/56199675/233337155-a8f07c34-d65e-4161-ba47-b8fc9992482a.mov </td><td> https://user-images.githubusercontent.com/56199675/233337200-67b1719e-5963-4e65-b187-551cf80966cf.mov </td></tr> </table> ## Test plan Run demo --------- Co-authored-by: Aleksandra Cynk <aleksandracynk@Aleksandras-MacBook-Pro.local> Co-authored-by: Aleksandra Cynk <aleksandracynk@swmansion.com>
Summary
We have decided to introduce two new config props - damping ratio and duration to make usage of
withSpring
easier.Implementation
When starting an animation with predefined duration we have to find a mass that would give us the expected duration. Thus we have to solve a simple numeric equation. We do it with bisection, since Newton method appears to be much slower in this case (it happens, when finding roots of functions that grow really fast 📈)
Testing that Newton's method is much slower
Screen.Recording.2023-04-20.at.11.01.07.mov
Recordings
In all the examples below duration is set to 5000ms
Screen.Recording.2023-04-20.at.12.11.53.mov
Screen.Recording.2023-04-20.at.12.13.11.mov
Screen.Recording.2023-04-20.at.12.14.11.mov
Screen.Recording.2023-04-20.at.12.15.20.mov
Test plan
Run demo