DEV Community

Cover image for After Effects: Favourite Expressions Part 1, Linear() and Ease()
Kat
Kat

Posted on • Updated on

After Effects: Favourite Expressions Part 1, Linear() and Ease()

Introduction

Now I've established the basics of After Effects, and why I like to use expressions in tandem with my work, I can finally start highlighting my favourite Expressions.

First up, is the linear() and ease() functions.

linear (t, tMin, tMax, value1, value2);

ease (t, tMin, tMax, value1, value2);
Enter fullscreen mode Exit fullscreen mode

ECAbrams has a wonderful video tutorial on these functions. I highly recommend watching this video to understand how the functions work in detail.

In short, these functions can be used to remap values, using linear impolation, or easing. What does that mean? The linear function remaps values evenly between two desired points, while the easing function remaps on a curve, slowly easing in and out of the motion. This allows us to link one type of data to another, such as connecting rotation to opacity, or position to time.

Both functions require the same 5 arguements.
t = the parameter we are using to remap our value.
tMin = the minimum parameter value we are telling the function to look at.
tMax = the maximum parameter value we are telling the function to look at.
value1 = the first value we are remapping to.
value2 = the second value we are remapping to.

Remapping Values Using Time

My favourite use of these functions is remapping values with time.

For this example, I will be looking at the position parameter.

I would like to create an expression, which would allow me to move my layer 500 pixels on the x axis, between the first and second seconds of the video. Taking into account that the position parameter needs to be an array, an x and y value, for After Effects to understand the coordinates, we could start with something like this:

var x = linear (time, 1, 2, value[0], value[0]+500);
[x, value[1]]
Enter fullscreen mode Exit fullscreen mode

Let's break this down.

First, we create the variable x, to put the linear function inside.

The linear function is being told to look at "time" while written inside of the position parameter. It's tMin and tMax are set to 1 and 2 respectively, meaning that it will remap values between the first and second seconds of the video.

value[0] is the position's x value, while value[1] is the position's y value (and if this layer was 3D, position[2] would be the z value). So our value1 is set to our x coordinate's default value, while value2 is set to our default value plus 500.

Finally, we add our variable to our array, making sure to reference the default y value for our y coordinate.

If we want the motion to appear smoother, we can use the ease function instead:

var x = ease (time, 1, 2, value[0], value[0]+500);
[x, value[1]]
Enter fullscreen mode Exit fullscreen mode

Now instead of a straight linear motion, our layer will ease from one position to the other.

Using More Than One Instance Of Linear Or Ease

This works well enough when we only need to move between 2 values. But what if we wanted to create an expression to animate the layer in, and out again?

This is where we can get a little more creative.

Sticking with the position parameter, we need to set up 2 different ease functions, our "inAnimation" and our "outAnimation":

var inAnimation = ease(time, 1, 2, value[0], value[0]+500);
var outAnimation = ease(time, 4, 5, value[0]+500, value[0]);
Enter fullscreen mode Exit fullscreen mode

Our inAnimation is the same function as our previous example. While our outAnimation has its value1 and value2 reversed, and is remapping between 4 and 5 seconds of our video.

Now that we have our in and out points established, we can use a simple if statement to toggle between the two functions:

if (time < 4) [inAnimation, value[1]]
else [outAnimation, value[1]]
Enter fullscreen mode Exit fullscreen mode

With this statement, we are simply telling After Effects, "If time is less than 4 seconds, use the inAnimation variable. However, if time is equal to or more than 4 seconds, use the outAnimation variable. This allows us to create in and out motions, without keyframes.

We may also want to push this further by creating a variable for the in and out points of the animation:

var inPoint = 1;
var outPoint = 4;

var inAnimation = ease(time, inPoint, inPoint+1, value[0], value[0]+500);
var outAnimation = ease(time, outPoint, outPoint+1, value[0]+500, value[0]);

if (time < outPoint) [inAnimation, value[1]]
else [outAnimation, value[1]]
Enter fullscreen mode Exit fullscreen mode

By creating variables for our in and out points, we can customise our expressions without having to change our values in several places. Because we want to keep our animation the same length as before, we simply have to tell After Effects that our tMax will always be our inPoint or outPoint + 1.

Or, if we want to experiment with the length of our animation, we can create a variable for that too:

var inPoint = 1;
var outPoint = 4;
var aniDuration = 1;

var inAnimation = ease(time, inPoint, inPoint+aniDuration, value[0], value[0]+500);
var outAnimation = ease(time, outPoint, outPoint+aniDuration, value[0]+500, value[0]);

if (time < outPoint) [inAnimation, value[1]]
else [outAnimation, value[1]]
Enter fullscreen mode Exit fullscreen mode

Now we can change the value of our aniDuration variable, to reflect the duration of our in and out animations in seconds.

Any questions? Feel free to leave a comment and ask!

Top comments (0)