home *** CD-ROM | disk | FTP | other *** search
/ Sears Catalog 2006 Spring/Summer (French Canadian) / SearsCanada-AutomneEte2006-FrenchVersion-WinMac.bin / fr / calendar / easing_equations.as < prev    next >
Text File  |  2005-07-27  |  11KB  |  317 lines

  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.   
  7.   These tweening functions provide different flavors of 
  8.   math-based motion under a consistent API. 
  9.   
  10.   Types of easing:
  11.   
  12.       Linear
  13.       Quadratic
  14.       Cubic
  15.       Quartic
  16.       Quintic
  17.       Sinusoidal
  18.       Exponential
  19.       Circular
  20.       Elastic
  21.       Back
  22.       Bounce
  23.  
  24.   Changes:
  25.   1.5 - added bounce easing
  26.   1.4 - added elastic and back easing
  27.   1.3 - tweaked the exponential easing functions to make endpoints exact
  28.   1.2 - inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea
  29.   
  30.   Discussed in Chapter 7 of 
  31.   Robert Penner's Programming Macromedia Flash MX
  32.   (including graphs of the easing equations)
  33.   
  34.   http://www.robertpenner.com/profmx
  35.   http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
  36. */
  37.  
  38.  
  39. // simple linear tweening - no easing
  40. // t: current time, b: beginning value, c: change in value, d: duration
  41. Math.linearTween = function (t, b, c, d) {
  42.     return c*t/d + b;
  43. };
  44.  
  45.  
  46.  ///////////// QUADRATIC EASING: t^2 ///////////////////
  47.  
  48. // quadratic easing in - accelerating from zero velocity
  49. // t: current time, b: beginning value, c: change in value, d: duration
  50. // t and d can be in frames or seconds/milliseconds
  51. Math.easeInQuad = function (t, b, c, d) {
  52.     return c*(t/=d)*t + b;
  53. };
  54.  
  55. // quadratic easing out - decelerating to zero velocity
  56. Math.easeOutQuad = function (t, b, c, d) {
  57.     return -c *(t/=d)*(t-2) + b;
  58. };
  59.  
  60. // quadratic easing in/out - acceleration until halfway, then deceleration
  61. Math.easeInOutQuad = function (t, b, c, d) {
  62.     if ((t/=d/2) < 1) return c/2*t*t + b;
  63.     return -c/2 * ((--t)*(t-2) - 1) + b;
  64. };
  65. Math.easeOutInQuad = function(t, b, c, d) {
  66.     if ((t/=d/2) < 1) return -c/2 * (--t*t - 1) + b;
  67.     return c/2*(--t*t + 1) + b;
  68. }
  69.  
  70.  
  71. ///////////// CUBIC EASING: t^3 ///////////////////////
  72. // cubic easing in - accelerating from zero velocity
  73. // t: current time, b: beginning value, c: change in value, d: duration
  74. // t and d can be frames or seconds/milliseconds
  75. Math.easeInCubic = function (t, b, c, d) {
  76.     return c*(t/=d)*t*t + b;
  77. };
  78. // cubic easing out - decelerating to zero velocity
  79. Math.easeOutCubic = function (t, b, c, d) {
  80.     return c*((t=t/d-1)*t*t + 1) + b;
  81. };
  82. // cubic easing in/out - acceleration until halfway, then deceleration
  83. Math.easeInOutCubic = function (t, b, c, d) {
  84.     if ((t/=d/2) < 1) return c/2*t*t*t + b;
  85.     return c/2*((t-=2)*t*t + 2) + b;
  86. };
  87. Math.easeOutInCubic = function (t, b, c, d) {
  88.     t/=d/2;
  89.     return c/2*(--t*t*t+1) + b;
  90. };
  91.  
  92.  
  93. ///////////// QUARTIC EASING: t^4 /////////////////////
  94. // quartic easing in - accelerating from zero velocity
  95. // t: current time, b: beginning value, c: change in value, d: duration
  96. // t and d can be frames or seconds/milliseconds
  97. Math.easeInQuart = function (t, b, c, d) {
  98.     return c*(t/=d)*t*t*t + b;
  99. };
  100.  
  101. // quartic easing out - decelerating to zero velocity
  102. Math.easeOutQuart = function (t, b, c, d) {
  103.     return -c * ((t=t/d-1)*t*t*t - 1) + b;
  104. };
  105.  
  106. // quartic easing in/out - acceleration until halfway, then deceleration
  107. Math.easeInOutQuart = function (t, b, c, d) {
  108.     if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  109.     return -c/2 * ((t-=2)*t*t*t - 2) + b;
  110. };
  111. Math.easeOutInQuart = function(t, b, c, d) {
  112.     if ((t /= d/2)<1) return -c/2*(--t*t*t*t-1)+b;
  113.     return c/2*(--t*t*t*t+1)+b;
  114. };
  115.  
  116.  
  117.  ///////////// QUINTIC EASING: t^5  ////////////////////
  118.  
  119. // quintic easing in - accelerating from zero velocity
  120. // t: current time, b: beginning value, c: change in value, d: duration
  121. // t and d can be frames or seconds/milliseconds
  122. Math.easeInQuint = function (t, b, c, d) {
  123.     return c*(t/=d)*t*t*t*t + b;
  124. };
  125.  
  126. // quintic easing out - decelerating to zero velocity
  127. Math.easeOutQuint = function (t, b, c, d) {
  128.     return c*((t=t/d-1)*t*t*t*t + 1) + b;
  129. };
  130.  
  131. // quintic easing in/out - acceleration until halfway, then deceleration
  132. Math.easeInOutQuint = function (t, b, c, d) {
  133.     if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  134.     return c/2*((t-=2)*t*t*t*t + 2) + b;
  135. };
  136. // quintic easing out/in - deceleration until halfway, then acceleration
  137. Math.easeOutInQuint = function (t, b, c, d) {
  138.     t/=d/2;
  139.     return c/2*(--t*t*t*t*t+1) + b;
  140. };
  141.  
  142. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  143. // sinusoidal easing in - accelerating from zero velocity
  144. // t: current time, b: beginning value, c: change in position, d: duration
  145. Math.easeInSine = function (t, b, c, d) {
  146.     return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  147. };
  148.  
  149. // sinusoidal easing out - decelerating to zero velocity
  150. Math.easeOutSine = function (t, b, c, d) {
  151.     return c * Math.sin(t/d * (Math.PI/2)) + b;
  152. };
  153.  
  154. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  155. Math.easeInOutSine = function (t, b, c, d) {
  156.     return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  157. };
  158. Math.easeOutInSine = function (t, b, c, d) {
  159.     if ((t /= d/2)<1) return c/2 * (Math.sin(Math.PI*t/2) ) + b;
  160.     return -c/2 * (Math.cos(Math.PI*--t/2)-2) + b;
  161. };
  162.  
  163.  
  164.  ///////////// EXPONENTIAL EASING: 2^t /////////////////
  165.  
  166. // exponential easing in - accelerating from zero velocity
  167. // t: current time, b: beginning value, c: change in position, d: duration
  168. Math.easeInExpo = function (t, b, c, d) {
  169.     return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  170. };
  171.  
  172. // exponential easing out - decelerating to zero velocity
  173. Math.easeOutExpo = function (t, b, c, d) {
  174.     return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  175. };
  176.  
  177. // exponential easing in/out - accelerating until halfway, then decelerating
  178. Math.easeInOutExpo = function (t, b, c, d) {
  179.     if (t==0) return b;
  180.     if (t==d) return b+c;
  181.     if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
  182.     return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  183. };
  184. Math.easeOutInExpo = function (t, b, c, d) {
  185.     if (t==0) return b;
  186.     if (t==d) return b+c;
  187.     if ((t/=d/2) < 1) return c/2 * (-Math.pow(2, -10 * t) + 1) + b;
  188.     return c/2 * (Math.pow(2, 10 * (t-2))+1) + b;
  189. };
  190.  /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  191.  
  192. // circular easing in - accelerating from zero velocity
  193. // t: current time, b: beginning value, c: change in position, d: duration
  194. Math.easeInCirc = function (t, b, c, d) {
  195.     return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  196. };
  197. // circular easing out - decelerating to zero velocity
  198. Math.easeOutCirc = function (t, b, c, d) {
  199.     return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  200. };
  201. // circular easing in/out - acceleration until halfway, then deceleration
  202. Math.easeInOutCirc = function (t, b, c, d) {
  203.     if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  204.     return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  205. };
  206.  
  207. Math.easeOutInCirc = function (t, b, c, d) {
  208.     if ((t/=d/2) < 1) return c/2 * Math.sqrt(1 - --t*t)  + b;
  209.     return c/2 * (2-Math.sqrt(1 - --t*t) ) + b;
  210. };
  211. //
  212.  
  213.  
  214.  
  215.  
  216.  /////////// ELASTIC EASING: exponentially decaying sine wave  //////////////
  217.  
  218. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  219. // t and d can be in frames or seconds/milliseconds
  220.  
  221. Math.easeInElastic = function (t, b, c, d, a, p) {
  222.     if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
  223.     if (a < Math.abs(c)) { a=c; var s=p/4; }
  224.     else var s = p/(2*Math.PI) * Math.asin (c/a);
  225.     return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  226. };
  227. Math.easeOutElastic = function (t, b, c, d, a, p) {
  228.     if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
  229.     if (a < Math.abs(c)) { a=c; var s=p/4; }
  230.     else var s = p/(2*Math.PI) * Math.asin (c/a);
  231.     return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  232. };
  233. //
  234. Math.easeInOutElastic = function (t, b, c, d, a, p) {
  235.     if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
  236.     if (a < Math.abs(c)) { a=c; var s=p/4; }
  237.     else var s = p/(2*Math.PI) * Math.asin (c/a);
  238.     if (t < 1) return -.5*(a*Math.pow(2,10*--t) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  239.     return a*Math.pow(2,-10*--t) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  240. };
  241. //