ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Expression to define two positions, tween back and forth

FAQ   •   VIEW ALL
 Expression to define two positions, tween back and forth on Jan 30, 2014 at 10:13:50 pm

So I want to do something simple: define two sets of coordinates (a "from" and a "to" position), along with a duration in frames. I want the animation to be executed, then reversed, and looped.

I know I can set two keyframes, and use `loopOut("pingPong");` but I want to try and achieve this with a simple expression.

I'm sure it's possible!

 Re: Expression to define two positions, tween back and forthon Jan 30, 2014 at 10:39:45 pm

[Benjamin Allison] "I know I can set two keyframes, and use `loopOut("pingPong");` but I want to try and achieve this with a simple expression."

loopOut("pingpong") is a simple expression! :)

You won't be able to define any spatial paths any other way, but this should work:

```d = 5; fd = d * 2; p1 = [200, 500]; p2 = [500, 200]; t = (time - inPoint) % fd; if(t <= d) linear(t, 0, d, p1, p2); else linear(t, d, fd, p2, p1); ```

This starts from p1 at the beginning of the layer and runs to p2 over 5 seconds, then goes back to p1 and begins again. If you want the animation to ease in to each point you could use the ease() function instead of linear() providing the same arguments.

-Darby

Darby Edelen

 Re: Expression to define two positions, tween back and forthon Jan 30, 2014 at 10:46:24 pm

You're a king among men!

The concept is so simple I can't believe an hour of googling got me zilch.

Thanks so much Darby!