Easings.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var Easing = {}
  2. Easing.Linear = {}
  3. Easing.Linear.EaseNone = (percentageComplete) => percentageComplete
  4. Easing.Quadratic = {}
  5. Easing.Quadratic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete
  6. Easing.Quadratic.EaseOut = (percentageComplete) => -percentageComplete * ( percentageComplete - 2 )
  7. Easing.Quadratic.EaseInOut = (percentageComplete) => {
  8. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * percentageComplete * percentageComplete
  9. return - 0.5 * ( --percentageComplete * ( percentageComplete - 2 ) - 1 )
  10. }
  11. Easing.Cubic = {}
  12. Easing.Cubic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete * percentageComplete
  13. Easing.Cubic.EaseOut = (percentageComplete) => --percentageComplete * percentageComplete * percentageComplete + 1
  14. Easing.Cubic.EaseInOut = (percentageComplete) => {
  15. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * percentageComplete * percentageComplete * percentageComplete
  16. return 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * percentageComplete + 2 )
  17. }
  18. Easing.Quartic = {}
  19. Easing.Quartic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete * percentageComplete * percentageComplete
  20. Easing.Quartic.EaseOut = (percentageComplete) => -( --percentageComplete * percentageComplete * percentageComplete * percentageComplete - 1 )
  21. Easing.Quartic.EaseInOut = (percentageComplete) => {
  22. if ( ( percentageComplete *= 2 ) < 1) return 0.5 * percentageComplete * percentageComplete * percentageComplete * percentageComplete
  23. return - 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * percentageComplete * percentageComplete - 2 )
  24. }
  25. Easing.Quintic = {}
  26. Easing.Quintic.EaseIn = (percentageComplete) => percentageComplete * percentageComplete * percentageComplete * percentageComplete * percentageComplete
  27. Easing.Quintic.EaseOut = (percentageComplete) => ( percentageComplete = percentageComplete - 1 ) * percentageComplete * percentageComplete * percentageComplete * percentageComplete + 1
  28. Easing.Quintic.EaseInOut = (percentageComplete) => {
  29. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * percentageComplete * percentageComplete * percentageComplete * percentageComplete * percentageComplete
  30. return 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * percentageComplete * percentageComplete * percentageComplete + 2 )
  31. }
  32. Easing.Sinusoidal = {}
  33. Easing.Sinusoidal.EaseIn = (percentageComplete) => -Math.cos( percentageComplete * Math.PI / 2 ) + 1
  34. Easing.Sinusoidal.EaseOut = (percentageComplete) => Math.sin( percentageComplete * Math.PI / 2 )
  35. Easing.Sinusoidal.EaseInOut = (percentageComplete) => -0.5 * ( Math.cos( Math.PI * percentageComplete ) - 1 )
  36. Easing.Exponential = {}
  37. Easing.Exponential.EaseIn = (percentageComplete) => {
  38. return percentageComplete == 0 ? 0 : Math.pow( 2, 10 * ( percentageComplete - 1 ) )
  39. };
  40. Easing.Exponential.EaseOut = (percentageComplete) => {
  41. return percentageComplete == 1 ? 1 : - Math.pow( 2, - 10 * percentageComplete ) + 1
  42. };
  43. Easing.Exponential.EaseInOut = (percentageComplete) => {
  44. if ( percentageComplete == 0 ) return 0
  45. if ( percentageComplete == 1 ) return 1
  46. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * Math.pow( 2, 10 * ( percentageComplete - 1 ) )
  47. return 0.5 * ( - Math.pow( 2, - 10 * ( percentageComplete - 1 ) ) + 2 )
  48. };
  49. Easing.Circular = {}
  50. Easing.Circular.EaseIn = (percentageComplete) => -(Math.sqrt( 1 - percentageComplete * percentageComplete ) - 1)
  51. Easing.Circular.EaseOut = (percentageComplete) => Math.sqrt( 1 - (--percentageComplete * percentageComplete) )
  52. Easing.Circular.EaseInOut = (percentageComplete) => {
  53. if ( ( percentageComplete /= 0.5 ) < 1) return - 0.5 * ( Math.sqrt( 1 - percentageComplete * percentageComplete) - 1)
  54. return 0.5 * ( Math.sqrt( 1 - ( percentageComplete -= 2) * percentageComplete) + 1)
  55. };
  56. Easing.Elastic = {};
  57. Easing.Elastic.EaseIn = (percentageComplete) => {
  58. var s, a = 0.1, p = 0.4
  59. if ( percentageComplete == 0 ) return 0; if ( percentageComplete == 1 ) return 1; if ( !p ) p = 0.3
  60. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  61. else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a )
  62. return - ( a * Math.pow( 2, 10 * ( percentageComplete -= 1 ) ) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) )
  63. };
  64. Easing.Elastic.EaseOut = (percentageComplete) => {
  65. var s, a = 0.1, p = 0.4
  66. if ( percentageComplete == 0 ) return 0; if ( percentageComplete == 1 ) return 1; if ( !p ) p = 0.3
  67. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  68. else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a )
  69. return ( a * Math.pow( 2, - 10 * percentageComplete) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) + 1 )
  70. };
  71. Easing.Elastic.EaseInOut = (percentageComplete) => {
  72. var s, a = 0.1, p = 0.4
  73. if ( percentageComplete == 0 ) return 0; if ( percentageComplete == 1 ) return 1; if ( !p ) p = 0.3
  74. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  75. else s = p / ( 2 * Math.PI ) * Math.asin( 1 / a )
  76. if ( ( percentageComplete *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( percentageComplete -= 1 ) ) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) )
  77. return a * Math.pow( 2, -10 * ( percentageComplete -= 1 ) ) * Math.sin( ( percentageComplete - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1
  78. };
  79. Easing.Back = {}
  80. Easing.Back.EaseIn = (percentageComplete) => {
  81. var s = 1.70158
  82. return percentageComplete * percentageComplete * ( ( s + 1 ) * percentageComplete - s )
  83. };
  84. Easing.Back.EaseOut = (percentageComplete) => {
  85. var s = 1.70158
  86. return ( percentageComplete = percentageComplete - 1 ) * percentageComplete * ( ( s + 1 ) * percentageComplete + s ) + 1
  87. };
  88. Easing.Back.EaseInOut = (percentageComplete) => {
  89. var s = 1.70158 * 1.525
  90. if ( ( percentageComplete *= 2 ) < 1 ) return 0.5 * ( percentageComplete * percentageComplete * ( ( s + 1 ) * percentageComplete - s ) )
  91. return 0.5 * ( ( percentageComplete -= 2 ) * percentageComplete * ( ( s + 1 ) * percentageComplete + s ) + 2 )
  92. };
  93. Easing.Bounce = {}
  94. Easing.Bounce.EaseIn = (percentageComplete) => 1 - Easing.Bounce.EaseOut( 1 - percentageComplete )
  95. Easing.Bounce.EaseOut = (percentageComplete) => {
  96. if ( ( percentageComplete /= 1 ) < ( 1 / 2.75 ) ) {
  97. return 7.5625 * percentageComplete * percentageComplete
  98. } else if ( percentageComplete < ( 2 / 2.75 ) ) {
  99. return 7.5625 * ( percentageComplete -= ( 1.5 / 2.75 ) ) * percentageComplete + 0.75
  100. } else if ( percentageComplete < ( 2.5 / 2.75 ) ) {
  101. return 7.5625 * ( percentageComplete -= ( 2.25 / 2.75 ) ) * percentageComplete + 0.9375
  102. } else {
  103. return 7.5625 * ( percentageComplete -= ( 2.625 / 2.75 ) ) * percentageComplete + 0.984375
  104. }
  105. }
  106. Easing.Bounce.EaseInOut = (percentageComplete) => {
  107. if ( percentageComplete < 0.5 ) return Easing.Bounce.EaseIn( percentageComplete * 2 ) * 0.5
  108. return Easing.Bounce.EaseOut( percentageComplete * 2 - 1 ) * 0.5 + 0.5
  109. }
  110. export default Easing