phaser/v3/merge/tween/Easing.js

577 lines
13 KiB
JavaScript
Raw Normal View History

/* jshint curly: false */
2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2016-04-04 21:15:01 +00:00
* @copyright 2016 Photon Storm Ltd.
2013-10-01 12:54:29 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A collection of easing methods defining ease-in and ease-out curves.
2013-10-01 12:54:29 +00:00
*
* @class Phaser.Easing
*/
2013-08-28 23:09:12 +00:00
Phaser.Easing = {
/**
* Linear easing.
*
* @class Phaser.Easing.Linear
*/
Linear: {
2013-08-28 23:09:12 +00:00
/**
* Linear Easing (no variation).
*
* @method Phaser.Easing.Linear#None
* @param {number} k - The value to be tweened.
* @return {number} k.
*/
None: function ( k ) {
2013-08-28 23:09:12 +00:00
return k;
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quadratic easing.
*
* @class Phaser.Easing.Quadratic
*/
Quadratic: {
2013-08-28 23:09:12 +00:00
/**
* Ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Quadratic#In
* @param {number} k - The value to be tweened.
* @return {number} k^2.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return k * k;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Ease-out.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Quadratic#Out
* @param {number} k - The value to be tweened.
* @return {number} k* (2-k).
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
return k * ( 2 - k );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Ease-in/out.
*
* @method Phaser.Easing.Quadratic#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
return - 0.5 * ( --k * ( k - 2 ) - 1 );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Cubic easing.
*
* @class Phaser.Easing.Cubic
*/
Cubic: {
2013-08-28 23:09:12 +00:00
/**
* Cubic ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Cubic#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return k * k * k;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Cubic ease-out.
*
* @method Phaser.Easing.Cubic#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
return --k * k * k + 1;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Cubic ease-in/out.
*
* @method Phaser.Easing.Cubic#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k;
return 0.5 * ( ( k -= 2 ) * k * k + 2 );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quartic easing.
*
* @class Phaser.Easing.Quartic
*/
Quartic: {
2013-08-28 23:09:12 +00:00
/**
* Quartic ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Quartic#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return k * k * k * k;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quartic ease-out.
*
* @method Phaser.Easing.Quartic#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
return 1 - ( --k * k * k * k );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quartic ease-in/out.
*
* @method Phaser.Easing.Quartic#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( ( k *= 2 ) < 1) return 0.5 * k * k * k * k;
return - 0.5 * ( ( k -= 2 ) * k * k * k - 2 );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quintic easing.
*
* @class Phaser.Easing.Quintic
*/
Quintic: {
2013-08-28 23:09:12 +00:00
/**
* Quintic ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Quintic#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return k * k * k * k * k;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quintic ease-out.
*
* @method Phaser.Easing.Quintic#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
return --k * k * k * k * k + 1;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Quintic ease-in/out.
*
* @method Phaser.Easing.Quintic#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k * k * k;
return 0.5 * ( ( k -= 2 ) * k * k * k * k + 2 );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Sinusoidal easing.
*
* @class Phaser.Easing.Sinusoidal
*/
Sinusoidal: {
2013-08-28 23:09:12 +00:00
/**
* Sinusoidal ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Sinusoidal#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
if (k === 0) return 0;
if (k === 1) return 1;
return 1 - Math.cos( k * Math.PI / 2 );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Sinusoidal ease-out.
*
* @method Phaser.Easing.Sinusoidal#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
if (k === 0) return 0;
if (k === 1) return 1;
return Math.sin( k * Math.PI / 2 );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Sinusoidal ease-in/out.
*
* @method Phaser.Easing.Sinusoidal#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if (k === 0) return 0;
if (k === 1) return 1;
return 0.5 * ( 1 - Math.cos( Math.PI * k ) );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Exponential easing.
*
* @class Phaser.Easing.Exponential
*/
Exponential: {
2013-08-28 23:09:12 +00:00
/**
* Exponential ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Exponential#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return k === 0 ? 0 : Math.pow( 1024, k - 1 );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Exponential ease-out.
*
* @method Phaser.Easing.Exponential#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
return k === 1 ? 1 : 1 - Math.pow( 2, - 10 * k );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Exponential ease-in/out.
*
* @method Phaser.Easing.Exponential#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( k === 0 ) return 0;
if ( k === 1 ) return 1;
if ( ( k *= 2 ) < 1 ) return 0.5 * Math.pow( 1024, k - 1 );
return 0.5 * ( - Math.pow( 2, - 10 * ( k - 1 ) ) + 2 );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Circular easing.
*
* @class Phaser.Easing.Circular
*/
Circular: {
2013-08-28 23:09:12 +00:00
/**
* Circular ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Circular#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return 1 - Math.sqrt( 1 - k * k );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Circular ease-out.
*
* @method Phaser.Easing.Circular#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
return Math.sqrt( 1 - ( --k * k ) );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Circular ease-in/out.
*
* @method Phaser.Easing.Circular#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( ( k *= 2 ) < 1) return - 0.5 * ( Math.sqrt( 1 - k * k) - 1);
return 0.5 * ( Math.sqrt( 1 - ( k -= 2) * k) + 1);
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Elastic easing.
*
* @class Phaser.Easing.Elastic
*/
Elastic: {
/**
* Elastic ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Elastic#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
var s, a = 0.1, p = 0.4;
if ( k === 0 ) return 0;
if ( k === 1 ) return 1;
if ( !a || a < 1 ) { a = 1; s = p / 4; }
else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
return - ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
},
/**
* Elastic ease-out.
*
* @method Phaser.Easing.Elastic#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
var s, a = 0.1, p = 0.4;
if ( k === 0 ) return 0;
if ( k === 1 ) return 1;
if ( !a || a < 1 ) { a = 1; s = p / 4; }
else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
return ( a * Math.pow( 2, - 10 * k) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) + 1 );
},
/**
* Elastic ease-in/out.
*
* @method Phaser.Easing.Elastic#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
var s, a = 0.1, p = 0.4;
if ( k === 0 ) return 0;
if ( k === 1 ) return 1;
if ( !a || a < 1 ) { a = 1; s = p / 4; }
else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
if ( ( k *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
return a * Math.pow( 2, -10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
}
},
2013-08-28 23:09:12 +00:00
/**
* Back easing.
*
* @class Phaser.Easing.Back
*/
Back: {
2013-08-28 23:09:12 +00:00
/**
* Back ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Back#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
var s = 1.70158;
return k * k * ( ( s + 1 ) * k - s );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Back ease-out.
*
* @method Phaser.Easing.Back#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
var s = 1.70158;
return --k * k * ( ( s + 1 ) * k + s ) + 1;
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Back ease-in/out.
*
* @method Phaser.Easing.Back#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
var s = 1.70158 * 1.525;
if ( ( k *= 2 ) < 1 ) return 0.5 * ( k * k * ( ( s + 1 ) * k - s ) );
return 0.5 * ( ( k -= 2 ) * k * ( ( s + 1 ) * k + s ) + 2 );
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Bounce easing.
*
* @class Phaser.Easing.Bounce
*/
Bounce: {
2013-08-28 23:09:12 +00:00
/**
* Bounce ease-in.
*
2014-03-23 07:59:28 +00:00
* @method Phaser.Easing.Bounce#In
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
In: function ( k ) {
2013-08-28 23:09:12 +00:00
return 1 - Phaser.Easing.Bounce.Out( 1 - k );
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Bounce ease-out.
*
* @method Phaser.Easing.Bounce#Out
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
Out: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( k < ( 1 / 2.75 ) ) {
2013-08-28 23:09:12 +00:00
return 7.5625 * k * k;
2013-08-28 23:09:12 +00:00
} else if ( k < ( 2 / 2.75 ) ) {
2013-08-28 23:09:12 +00:00
return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
2013-08-28 23:09:12 +00:00
} else if ( k < ( 2.5 / 2.75 ) ) {
2013-08-28 23:09:12 +00:00
return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
2013-08-28 23:09:12 +00:00
} else {
2013-08-28 23:09:12 +00:00
return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
},
2013-08-28 23:09:12 +00:00
/**
* Bounce ease-in/out.
*
* @method Phaser.Easing.Bounce#InOut
2014-03-23 07:59:28 +00:00
* @param {number} k - The value to be tweened.
* @return {number} The tweened value.
*/
InOut: function ( k ) {
2013-08-28 23:09:12 +00:00
if ( k < 0.5 ) return Phaser.Easing.Bounce.In( k * 2 ) * 0.5;
return Phaser.Easing.Bounce.Out( k * 2 - 1 ) * 0.5 + 0.5;
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
}
2013-08-28 23:09:12 +00:00
};
Phaser.Easing.Default = Phaser.Easing.Linear.None;
Phaser.Easing.Power0 = Phaser.Easing.Linear.None;
Phaser.Easing.Power1 = Phaser.Easing.Quadratic.Out;
Phaser.Easing.Power2 = Phaser.Easing.Cubic.Out;
Phaser.Easing.Power3 = Phaser.Easing.Quartic.Out;
Phaser.Easing.Power4 = Phaser.Easing.Quintic.Out;