All our expressions are written for the newer JavaScript Expressions Engine

Blog

How to animate with expressions (intro to eKeys)

Photo of Tim Haywood
Tim Haywood

|

July 16, 2019

When making Motion Graphics Templates in After Effects, or any other automated work, there comes a time when you wish you were able to add and modify keyframes with expressions.

While you're able to get some functionality using the linear() or ease() functions, this quickly gets messy, and you're still not able to modify the easing like you are with keyframes.

eKeys solves this problem by giving you an easy way to create keyframes in expressions.

In this blog post, you'll learn how to get starting using eKeys in your projects.

What is eKeys

eKeys is an importable animation engine for After Effects that allows you to animate within expressions, with the same level of control as traditional keyframes.

It comes in the form of a .jsx file that's imported into your project, with the properties for each keyframe added/modified within the expression.

eKeys code on the left and the generated keyframes on the right

Installation

You can download the latest version of eKeys from the tools page. You can also find some more detailed information more detailed information there on using eKeys.

Once downloaded, all you need to do is import the file into your After Effects project like you would any other footage item.

It's recommended that you also place the eKeys.jsx file in any compositions where it is referenced. This will ensure After Effects includes the file when collecting assets or packaging into a Motion Graphics Template.

Writing the expression

To start using eKeys in an expression, you first need to create a reference to the file we imported earlier.

js
const eKeys = footage("eKeys.jsx").sourceData;

Then we can create our keyframe objects.

The Keyframe Array

eKeys represents each keyframe as an object in an array, with properties that define how to animate.

keyframe icons in an array, and the object properties of the first keyframe

Below is two example keyframes that will animate a position property over two seconds.

js
const eKeys = footage("eKeys.jsx").sourceData;
const keys = [
{
keyTime: 1,
keyValue: [100, 540],
easeOut: 90,
},
{
keyTime: 2,
keyValue: [800, 540],
easeIn: 100,
},
];

eKeys expects each keyframe to have a keyTime and keyValue property, while all the other properties are optional.

The format of each of these properties is

  • keyTime

    The location of the keyframe on the timeline, in seconds

  • keyValue

    The value of the keyframe, which can be a number or array

  • easeIn and easeOut

    The amount of easing to be applied as a number between 0 and 100 (both defaulting to 33).

    This works the same way as the influence percentage on traditional keyframes.

  • velocityIn and velocityOut

    The incoming and outgoing velocity of the keyframe (both defaulting to 0).

You can find more information on these properties on the [tools page][/tools/ekeys].

eKeys sorts the keyframes by their time before animating, so you don't have to put them in the right order in your expression. However, your expressions will be easier to read if you do.

Returning the final animation

You can now get the final animated value by calling the animate() function, and passing it your keyframe array and a time value.

js
const eKeys = footage("eKeys.jsx").sourceData;
const keys = [
{
keyTime: 1,
keyValue: [100, 540],
easeOut: 90,
},
{
keyTime: 2,
keyValue: [800, 540],
easeIn: 100,
},
];
eKeys.animate(keys, (time = thisLayer.time));

You can call eKeys.animate() with any number to return the animated value at that time. This allows you to get the value at a specific time, reverse the animation, or play the animation at different speeds.

If you omit the time parameter, the composition time is used as a default.

You can also destructure the animate function from the "eKeys" file:

js
const { animate } = footage("eKeys.jsx").sourceData;

Why use eKeys

While the example in this post would have been easier to create with traditional keyframes, the real benefit of eKeys comes when using JavaScript to set the keyframe properties dynamically.

eKeys simplifies tasks such as:

  • Creating dynamic templates that respond to user input
  • Animating between wiggles or parents with easing
  • Creating After Effects tools and presets

Being able to create keyframes with JavaScript also has the benefits of being able to create and modify the keyframe array programmatically.

One example of this is setting default parameters with the spread syntax.

js
const keyDefaults = {
easeOut: 80,
easeIn: 100,
velocityOut: 40,
};
const keys = [
{
keyTime: 1,
keyValue: someValue,
...keyDefaults,
},
];

Blog

For enquiries contact us at hey@motiondeveloper.com