Timing Animations and Easing Curves

+15 Mana ✨

Animated.timing() is the animation type used in most cases. It moves a value to a target over a set number of milliseconds, and an easing curve decides how that movement is spread over the time. The same 300 ms slide can move at one steady speed or ease into its resting place, depending only on the curve you pass.

Key terms

  • Animated.timing: animates a value to toValue along a timed easing curve and returns a CompositeAnimation.
  • Easing: the module exported by react-native that implements common easing functions for Animated.timing().
  • Easing function: a function that takes a progress number t and returns a number, typed as EasingFunction.
  • Easing.inOut: a helper that makes any easing function symmetrical, running it forwards for the first half of the duration and backwards for the rest.

How timing turns elapsed time into a value

On the JS driver, each frame of a timing animation takes the fraction of the duration that has elapsed, passes it to the easing function, and scales the result between the start and end values. With a linear curve, the halfway moment puts the value halfway. With any other curve, the easing function decides how far along the value is at that moment.

Timing options and their defaults

The config takes the target in toValue. Three optional settings shape the trip:

  • duration: the length of the animation in milliseconds, 500 by default.
  • delay: how many milliseconds to wait before starting, 0 by default. A non-zero delay postpones the start with setTimeout.
  • easing: the curve, Easing.inOut(Easing.ease) by default.

A config with only toValue (plus the required useNativeDriver) therefore starts at once, runs for 500 ms, and follows a symmetric easeInOut curve: gradual acceleration, then gradual deceleration to a stop. Pass easing to replace that curve with one from Easing or with your own function of the same shape.

The curves in Easing

The basic curves map progress to progress with a formula:

  • Easing.linear is f(t) = t, so position follows elapsed time one to one.
  • Easing.quad is t * t and Easing.cubic is t * t * t. Easing.poly(n) takes the exponent and gives position as the Nth power of elapsed time.
  • Easing.ease is a basic inertial curve, like an object slowly accelerating to speed. It is implemented as the cubic bezier (0.42, 0, 1, 1).
  • Easing.bezier(x1, y1, x2, y2) builds a cubic bezier curve equivalent to the CSS transition-timing-function.

Other curves pull back, overshoot or bounce. Easing.back(s) makes the object go slightly back before moving forward. Its parameter s defaults to 1.70158. Easing.elastic(bounciness) overshoots: a bounciness of 1, the default, overshoots a little once, 0 doesn't overshoot, and a value N above 1 overshoots about N times. Easing.bounce gives a bouncing animation. Easing.step0 jumps instead, returning 1 for any positive input.

Reshaping a curve with in, out and inOut

The three helpers take an easing function and return a new one. Easing.in(fn) runs it forwards. Easing.out(fn) runs it backwards, computed as 1 - fn(1 - t). Easing.inOut(fn) runs it forwards for the first half of the duration and backwards for the rest, which is where the default Easing.inOut(Easing.ease) gets its symmetric shape.

Direction matters a lot. At the halfway point Easing.cubic returns 0.125, so Easing.in(Easing.cubic) has covered an eighth of the distance. Easing.out(Easing.cubic) returns 1 - 0.5 * 0.5 * 0.5, which is 0.875, so it has already covered seven eighths and spends the second half on the last eighth.

The easing reference lists in, out and inOut as taking a number, which would lead you to write Easing.out(3). The type definitions declare all three as taking an EasingFunction and returning one, so pass a curve such as Easing.cubic.

Sliding a receipt panel into place

A panel entering from below is a good fit for out: it covers most of the distance early and slows down as it reaches its spot. The ReceiptPanel component below sets all three timing options.

duration: 350 and delay: 150 replace the 500 and 0 defaults, and easing: Easing.out(Easing.cubic) replaces the symmetric default curve. The value is created at 240 and animates translateY to 0, and since transforms can run on the native driver, the config sets useNativeDriver: true. To make the panel drop slightly before it rises, swap the curve for Easing.back(), which moves backwards first.

Code Examples

tsx
import {useEffect} from 'react';
import {Animated, Easing, Text, useAnimatedValue} from 'react-native';

export function ReceiptPanel({total}: {total: string}) {
  const translateY = useAnimatedValue(240);

  useEffect(() => {
    Animated.timing(translateY, {
      toValue: 0,
      duration: 350,
      delay: 150,
      easing: Easing.out(Easing.cubic),
      useNativeDriver: true,
    }).start();
  }, [translateY]);

  return (
    <Animated.View style={{transform: [{translateY}]}}>
      <Text>Total: {total}</Text>
    </Animated.View>
  );
}
✓ Completed