mEase.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. ROBERT PENNER'S MOST EXCELLENT EASING METHODS - ported to Torque C++ by Paul Dana
  3. Easing Equations v1.5
  4. May 1, 2003
  5. (c) 2003 Robert Penner, all rights reserved.
  6. This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.
  7. These tweening functions provide different flavors of
  8. math-based motion under a consistent API.
  9. Types of easing:
  10. Linear
  11. Quadratic
  12. Cubic
  13. Quartic
  14. Quintic
  15. Sinusoidal
  16. Exponential
  17. Circular
  18. Elastic
  19. Back
  20. Bounce
  21. Changes:
  22. 1.5 - added bounce easing
  23. 1.4 - added elastic and back easing
  24. 1.3 - tweaked the exponential easing functions to make endpoints exact
  25. 1.2 - inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea
  26. Discussed in Chapter 7 of
  27. Robert Penner's Programming Macromedia Flash MX
  28. (including graphs of the easing equations)
  29. http://www.robertpenner.com/profmx
  30. http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
  31. */
  32. #ifndef _MEASE_H_
  33. #define _MEASE_H_
  34. // the ease methods below all are static and take atomic types as params
  35. // so they are the most generally useful. for convenience, define here
  36. // a type that can contain all the params needed for below to make
  37. // data structures that use these methods cleaner...
  38. //------------------------------------------------------------------------------
  39. class Ease
  40. {
  41. //-------------------------------------- Public data
  42. public:
  43. enum enumDirection
  44. {
  45. InOut=0,
  46. In,
  47. Out
  48. };
  49. enum enumType
  50. {
  51. Linear=0,
  52. Quadratic,
  53. Cubic,
  54. Quartic,
  55. Quintic,
  56. Sinusoidal,
  57. Exponential,
  58. Circular,
  59. Elastic,
  60. Back,
  61. Bounce,
  62. };
  63. };
  64. class EaseF : public Ease
  65. {
  66. //-------------------------------------- Public data
  67. public:
  68. S32 dir; // inout, in, out
  69. S32 type; // linear, etc...
  70. F32 param[2]; // optional params
  71. //-------------------------------------- Public interface
  72. public:
  73. EaseF();
  74. EaseF(const EaseF &ease);
  75. EaseF(const S32 dir, const S32 type);
  76. EaseF(const S32 dir, const S32 type, F32 param[2]);
  77. //-------------------------------------- Non-math mutators and misc functions
  78. void set(const S32 dir, const S32 type);
  79. void set(const S32 dir, const S32 type, F32 param[2]);
  80. void set(const S32 dir, const S32 type, F32 param0, F32 param1);
  81. void set(const char *s);
  82. F32 getValue(F32 t, F32 b, F32 c, F32 d) const;
  83. F32 getUnitValue(F32 t, bool noExtrapolation) const
  84. {
  85. F32 v = getValue(t,0.0f,1.0f,1.0f);
  86. if (noExtrapolation)
  87. v = mClampF(v,0.0f,1.0f);
  88. return v;
  89. }
  90. F32 getUnitValue(F32 t) const
  91. {
  92. return getValue(t,0.0f,1.0f,1.0f);
  93. }
  94. };
  95. // simple linear tweening - no easing
  96. // t: current time, b: beginning value, c: change in value, d: duration
  97. inline F32 mLinearTween(F32 t, F32 b, F32 c, F32 d) {
  98. return c*t/d + b;
  99. }
  100. ///////////// QUADRATIC EASING: t^2 ///////////////////
  101. // quadratic easing in - accelerating from zero velocity
  102. // t: current time, b: beginning value, c: change in value, d: duration
  103. // t and d can be in frames or seconds/milliseconds
  104. inline F32 mEaseInQuad(F32 t, F32 b, F32 c, F32 d) {
  105. return c*(t/=d)*t + b;
  106. };
  107. // quadratic easing out - decelerating to zero velocity
  108. inline F32 mEaseOutQuad(F32 t, F32 b, F32 c, F32 d) {
  109. return -c *(t/=d)*(t-2) + b;
  110. };
  111. // quadratic easing in/out - acceleration until halfway, then deceleration
  112. inline F32 mEaseInOutQuad(F32 t, F32 b, F32 c, F32 d) {
  113. if ((t/=d/2) < 1) return c/2*t*t + b;
  114. return -c/2 * ((--t)*(t-2) - 1) + b;
  115. };
  116. ///////////// CUBIC EASING: t^3 ///////////////////////
  117. // cubic easing in - accelerating from zero velocity
  118. // t: current time, b: beginning value, c: change in value, d: duration
  119. // t and d can be frames or seconds/milliseconds
  120. inline F32 mEaseInCubic(F32 t, F32 b, F32 c, F32 d) {
  121. return c*(t/=d)*t*t + b;
  122. };
  123. // cubic easing out - decelerating to zero velocity
  124. inline F32 mEaseOutCubic(F32 t, F32 b, F32 c, F32 d) {
  125. return c*((t=t/d-1)*t*t + 1) + b;
  126. };
  127. // cubic easing in/out - acceleration until halfway, then deceleration
  128. inline F32 mEaseInOutCubic(F32 t, F32 b, F32 c, F32 d) {
  129. if ((t/=d/2) < 1) return c/2*t*t*t + b;
  130. return c/2*((t-=2)*t*t + 2) + b;
  131. };
  132. ///////////// QUARTIC EASING: t^4 /////////////////////
  133. // quartic easing in - accelerating from zero velocity
  134. // t: current time, b: beginning value, c: change in value, d: duration
  135. // t and d can be frames or seconds/milliseconds
  136. inline F32 mEaseInQuart(F32 t, F32 b, F32 c, F32 d) {
  137. return c*(t/=d)*t*t*t + b;
  138. };
  139. // quartic easing out - decelerating to zero velocity
  140. inline F32 mEaseOutQuart(F32 t, F32 b, F32 c, F32 d) {
  141. return -c * ((t=t/d-1)*t*t*t - 1) + b;
  142. };
  143. // quartic easing in/out - acceleration until halfway, then deceleration
  144. inline F32 mEaseInOutQuart(F32 t, F32 b, F32 c, F32 d) {
  145. if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  146. return -c/2 * ((t-=2)*t*t*t - 2) + b;
  147. };
  148. ///////////// QUINTIC EASING: t^5 ////////////////////
  149. // quintic easing in - accelerating from zero velocity
  150. // t: current time, b: beginning value, c: change in value, d: duration
  151. // t and d can be frames or seconds/milliseconds
  152. inline F32 mEaseInQuint(F32 t, F32 b, F32 c, F32 d) {
  153. return c*(t/=d)*t*t*t*t + b;
  154. };
  155. // quintic easing out - decelerating to zero velocity
  156. inline F32 mEaseOutQuint(F32 t, F32 b, F32 c, F32 d) {
  157. return c*((t=t/d-1)*t*t*t*t + 1) + b;
  158. };
  159. // quintic easing in/out - acceleration until halfway, then deceleration
  160. inline F32 mEaseInOutQuint(F32 t, F32 b, F32 c, F32 d) {
  161. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  162. return c/2*((t-=2)*t*t*t*t + 2) + b;
  163. };
  164. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  165. // sinusoidal easing in - accelerating from zero velocity
  166. // t: current time, b: beginning value, c: change in position, d: duration
  167. inline F32 mEaseInSine(F32 t, F32 b, F32 c, F32 d) {
  168. return -c * mCos(t/d * (M_PI_F/2)) + c + b;
  169. };
  170. // sinusoidal easing out - decelerating to zero velocity
  171. inline F32 mEaseOutSine(F32 t, F32 b, F32 c, F32 d) {
  172. return c * mSin(t/d * (M_PI_F/2)) + b;
  173. };
  174. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  175. inline F32 mEaseInOutSine(F32 t, F32 b, F32 c, F32 d) {
  176. return -c/2 * (mCos(M_PI_F*t/d) - 1) + b;
  177. };
  178. ///////////// EXPONENTIAL EASING: 2^t /////////////////
  179. // exponential easing in - accelerating from zero velocity
  180. // t: current time, b: beginning value, c: change in position, d: duration
  181. inline F32 mEaseInExpo(F32 t, F32 b, F32 c, F32 d) {
  182. return (t==0) ? b : c * mPow(2, 10 * (t/d - 1)) + b;
  183. };
  184. // exponential easing out - decelerating to zero velocity
  185. inline F32 mEaseOutExpo(F32 t, F32 b, F32 c, F32 d) {
  186. return (t==d) ? b+c : c * (-mPow(2, -10 * t/d) + 1) + b;
  187. };
  188. // exponential easing in/out - accelerating until halfway, then decelerating
  189. inline F32 mEaseInOutExpo(F32 t, F32 b, F32 c, F32 d) {
  190. if (t==0) return b;
  191. if (t==d) return b+c;
  192. if ((t/=d/2) < 1) return c/2 * mPow(2, 10 * (t - 1)) + b;
  193. return c/2 * (-mPow(2, -10 * --t) + 2) + b;
  194. };
  195. /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  196. // circular easing in - accelerating from zero velocity
  197. // t: current time, b: beginning value, c: change in position, d: duration
  198. inline F32 mEaseInCirc (F32 t, F32 b, F32 c, F32 d) {
  199. return -c * (mSqrt(1 - (t/=d)*t) - 1) + b;
  200. };
  201. // circular easing out - decelerating to zero velocity
  202. inline F32 mEaseOutCirc (F32 t, F32 b, F32 c, F32 d) {
  203. return c * mSqrt(1 - (t=t/d-1)*t) + b;
  204. };
  205. // circular easing in/out - acceleration until halfway, then deceleration
  206. inline F32 mEaseInOutCirc(F32 t, F32 b, F32 c, F32 d) {
  207. if ((t/=d/2) < 1) return -c/2 * (mSqrt(1 - t*t) - 1) + b;
  208. return c/2 * (mSqrt(1 - (t-=2)*t) + 1) + b;
  209. };
  210. /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
  211. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  212. // t and d can be in frames or seconds/milliseconds
  213. inline F32 mEaseInElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p) {
  214. if (t==0) return b; if ((t/=d)==1) return b+c; if (p<=0) p=d*.3f;
  215. F32 s;
  216. if (a < mFabs(c)) { a=c; s=p/4; }
  217. else s = p/(2*M_PI_F) * mAsin (c/a);
  218. return -(a*mPow(2,10*(t-=1)) * mSin( (t*d-s)*(2*M_PI_F)/p )) + b;
  219. };
  220. inline F32 mEaseOutElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p) {
  221. if (t==0) return b; if ((t/=d)==1) return b+c; if (p<=0) p=d*.3f;
  222. F32 s;
  223. if (a < mFabs(c)) { a=c; s=p/4; }
  224. else s = p/(2*M_PI_F) * mAsin (c/a);
  225. return a*mPow(2,-10*t) * mSin( (t*d-s)*(2*M_PI_F)/p ) + c + b;
  226. };
  227. inline F32 mEaseInOutElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p) {
  228. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (p<=0) p=d*(.3f*1.5f);
  229. F32 s;
  230. if (a < mFabs(c)) { a=c; s=p/4; }
  231. else s = p/(2*M_PI_F) * mAsin (c/a);
  232. if (t < 1) return -.5f*(a*mPow(2,10*(t-=1)) * mSin( (t*d-s)*(2*M_PI_F)/p )) + b;
  233. return a*mPow(2,-10*(t-=1)) * mSin( (t*d-s)*(2*M_PI_F)/p )*.5f + c + b;
  234. };
  235. /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
  236. // back easing in - backtracking slightly, then reversing direction and moving to target
  237. // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
  238. // t and d can be in frames or seconds/milliseconds
  239. // s controls the amount of overshoot: higher s means greater overshoot
  240. // s has a default value of 1.70158, which produces an overshoot of 10 percent
  241. // s==0 produces cubic easing with no overshoot
  242. inline F32 mEaseInBack(F32 t, F32 b, F32 c, F32 d, F32 s) {
  243. if (s < 0) s = 1.70158f;
  244. return c*(t/=d)*t*((s+1)*t - s) + b;
  245. };
  246. // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
  247. inline F32 mEaseOutBack(F32 t, F32 b, F32 c, F32 d, F32 s) {
  248. if (s < 0) s = 1.70158f;
  249. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  250. };
  251. // back easing in/out - backtracking slightly, then reversing direction and moving to target,
  252. // then overshooting target, reversing, and finally coming back to target
  253. inline F32 mEaseInOutBack(F32 t, F32 b, F32 c, F32 d, F32 s) {
  254. if (s < 0) s = 1.70158f;
  255. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525f))+1)*t - s)) + b;
  256. return c/2*((t-=2)*t*(((s*=(1.525f))+1)*t + s) + 2) + b;
  257. };
  258. /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
  259. // bounce easing out
  260. inline F32 mEaseOutBounce(F32 t, F32 b, F32 c, F32 d) {
  261. if ((t/=d) < (1/2.75f)) {
  262. return c*(7.5625f*t*t) + b;
  263. } else if (t < (2/2.75)) {
  264. return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
  265. } else if (t < (2.5/2.75)) {
  266. return c*(7.5625f*(t-=(2.25f/2.75f))*t + .9375f) + b;
  267. } else {
  268. return c*(7.5625f*(t-=(2.625f/2.75f))*t + .984375f) + b;
  269. }
  270. };
  271. // bounce easing in
  272. // t: current time, b: beginning value, c: change in position, d: duration
  273. inline F32 mEaseInBounce(F32 t, F32 b, F32 c, F32 d) {
  274. return c - mEaseOutBounce (d-t, 0, c, d) + b;
  275. };
  276. // bounce easing in/out
  277. inline F32 mEaseInOutBounce(F32 t, F32 b, F32 c, F32 d) {
  278. if (t < d/2) return mEaseInBounce (t*2, 0, c, d) * .5f + b;
  279. return mEaseOutBounce (t*2-d, 0, c, d) * .5f + c*.5f + b;
  280. };
  281. #if 0
  282. // ORIGINAL ACTION SCRIPT CODE:
  283. // simple linear tweening - no easing
  284. // t: current time, b: beginning value, c: change in value, d: duration
  285. Math.linearTween = function (t, b, c, d) {
  286. return c*t/d + b;
  287. };
  288. ///////////// QUADRATIC EASING: t^2 ///////////////////
  289. // quadratic easing in - accelerating from zero velocity
  290. // t: current time, b: beginning value, c: change in value, d: duration
  291. // t and d can be in frames or seconds/milliseconds
  292. Math.easeInQuad = function (t, b, c, d) {
  293. return c*(t/=d)*t + b;
  294. };
  295. // quadratic easing out - decelerating to zero velocity
  296. Math.easeOutQuad = function (t, b, c, d) {
  297. return -c *(t/=d)*(t-2) + b;
  298. };
  299. // quadratic easing in/out - acceleration until halfway, then deceleration
  300. Math.easeInOutQuad = function (t, b, c, d) {
  301. if ((t/=d/2) < 1) return c/2*t*t + b;
  302. return -c/2 * ((--t)*(t-2) - 1) + b;
  303. };
  304. ///////////// CUBIC EASING: t^3 ///////////////////////
  305. // cubic easing in - accelerating from zero velocity
  306. // t: current time, b: beginning value, c: change in value, d: duration
  307. // t and d can be frames or seconds/milliseconds
  308. Math.easeInCubic = function (t, b, c, d) {
  309. return c*(t/=d)*t*t + b;
  310. };
  311. // cubic easing out - decelerating to zero velocity
  312. Math.easeOutCubic = function (t, b, c, d) {
  313. return c*((t=t/d-1)*t*t + 1) + b;
  314. };
  315. // cubic easing in/out - acceleration until halfway, then deceleration
  316. Math.easeInOutCubic = function (t, b, c, d) {
  317. if ((t/=d/2) < 1) return c/2*t*t*t + b;
  318. return c/2*((t-=2)*t*t + 2) + b;
  319. };
  320. ///////////// QUARTIC EASING: t^4 /////////////////////
  321. // quartic easing in - accelerating from zero velocity
  322. // t: current time, b: beginning value, c: change in value, d: duration
  323. // t and d can be frames or seconds/milliseconds
  324. Math.easeInQuart = function (t, b, c, d) {
  325. return c*(t/=d)*t*t*t + b;
  326. };
  327. // quartic easing out - decelerating to zero velocity
  328. Math.easeOutQuart = function (t, b, c, d) {
  329. return -c * ((t=t/d-1)*t*t*t - 1) + b;
  330. };
  331. // quartic easing in/out - acceleration until halfway, then deceleration
  332. Math.easeInOutQuart = function (t, b, c, d) {
  333. if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  334. return -c/2 * ((t-=2)*t*t*t - 2) + b;
  335. };
  336. ///////////// QUINTIC EASING: t^5 ////////////////////
  337. // quintic easing in - accelerating from zero velocity
  338. // t: current time, b: beginning value, c: change in value, d: duration
  339. // t and d can be frames or seconds/milliseconds
  340. Math.easeInQuint = function (t, b, c, d) {
  341. return c*(t/=d)*t*t*t*t + b;
  342. };
  343. // quintic easing out - decelerating to zero velocity
  344. Math.easeOutQuint = function (t, b, c, d) {
  345. return c*((t=t/d-1)*t*t*t*t + 1) + b;
  346. };
  347. // quintic easing in/out - acceleration until halfway, then deceleration
  348. Math.easeInOutQuint = function (t, b, c, d) {
  349. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  350. return c/2*((t-=2)*t*t*t*t + 2) + b;
  351. };
  352. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  353. // sinusoidal easing in - accelerating from zero velocity
  354. // t: current time, b: beginning value, c: change in position, d: duration
  355. Math.easeInSine = function (t, b, c, d) {
  356. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  357. };
  358. // sinusoidal easing out - decelerating to zero velocity
  359. Math.easeOutSine = function (t, b, c, d) {
  360. return c * Math.sin(t/d * (Math.PI/2)) + b;
  361. };
  362. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  363. Math.easeInOutSine = function (t, b, c, d) {
  364. return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  365. };
  366. ///////////// EXPONENTIAL EASING: 2^t /////////////////
  367. // exponential easing in - accelerating from zero velocity
  368. // t: current time, b: beginning value, c: change in position, d: duration
  369. Math.easeInExpo = function (t, b, c, d) {
  370. return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  371. };
  372. // exponential easing out - decelerating to zero velocity
  373. Math.easeOutExpo = function (t, b, c, d) {
  374. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  375. };
  376. // exponential easing in/out - accelerating until halfway, then decelerating
  377. Math.easeInOutExpo = function (t, b, c, d) {
  378. if (t==0) return b;
  379. if (t==d) return b+c;
  380. if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
  381. return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  382. };
  383. /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  384. // circular easing in - accelerating from zero velocity
  385. // t: current time, b: beginning value, c: change in position, d: duration
  386. Math.easeInCirc = function (t, b, c, d) {
  387. return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  388. };
  389. // circular easing out - decelerating to zero velocity
  390. Math.easeOutCirc = function (t, b, c, d) {
  391. return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  392. };
  393. // circular easing in/out - acceleration until halfway, then deceleration
  394. Math.easeInOutCirc = function (t, b, c, d) {
  395. if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  396. return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  397. };
  398. /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
  399. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  400. // t and d can be in frames or seconds/milliseconds
  401. Math.easeInElastic = function (t, b, c, d, a, p) {
  402. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  403. if (a < Math.abs(c)) { a=c; var s=p/4; }
  404. else var s = p/(2*Math.PI) * Math.asin (c/a);
  405. return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  406. };
  407. Math.easeOutElastic = function (t, b, c, d, a, p) {
  408. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  409. if (a < Math.abs(c)) { a=c; var s=p/4; }
  410. else var s = p/(2*Math.PI) * Math.asin (c/a);
  411. return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  412. };
  413. Math.easeInOutElastic = function (t, b, c, d, a, p) {
  414. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
  415. if (a < Math.abs(c)) { a=c; var s=p/4; }
  416. else var s = p/(2*Math.PI) * Math.asin (c/a);
  417. if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  418. return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  419. };
  420. /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
  421. // back easing in - backtracking slightly, then reversing direction and moving to target
  422. // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
  423. // t and d can be in frames or seconds/milliseconds
  424. // s controls the amount of overshoot: higher s means greater overshoot
  425. // s has a default value of 1.70158, which produces an overshoot of 10 percent
  426. // s==0 produces cubic easing with no overshoot
  427. Math.easeInBack = function (t, b, c, d, s) {
  428. if (s == undefined) s = 1.70158;
  429. return c*(t/=d)*t*((s+1)*t - s) + b;
  430. };
  431. // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
  432. Math.easeOutBack = function (t, b, c, d, s) {
  433. if (s == undefined) s = 1.70158;
  434. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  435. };
  436. // back easing in/out - backtracking slightly, then reversing direction and moving to target,
  437. // then overshooting target, reversing, and finally coming back to target
  438. Math.easeInOutBack = function (t, b, c, d, s) {
  439. if (s == undefined) s = 1.70158;
  440. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
  441. return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  442. };
  443. /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
  444. // bounce easing in
  445. // t: current time, b: beginning value, c: change in position, d: duration
  446. Math.easeInBounce = function (t, b, c, d) {
  447. return c - Math.easeOutBounce (d-t, 0, c, d) + b;
  448. };
  449. // bounce easing out
  450. Math.easeOutBounce = function (t, b, c, d) {
  451. if ((t/=d) < (1/2.75)) {
  452. return c*(7.5625*t*t) + b;
  453. } else if (t < (2/2.75)) {
  454. return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
  455. } else if (t < (2.5/2.75)) {
  456. return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
  457. } else {
  458. return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
  459. }
  460. };
  461. // bounce easing in/out
  462. Math.easeInOutBounce = function (t, b, c, d) {
  463. if (t < d/2) return Math.easeInBounce (t*2, 0, c, d) * .5 + b;
  464. return Math.easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;
  465. };
  466. #endif
  467. #endif // _MEASE_H_