elastic.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {tpmt} from "./math.js";
  2. var tau = 2 * Math.PI,
  3. amplitude = 1,
  4. period = 0.3;
  5. export var elasticIn = (function custom(a, p) {
  6. var s = Math.asin(1 / (a = Math.max(1, a))) * (p /= tau);
  7. function elasticIn(t) {
  8. return a * tpmt(-(--t)) * Math.sin((s - t) / p);
  9. }
  10. elasticIn.amplitude = function(a) { return custom(a, p * tau); };
  11. elasticIn.period = function(p) { return custom(a, p); };
  12. return elasticIn;
  13. })(amplitude, period);
  14. export var elasticOut = (function custom(a, p) {
  15. var s = Math.asin(1 / (a = Math.max(1, a))) * (p /= tau);
  16. function elasticOut(t) {
  17. return 1 - a * tpmt(t = +t) * Math.sin((t + s) / p);
  18. }
  19. elasticOut.amplitude = function(a) { return custom(a, p * tau); };
  20. elasticOut.period = function(p) { return custom(a, p); };
  21. return elasticOut;
  22. })(amplitude, period);
  23. export var elasticInOut = (function custom(a, p) {
  24. var s = Math.asin(1 / (a = Math.max(1, a))) * (p /= tau);
  25. function elasticInOut(t) {
  26. return ((t = t * 2 - 1) < 0
  27. ? a * tpmt(-t) * Math.sin((s - t) / p)
  28. : 2 - a * tpmt(t) * Math.sin((s + t) / p)) / 2;
  29. }
  30. elasticInOut.amplitude = function(a) { return custom(a, p * tau); };
  31. elasticInOut.period = function(p) { return custom(a, p); };
  32. return elasticInOut;
  33. })(amplitude, period);