Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
timhaywood committed Aug 3, 2019
1 parent 398ae47 commit 77cd51b
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions eKeys.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{

// The function that's called from After Effects
// as eKeys.animate()
'animate': function(
inputKeyframes = requiredArgumentError('Keyframe Array', '.animate() inputs'),
inputTime = requiredArgumentError('Input Time', '.animate() inputs')
Expand Down Expand Up @@ -98,7 +99,9 @@ const validKeys = inputKeyframes
checkTypes([[inputTime, 'number']]);

// Returns the final animated value
// This is the function that's returned
const animateBetweenKeys = function(keys, time) {

// Creates bezier curve and returns function
// to calculate eased value
const bezier = (mX1, mY1, mX2, mY2) => {
Expand Down Expand Up @@ -229,10 +232,9 @@ const animateBetweenKeys = function(keys, time) {
return bezierEasing;
};

// Set current key to most recent keyframe
function getCurrentKeyNum(keys, time) {
const lastKeyNum = keys.length - 1;

// Set current key to most recent keyframe
let curKeyNum = 0;
while (curKeyNum < lastKeyNum && time >= keys[curKeyNum + 1].keyTime) {
curKeyNum++;
Expand Down Expand Up @@ -270,19 +272,13 @@ const animateBetweenKeys = function(keys, time) {
1 - nextKey.velocityIn / 100
);

// Delta calculations
const deltaT = nextKey.keyTime - curKey.keyTime;

// Create incrementing time value that is zero
// at start keyframe time
// Incrementing time value that
// starts from the current keyTime
const movedTime = Math.max(time - curKey.keyTime, 0);

// Normalize time input to maximum of one
// and to correct speed
const timeInput = Math.min(1, movedTime / deltaT);

// Get progress value according to easing spline
const progress = easingCurve(timeInput);
const animationLength = nextKey.keyTime - curKey.keyTime;
const linearProgress = Math.min(1, movedTime / animationLength);
const easedProgress = easingCurve(linearProgress);

// Performs animation on each element of array individually
const animateArrayFromProgress = (startArray, endArray, progressAmount) => {
Expand All @@ -295,14 +291,15 @@ const animateBetweenKeys = function(keys, time) {
// Add to current key and return
return startArray.map((item, index) => item + deltaProgressed[index]);
};

// Animate between values according to progress
const animateValueFromProgress = (startVal, endVal, progressAmount) => {
const valueDelta = endVal - startVal;
return startVal + valueDelta * progressAmount;
};

// Return animation according to whether values are an array
const animateProps = [curKey.keyValue, nextKey.keyValue, progress];
const animateProps = [curKey.keyValue, nextKey.keyValue, easedProgress];
return Array.isArray(curKey.keyValue) || Array.isArray(nextKey.keyValue)
? animateArrayFromProgress(...animateProps)
: animateValueFromProgress(...animateProps);
Expand Down

0 comments on commit 77cd51b

Please sign in to comment.