timing.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Timing = void 0;
  4. var Timing;
  5. (function (Timing) {
  6. Timing.linear = (t) => t;
  7. Timing.quad = (t) => t * t;
  8. Timing.cubic = (t) => t * t * t;
  9. Timing.inout = (t) => {
  10. if (t <= 0) {
  11. return 0;
  12. }
  13. if (t >= 1) {
  14. return 1;
  15. }
  16. const t2 = t * t;
  17. const t3 = t2 * t;
  18. return 4 * (t < 0.5 ? t3 : 3 * (t - t2) + t3 - 0.75);
  19. };
  20. Timing.exponential = (t) => {
  21. return Math.pow(2, 10 * (t - 1)); // eslint-disable-line
  22. };
  23. Timing.bounce = ((t) => {
  24. // eslint-disable-next-line
  25. for (let a = 0, b = 1; 1; a += b, b /= 2) {
  26. if (t >= (7 - 4 * a) / 11) {
  27. const q = (11 - 6 * a - 11 * t) / 4;
  28. return -q * q + b * b;
  29. }
  30. }
  31. });
  32. })(Timing = exports.Timing || (exports.Timing = {}));
  33. (function (Timing) {
  34. Timing.decorators = {
  35. reverse(f) {
  36. return (t) => 1 - f(1 - t);
  37. },
  38. reflect(f) {
  39. return (t) => 0.5 * (t < 0.5 ? f(2 * t) : 2 - f(2 - 2 * t));
  40. },
  41. clamp(f, n = 0, x = 1) {
  42. return (t) => {
  43. const r = f(t);
  44. return r < n ? n : r > x ? x : r;
  45. };
  46. },
  47. back(s = 1.70158) {
  48. return (t) => t * t * ((s + 1) * t - s);
  49. },
  50. elastic(x = 1.5) {
  51. return (t) => Math.pow(2, 10 * (t - 1)) * Math.cos(((20 * Math.PI * x) / 3) * t); // eslint-disable-line
  52. },
  53. };
  54. })(Timing = exports.Timing || (exports.Timing = {}));
  55. (function (Timing) {
  56. // Slight acceleration from zero to full speed
  57. function easeInSine(t) {
  58. return -1 * Math.cos(t * (Math.PI / 2)) + 1;
  59. }
  60. Timing.easeInSine = easeInSine;
  61. // Slight deceleration at the end
  62. function easeOutSine(t) {
  63. return Math.sin(t * (Math.PI / 2));
  64. }
  65. Timing.easeOutSine = easeOutSine;
  66. // Slight acceleration at beginning and slight deceleration at end
  67. function easeInOutSine(t) {
  68. return -0.5 * (Math.cos(Math.PI * t) - 1);
  69. }
  70. Timing.easeInOutSine = easeInOutSine;
  71. // Accelerating from zero velocity
  72. function easeInQuad(t) {
  73. return t * t;
  74. }
  75. Timing.easeInQuad = easeInQuad;
  76. // Decelerating to zero velocity
  77. function easeOutQuad(t) {
  78. return t * (2 - t);
  79. }
  80. Timing.easeOutQuad = easeOutQuad;
  81. // Acceleration until halfway, then deceleration
  82. function easeInOutQuad(t) {
  83. return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
  84. }
  85. Timing.easeInOutQuad = easeInOutQuad;
  86. // Accelerating from zero velocity
  87. function easeInCubic(t) {
  88. return t * t * t;
  89. }
  90. Timing.easeInCubic = easeInCubic;
  91. // Decelerating to zero velocity
  92. function easeOutCubic(t) {
  93. const t1 = t - 1;
  94. return t1 * t1 * t1 + 1;
  95. }
  96. Timing.easeOutCubic = easeOutCubic;
  97. // Acceleration until halfway, then deceleration
  98. function easeInOutCubic(t) {
  99. return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
  100. }
  101. Timing.easeInOutCubic = easeInOutCubic;
  102. // Accelerating from zero velocity
  103. function easeInQuart(t) {
  104. return t * t * t * t;
  105. }
  106. Timing.easeInQuart = easeInQuart;
  107. // Decelerating to zero velocity
  108. function easeOutQuart(t) {
  109. const t1 = t - 1;
  110. return 1 - t1 * t1 * t1 * t1;
  111. }
  112. Timing.easeOutQuart = easeOutQuart;
  113. // Acceleration until halfway, then deceleration
  114. function easeInOutQuart(t) {
  115. const t1 = t - 1;
  116. return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * t1 * t1 * t1 * t1;
  117. }
  118. Timing.easeInOutQuart = easeInOutQuart;
  119. // Accelerating from zero velocity
  120. function easeInQuint(t) {
  121. return t * t * t * t * t;
  122. }
  123. Timing.easeInQuint = easeInQuint;
  124. // Decelerating to zero velocity
  125. function easeOutQuint(t) {
  126. const t1 = t - 1;
  127. return 1 + t1 * t1 * t1 * t1 * t1;
  128. }
  129. Timing.easeOutQuint = easeOutQuint;
  130. // Acceleration until halfway, then deceleration
  131. function easeInOutQuint(t) {
  132. const t1 = t - 1;
  133. return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * t1 * t1 * t1 * t1 * t1;
  134. }
  135. Timing.easeInOutQuint = easeInOutQuint;
  136. // Accelerate exponentially until finish
  137. function easeInExpo(t) {
  138. if (t === 0) {
  139. return 0;
  140. }
  141. return Math.pow(2, 10 * (t - 1)); // eslint-disable-line
  142. }
  143. Timing.easeInExpo = easeInExpo;
  144. // Initial exponential acceleration slowing to stop
  145. function easeOutExpo(t) {
  146. if (t === 1) {
  147. return 1;
  148. }
  149. return -Math.pow(2, -10 * t) + 1; // eslint-disable-line
  150. }
  151. Timing.easeOutExpo = easeOutExpo;
  152. // Exponential acceleration and deceleration
  153. function easeInOutExpo(t) {
  154. if (t === 0 || t === 1) {
  155. return t;
  156. }
  157. const scaledTime = t * 2;
  158. const scaledTime1 = scaledTime - 1;
  159. if (scaledTime < 1) {
  160. return 0.5 * Math.pow(2, 10 * scaledTime1); // eslint-disable-line
  161. }
  162. return 0.5 * (-Math.pow(2, -10 * scaledTime1) + 2); // eslint-disable-line
  163. }
  164. Timing.easeInOutExpo = easeInOutExpo;
  165. // Increasing velocity until stop
  166. function easeInCirc(t) {
  167. const scaledTime = t / 1;
  168. return -1 * (Math.sqrt(1 - scaledTime * t) - 1);
  169. }
  170. Timing.easeInCirc = easeInCirc;
  171. // Start fast, decreasing velocity until stop
  172. function easeOutCirc(t) {
  173. const t1 = t - 1;
  174. return Math.sqrt(1 - t1 * t1);
  175. }
  176. Timing.easeOutCirc = easeOutCirc;
  177. // Fast increase in velocity, fast decrease in velocity
  178. function easeInOutCirc(t) {
  179. const scaledTime = t * 2;
  180. const scaledTime1 = scaledTime - 2;
  181. if (scaledTime < 1) {
  182. return -0.5 * (Math.sqrt(1 - scaledTime * scaledTime) - 1);
  183. }
  184. return 0.5 * (Math.sqrt(1 - scaledTime1 * scaledTime1) + 1);
  185. }
  186. Timing.easeInOutCirc = easeInOutCirc;
  187. // Slow movement backwards then fast snap to finish
  188. function easeInBack(t, magnitude = 1.70158) {
  189. return t * t * ((magnitude + 1) * t - magnitude);
  190. }
  191. Timing.easeInBack = easeInBack;
  192. // Fast snap to backwards point then slow resolve to finish
  193. function easeOutBack(t, magnitude = 1.70158) {
  194. const scaledTime = t / 1 - 1;
  195. return (scaledTime * scaledTime * ((magnitude + 1) * scaledTime + magnitude) + 1);
  196. }
  197. Timing.easeOutBack = easeOutBack;
  198. // Slow movement backwards, fast snap to past finish, slow resolve to finish
  199. function easeInOutBack(t, magnitude = 1.70158) {
  200. const scaledTime = t * 2;
  201. const scaledTime2 = scaledTime - 2;
  202. const s = magnitude * 1.525;
  203. if (scaledTime < 1) {
  204. return 0.5 * scaledTime * scaledTime * ((s + 1) * scaledTime - s);
  205. }
  206. return 0.5 * (scaledTime2 * scaledTime2 * ((s + 1) * scaledTime2 + s) + 2);
  207. }
  208. Timing.easeInOutBack = easeInOutBack;
  209. // Bounces slowly then quickly to finish
  210. function easeInElastic(t, magnitude = 0.7) {
  211. if (t === 0 || t === 1) {
  212. return t;
  213. }
  214. const scaledTime = t / 1;
  215. const scaledTime1 = scaledTime - 1;
  216. const p = 1 - magnitude;
  217. const s = (p / (2 * Math.PI)) * Math.asin(1);
  218. return -(Math.pow(2, 10 * scaledTime1) * // eslint-disable-line
  219. Math.sin(((scaledTime1 - s) * (2 * Math.PI)) / p));
  220. }
  221. Timing.easeInElastic = easeInElastic;
  222. // Fast acceleration, bounces to zero
  223. function easeOutElastic(t, magnitude = 0.7) {
  224. const p = 1 - magnitude;
  225. const scaledTime = t * 2;
  226. if (t === 0 || t === 1) {
  227. return t;
  228. }
  229. const s = (p / (2 * Math.PI)) * Math.asin(1);
  230. return (Math.pow(2, -10 * scaledTime) * // eslint-disable-line
  231. Math.sin(((scaledTime - s) * (2 * Math.PI)) / p) +
  232. 1);
  233. }
  234. Timing.easeOutElastic = easeOutElastic;
  235. // Slow start and end, two bounces sandwich a fast motion
  236. function easeInOutElastic(t, magnitude = 0.65) {
  237. const p = 1 - magnitude;
  238. if (t === 0 || t === 1) {
  239. return t;
  240. }
  241. const scaledTime = t * 2;
  242. const scaledTime1 = scaledTime - 1;
  243. const s = (p / (2 * Math.PI)) * Math.asin(1);
  244. if (scaledTime < 1) {
  245. return (-0.5 *
  246. (Math.pow(2, 10 * scaledTime1) * // eslint-disable-line
  247. Math.sin(((scaledTime1 - s) * (2 * Math.PI)) / p)));
  248. }
  249. return (Math.pow(2, -10 * scaledTime1) * // eslint-disable-line
  250. Math.sin(((scaledTime1 - s) * (2 * Math.PI)) / p) *
  251. 0.5 +
  252. 1);
  253. }
  254. Timing.easeInOutElastic = easeInOutElastic;
  255. // Bounce to completion
  256. function easeOutBounce(t) {
  257. const scaledTime = t / 1;
  258. if (scaledTime < 1 / 2.75) {
  259. return 7.5625 * scaledTime * scaledTime;
  260. }
  261. if (scaledTime < 2 / 2.75) {
  262. const scaledTime2 = scaledTime - 1.5 / 2.75;
  263. return 7.5625 * scaledTime2 * scaledTime2 + 0.75;
  264. }
  265. if (scaledTime < 2.5 / 2.75) {
  266. const scaledTime2 = scaledTime - 2.25 / 2.75;
  267. return 7.5625 * scaledTime2 * scaledTime2 + 0.9375;
  268. }
  269. {
  270. const scaledTime2 = scaledTime - 2.625 / 2.75;
  271. return 7.5625 * scaledTime2 * scaledTime2 + 0.984375;
  272. }
  273. }
  274. Timing.easeOutBounce = easeOutBounce;
  275. // Bounce increasing in velocity until completion
  276. function easeInBounce(t) {
  277. return 1 - easeOutBounce(1 - t);
  278. }
  279. Timing.easeInBounce = easeInBounce;
  280. // Bounce in and bounce out
  281. function easeInOutBounce(t) {
  282. if (t < 0.5) {
  283. return easeInBounce(t * 2) * 0.5;
  284. }
  285. return easeOutBounce(t * 2 - 1) * 0.5 + 0.5;
  286. }
  287. Timing.easeInOutBounce = easeInOutBounce;
  288. })(Timing = exports.Timing || (exports.Timing = {}));
  289. //# sourceMappingURL=timing.js.map