Easings.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /************************************************************************
  2. * Easings.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /*
  22. *
  23. * Derived from Robert Penner's easing equations: http://robertpenner.com/easing/
  24. * The original copyright notice is as follows:
  25. *
  26. * TERMS OF USE - EASING EQUATIONS
  27. *
  28. * Open source under the BSD License.
  29. *
  30. * Copyright © 2001 Robert Penner
  31. * All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without modification,
  34. * are permitted provided that the following conditions are met:
  35. *
  36. * Redistributions of source code must retain the above copyright notice, this list of
  37. * conditions and the following disclaimer.
  38. * Redistributions in binary form must reproduce the above copyright notice, this list
  39. * of conditions and the following disclaimer in the documentation and/or other materials
  40. * provided with the distribution.
  41. *
  42. * Neither the name of the author nor the names of contributors may be used to endorse
  43. * or promote products derived from this software without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  46. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  47. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  48. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  49. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  50. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  51. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  52. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  53. * OF THE POSSIBILITY OF SUCH DAMAGE.
  54. *
  55. */
  56. const Easings = {
  57. // See https://easings.net/ for more details on each type of easing.
  58. // t: current time, b: begInnIng value, c: change In value, d: duration.
  59. Linear:
  60. {
  61. ease: function(t, b, c, d)
  62. {
  63. return b + (c * t / d);
  64. }
  65. },
  66. Quad:
  67. {
  68. easeIn: function(t, b, c, d)
  69. {
  70. return c * (t /= d) * t + b;
  71. },
  72. easeOut: function(t, b, c, d)
  73. {
  74. return -c * (t /= d) * (t - 2) + b;
  75. },
  76. easeInOut: function(t, b, c, d)
  77. {
  78. if ((t /= d / 2) < 1) return c / 2 * t * t + b;
  79. return -c / 2 * ((--t) * (t - 2) - 1) + b;
  80. }
  81. },
  82. Cubic:
  83. {
  84. easeIn: function(t, b, c, d)
  85. {
  86. return c * (t /= d) * t * t + b;
  87. },
  88. easeOut: function(t, b, c, d)
  89. {
  90. return c * ((t = t / d - 1) * t * t + 1) + b;
  91. },
  92. easeInOut: function(t, b, c, d)
  93. {
  94. if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
  95. return c / 2 * ((t -= 2) * t * t + 2) + b;
  96. }
  97. },
  98. Quart:
  99. {
  100. easeIn: function(t, b, c, d)
  101. {
  102. return c * (t /= d) * t * t * t + b;
  103. },
  104. easeOut: function(t, b, c, d)
  105. {
  106. return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  107. },
  108. easeInOut: function(t, b, c, d)
  109. {
  110. if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
  111. return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
  112. }
  113. },
  114. Quint:
  115. {
  116. easeIn: function(t, b, c, d)
  117. {
  118. return c * (t /= d) * t * t * t * t + b;
  119. },
  120. easeOut: function(t, b, c, d)
  121. {
  122. return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  123. },
  124. easeInOut: function(t, b, c, d)
  125. {
  126. if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
  127. return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
  128. }
  129. },
  130. Sine:
  131. {
  132. easeIn: function(t, b, c, d)
  133. {
  134. return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
  135. },
  136. easeOut: function(t, b, c, d)
  137. {
  138. return c * Math.sin(t / d * (Math.PI / 2)) + b;
  139. },
  140. easeInOut: function(t, b, c, d)
  141. {
  142. return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
  143. }
  144. },
  145. Expo:
  146. {
  147. easeIn: function(t, b, c, d)
  148. {
  149. return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
  150. },
  151. easeOut: function(t, b, c, d)
  152. {
  153. return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
  154. },
  155. easeInOut: function(t, b, c, d)
  156. {
  157. if (t == 0) return b;
  158. if (t == d) return b + c;
  159. if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
  160. return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
  161. }
  162. },
  163. Circ:
  164. {
  165. easeIn: function(t, b, c, d)
  166. {
  167. return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
  168. },
  169. easeOut: function(t, b, c, d)
  170. {
  171. return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
  172. },
  173. easeInOut: function(t, b, c, d)
  174. {
  175. if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
  176. return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
  177. }
  178. },
  179. Elastic:
  180. {
  181. easeIn: function(t, b, c, d)
  182. {
  183. var s = 1.70158;
  184. var p = 0;
  185. var a = c;
  186. if (t == 0) return b;
  187. if ((t /= d) == 1) return b + c;
  188. if (!p) p = d * .3;
  189. if (a < Math.abs(c))
  190. {
  191. a = c;
  192. var s = p / 4;
  193. }
  194. else var s = p / (2 * Math.PI) * Math.asin(c / a);
  195. return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
  196. },
  197. easeOut: function(t, b, c, d)
  198. {
  199. var s = 1.70158;
  200. var p = 0;
  201. var a = c;
  202. if (t == 0) return b;
  203. if ((t /= d) == 1) return b + c;
  204. if (!p) p = d * .3;
  205. if (a < Math.abs(c))
  206. {
  207. a = c;
  208. var s = p / 4;
  209. }
  210. else var s = p / (2 * Math.PI) * Math.asin(c / a);
  211. return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
  212. },
  213. easeInOut: function(t, b, c, d)
  214. {
  215. var s = 1.70158;
  216. var p = 0;
  217. var a = c;
  218. if (t == 0) return b;
  219. if ((t /= d / 2) == 2) return b + c;
  220. if (!p) p = d * (.3 * 1.5);
  221. if (a < Math.abs(c))
  222. {
  223. a = c;
  224. var s = p / 4;
  225. }
  226. else var s = p / (2 * Math.PI) * Math.asin(c / a);
  227. if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
  228. return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
  229. },
  230. },
  231. Back:
  232. {
  233. easeIn: function(t, b, c, d, s)
  234. {
  235. if (s == undefined) s = 1.70158;
  236. return c * (t /= d) * t * ((s + 1) * t - s) + b;
  237. },
  238. easeOut: function(t, b, c, d, s)
  239. {
  240. if (s == undefined) s = 1.70158;
  241. return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  242. },
  243. easeInOut: function(t, b, c, d, s)
  244. {
  245. if (s == undefined) s = 1.70158;
  246. if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
  247. return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
  248. }
  249. },
  250. Bounce:
  251. {
  252. easeIn: function(t, b, c, d)
  253. {
  254. return c - Easings.Bounce.easeOut(d - t, 0, c, d) + b;
  255. },
  256. easeOut: function(t, b, c, d)
  257. {
  258. if ((t /= d) < (1 / 2.75))
  259. return c * (7.5625 * t * t) + b;
  260. else if (t < (2 / 2.75))
  261. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
  262. else if (t < (2.5 / 2.75))
  263. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
  264. else
  265. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
  266. },
  267. easeInOut: function(t, b, c, d)
  268. {
  269. if (t < d / 2) return Easings.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
  270. return Easings.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
  271. }
  272. },
  273. }