-
Expression that finds the distance that a layer has traveled
I’m currently working on an expression that would be applied to the size of a rectangle shape layer. The end result should be the rectangle being as long as the distance that the layer has traveled over a number of previous key frames. Currently I have written the expression below, but it creates an error and I have no idea why. Could someone please help me out by explaining what I’m doing wrong? Thanks!
Note: For some reason, a < symbol in my for loop is not appearing correctly when I paste it here, so be sure to change that back if you paste this expression into After Effects for yourself
function getDistanceTraveled(){
var pos = thisLayer.toWorld(thisLayer.anchorPoint);
var sFrame = framesToTime(1,1/thisComp.frameDuration);
var tSample = Math.ceil(effect("Time Sample (Frames)")("Slider"));var dist = 0;
for(i = 0; i < tSample; i++){
let tOne = time - (sFrame * i);
if (tOne < 0) {timeOne = 0;};
let tTwo = tOne - sFrame;
if (tTwo < 0) {tTwo = 0;};
let xDist = Math.abs( pos.valueAtTime(tOne)[0] - pos.valueAtTime(tTwo)[0] );
let yDist = Math.abs( pos.valueAtTime(tOne)[1] - pos.valueAtTime(tTwo)[1] );
dist += Math.sqrt( Math.pow(xDist,2) + Math.pow(xDist,2) );
};return dist;
};var x = getDistanceTraveled();
[x,0];