# Code Bites AE Fun JavaScript for After Effects

I love writing JavaScript in After Effects. You get to instantly see the results!

Sine Animations

Your basic oscillating wave form. It's fun to make a predictable animation for things like clocks, or HUD images. You can also use them to draw fun lines and graphs.

``````
// SINE ---------------y = position;x = position;freq = 1;amp = 100;sin = amp * Math.sin(freq * time * Math.PI);[x, y + sin]// SINE CLIPPED ---------------y = position;x = position;freq = 1;amp = 100;sin = amp * Math.sin(freq * time * Math.PI);// creates a toggle animation 100 on/offif (sin < 0) sin = -100;if (sin > 0) sin = 100;[x, y + sin]// SINE CLIPPED (SOFT)---------------y = position;x = position;freq = 1;amp = 100;adj = 50;sin = amp * Math.sin(freq * time * Math.PI);if (sin < adj && sin > -adj) {    if (sin > 0) sin = adj;    if (sin < 0) sin = -adj;}[x, y + sin]// SAW ---------------y = position;x = position;loopTime = 1; // loop every X secondst = (time - inPoint) % loopTime;val1 = y - 100;val2 = y + 100;saw = linear(t, 0, loopTime, val1, val2);[x, y + saw]// PENDULUM ---------------y = position;x = position;veloc = 10;amplitude = 100;decay = .1;pendulum = amplitude * Math.sin(veloc * time) / Math.exp(decay * time);[x, y + pendulum]// SPIROGRAPH ---------------y = position;x = position;r1 = 10; // r1 - radius of wheel Ar2 = 50; // r2 - radius of wheel Bo = 100; // o  - offsetv = 5; // v  - speeds = 1.9; // s  - scaler = r1 + r2;// x = r*Math.cos(time*v) - (r2 + o)*Math.cos(r*time*v/r2); sy = r * Math.sin(time * v) - (r2 + o) * Math.sin(r * time * v / r2);[x, y + sy]// WIGGLE (CONTROLED) ---------------y = position; // not usedx = position;l = 8;        // loopTime (in sec)f = 3;         // frequency (per second)a = 100;     // amplituded (size)o = 1;         // octivesm = 0.5;     // amp_multipliert = time % l;     // timew = wiggle(f,a,o,m,t);[x,w] // WIGGLE (QUICK) ---------------y = position; // not usedx = position;f = 3;         // frequency (per second)a = 100;     // amplituded (size)w = wiggle(f,a);[x,w]
`````` Random Color Change

I'm not sure why this was so hard, but Random Colors in AE took way longer then it should have. I hope this saves someone some time. I grabbed “Change Color” from the Effects & Presets plane, dropped it onto a Black Solid that I have masked by an image layer. Next I raised the “Lightness” and “Saturation” to where I thought they felt good. Next I oppened “Hue”

x = number of seconds
f = frames per second
posterizeTime = how many frames to wait before refreshing value
random = any color value between 1° and 360°

``````
x = 3;f = thisComp.frameDuration * x;posterizeTime(1 / f); random(1, 360);
`````` Pull in text from an external file!

I use this script a lot. It allows me to set up the text effects I want in one clip, while keeping the text in a separate txt file.

``````
// In After Effects:filePath = "/Volumes/your path to/TEXT.txt";\$.evalFile(filePath);text.sourceText = eval(thisComp.name); // first position in array// in your TEXT.txt file/*Text1 = ["CARDONE!","Heather is the best!"];Text2 = ["OPTIMIZATION",""];Text3 = ["TV APP",""];Text4 = ["STB TV APP",""];Text5 = ["KID'S ZONE","Until the police arrive."];*/
``````