-
Elegant “Current” Keyframe solution?
I love the nearestKey(time) expression and have been using it to trigger animations with (time – nearestKey(time).time). However, nearestKey looks both forward and backward when determining which Keyframe is the nearest. I would love it if it only looked backward. I have to wonder if anyone in this community has figured out an elegant solution to this problem. I’ve tried a few different things based on my relatively limited knowledge of AE Expressions/Javascript Here is what I’m currently doing. There has to be a better way, right?
x = thisComp.layer("Test").effect("Slider Control")("Slider");
y = x.nearestKey(time).index;
d = (time - x.nearestKey(time).time);if (d>=0){
currentKey = y; //if the time between the playhead and the nearest keyframe is positive, the current key is the nearest key
}else{
currentKey = y-1; //if the time between the playhead and the nearest keyframe is negative, the current key is the previous key
}
linear(currentKey,1,100,1,100) //keeps currentKey value above 0 since it will be used for index. Doesn't work if over 100 keyframes in effect.d = (time - x.key(currentKey).time);