Advertisement
Iavra

[Ace] Animate Everything - Library

Jun 3rd, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.58 KB | None | 0 0
  1. #==============================================================================
  2. # Iavra Animate Everything - Easing Library
  3. # -----------------------------------------------------------------------------
  4. # Description:
  5. # Implementation of the Robert Penner easing functions. I decided to split this
  6. # from the actual animation logic, since it's just a lot of stuff that clutters
  7. # the script. Also, you can delete some of those methods to free up memory or
  8. # provide your own implementations.
  9. # -----------------------------------------------------------------------------
  10. # How to Use:
  11. # Simply put this script anywhere, either above or below Animate Everything.
  12. # The method names directly translate to symbols, that can be passed to the
  13. # IAVRA::ANIMATE.start method.
  14. # -----------------------------------------------------------------------------
  15. # Also see:
  16. # http://robertpenner.com/easing/
  17. #==============================================================================
  18.  
  19. module IAVRA
  20.     module ANIMATE
  21.         module EASING
  22.            
  23.             #========================================================================
  24.             # Quad
  25.             #========================================================================
  26.            
  27.             def self.quad_in(b, c, t, d)
  28.                 c*(t/=d)*t + b
  29.             end
  30.              
  31.             def self.quad_out(b, c, t, d)
  32.                 -c*(t/=d)*(t-2) + b
  33.             end
  34.            
  35.             def self.quad_in_out(b, c, t, d)
  36.                 return c/2*t*t + b if ((t/=d/2) < 1)
  37.                 return -c/2 * ((t-=1)*(t-2) - 1) + b
  38.             end
  39.            
  40.             #========================================================================
  41.             # Cubic
  42.             #========================================================================
  43.            
  44.             def self.cubic_in(b, c, t, d)
  45.                 c*(t/=d)*t*t + b
  46.             end
  47.              
  48.             def self.cubic_ou(b, c, t, d)
  49.                 c*((t=t/d-1)*t*t + 1) + b
  50.             end
  51.            
  52.             def self.cubic_in_out(b, c, t, d)
  53.                 return c/2*t*t*t + b if ((t/=d/2) < 1)
  54.                 return c/2*((t-=2)*t*t + 2) + b
  55.             end
  56.            
  57.             #========================================================================
  58.             # Quartic
  59.             #========================================================================
  60.            
  61.             def self.quart_in(b, c, t, d)
  62.                 c*(t/=d)*t*t*t + b
  63.             end
  64.              
  65.             def self.quart_out(b, c, t, d)
  66.                 -c * ((t=t/d-1)*t*t*t - 1) + b
  67.             end
  68.            
  69.             def self.quart_in_out(b, c, t, d)
  70.                 return c/2*t*t*t*t + b if ((t/=d/2) < 1)
  71.                 return -c/2 * ((t-=2)*t*t*t - 2) + b
  72.             end
  73.            
  74.             #========================================================================
  75.             # Quintic
  76.             #========================================================================
  77.            
  78.             def self.quint_in(b, c, t, d)
  79.                 c*(t/=d)*t*t*t*t + b
  80.             end
  81.            
  82.             def self.quint_out(b, c, t, d)
  83.                 c*((t=t/d-1)*t*t*t*t + 1) + b
  84.             end
  85.            
  86.             def self.quint_in_out(b, c, t, d)
  87.                 return c/2*t*t*t*t*t + b if ((t/=d/2) < 1)
  88.                 return c/2*((t-=2)*t*t*t*t + 2) + b
  89.             end
  90.            
  91.             #========================================================================
  92.             # Sine
  93.             #========================================================================
  94.            
  95.             def self.sine_in(b, c, t, d)
  96.                 -c * Math.cos(t/d * (Math::PI/2)) + c + b
  97.             end
  98.              
  99.             def self.sine_out(b, c, t, d)
  100.                 c * Math.sin(t/d * (Math::PI/2)) + b
  101.             end
  102.            
  103.             def self.sine_in_out(b, c, t, d)
  104.                 -c/2 * (Math.cos(Math::PI*t/d) - 1) + b
  105.             end
  106.            
  107.             #========================================================================
  108.             # Exponential
  109.             #========================================================================
  110.            
  111.             def self.exp_in(b, c, t, d)
  112.                 (t==0) ? b : c * (2 ** (10 * (t/d - 1))) + b
  113.             end
  114.              
  115.             def self.exp_out(b, c, t, d)
  116.                 (t==d) ? b+c : c * (-2**(-10 * t/d) + 1) + b
  117.             end
  118.              
  119.             def self.exp_in_out(b, c, t, d)
  120.                 return b if t == 0
  121.                 return b + c if t == d
  122.                 return (c/2) * 2**(10 * (t-1)) + b if ((t /= d/2) < 1)
  123.                 return (c/2) * (-2**(-10 * t-=1) + 2) + b
  124.             end
  125.            
  126.             #========================================================================
  127.             # Circular
  128.             #========================================================================
  129.            
  130.             def self.circ_in(b, c, t, d)
  131.                 -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b
  132.             end
  133.            
  134.             def self.circ_out(b, c, t, d)
  135.                 c * Math.sqrt(1 - (t=t/d-1)*t) + b
  136.             end
  137.              
  138.             def self.circ_in_out(b, c, t, d)
  139.                 return -c/2 * (Math.sqrt(1 - t*t) - 1) + b if ((t/=d/2) < 1)
  140.                 return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b
  141.             end
  142.            
  143.             #========================================================================
  144.             # Bounce
  145.             #========================================================================
  146.  
  147.             def self.bounce_out(b, c, t, d)
  148.                 if (t /= d) < (1/2.75)
  149.                     c * 7.5625*t*t + b
  150.                 elsif t < (2/2.75)
  151.                     c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b
  152.                 elsif t < (2.5/2.75)
  153.                     c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b
  154.                 else
  155.                     c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b
  156.                 end
  157.             end
  158.            
  159.             def self.bounce_in(b, c, t, d)
  160.                 c - bounce_out(0, c, d-t, d) + b
  161.             end
  162.            
  163.             def self.bounce_in_out(b, c, t, d)
  164.                 if(t < d/2)
  165.                     bounce_in(0, c, t*2, d) * 0.5 + b
  166.                 else
  167.                     bounce_out(0, c, t*2 - d, d) * 0.5 + c*0.5 + b
  168.                 end
  169.             end
  170.            
  171.             #========================================================================
  172.             # Back
  173.             #========================================================================
  174.            
  175.             def self.back_in(b, c, t, d)
  176.                 s = 1.70158
  177.                 c * (t/=d) * t * ((s+1) * t - s) + b
  178.             end
  179.            
  180.             def self.back_out(b, c, t, d)
  181.                 s = 1.70158
  182.                 c * ((t=t/d-1) * t * ((s+1) * t + s) + 1) + b
  183.             end
  184.            
  185.             def self.back_in_out(b, c, t, d)
  186.                 s = 1.70158
  187.                 if (t /= d/2) < 1
  188.                     c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b
  189.                 else
  190.                     c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b
  191.                 end
  192.             end
  193.            
  194.             #========================================================================
  195.             # Elastic
  196.             #========================================================================
  197.            
  198.             def self.elastic_in(b, c, t, d)
  199.                 a, x, s = 5.0, d * 0.3, 0.0
  200.                 return b if (t == 0)
  201.                 return b + c if ((t /= d) >= 1)
  202.                 if(a < c.abs)
  203.                     a = c
  204.                     s = x / 4.0
  205.                 else
  206.                     s = x / (2 * Math::PI) * Math.asin(c / a)
  207.                 end
  208.                 -(a * (2 ** (10 * (t -= 1))) * Math.sin((t * d - s) * (2 * Math::PI) / x)) + b
  209.             end
  210.            
  211.             def self.elastic_out(b, c, t, d)
  212.                 a, x, s = 0.1, d * 0.3, 0.0
  213.                 return b if (t == 0)
  214.                 return b + c if ((t /= d) >= 1)
  215.                 if(a < c.abs)
  216.                     a = c
  217.                     s = x / 4.0
  218.                 else
  219.                     s = x / (2 * Math::PI) * Math.asin(c / a)
  220.                 end
  221.                 a * (2 ** (-10 * t)) * Math.sin((t * d - s) * (2 * Math::PI) / x) + c + b
  222.             end
  223.            
  224.         end
  225.     end
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement