Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. package util;
  2.  
  3. typedef EasingFunction = Float->Float->Float->Float->Float->Float;
  4.  
  5. class Easing
  6. {
  7. // simple linear tweening - no easing:Float, no acceleration
  8. public static function linear(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  9. {
  10. return changeInValue * time / duration + startValue;
  11. }
  12.  
  13. // quadratic easing in - accelerating from zero velocity
  14. public static function easeInQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  15. {
  16. time /= duration;
  17. return changeInValue * time * time + startValue;
  18. }
  19.  
  20. // quadratic easing out - decelerating to zero velocity
  21. public static function easeOutQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  22. {
  23. time /= duration;
  24. return - changeInValue * time * (time - 2) + startValue;
  25. }
  26.  
  27. // quadratic easing in/out - acceleration until halfway:Float, then deceleration
  28. public static function easeInOutQuad(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  29. {
  30. time /= duration / 2;
  31. if(time < 1) return changeInValue / 2 * time * time + startValue;
  32. time --;
  33. return - changeInValue / 2 * (time * (time - 2) - 1) + startValue;
  34. }
  35.  
  36. // cubic easing in - accelerating from zero velocity
  37. public static function easeInCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  38. {
  39. time /= duration;
  40. return changeInValue * Math.pow(time, 3) + startValue;
  41. }
  42.  
  43. // cubic easing out - decelerating to zero velocity
  44. public static function easeOutCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  45. {
  46. time /= duration;
  47. time --;
  48. return changeInValue * (Math.pow(time, 3) + 1) + startValue;
  49. }
  50.  
  51. // cubic easing in/out - acceleration until halfway:Float, then deceleration
  52. public static function easeInOutCubic(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  53. {
  54. time /= duration / 2;
  55. if(time < 1) return changeInValue / 2 * Math.pow(time, 3) + startValue;
  56. time -= 2;
  57. return changeInValue / 2 * (Math.pow(time, 3) + 2) + startValue;
  58. }
  59.  
  60. // quartic easing in - accelerating from zero velocity
  61. public static function easeInQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  62. {
  63. time /= duration;
  64. return changeInValue * Math.pow(time, 4) + startValue;
  65. }
  66.  
  67. // quartic easing out - decelerating to zero velocity
  68. public static function easeOutQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  69. {
  70. time /= duration;
  71. time --;
  72. return - changeInValue * (Math.pow(time, 4) - 1) + startValue;
  73. }
  74.  
  75. // quartic easing in/out - acceleration until halfway:Float, then deceleration
  76. public static function easeInOutQuart(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  77. {
  78. time /= duration / 2;
  79. if(time < 1) return changeInValue / 2 * Math.pow(time, 4) + startValue;
  80. time -= 2;
  81. return - changeInValue / 2 * (Math.pow(time, 4) - 2) + startValue;
  82. }
  83.  
  84. // quintic easing in - accelerating from zero velocity
  85. public static function easeInQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  86. {
  87. time /= duration;
  88. return changeInValue * Math.pow(time, 5) + startValue;
  89. }
  90.  
  91. // quintic easing out - decelerating to zero velocity
  92. public static function easeOutQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  93. {
  94. time /= duration;
  95. time --;
  96. return changeInValue * (Math.pow(time, 5) + 1) + startValue;
  97. }
  98.  
  99. // quintic easing in/out - acceleration until halfway:Float, then deceleration
  100. public static function easeInOutQuint(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  101. {
  102. time /= duration / 2;
  103. if(time < 1) return changeInValue / 2 * Math.pow(time, 5) + startValue;
  104. time -= 2;
  105. return changeInValue / 2 * (Math.pow(time, 5) + 2) + startValue;
  106. }
  107.  
  108. // sinusoidal easing in - accelerating from zero velocity
  109. public static function easeInSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  110. {
  111. return - changeInValue * Math.cos(time / duration * (Math.PI / 2)) + changeInValue + startValue;
  112. }
  113.  
  114. // sinusoidal easing out - decelerating to zero velocity
  115. public static function easeOutSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  116. {
  117. return changeInValue * Math.sin(time / duration * (Math.PI / 2)) + startValue;
  118. }
  119.  
  120. // sinusoidal easing in/out - accelerating until halfway:Float, then decelerating
  121. public static function easeInOutSine(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  122. {
  123. return - changeInValue / 2 * (Math.cos(Math.PI * time / d) - 1) + startValue;
  124. }
  125.  
  126. // exponential easing in - accelerating from zero velocity
  127. public static function easeInExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  128. {
  129. return changeInValue * Math.pow(2, 10 * (time / duration - 1) ) + startValue;
  130. }
  131.  
  132. // exponential easing out - decelerating to zero velocity
  133. public static function easeOutExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  134. {
  135. return changeInValue * (-Math.pow(2, - 10 * time / duration ) + 1 ) + startValue;
  136. }
  137.  
  138. // exponential easing in/out - accelerating until halfway:Float, then decelerating
  139. public static function easeInOutExpo(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  140. {
  141. time /= duration / 2;
  142. if(time < 1) return changeInValue / 2 * Math.pow(2, 10 * (time - 1) ) + startValue;
  143. time --;
  144. return changeInValue / 2 * (-Math.pow(2, - 10 * t) + 2 ) + startValue;
  145. }
  146.  
  147. // circular easing in - accelerating from zero velocity
  148. public static function easeInCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  149. {
  150. time /= duration;
  151. return - changeInValue * (Math.sqrt(1 - time * t) - 1) + startValue;
  152. }
  153.  
  154. // circular easing out - decelerating to zero velocity
  155. public static function easeOutCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  156. {
  157. time /= duration;
  158. time --;
  159. return changeInValue * Math.sqrt(1 - time * t) + startValue;
  160. }
  161.  
  162. // circular easing in/out - acceleration until halfway:Float, then deceleration
  163. public static function easeInOutCirc(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  164. {
  165. time /= duration / 2;
  166. if(time < 1) return - changeInValue / 2 * (Math.sqrt(1 - time * t) - 1) + startValue;
  167. time -= 2;
  168. return changeInValue / 2 * (Math.sqrt(1 - time * t) + 1) + startValue;
  169. }
  170.  
  171. // back easing in - back up on source
  172. // o=1.70158 for a 10% bounce
  173. public static function easeInBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
  174. {
  175. return changeInValue * (time /= d) * time * ((o + 1) * time - o) + startValue;
  176. }
  177.  
  178. // back easing out - overshoot target
  179. // o=1.70158 for a 10% bounce
  180. public static function easeOutBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
  181. {
  182. return changeInValue * ((time = time / d-1) * time * ((o+1) * time + o) + 1) + startValue;
  183. }
  184.  
  185. // back easing in/out - back up on source then overshoot target
  186. // o=1.70158 for a 10% bounce
  187. public static function easeInOutBack(time:Float, startValue:Float, changeInValue:Float, duration:Float, o:Float): Float
  188. {
  189. if ((time /=duration / 2) < 1) return changeInValue / 2 * (time * time * (((o *= (1.525)) + 1) * time - o)) + startValue;
  190. return changeInValue / 2 * ((time -= 2) * time * (((o *= (1.525)) + 1) * time + o) + 2) + startValue;
  191. }
  192.  
  193. // bounce easing in
  194. public static function easeInBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  195. {
  196. return changeInValue - easeOutBounce (duration - time, 0, changeInValue, d) + startValue;
  197. }
  198.  
  199. // bounce easing out
  200. public static function easeOutBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  201. {
  202. if ((time /= d) < (1 / 2.75))
  203. return changeInValue * (7.5625 * time * t) + startValue;
  204. else if (time < (2 / 2.75))
  205. return changeInValue * (7.5625 * (time -= (1.5 / 2.75)) * time + .75) + startValue;
  206. else if (time < (2.5 / 2.75))
  207. return changeInValue * (7.5625 * (time -= (2.25 / 2.75)) * time + .9375) + startValue;
  208. return changeInValue * (7.5625 * (time -= (2.625 / 2.75)) * time + .984375) + startValue;
  209. }
  210.  
  211. // bounce easing in/out
  212. public static function easeInOutBounce(time:Float, startValue:Float, changeInValue:Float, duration:Float, ?o:Float): Float
  213. {
  214. if (time < duration / 2) return easeInBounce (time * 2, 0, changeInValue, d) * .5 + startValue;
  215. return easeOutBounce(time * 2 - duration, 0, changeInValue, d) * .5 + changeInValue * .5 + startValue;
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement