easing.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var easingFuncs = {
  2. linear: function (k) {
  3. return k;
  4. },
  5. quadraticIn: function (k) {
  6. return k * k;
  7. },
  8. quadraticOut: function (k) {
  9. return k * (2 - k);
  10. },
  11. quadraticInOut: function (k) {
  12. if ((k *= 2) < 1) {
  13. return 0.5 * k * k;
  14. }
  15. return -0.5 * (--k * (k - 2) - 1);
  16. },
  17. cubicIn: function (k) {
  18. return k * k * k;
  19. },
  20. cubicOut: function (k) {
  21. return --k * k * k + 1;
  22. },
  23. cubicInOut: function (k) {
  24. if ((k *= 2) < 1) {
  25. return 0.5 * k * k * k;
  26. }
  27. return 0.5 * ((k -= 2) * k * k + 2);
  28. },
  29. quarticIn: function (k) {
  30. return k * k * k * k;
  31. },
  32. quarticOut: function (k) {
  33. return 1 - (--k * k * k * k);
  34. },
  35. quarticInOut: function (k) {
  36. if ((k *= 2) < 1) {
  37. return 0.5 * k * k * k * k;
  38. }
  39. return -0.5 * ((k -= 2) * k * k * k - 2);
  40. },
  41. quinticIn: function (k) {
  42. return k * k * k * k * k;
  43. },
  44. quinticOut: function (k) {
  45. return --k * k * k * k * k + 1;
  46. },
  47. quinticInOut: function (k) {
  48. if ((k *= 2) < 1) {
  49. return 0.5 * k * k * k * k * k;
  50. }
  51. return 0.5 * ((k -= 2) * k * k * k * k + 2);
  52. },
  53. sinusoidalIn: function (k) {
  54. return 1 - Math.cos(k * Math.PI / 2);
  55. },
  56. sinusoidalOut: function (k) {
  57. return Math.sin(k * Math.PI / 2);
  58. },
  59. sinusoidalInOut: function (k) {
  60. return 0.5 * (1 - Math.cos(Math.PI * k));
  61. },
  62. exponentialIn: function (k) {
  63. return k === 0 ? 0 : Math.pow(1024, k - 1);
  64. },
  65. exponentialOut: function (k) {
  66. return k === 1 ? 1 : 1 - Math.pow(2, -10 * k);
  67. },
  68. exponentialInOut: function (k) {
  69. if (k === 0) {
  70. return 0;
  71. }
  72. if (k === 1) {
  73. return 1;
  74. }
  75. if ((k *= 2) < 1) {
  76. return 0.5 * Math.pow(1024, k - 1);
  77. }
  78. return 0.5 * (-Math.pow(2, -10 * (k - 1)) + 2);
  79. },
  80. circularIn: function (k) {
  81. return 1 - Math.sqrt(1 - k * k);
  82. },
  83. circularOut: function (k) {
  84. return Math.sqrt(1 - (--k * k));
  85. },
  86. circularInOut: function (k) {
  87. if ((k *= 2) < 1) {
  88. return -0.5 * (Math.sqrt(1 - k * k) - 1);
  89. }
  90. return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
  91. },
  92. elasticIn: function (k) {
  93. var s;
  94. var a = 0.1;
  95. var p = 0.4;
  96. if (k === 0) {
  97. return 0;
  98. }
  99. if (k === 1) {
  100. return 1;
  101. }
  102. if (!a || a < 1) {
  103. a = 1;
  104. s = p / 4;
  105. }
  106. else {
  107. s = p * Math.asin(1 / a) / (2 * Math.PI);
  108. }
  109. return -(a * Math.pow(2, 10 * (k -= 1))
  110. * Math.sin((k - s) * (2 * Math.PI) / p));
  111. },
  112. elasticOut: function (k) {
  113. var s;
  114. var a = 0.1;
  115. var p = 0.4;
  116. if (k === 0) {
  117. return 0;
  118. }
  119. if (k === 1) {
  120. return 1;
  121. }
  122. if (!a || a < 1) {
  123. a = 1;
  124. s = p / 4;
  125. }
  126. else {
  127. s = p * Math.asin(1 / a) / (2 * Math.PI);
  128. }
  129. return (a * Math.pow(2, -10 * k)
  130. * Math.sin((k - s) * (2 * Math.PI) / p) + 1);
  131. },
  132. elasticInOut: function (k) {
  133. var s;
  134. var a = 0.1;
  135. var p = 0.4;
  136. if (k === 0) {
  137. return 0;
  138. }
  139. if (k === 1) {
  140. return 1;
  141. }
  142. if (!a || a < 1) {
  143. a = 1;
  144. s = p / 4;
  145. }
  146. else {
  147. s = p * Math.asin(1 / a) / (2 * Math.PI);
  148. }
  149. if ((k *= 2) < 1) {
  150. return -0.5 * (a * Math.pow(2, 10 * (k -= 1))
  151. * Math.sin((k - s) * (2 * Math.PI) / p));
  152. }
  153. return a * Math.pow(2, -10 * (k -= 1))
  154. * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
  155. },
  156. backIn: function (k) {
  157. var s = 1.70158;
  158. return k * k * ((s + 1) * k - s);
  159. },
  160. backOut: function (k) {
  161. var s = 1.70158;
  162. return --k * k * ((s + 1) * k + s) + 1;
  163. },
  164. backInOut: function (k) {
  165. var s = 1.70158 * 1.525;
  166. if ((k *= 2) < 1) {
  167. return 0.5 * (k * k * ((s + 1) * k - s));
  168. }
  169. return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
  170. },
  171. bounceIn: function (k) {
  172. return 1 - easingFuncs.bounceOut(1 - k);
  173. },
  174. bounceOut: function (k) {
  175. if (k < (1 / 2.75)) {
  176. return 7.5625 * k * k;
  177. }
  178. else if (k < (2 / 2.75)) {
  179. return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
  180. }
  181. else if (k < (2.5 / 2.75)) {
  182. return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
  183. }
  184. else {
  185. return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
  186. }
  187. },
  188. bounceInOut: function (k) {
  189. if (k < 0.5) {
  190. return easingFuncs.bounceIn(k * 2) * 0.5;
  191. }
  192. return easingFuncs.bounceOut(k * 2 - 1) * 0.5 + 0.5;
  193. }
  194. };
  195. export default easingFuncs;