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:withDecay animation not starting if inital value is set to 0 #6769

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const withDecay = function (
onFrame: decay,
onStart,
callback,
forceRunAnimation: true,
velocity: config.velocity ?? 0,
initialVelocity: 0,
current: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding that artificial prop, we should calculate the finish position like we do for withTiming, for example. You can see how it's done here: https://github.com/software-mansion/react-native-reanimated/blob/main/packages/react-native-reanimated/src/animation/timing.ts#L154. This solution should be clearer and simpler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see timing doesn't rely on previous current to calculate recent current value, as the decay does it. Wouldn't setting current in onStart to toValue break for example this decay function fragment:

  animation.current = current + (v * config.velocityFactor * deltaTime) / 1000;

Just wondering, because even this fragment can be rewritten to support your idea (so not a problem really)

In conclusion, to fully understand, you want to replace the current with toValue instead of - in this case - 0, so that it will not go into this conditional?

    if (
      mutable._value === animation.current &&
      !animation.isHigherOrder &&
      !forceUpdate
    ) {
      animation.callback && animation.callback(true);
      return;
    }

Should I also mark it as a isHigherOrder as it will hold its target value in .current?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ const IS_WEB = isWeb();
export const VELOCITY_EPS = IS_WEB ? 1 / 20 : 1;
export const SLOPE_FACTOR = 0.1;

/*
* The `forceRunAnimation` prop is necessary to run withDecay animation when
* related sharedValue stays as `0`. It allows to skip animation check and start
* animation immediately
*/

export interface DecayAnimation extends Animation<DecayAnimation> {
lastTimestamp: Timestamp;
startTimestamp: Timestamp;
initialVelocity: number;
velocity: number;
current: AnimatableValue;
forceRunAnimation: boolean;
}

export interface InnerDecayAnimation
extends Omit<DecayAnimation, 'current'>,
extends Omit<DecayAnimation, 'current' | 'forceRunAnimation'>,
AnimationObject {
current: number;
springActive?: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-reanimated/src/valueSetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function valueSetter<Value>(
// built in animations that are not higher order(withTiming, withSpring) hold target value in .current
if (
mutable._value === animation.current &&
!animation.forceRunAnimation &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like this check has a purpose (explained above), does introducing this flag not defeat that purpose? i think this needs to be rethought more holistically with the above comment in mind

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear i'm not a maintainer, just thinking based on the code

!animation.isHigherOrder &&
!forceUpdate
) {
Expand Down
Loading