Last active
August 29, 2015 14:18
-
-
Save lachsen/ea67e8679aef88132832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.KeySpline = function(mX1, mY1, mX2, mY2) { | |
this.get = function(aX) { | |
if (mX1 == mY1 && mX2 == mY2) return aX; // linear | |
return CalcBezier(GetTForX(aX), mY1, mY2); | |
}; | |
function A(aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1; } | |
function B(aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1; } | |
function C(aA1) { return 3.0 * aA1; } | |
// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2. | |
function CalcBezier(aT, aA1, aA2) { | |
return ((A(aA1, aA2)*aT + B(aA1, aA2))*aT + C(aA1))*aT; | |
} | |
// Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2. | |
function GetSlope(aT, aA1, aA2) { | |
return 3.0 * A(aA1, aA2)*aT*aT + 2.0 * B(aA1, aA2) * aT + C(aA1); | |
} | |
function GetTForX(aX) { | |
// Newton raphson iteration | |
var aGuessT = aX; | |
for (var i = 0; i < 4; ++i) { | |
var currentSlope = GetSlope(aGuessT, mX1, mX2); | |
if (currentSlope == 0.0) return aGuessT; | |
var currentX = CalcBezier(aGuessT, mX1, mX2) - aX; | |
aGuessT -= currentX / currentSlope; | |
} | |
return aGuessT; | |
} | |
}; | |
window.KEY_SPLINES = { | |
'EASE' : new window.KeySpline(0.25, 0.1, 0.25, 1.0), | |
'LINEAR': new window.KeySpline(0, 0, 1, 1), | |
'EASE_IN': new window.KeySpline(0.42, 0.0, 1.00, 1.0), | |
'EASE_OUT': new window.KeySpline(0.00, 0.0, 0.58, 1.0), | |
'EASE_IN_OUT': new window.KeySpline(0.42, 0.0, 0.58, 1.0), | |
'JUMPY': new window.KeySpline(0.31, 1.51, 0.53, 0.94), | |
'EASE_OUT_STRONG': new window.KeySpline(0.00, 0.5, 0.58, 1.0) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment