Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native animations - translateX and translateY while scaling

Tags:

My Animated.View has the following style:

    {
        transform: [
            {
                scale: this.animatedValue.interpolate({
                    inputRange: [0, 1],
                    outputRange: [initialScale, 1]
            })},
            {
                translateX: this.animatedValue.interpolate({
                    inputRange: [0, 1],
                    outputRange: [startX, endX]
            })},
            {
                translateY: this.animatedValue.interpolate({
                    inputRange: [0, 1],
                    outputRange: [startY, endY]
            })},
        ]
    }

When initialScale is 1 and the animation starts, I see the expected behavior: Animated.View starts at (startX, startY) and linearly moves to (endX, endY).
However, when initialScale is 0.5 for example, the starting point of the view is not (startX, startY), the movement is not linear (a bit spheric) and the end point is still as expected - (endX, endY).

How can I scale my View while keeping a linear movement and expected start position?

like image 733
nirsky Avatar asked Jan 24 '17 14:01

nirsky


People also ask

How do you increase the speed of animation in React Native?

Enabling usage of native driver is the easiest way of improving your animations performance quickly. However, the subset of style props that can be used together with native driver is limited. You can use it with non-layout properties like transforms and opacity. It will not work with colors, height, and others.

How do you handle animation in RN?

Working with animations​Animations are started by calling start() on your animation. start() takes a completion callback that will be called when the animation is done.

How do you interpolate in React Native?

In React Native, you can interpolate animated values using . interpolate which takes an inputRange, that interpolates and maps the values to an outputRange. Great thing is that you can interpolate numbers not just to other numbers but also to colours and degrees.

How do you use the transition effect in React Native?

The transition animation is one of the easiest ways to easily animate a React Native application. It provides a way to create a seamless change between various states in your application. In this tutorial, we will be exploring the Transition API and a use-case where the transition animation can be employed; Dark mode.


1 Answers

Like the user @ArneHugo pointed out in the comments, the non-linear movement can be solved by positioning the full-size container element and scaling another element within it.

The position of the element is not as expected, because the origin for the scale transform is the center point of the element. React Native doesn't (yet) support specifying the transform origin, but if the width and height of the scaled element are known in advance, it's easy to calculate the offset as follows:

const width = 100;
const height = 20;
const scale = {
  transform: [
    {
      scale: this.animatedValue.interpolate({
        inputRange: [0, 1],
        outputRange: [initialScale, 1]
      })
    }
  ]
};

const position= {
  transform: [
    {
      translateX: this.animatedValue.interpolate({
        inputRange: [0, 1],
        outputRange: [startX - (width / 2) - (width * initialScale / 2), endX]
      })
    },
    {
      translateY: this.animatedValue.interpolate({
        inputRange: [0, 1],
        outputRange: [startY - (height / 2) - (height * initialScale / 2), endY]
      })
    }
  ]
};

return (
  <Animated.View style={position}>
    <Animated.View style={[styles.thing, scale]} />
  </Animated.View>
);
like image 59
jevakallio Avatar answered Sep 29 '22 05:09

jevakallio