EasingEquations.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. Easing Equations v1.5
  3. May 1, 2003
  4. (c) 2003 Robert Penner, all rights reserved.
  5. This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.
  6. These tweening functions provide different flavors of
  7. math-based motion under a consistent API.
  8. Types of easing:
  9. Linear
  10. Quadratic
  11. Cubic
  12. Quartic
  13. Quintic
  14. Sinusoidal
  15. Exponential
  16. Circular
  17. Elastic
  18. Back
  19. Bounce
  20. Changes:
  21. 1.5 - added bounce easing
  22. 1.4 - added elastic and back easing
  23. 1.3 - tweaked the exponential easing functions to make endpoints exact
  24. 1.2 - inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea
  25. Discussed in Chapter 7 of
  26. Robert Penner's Programming Macromedia Flash MX
  27. (including graphs of the easing equations)
  28. http://www.robertpenner.com/profmx
  29. http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
  30. */
  31. // simple linear tweening - no easing
  32. // t: current time, b: beginning value, c: change in value, d: duration
  33. var Penner = {};
  34. Penner.Linear = function (t, b, c, d) {
  35. return c*t/d + b;
  36. };
  37. ///////////// QUADRATIC EASING: t^2 ///////////////////
  38. // quadratic easing in - accelerating from zero velocity
  39. // t: current time, b: beginning value, c: change in value, d: duration
  40. // t and d can be in frames or seconds/milliseconds
  41. Penner.InQuad = function (t, b, c, d) {
  42. return c*(t/=d)*t + b;
  43. };
  44. // quadratic easing out - decelerating to zero velocity
  45. Penner.OutQuad = function (t, b, c, d) {
  46. return -c *(t/=d)*(t-2) + b;
  47. };
  48. // quadratic easing in/out - acceleration until halfway, then deceleration
  49. Penner.InOutQuad = function (t, b, c, d) {
  50. if ((t/=d/2) < 1) return c/2*t*t + b;
  51. return -c/2 * ((--t)*(t-2) - 1) + b;
  52. };
  53. ///////////// CUBIC EASING: t^3 ///////////////////////
  54. // cubic easing in - accelerating from zero velocity
  55. // t: current time, b: beginning value, c: change in value, d: duration
  56. // t and d can be frames or seconds/milliseconds
  57. Penner.InCubic = function (t, b, c, d) {
  58. return c*(t/=d)*t*t + b;
  59. };
  60. // cubic easing out - decelerating to zero velocity
  61. Penner.OutCubic = function (t, b, c, d) {
  62. return c*((t=t/d-1)*t*t + 1) + b;
  63. };
  64. // cubic easing in/out - acceleration until halfway, then deceleration
  65. Penner.InOutCubic = function (t, b, c, d) {
  66. if ((t/=d/2) < 1) return c/2*t*t*t + b;
  67. return c/2*((t-=2)*t*t + 2) + b;
  68. };
  69. ///////////// QUARTIC EASING: t^4 /////////////////////
  70. // quartic easing in - accelerating from zero velocity
  71. // t: current time, b: beginning value, c: change in value, d: duration
  72. // t and d can be frames or seconds/milliseconds
  73. Penner.InQuart = function (t, b, c, d) {
  74. return c*(t/=d)*t*t*t + b;
  75. };
  76. // quartic easing out - decelerating to zero velocity
  77. Penner.OutQuart = function (t, b, c, d) {
  78. return -c * ((t=t/d-1)*t*t*t - 1) + b;
  79. };
  80. // quartic easing in/out - acceleration until halfway, then deceleration
  81. Penner.InOutQuart = function (t, b, c, d) {
  82. if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  83. return -c/2 * ((t-=2)*t*t*t - 2) + b;
  84. };
  85. ///////////// QUINTIC EASING: t^5 ////////////////////
  86. // quintic easing in - accelerating from zero velocity
  87. // t: current time, b: beginning value, c: change in value, d: duration
  88. // t and d can be frames or seconds/milliseconds
  89. Penner.InQuint = function (t, b, c, d) {
  90. return c*(t/=d)*t*t*t*t + b;
  91. };
  92. // quintic easing out - decelerating to zero velocity
  93. Penner.OutQuint = function (t, b, c, d) {
  94. return c*((t=t/d-1)*t*t*t*t + 1) + b;
  95. };
  96. // quintic easing in/out - acceleration until halfway, then deceleration
  97. Penner.InOutQuint = function (t, b, c, d) {
  98. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  99. return c/2*((t-=2)*t*t*t*t + 2) + b;
  100. };
  101. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  102. // sinusoidal easing in - accelerating from zero velocity
  103. // t: current time, b: beginning value, c: change in position, d: duration
  104. Penner.InSine = function (t, b, c, d) {
  105. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  106. };
  107. // sinusoidal easing out - decelerating to zero velocity
  108. Penner.OutSine = function (t, b, c, d) {
  109. return c * Math.sin(t/d * (Math.PI/2)) + b;
  110. };
  111. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  112. Penner.InOutSine = function (t, b, c, d) {
  113. return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  114. };
  115. ///////////// EXPONENTIAL EASING: 2^t /////////////////
  116. // exponential easing in - accelerating from zero velocity
  117. // t: current time, b: beginning value, c: change in position, d: duration
  118. Penner.InExpo = function (t, b, c, d) {
  119. return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  120. };
  121. // exponential easing out - decelerating to zero velocity
  122. Penner.OutExpo = function (t, b, c, d) {
  123. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  124. };
  125. // exponential easing in/out - accelerating until halfway, then decelerating
  126. Penner.InOutExpo = function (t, b, c, d) {
  127. if (t==0) return b;
  128. if (t==d) return b+c;
  129. if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
  130. return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  131. };
  132. /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  133. // circular easing in - accelerating from zero velocity
  134. // t: current time, b: beginning value, c: change in position, d: duration
  135. Penner.InCirc = function (t, b, c, d) {
  136. return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  137. };
  138. // circular easing out - decelerating to zero velocity
  139. Penner.OutCirc = function (t, b, c, d) {
  140. return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  141. };
  142. // circular easing in/out - acceleration until halfway, then deceleration
  143. Penner.InOutCirc = function (t, b, c, d) {
  144. if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  145. return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  146. };
  147. /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
  148. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  149. // t and d can be in frames or seconds/milliseconds
  150. Penner.InElastic = function (t, b, c, d, a, p) {
  151. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  152. if ((!a) || a < Math.abs(c)) { a=c; var s=p/4; }
  153. else var s = p/(2*Math.PI) * Math.asin (c/a);
  154. return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  155. };
  156. Penner.OutElastic = function (t, b, c, d, a, p) {
  157. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  158. if ((!a) || a < Math.abs(c)) { a=c; var s=p/4; }
  159. else var s = p/(2*Math.PI) * Math.asin (c/a);
  160. return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  161. };
  162. Penner.InOutElastic = function (t, b, c, d, a, p) {
  163. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
  164. if ((!a) || a < Math.abs(c)) { a=c; var s=p/4; }
  165. else var s = p/(2*Math.PI) * Math.asin (c/a);
  166. if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  167. return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  168. };
  169. /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
  170. // back easing in - backtracking slightly, then reversing direction and moving to target
  171. // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
  172. // t and d can be in frames or seconds/milliseconds
  173. // s controls the amount of overshoot: higher s means greater overshoot
  174. // s has a default value of 1.70158, which produces an overshoot of 10 percent
  175. // s==0 produces cubic easing with no overshoot
  176. Penner.InBack = function (t, b, c, d, s) {
  177. if (s == undefined) s = 1.70158;
  178. return c*(t/=d)*t*((s+1)*t - s) + b;
  179. };
  180. // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
  181. Penner.OutBack = function (t, b, c, d, s) {
  182. if (s == undefined) s = 1.70158;
  183. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  184. };
  185. // back easing in/out - backtracking slightly, then reversing direction and moving to target,
  186. // then overshooting target, reversing, and finally coming back to target
  187. Penner.InOutBack = function (t, b, c, d, s) {
  188. if (s == undefined) s = 1.70158;
  189. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
  190. return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  191. };
  192. /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
  193. // bounce easing in
  194. // t: current time, b: beginning value, c: change in position, d: duration
  195. Penner.InBounce = function (t, b, c, d) {
  196. return c - Penner.OutBounce (d-t, 0, c, d) + b;
  197. };
  198. // bounce easing out
  199. Penner.OutBounce = function (t, b, c, d) {
  200. if ((t/=d) < (1/2.75)) {
  201. return c*(7.5625*t*t) + b;
  202. } else if (t < (2/2.75)) {
  203. return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
  204. } else if (t < (2.5/2.75)) {
  205. return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
  206. } else {
  207. return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
  208. }
  209. };
  210. // bounce easing in/out
  211. Penner.InOutBounce = function (t, b, c, d) {
  212. if (t < d/2) return Penner.InBounce (t*2, 0, c, d) * .5 + b;
  213. return Penner.OutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;
  214. };
  215. //BEGIN_ATLAS_NOTIFY
  216. if (typeof(Sys) != "undefined")
  217. {
  218. if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
  219. {
  220. Sys.Application.notifyScriptLoaded();
  221. }
  222. }
  223. //END_ATLAS_NOTIFY