# Expression Examples

Copy Codes

Click the code box, and then click the lower right corner to copy.

# Wiggle

Open the expression of the [Transform - Position] parameter, then copy & paste the following code:

wiggle(5,1)

# Loop

First add two keyframes for [Transform - Position] to make the layer move, then open the expression, copy & paste the following code:

loopIn()

# Layers fall one by one

Open the expression of the [Transform - Position] parameter, and copy & paste the following code. Then copy the layer multiple times, such as 5, and each layer will start to animate in turn after playback.

spd=2.0
interval=0.2
[0.5,easeOut(time*spd-index*interval,1.5,0.5)]

# Countdown

Create a [Text] layer, open the expression of the [Text] parameter, and copy the following code:

n=1
start=10
suffix=""
toFixed(start-time,n) + suffix

# Show Timecode

Create a [Text] layer, open the expression of the [Text] parameter, and copy the following code:

timeToTimecode()

# Display a random number every second

Create a [Text] layer, open the expression of the [Text] parameter, and copy the following code:

posterizeTime(1)
toFixed(random(0,100),0)

# Surround Sound

Import any music, open the [Audio - Left Channel - Volume] expression, and copy the following code:

spd=2.0
(sin(time*spd)+1)*0.5

Continue to open the [Audio- Right Channel - Volume] expression, and copy the following code:

spd=2.0
(sin(time*spd+pi)+1)*0.5

# Keep layers from rotating with group

By default, the layer is rotated with the group. If you want to keep a certain layer still, you can open the expression of the [Transform - Rotation] parameter and copy the following code:

parent = thisLayer.parent
//parentRot = 0
parentRot=value*0

while parent != null
    parentRot += parent.rotation
    parent = parent.parent
end while

//-parentRot
parentRot*-1

# Keep layer centered between two layers

Create any two layers and apply keyframe animation to their [Transform - Position]. Create another layer, place it between them, and then open its [Transform - Position] expression, copy the following code. The layer will remain in the middle of the left and right layers.

(layer(-1).position+layer(1).position)/2

# Jiggle

Open the expression of the [Transform - Scale] parameter, and copy the following code:

spd =20
amplitude =0.5
decay = 3
startTime=0

if time < startTime then
    value
else
    offset = amplitude*sin(time*spd)/exp(time*decay)
    scaleX = value[0] + offset
    scaleY = value[1] - offset
    [scaleX,scaleY]
end if

# Bounce

First add two keyframes for [Transform - Position] to make the layer move, then open the expression, and copy the following code. If the effect is too small or too large, please modify the strength parameter by yourself.

elasticity = 0.7//Range[0-1]
gravity = 5
maxCount = 9
strength = 30

n = nearestKey(time).index

if key(n).time > time then
  n -= 1
end if

if n > numKeys - 2 then
  t = time - key(n).time
  v = value * 0 - velocityAtTime(key(n).time - frameDuration / 10) * elasticity
  vl = length(v) * strength
  if vl > 0 then
  vu = normalize(v)
else
  vu = value * 0
end if


tCur = 0
segDur = 2 * vl / gravity
tNext = segDur
nb = 1
while tNext < t and nb <= maxCount
  vl *= elasticity
  segDur *= elasticity
  tCur = tNext
  tNext = tNext + segDur
  nb += 1
end while


if nb <= maxCount then
  delta = t - tCur
  value + vu * delta * (vl - gravity * delta / 2)
else
  value
end if

else
    value
end if

# Elastic Bouncing

First add two keyframes for [Transform - Position] to make the layer move, then open the expression, and copy the following code. If the effect is too small or too large, please modify the strength parameter by yourself.

amp = 1
freq = 3
decay = 4
duration = 2

nK = nearestKey(time)


if nK.time<=time then
   n=nK.index
else
    n=nK.index-1
end if


if n != 0 then
    t=time - key(n).time
end if

if n > numKeys - 2 and t < duration then
    v = velocityAtTime( key( n ).time - frameDuration/10 ) 
    value + v * amp * sin(freq * t * 2*pi) / exp(decay * t)
else
    value
end if