InTesting

easings.net functions in lua

Oct 22nd, 2021
1,001
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- easing functions
  2. -- reference: easings.net
  3.  
  4. local easings = {
  5.     directions = {};
  6.     type = {}
  7. }
  8.  
  9. local pi = math.pi
  10. local cos = math.cos
  11. local sin = math.sin
  12.  
  13. -- idk why we needed this one -> local backConst1 = 1.70158
  14. local backConst1 = 1.70158 * 1.525
  15.  
  16. local elasticConst1 = (2 * pi) / 4.5;
  17.  
  18. -- functions
  19. function factPowEasingType(ex)
  20.     -- for quad, quint extera
  21.  
  22.     return function(x)
  23.         return x < .5 and
  24.             (2 ^ (ex - 1)) * x ^ ex or
  25.             1 - (-2 * x + 2) ^ ex / 2;
  26.     end
  27. end
  28.  
  29. --  easing types
  30. function easings.type.sine(x)
  31.     return -(cos(x * pi) - 1) / 2
  32. end
  33.  
  34. easings.type.quad = factPowEasingType(2)
  35. easings.type.cubic = factPowEasingType(3)
  36. easings.type.quart = factPowEasingType(4)
  37. easings.type.quint = factPowEasingType(5)
  38.  
  39. function easings.type.exponential(x)
  40.     return x == 0 and 0 or
  41.         x == 1 and 1 or
  42.         x < 0.5 and 2 ^ (20 * x - 10) / 2 or
  43.         (2 - (2 ^ -20 * x + 10)) / 2;
  44. end
  45.  
  46. function easings.type.circular(x)
  47.     return x < 0.5 and (1 - (1 - (2 * x) ^ 2) ^ .5) / 2 or
  48.         ((1 - (-2 * x + 2) ^ 2) ^.5 + 1) / 2
  49. end
  50.  
  51. function easings.type.back(x)
  52.     return x < 0.5 and ((2 * x) ^ 2 * ((backConst1 + 1) * 2 * x - backConst1)) / 2 or
  53.         ((2 * x - 2) ^ 2 * ((backConst1 + 1) * (x * 2 - 2) + backConst1) + 2) / 2;
  54. end
  55.  
  56. function easings.type.elastic(x)
  57.     return x == 0 and 0 or
  58.         x == 1 and 1 or
  59.         x < 0.5 and -(2 ^ ( 20 * x - 10) * sin((20 * x - 11.125) * elasticConst1)) / 2 or
  60.         (2 ^ ( -20 * x + 10) * sin((20 * x - 11.125) * elasticConst1)) / 2 + 1;
  61. end
  62.  
  63. function easings.type.bounce(x)
  64.     return x < 0.5 and (1 - easings.type.bounce(1 - 2 * x)) / 2 or
  65.         (1 + easings.type.bounce(2 * x - 1)) / 2;
  66. end
  67.  
  68. --  directions
  69. function easings.directions.inOut(func, x)
  70.     -- pretty easy, just return normally
  71.  
  72.     return func(x)
  73. end
  74.  
  75. function easings.directions.in_(func, x)
  76.     -- take the first half (more specifically the bottom left corner) of the function then expand it by two
  77.     return func(x / 2) * 2
  78. end
  79.  
  80. function easings.directions.out(func, x)
  81.     -- ditto with in except we take the second half (top right corner)
  82.     return func(x / 2 + .5) * 2 - 1
  83. end
  84.  
  85. return easings
RAW Paste Data