mEase.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. t /= d;
  106. return c*t*t + b;
  107. };
  108. // quadratic easing out - decelerating to zero velocity
  109. inline F32 mEaseOutQuad(F32 t, F32 b, F32 c, F32 d) {
  110. t /= d;
  111. return -c * t*(t-2) + b;
  112. };
  113. // quadratic easing in/out - acceleration until halfway, then deceleration
  114. inline F32 mEaseInOutQuad(F32 t, F32 b, F32 c, F32 d) {
  115. t /= d/2;
  116. if (t < 1) return c/2*t*t + b;
  117. t--;
  118. return -c/2 * (t*(t-2) - 1) + b;
  119. };
  120. ///////////// CUBIC EASING: t^3 ///////////////////////
  121. // cubic easing in - accelerating from zero velocity
  122. // t: current time, b: beginning value, c: change in value, d: duration
  123. // t and d can be frames or seconds/milliseconds
  124. inline F32 mEaseInCubic(F32 t, F32 b, F32 c, F32 d) {
  125. t /= d;
  126. return c*t*t*t + b;
  127. };
  128. // cubic easing out - decelerating to zero velocity
  129. inline F32 mEaseOutCubic(F32 t, F32 b, F32 c, F32 d) {
  130. t /= d;
  131. t--;
  132. return c*(t*t*t + 1) + b;
  133. };
  134. // cubic easing in/out - acceleration until halfway, then deceleration
  135. inline F32 mEaseInOutCubic(F32 t, F32 b, F32 c, F32 d) {
  136. t /= d/2;
  137. if (t < 1) return c/2*t*t*t + b;
  138. t -= 2;
  139. return c/2*(t*t*t + 2) + b;
  140. };
  141. ///////////// QUARTIC EASING: t^4 /////////////////////
  142. // quartic easing in - accelerating from zero velocity
  143. // t: current time, b: beginning value, c: change in value, d: duration
  144. // t and d can be frames or seconds/milliseconds
  145. inline F32 mEaseInQuart(F32 t, F32 b, F32 c, F32 d) {
  146. t /= d;
  147. return c*t*t*t*t + b;
  148. };
  149. // quartic easing out - decelerating to zero velocity
  150. inline F32 mEaseOutQuart(F32 t, F32 b, F32 c, F32 d) {
  151. t /= d;
  152. t--;
  153. return -c * (t*t*t*t - 1) + b;
  154. };
  155. // quartic easing in/out - acceleration until halfway, then deceleration
  156. inline F32 mEaseInOutQuart(F32 t, F32 b, F32 c, F32 d) {
  157. t /= d/2;
  158. if (t < 1) return c/2*t*t*t*t + b;
  159. t -= 2;
  160. return -c/2 * (t*t*t*t - 2) + b;
  161. };
  162. ///////////// QUINTIC EASING: t^5 ////////////////////
  163. // quintic easing in - accelerating from zero velocity
  164. // t: current time, b: beginning value, c: change in value, d: duration
  165. // t and d can be frames or seconds/milliseconds
  166. inline F32 mEaseInQuint(F32 t, F32 b, F32 c, F32 d) {
  167. t /= d;
  168. return c*t*t*t*t*t + b;
  169. };
  170. // quintic easing out - decelerating to zero velocity
  171. inline F32 mEaseOutQuint(F32 t, F32 b, F32 c, F32 d) {
  172. t /= d;
  173. t--;
  174. return c*(t*t*t*t*t + 1) + b;
  175. };
  176. // quintic easing in/out - acceleration until halfway, then deceleration
  177. inline F32 mEaseInOutQuint(F32 t, F32 b, F32 c, F32 d) {
  178. t /= d/2;
  179. if (t < 1) return c/2*t*t*t*t*t + b;
  180. t -= 2;
  181. return c/2*(t*t*t*t*t + 2) + b;
  182. };
  183. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  184. // sinusoidal easing in - accelerating from zero velocity
  185. // t: current time, b: beginning value, c: change in position, d: duration
  186. inline F32 mEaseInSine(F32 t, F32 b, F32 c, F32 d) {
  187. return -c * mCos(t/d * (M_PI_F/2)) + c + b;
  188. };
  189. // sinusoidal easing out - decelerating to zero velocity
  190. inline F32 mEaseOutSine(F32 t, F32 b, F32 c, F32 d) {
  191. return c * mSin(t/d * (M_PI_F/2)) + b;
  192. };
  193. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  194. inline F32 mEaseInOutSine(F32 t, F32 b, F32 c, F32 d) {
  195. return -c/2 * (mCos(M_PI_F*t/d) - 1) + b;
  196. };
  197. ///////////// EXPONENTIAL EASING: 2^t /////////////////
  198. // exponential easing in - accelerating from zero velocity
  199. // t: current time, b: beginning value, c: change in position, d: duration
  200. inline F32 mEaseInExpo(F32 t, F32 b, F32 c, F32 d) {
  201. return c * mPow( 2, 10 * (t/d - 1) ) + b;
  202. };
  203. // exponential easing out - decelerating to zero velocity
  204. inline F32 mEaseOutExpo(F32 t, F32 b, F32 c, F32 d) {
  205. return c * ( -mPow( 2, -10 * t/d ) + 1 ) + b;
  206. };
  207. // exponential easing in/out - accelerating until halfway, then decelerating
  208. inline F32 mEaseInOutExpo(F32 t, F32 b, F32 c, F32 d) {
  209. t /= d/2;
  210. if (t < 1) return c/2 * mPow( 2, 10 * (t - 1) ) + b;
  211. t--;
  212. return c/2 * ( -mPow( 2, -10 * t) + 2 ) + b;
  213. };
  214. /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  215. // circular easing in - accelerating from zero velocity
  216. // t: current time, b: beginning value, c: change in position, d: duration
  217. inline F32 mEaseInCirc (F32 t, F32 b, F32 c, F32 d) {
  218. return -c * (mSqrt(1 - (t/=d)*t) - 1) + b;
  219. };
  220. // circular easing out - decelerating to zero velocity
  221. inline F32 mEaseOutCirc (F32 t, F32 b, F32 c, F32 d) {
  222. return c * mSqrt(1 - (t=t/d-1)*t) + b;
  223. };
  224. // circular easing in/out - acceleration until halfway, then deceleration
  225. inline F32 mEaseInOutCirc(F32 t, F32 b, F32 c, F32 d) {
  226. if ((t/=d/2) < 1) return -c/2 * (mSqrt(1 - t*t) - 1) + b;
  227. return c/2 * (mSqrt(1 - (t-=2)*t) + 1) + b;
  228. };
  229. /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
  230. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  231. // t and d can be in frames or seconds/milliseconds
  232. inline F32 mEaseInElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p) {
  233. if (t==0) return b; if ((t/=d)==1) return b+c; if (p<=0) p=d*.3f;
  234. F32 s;
  235. if (a < mFabs(c)) { a=c; s=p/4; }
  236. else s = p/(2*M_PI_F) * mAsin (c/a);
  237. return -(a*mPow(2,10*(t-=1)) * mSin( (t*d-s)*(2*M_PI_F)/p )) + b;
  238. };
  239. inline F32 mEaseOutElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p) {
  240. if (t==0) return b; if ((t/=d)==1) return b+c; if (p<=0) p=d*.3f;
  241. F32 s;
  242. if (a < mFabs(c)) { a=c; s=p/4; }
  243. else s = p/(2*M_PI_F) * mAsin (c/a);
  244. return a*mPow(2,-10*t) * mSin( (t*d-s)*(2*M_PI_F)/p ) + c + b;
  245. };
  246. inline F32 mEaseInOutElastic(F32 t, F32 b, F32 c, F32 d, F32 a, F32 p) {
  247. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (p<=0) p=d*(.3f*1.5f);
  248. F32 s;
  249. if (a < mFabs(c)) { a=c; s=p/4; }
  250. else s = p/(2*M_PI_F) * mAsin (c/a);
  251. if (t < 1) return -.5f*(a*mPow(2,10*(t-=1)) * mSin( (t*d-s)*(2*M_PI_F)/p )) + b;
  252. return a*mPow(2,-10*(t-=1)) * mSin( (t*d-s)*(2*M_PI_F)/p )*.5f + c + b;
  253. };
  254. /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
  255. // back easing in - backtracking slightly, then reversing direction and moving to target
  256. // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
  257. // t and d can be in frames or seconds/milliseconds
  258. // s controls the amount of overshoot: higher s means greater overshoot
  259. // s has a default value of 1.70158, which produces an overshoot of 10 percent
  260. // s==0 produces cubic easing with no overshoot
  261. inline F32 mEaseInBack(F32 t, F32 b, F32 c, F32 d, F32 s) {
  262. if (s < 0) s = 1.70158f;
  263. return c*(t/=d)*t*((s+1)*t - s) + b;
  264. };
  265. // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
  266. inline F32 mEaseOutBack(F32 t, F32 b, F32 c, F32 d, F32 s) {
  267. if (s < 0) s = 1.70158f;
  268. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  269. };
  270. // back easing in/out - backtracking slightly, then reversing direction and moving to target,
  271. // then overshooting target, reversing, and finally coming back to target
  272. inline F32 mEaseInOutBack(F32 t, F32 b, F32 c, F32 d, F32 s) {
  273. if (s < 0) s = 1.70158f;
  274. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525f))+1)*t - s)) + b;
  275. return c/2*((t-=2)*t*(((s*=(1.525f))+1)*t + s) + 2) + b;
  276. };
  277. /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
  278. // bounce easing out
  279. inline F32 mEaseOutBounce(F32 t, F32 b, F32 c, F32 d) {
  280. if ((t/=d) < (1/2.75f)) {
  281. return c*(7.5625f*t*t) + b;
  282. } else if (t < (2/2.75)) {
  283. return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
  284. } else if (t < (2.5/2.75)) {
  285. return c*(7.5625f*(t-=(2.25f/2.75f))*t + .9375f) + b;
  286. } else {
  287. return c*(7.5625f*(t-=(2.625f/2.75f))*t + .984375f) + b;
  288. }
  289. };
  290. // bounce easing in
  291. // t: current time, b: beginning value, c: change in position, d: duration
  292. inline F32 mEaseInBounce(F32 t, F32 b, F32 c, F32 d) {
  293. return c - mEaseOutBounce (d-t, 0, c, d) + b;
  294. };
  295. // bounce easing in/out
  296. inline F32 mEaseInOutBounce(F32 t, F32 b, F32 c, F32 d) {
  297. if (t < d/2) return mEaseInBounce (t*2, 0, c, d) * .5f + b;
  298. return mEaseOutBounce (t*2-d, 0, c, d) * .5f + c*.5f + b;
  299. };
  300. #if 0
  301. // ORIGINAL ACTION SCRIPT CODE:
  302. // simple linear tweening - no easing
  303. // t: current time, b: beginning value, c: change in value, d: duration
  304. Math.linearTween = function (t, b, c, d) {
  305. return c*t/d + b;
  306. };
  307. ///////////// QUADRATIC EASING: t^2 ///////////////////
  308. // quadratic easing in - accelerating from zero velocity
  309. // t: current time, b: beginning value, c: change in value, d: duration
  310. // t and d can be in frames or seconds/milliseconds
  311. Math.easeInQuad = function (t, b, c, d) {
  312. return c*(t/=d)*t + b;
  313. };
  314. // quadratic easing out - decelerating to zero velocity
  315. Math.easeOutQuad = function (t, b, c, d) {
  316. return -c *(t/=d)*(t-2) + b;
  317. };
  318. // quadratic easing in/out - acceleration until halfway, then deceleration
  319. Math.easeInOutQuad = function (t, b, c, d) {
  320. if ((t/=d/2) < 1) return c/2*t*t + b;
  321. return -c/2 * ((--t)*(t-2) - 1) + b;
  322. };
  323. ///////////// CUBIC EASING: t^3 ///////////////////////
  324. // cubic easing in - accelerating from zero velocity
  325. // t: current time, b: beginning value, c: change in value, d: duration
  326. // t and d can be frames or seconds/milliseconds
  327. Math.easeInCubic = function (t, b, c, d) {
  328. return c*(t/=d)*t*t + b;
  329. };
  330. // cubic easing out - decelerating to zero velocity
  331. Math.easeOutCubic = function (t, b, c, d) {
  332. return c*((t=t/d-1)*t*t + 1) + b;
  333. };
  334. // cubic easing in/out - acceleration until halfway, then deceleration
  335. Math.easeInOutCubic = function (t, b, c, d) {
  336. if ((t/=d/2) < 1) return c/2*t*t*t + b;
  337. return c/2*((t-=2)*t*t + 2) + b;
  338. };
  339. ///////////// QUARTIC EASING: t^4 /////////////////////
  340. // quartic easing in - accelerating from zero velocity
  341. // t: current time, b: beginning value, c: change in value, d: duration
  342. // t and d can be frames or seconds/milliseconds
  343. Math.easeInQuart = function (t, b, c, d) {
  344. return c*(t/=d)*t*t*t + b;
  345. };
  346. // quartic easing out - decelerating to zero velocity
  347. Math.easeOutQuart = function (t, b, c, d) {
  348. return -c * ((t=t/d-1)*t*t*t - 1) + b;
  349. };
  350. // quartic easing in/out - acceleration until halfway, then deceleration
  351. Math.easeInOutQuart = function (t, b, c, d) {
  352. if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
  353. return -c/2 * ((t-=2)*t*t*t - 2) + b;
  354. };
  355. ///////////// QUINTIC EASING: t^5 ////////////////////
  356. // quintic easing in - accelerating from zero velocity
  357. // t: current time, b: beginning value, c: change in value, d: duration
  358. // t and d can be frames or seconds/milliseconds
  359. Math.easeInQuint = function (t, b, c, d) {
  360. return c*(t/=d)*t*t*t*t + b;
  361. };
  362. // quintic easing out - decelerating to zero velocity
  363. Math.easeOutQuint = function (t, b, c, d) {
  364. return c*((t=t/d-1)*t*t*t*t + 1) + b;
  365. };
  366. // quintic easing in/out - acceleration until halfway, then deceleration
  367. Math.easeInOutQuint = function (t, b, c, d) {
  368. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  369. return c/2*((t-=2)*t*t*t*t + 2) + b;
  370. };
  371. ///////////// SINUSOIDAL EASING: sin(t) ///////////////
  372. // sinusoidal easing in - accelerating from zero velocity
  373. // t: current time, b: beginning value, c: change in position, d: duration
  374. Math.easeInSine = function (t, b, c, d) {
  375. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  376. };
  377. // sinusoidal easing out - decelerating to zero velocity
  378. Math.easeOutSine = function (t, b, c, d) {
  379. return c * Math.sin(t/d * (Math.PI/2)) + b;
  380. };
  381. // sinusoidal easing in/out - accelerating until halfway, then decelerating
  382. Math.easeInOutSine = function (t, b, c, d) {
  383. return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  384. };
  385. ///////////// EXPONENTIAL EASING: 2^t /////////////////
  386. // exponential easing in - accelerating from zero velocity
  387. // t: current time, b: beginning value, c: change in position, d: duration
  388. Math.easeInExpo = function (t, b, c, d) {
  389. return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  390. };
  391. // exponential easing out - decelerating to zero velocity
  392. Math.easeOutExpo = function (t, b, c, d) {
  393. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  394. };
  395. // exponential easing in/out - accelerating until halfway, then decelerating
  396. Math.easeInOutExpo = function (t, b, c, d) {
  397. if (t==0) return b;
  398. if (t==d) return b+c;
  399. if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
  400. return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  401. };
  402. /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
  403. // circular easing in - accelerating from zero velocity
  404. // t: current time, b: beginning value, c: change in position, d: duration
  405. Math.easeInCirc = function (t, b, c, d) {
  406. return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  407. };
  408. // circular easing out - decelerating to zero velocity
  409. Math.easeOutCirc = function (t, b, c, d) {
  410. return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  411. };
  412. // circular easing in/out - acceleration until halfway, then deceleration
  413. Math.easeInOutCirc = function (t, b, c, d) {
  414. if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  415. return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  416. };
  417. /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
  418. // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
  419. // t and d can be in frames or seconds/milliseconds
  420. Math.easeInElastic = function (t, b, c, d, a, p) {
  421. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  422. if (a < Math.abs(c)) { a=c; var s=p/4; }
  423. else var s = p/(2*Math.PI) * Math.asin (c/a);
  424. return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  425. };
  426. Math.easeOutElastic = function (t, b, c, d, a, p) {
  427. if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
  428. if (a < Math.abs(c)) { a=c; var s=p/4; }
  429. else var s = p/(2*Math.PI) * Math.asin (c/a);
  430. return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  431. };
  432. Math.easeInOutElastic = function (t, b, c, d, a, p) {
  433. if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
  434. if (a < Math.abs(c)) { a=c; var s=p/4; }
  435. else var s = p/(2*Math.PI) * Math.asin (c/a);
  436. if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  437. return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  438. };
  439. /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
  440. // back easing in - backtracking slightly, then reversing direction and moving to target
  441. // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
  442. // t and d can be in frames or seconds/milliseconds
  443. // s controls the amount of overshoot: higher s means greater overshoot
  444. // s has a default value of 1.70158, which produces an overshoot of 10 percent
  445. // s==0 produces cubic easing with no overshoot
  446. Math.easeInBack = function (t, b, c, d, s) {
  447. if (s == undefined) s = 1.70158;
  448. return c*(t/=d)*t*((s+1)*t - s) + b;
  449. };
  450. // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
  451. Math.easeOutBack = function (t, b, c, d, s) {
  452. if (s == undefined) s = 1.70158;
  453. return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  454. };
  455. // back easing in/out - backtracking slightly, then reversing direction and moving to target,
  456. // then overshooting target, reversing, and finally coming back to target
  457. Math.easeInOutBack = function (t, b, c, d, s) {
  458. if (s == undefined) s = 1.70158;
  459. if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
  460. return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  461. };
  462. /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
  463. // bounce easing in
  464. // t: current time, b: beginning value, c: change in position, d: duration
  465. Math.easeInBounce = function (t, b, c, d) {
  466. return c - Math.easeOutBounce (d-t, 0, c, d) + b;
  467. };
  468. // bounce easing out
  469. Math.easeOutBounce = function (t, b, c, d) {
  470. if ((t/=d) < (1/2.75)) {
  471. return c*(7.5625*t*t) + b;
  472. } else if (t < (2/2.75)) {
  473. return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
  474. } else if (t < (2.5/2.75)) {
  475. return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
  476. } else {
  477. return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
  478. }
  479. };
  480. // bounce easing in/out
  481. Math.easeInOutBounce = function (t, b, c, d) {
  482. if (t < d/2) return Math.easeInBounce (t*2, 0, c, d) * .5 + b;
  483. return Math.easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;
  484. };
  485. #endif
  486. #endif // _MEASE_H_