// method 1: using mapget character index let array = [1,5,6,8,10],sum;array =array.map(elem => sum = (sum ||0) + elem);// method 2: using reduce and maplet array = [280,430,408,430,408];array =array.map((elem, index) =>array.slice(0,index +1).reduce((a, b) => a + b));// best method: using double arrow functionsvar array =[280,430,408,430,408]result =array.map((s => a => s += a)(0));result;
Comparing a value to elements, filtering conditions
let array = [1,3,5,7,10];v =6;// method 1: conditional -> sum of array row = (array.map(c => v >= c ?1:0)).reduce((a, b) => a + b,0)// method 2: get index of true results, then get max valuerow =Math.max(...array.map(c => v>=c?array.indexOf(c):0))// best method: filter row =array.filter(c => v >= c).length
//If you want it relative to the comp’s [0,0] (upper left corner)[length(xyArray),radiansToDegrees(Math.atan2(xyArray[1],xyArray[0]))]
Explanation
Using simple trigonometry:
Imagine you have the point [960, 540];
Those 2 values, 960 and 540, represent two adjacent sides of a triangle.
Now, if you recall back to 9th Grade, you may remember that there is a simple equation to get the third side of that triangle. A² + B² = C². So, we can find the third side (the magnitude) by using an equation like:
var mag = Math.sqrt(Math.exp(position[0], 2) + Math.exp(position[1], 2));
Essentially saying take the square root of X² + Y².
Fortunately, AE gives us the handy little ‘length’ operator, which simply does all of these computations for us.
—
Another similar equation can be used to find the angle of a triangle when you know two other sides, you may remember SOH CAH TOA.
Imagine the angle at the top left of your comp, now notice that you have the opposite side (the Y coord) and the adjacent side (the X coord). Since TOA uses both opposite and adjacent sides, we can get that angle using an equation like this:
var n =thisLayer("Effects").numPropertiesvar fxstr ="z_add"for(i=1;i<=n;i++){if(effect(i).name.includes(str)){ value+=effect(i)(1); }}
functionsliderAdd(fxName){ n =thisLayer("Effects").numProperties;for(i=1;i<=n;i++){if(effect(i).name.indexOf(fxName)==0){ value+=effect(i)(1); } }return value;};sliderAdd("Slider");
Autonomous agents
Auto-capture ball by Aaron Cobb
var ball = thisLayer;var cup =thisComp.layer("Cup");var captureDuration =.25; //time for ball to reach center of cup once capture beingsvar captureRadius =100; //radius around anchor point of cup at which ball will be captured.var captureTime =thisComp.duration; //time at which capture begins, default to end of compvar currentDistance;for(t =0; t < captureTime; t = t +thisComp.frameDuration){ //loop through frames currentDistance =length(ball.toComp(ball.anchorPoint.valueAtTime(t), t),cup.toComp(cup.anchorPoint.valueAtTime(t), t));if(currentDistance < captureRadius) captureTime = t; //if inside capture radius exit the loop }// executeease(time, captureTime, captureTime + captureDuration, value,cup.toComp(cup.anchorPoint.value));
You can't control the influence directly, but you can compound the ease such as below.
if (numKeys >1){ t1 =key(1).time; t2 =key(2).time; v1 = [0,540]; v2 = [1080,540]; t =easeOut(time,t1,t2,0,1); // using a normalized value to drive the 2nd easeeaseOut(t,0,1,v1,v2);}else value