# Looping Wiggle()

Posted by laurenskuiper on / 0 Comments

Without question, wiggle() is an extremely handy and versatile tool for adding an element of randomness to a project. Sometimes though, it would be extremely useful to be able to get the wiggle motion to loop. Here we’ll take a look at a way to accomplish this by using a bit of clever math and taking advantage of one of wiggle()’s seldom used parameters.

In its fully populated form, wiggle() can take up to five parameters. We only need to use the first, second and fifth parameters for this task. However, to get to the fifth parameter, we have to specify the third and fourth paramters. So we’ll just plug in the default values (the values that After Effects uses if only two parameters are specified).

#### Copy Paste all

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)

#### How does it work

Create variable “freq” to represent wiggle frequency and set it to one wiggle per second.

freq = 1;

Create variable “amp” to represent wiggle amplitude and set it to 110 pixels.

amp = 110;

The variable loopTime will represent the lenght of our wiffle loop (3 seconds).

loopTime = 3;

Variable “t” represents how far we are in the current loop cycle. By using the JavaScript modulus operator (%) we get a time value that resets to zero when it reaches 3 seconds.

t = time % loopTime;

“wiggle1” is a variable that we use to capture the wiggle value that occurs between zero and 3 seconds. Note that we have specified the default values for wiggle()’s 3rd and 4th parameters so that we can get to the 5th parameter (time) where we plug in our time variable, “t”.

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

Notice that “wiggle2” is calculated the same way as “wiggle1” except for the time parameter. Here we’re using a time that runs from -3 to zero.

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

Finally, we blend the two wiggles using the linear() interpolation method. At “t” goes from 0 to 3, the output of linear() starts at the value of “wiggle1” and ends at the value of “wiggle2”.

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

Goodluck!