Skip to content

Animated.diffClamp does not account for diff in NativeAnimated #9252

@rozele

Description

@rozele

Problem Description

In the NativeAnimatedModule, Animated.diffClamp is implemented as a simple clamp on the value. This is not the intent of the Animated.diffClamp node. Animated.diffClamp should add the difference between the previous value and the current value to the current value and clamp that. NativeAnimated only clamps the current value.

Steps To Reproduce

  1. Add a simple example to RNTester for Animated.diffClamp, e.g.:
  {
    title: 'Opacity with diffClamp',
    render: function (): React.Node {
      return (
        <Tester type="timing" config={{duration: 4000}}>
          {(anim) => (
            <Animated.View
              style={[
                styles.block,
                {
                  opacity: Animated.diffClamp(
                    anim.interpolate({
                      inputRange: [0, 0.5, 1],
                      outputRange: [0, 1, 0],
                    }),
                    0,
                    0.5,
                  ),
                },
              ]}
            />
          )}
        </Tester>
      );
    },
  },
  1. Run the example
  2. The diffClamp should add the diff to the current value, so you should see this animation run slightly faster than the configured 4 seconds (given the diff is added or subtracted from the value).
  3. Since NativeAnimated only clamps the value, the animation starts animating opacity from 0.5 back to 0 at around the 75% mark, whereas the JS driver starts reducing the opacity much sooner.

Expected Results

The NativeAnimated behavior should match the JS driver behavior.

CLI version

npx react-native --version

Environment

npx react-native info

Target Platform Version

No response

Target Device(s)

No response

Visual Studio Version

No response

Build Configuration

No response

Snack, code example, screenshot, or link to a repository

React.Native.Playground.Win32.2021-12-08.13-48-29.mp4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions