Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ease",
"main": "index.js",
"version": "1.0.0",
"homepage": "https://github.com/component/ease.git",
"description": "Easing functions (for canvas etc)",
"keywords": [
"ease",
"easing",
"tween"
],
"license": "MIT",
"dependencies": {
}
}
149 changes: 85 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,132 @@
(function(window, undefined) {
'use strict';

var Ease = {};

// easing functions from "Tween.js"

exports.linear = function(n){
Ease.linear = function(n){
return n;
};

exports.inQuad = function(n){
Ease.inQuad = function(n){
return n * n;
};

exports.outQuad = function(n){
Ease.outQuad = function(n){
return n * (2 - n);
};

exports.inOutQuad = function(n){
Ease.inOutQuad = function(n){
n *= 2;
if (n < 1) return 0.5 * n * n;
return - 0.5 * (--n * (n - 2) - 1);
};

exports.inCube = function(n){
Ease.inCube = function(n){
return n * n * n;
};

exports.outCube = function(n){
Ease.outCube = function(n){
return --n * n * n + 1;
};

exports.inOutCube = function(n){
Ease.inOutCube = function(n){
n *= 2;
if (n < 1) return 0.5 * n * n * n;
return 0.5 * ((n -= 2 ) * n * n + 2);
};

exports.inQuart = function(n){
Ease.inQuart = function(n){
return n * n * n * n;
};

exports.outQuart = function(n){
Ease.outQuart = function(n){
return 1 - (--n * n * n * n);
};

exports.inOutQuart = function(n){
Ease.inOutQuart = function(n){
n *= 2;
if (n < 1) return 0.5 * n * n * n * n;
return -0.5 * ((n -= 2) * n * n * n - 2);
};

exports.inQuint = function(n){
Ease.inQuint = function(n){
return n * n * n * n * n;
}

exports.outQuint = function(n){
Ease.outQuint = function(n){
return --n * n * n * n * n + 1;
}

exports.inOutQuint = function(n){
Ease.inOutQuint = function(n){
n *= 2;
if (n < 1) return 0.5 * n * n * n * n * n;
return 0.5 * ((n -= 2) * n * n * n * n + 2);
};

exports.inSine = function(n){
Ease.inSine = function(n){
return 1 - Math.cos(n * Math.PI / 2 );
};

exports.outSine = function(n){
Ease.outSine = function(n){
return Math.sin(n * Math.PI / 2);
};

exports.inOutSine = function(n){
Ease.inOutSine = function(n){
return .5 * (1 - Math.cos(Math.PI * n));
};

exports.inExpo = function(n){
Ease.inExpo = function(n){
return 0 == n ? 0 : Math.pow(1024, n - 1);
};

exports.outExpo = function(n){
Ease.outExpo = function(n){
return 1 == n ? n : 1 - Math.pow(2, -10 * n);
};

exports.inOutExpo = function(n){
Ease.inOutExpo = function(n){
if (0 == n) return 0;
if (1 == n) return 1;
if ((n *= 2) < 1) return .5 * Math.pow(1024, n - 1);
return .5 * (-Math.pow(2, -10 * (n - 1)) + 2);
};

exports.inCirc = function(n){
Ease.inCirc = function(n){
return 1 - Math.sqrt(1 - n * n);
};

exports.outCirc = function(n){
Ease.outCirc = function(n){
return Math.sqrt(1 - (--n * n));
};

exports.inOutCirc = function(n){
Ease.inOutCirc = function(n){
n *= 2
if (n < 1) return -0.5 * (Math.sqrt(1 - n * n) - 1);
return 0.5 * (Math.sqrt(1 - (n -= 2) * n) + 1);
};

exports.inBack = function(n){
Ease.inBack = function(n){
var s = 1.70158;
return n * n * (( s + 1 ) * n - s);
};

exports.outBack = function(n){
Ease.outBack = function(n){
var s = 1.70158;
return --n * n * ((s + 1) * n + s) + 1;
};

exports.inOutBack = function(n){
Ease.inOutBack = function(n){
var s = 1.70158 * 1.525;
if ( ( n *= 2 ) < 1 ) return 0.5 * ( n * n * ( ( s + 1 ) * n - s ) );
return 0.5 * ( ( n -= 2 ) * n * ( ( s + 1 ) * n + s ) + 2 );
};

exports.inBounce = function(n){
return 1 - exports.outBounce(1 - n);
Ease.inBounce = function(n){
return 1 - Ease.outBounce(1 - n);
};

exports.outBounce = function(n){
Ease.outBounce = function(n){
if ( n < ( 1 / 2.75 ) ) {
return 7.5625 * n * n;
} else if ( n < ( 2 / 2.75 ) ) {
Expand All @@ -134,12 +138,12 @@ exports.outBounce = function(n){
}
};

exports.inOutBounce = function(n){
if (n < .5) return exports.inBounce(n * 2) * .5;
return exports.outBounce(n * 2 - 1) * .5 + .5;
Ease.inOutBounce = function(n){
if (n < .5) return Ease.inBounce(n * 2) * .5;
return Ease.outBounce(n * 2 - 1) * .5 + .5;
};

exports.inElastic = function(n){
Ease.inElastic = function(n){
var s, a = 0.1, p = 0.4;
if ( n === 0 ) return 0;
if ( n === 1 ) return 1;
Expand All @@ -148,7 +152,7 @@ exports.inElastic = function(n){
return - ( a * Math.pow( 2, 10 * ( n -= 1 ) ) * Math.sin( ( n - s ) * ( 2 * Math.PI ) / p ) );
};

exports.outElastic = function(n){
Ease.outElastic = function(n){
var s, a = 0.1, p = 0.4;
if ( n === 0 ) return 0;
if ( n === 1 ) return 1;
Expand All @@ -157,7 +161,7 @@ exports.outElastic = function(n){
return ( a * Math.pow( 2, - 10 * n) * Math.sin( ( n - s ) * ( 2 * Math.PI ) / p ) + 1 );
};

exports.inOutElastic = function(n){
Ease.inOutElastic = function(n){
var s, a = 0.1, p = 0.4;
if ( n === 0 ) return 0;
if ( n === 1 ) return 1;
Expand All @@ -169,33 +173,50 @@ exports.inOutElastic = function(n){

// aliases

exports['in-quad'] = exports.inQuad;
exports['out-quad'] = exports.outQuad;
exports['in-out-quad'] = exports.inOutQuad;
exports['in-cube'] = exports.inCube;
exports['out-cube'] = exports.outCube;
exports['in-out-cube'] = exports.inOutCube;
exports['in-quart'] = exports.inQuart;
exports['out-quart'] = exports.outQuart;
exports['in-out-quart'] = exports.inOutQuart;
exports['in-quint'] = exports.inQuint;
exports['out-quint'] = exports.outQuint;
exports['in-out-quint'] = exports.inOutQuint;
exports['in-sine'] = exports.inSine;
exports['out-sine'] = exports.outSine;
exports['in-out-sine'] = exports.inOutSine;
exports['in-expo'] = exports.inExpo;
exports['out-expo'] = exports.outExpo;
exports['in-out-expo'] = exports.inOutExpo;
exports['in-circ'] = exports.inCirc;
exports['out-circ'] = exports.outCirc;
exports['in-out-circ'] = exports.inOutCirc;
exports['in-back'] = exports.inBack;
exports['out-back'] = exports.outBack;
exports['in-out-back'] = exports.inOutBack;
exports['in-bounce'] = exports.inBounce;
exports['out-bounce'] = exports.outBounce;
exports['in-out-bounce'] = exports.inOutBounce;
exports['in-elastic'] = exports.inElastic;
exports['out-elastic'] = exports.outElastic;
exports['in-out-elastic'] = exports.inOutElastic;
Ease['in-quad'] = Ease.inQuad;
Ease['out-quad'] = Ease.outQuad;
Ease['in-out-quad'] = Ease.inOutQuad;
Ease['in-cube'] = Ease.inCube;
Ease['out-cube'] = Ease.outCube;
Ease['in-out-cube'] = Ease.inOutCube;
Ease['in-quart'] = Ease.inQuart;
Ease['out-quart'] = Ease.outQuart;
Ease['in-out-quart'] = Ease.inOutQuart;
Ease['in-quint'] = Ease.inQuint;
Ease['out-quint'] = Ease.outQuint;
Ease['in-out-quint'] = Ease.inOutQuint;
Ease['in-sine'] = Ease.inSine;
Ease['out-sine'] = Ease.outSine;
Ease['in-out-sine'] = Ease.inOutSine;
Ease['in-expo'] = Ease.inExpo;
Ease['out-expo'] = Ease.outExpo;
Ease['in-out-expo'] = Ease.inOutExpo;
Ease['in-circ'] = Ease.inCirc;
Ease['out-circ'] = Ease.outCirc;
Ease['in-out-circ'] = Ease.inOutCirc;
Ease['in-back'] = Ease.inBack;
Ease['out-back'] = Ease.outBack;
Ease['in-out-back'] = Ease.inOutBack;
Ease['in-bounce'] = Ease.inBounce;
Ease['out-bounce'] = Ease.outBounce;
Ease['in-out-bounce'] = Ease.inOutBounce;
Ease['in-elastic'] = Ease.inElastic;
Ease['out-elastic'] = Ease.outElastic;
Ease['in-out-elastic'] = Ease.inOutElastic;

// AMD export
if(typeof define == 'function' && define.amd) {
define(function(){
return Ease;
});
}
// commonjs export
else if(typeof module == 'object' && module.exports) {
module.exports = Ease;
}
// browser export
else {
window.Ease = Ease;
}

})(window);