Mastering Expressions in After Effects

Unlocking Advanced Animation Techniques with Creative Code

LoopIn Expression

This expression enables you to loop anything that occurs before the first keyframe.

loopIn()

LoopOut Expression

This expression allows you to loop everything after the last keyframe.

loopOut()

Ping Pong Expression

This expression lets you loop back and forth between two keyframes, often referred to as the “boomerang” expression. 

loopOut("pingpong") or loopIn("pingpong")

Loop Continue Expression

This expression uses the last keyframe to maintain its direction and speed, extending the movement until the end of your timeline. This expression is particularly useful if you want a layer to continue moving over time.

loopOut("continue");

Loop Cycle

The loop cycle expression is essentially the default setting; writing loopOut("cycle") is the same as simply writing loopOut().

loopOut("cycle")

Loop Offset

This expression allows you to repeat the first or last keyframe, or offset the path over time.

loopOut("offset")

Loop Duration

The loop duration feature allows you to pause and repeat your loop after a specified number of seconds. For instance, loopOutDuration("cycle", 3) will repeat your loop every 3 seconds.

loopOutDuration("cycle",3)

Floating Layer Expression

This is ideal if you want a layer to float up and down, simulating an object bobbing in space or water. It essentially conveys the weightlessness of the object. You can apply this code to the layer’s position and scale.

amp = 250;

freq = 1;

y = amp*Math.sin(time*freq*Math.PI*2);

value + [0,y,0]

Floating Bounce Layer
with more control

Similar to the previous expression, this one allows you to control the movement and frequency of your layer.

ampY = 60;

ampZ = 150;

freqY = 0.5;

freqZ = 2;

y = ampY*Math.sin(time*freqY*Math.PI*2);

z = ampZ*Math.cos(time*freqZ*Math.PI*2);

value + [0,y,z]

Delay and Index a Layer

The Delay and Index expression allows child layers that are parented to a main layer to follow its movement with a time delay.

// Sets a delay amount in frames
var delay = 5;

// Multiplies delay based on this layer's index relative to it's parent
var multiplyDelay = delay * ( index - parent.index )

// Offsets layer's Position in time based on delay
parent.fromComp( toComp( anchorPoint, time - framesToTime( multiplyDelay ) ) );

Wiggle Simple Expression

The first number, 5, defines how many times your layer will wiggle per second. In this case, it moves 5 times within 1 second. The second number, 10, determines the amount of movement, specifying that the layer will move up and down by 10 pixels.

wiggle(8,120)

Start & Stop wiggle expression

Using a slider control, you can easily manage when your wiggle motion starts and stops.

wiggle(thisComp.layer("Null 1").effect("Slider Control")("Slider"),10)

Wiggle Height Vertical expression

This Wiggle expression allows you to animate movement vertically along the Y-axis.

org=value; temp=wiggle (8,40); [org[0],temp[1]];

Wiggle Horizontal

Here’s an expression to create a wiggle effect horizontally along the X-axis.

org=value; temp=wiggle (5,120); [temp[0],org[1]];

Wiggle with Depth on Z axis

Convert your layer to a 3D layer and use this expression to create a wiggle effect along the Z-axis.

a =wiggle(0,0);

b =wiggle(0,0);

c = wiggle(5,95);[a[0],b[1],c[2]]

Uniform Scale Wiggle

This expression allows you to wiggle the scale of your layer proportionally.

w = wiggle(5, 80);

[w[0],w[0]]

Wiggle Seamless Loop Expression

This wiggle expression allow you to loop perfectly. Just don’t forget to adjust the loopTime base on your composition timeline

freq = 1;

amp = 110;

loopTime = 3;

t = time % loopTime;

wiggle1 = wiggle(freq, amp, 1, 0.5, t);

wiggle2 = wiggle(freq, amp, 1, 0.5, t - loopTime);

linear(t, 0, loopTime, wiggle1, wiggle2)

Inertial Bounce Expression

This bounce expression allow you to have a elastic feel to your layer position.

amp = .1;

freq = 2.0;

decay = 2.0;

n = 0;

if (numKeys > 0){

n = nearestKey(time).index;

if (key(n).time > time){

n--;

}}

if (n == 0){ t = 0;

}else{

t = time - key(n).time;

}

if (n > 0 && t < 1){

v = velocityAtTime(key(n).time - thisComp.frameDuration/10);

value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);

}else{value}

Scale Bounce Expression

Here is a super cool expression to make your layer bounce like jello or gelatin.

timeToStart = .5;

if (time > timeToStart) {

maxDev = 30; // max deviation in pixels

spd = 30; //speed of oscillation

decay = 1.0; //how fast it slows down

t = time - inPoint;

x = scale[0] + maxDev*Math.sin(spd*t)/Math.exp(decay*t);

y = scale[0]*scale[1]/x;

[x,y]

}

 else {

value;

}

Blink Expression

On your layer opacity apply this script to make it blink, you can also customize the syntax if it blink to fast.

blinkSpeed=15;

n= Math.sin(time*blinkSpeed);

if(n<0) 0 else 100;

Normal Bounce Expression

First create an animation, next apply bellow’s expression to the property you’ve key-framed to make the animation bounce.

n = 0;

if (numKeys > 0){

n = nearestKey(time).index;

if (key(n).time > time){

n--;

}

}

if (n == 0){

t = 0;

}else{

t = time - key(n).time;

}

if (n > 0 && t < 1){

v = velocityAtTime(key(n).time - thisComp.frameDuration/10);

amp = .06;

freq = 3;

decay = 5.0;

value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);

}else{

value;

}

Squash and Stretch Expression

Apply it to the scale attribute of any object. Works best on icons or bullet points.

maxDev = 13; // max deviation in pixels
spd = 30; //speed of oscillation
decay = 1.0; //how fast it slows down
t = time - inPoint;
x = scale[0] + maxDev*Math.sin(spd*t)/Math.exp(decay*t);
y = scale[0]*scale[1]/x;
[x,y]