Iavra

Iavra Easing Library

Oct 24th, 2015 (edited)
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * =============================================================================
  3.  * Iavra Easing Library.js
  4.  * =============================================================================
  5.  *
  6.  * @plugindesc JavaScript implementation of Robert Penner's easing functions. All Credits go to him.
  7.  * <Iavra Easing Library>
  8.  * @author Iavra
  9.  *
  10.  * @help
  11.  * Each function takes a single parameter k, which is defined as t/d or "current animation step" /
  12.  * "total animation duration".
  13.  *
  14.  * Penner's original functions take 4 parameters and can be translated to these ones as:
  15.  * easingWith4Parameters(b, c, t, d) === b + c * easingWith1Parameter(t/d);
  16.  *
  17.  * This library contains the following functions (each with a sample link showcasing the animation):
  18.  *
  19.  * quad
  20.  *     in:    http://easings.net/en#easeInQuad
  21.  *     out:   http://easings.net/en#easeOutQuad
  22.  *     inOut: http://easings.net/en#easeInOutQuad
  23.  * cubic
  24.  *     in:    http://easings.net/en#easeInCubic
  25.  *     out:   http://easings.net/en#easeOutCubic
  26.  *     inOut: http://easings.net/en#easeInOutCubic
  27.  * quart
  28.  *     in:    http://easings.net/en#easeInQuart
  29.  *     out:   http://easings.net/en#easeOutQuart
  30.  *     inOut: http://easings.net/en#easeInOutQuart
  31.  * quint
  32.  *     in:    http://easings.net/en#easeInQuint
  33.  *     out:   http://easings.net/en#easeOutQuint
  34.  *     inOut: http://easings.net/en#easeInOutQuint
  35.  * sine
  36.  *     in:    http://easings.net/en#easeInSine
  37.  *     out:   http://easings.net/en#easeOutSine
  38.  *     inOut: http://easings.net/en#easeInOutSine
  39.  * exp
  40.  *     in:    http://easings.net/en#easeInExpo
  41.  *     out:   http://easings.net/en#easeOutExpo
  42.  *     inOut: http://easings.net/en#easeInOutExpo
  43.  * circ
  44.  *     in:    http://easings.net/en#easeInCirc
  45.  *     out:   http://easings.net/en#easeOutCirc
  46.  *     inOut: http://easings.net/en#easeInOutCirc
  47.  * elastic
  48.  *     in:    http://easings.net/en#easeInElastic
  49.  *     out:   http://easings.net/en#easeOutElastic
  50.  *     inOut: http://easings.net/en#easeInOutElastic
  51.  * back
  52.  *     in:    http://easings.net/en#easeInBack
  53.  *     out:   http://easings.net/en#easeOutBack
  54.  *     inOut: http://easings.net/en#easeInOutBack
  55.  * bounce
  56.  *     in:    http://easings.net/en#easeInBounce
  57.  *     out:   http://easings.net/en#easeOutBounce
  58.  *     inOut: http://easings.net/en#easeInOutBounce
  59.  */
  60.  
  61. var Imported = Imported || {};
  62. Imported["iavra_easing"] = true;
  63.  
  64. //=============================================================================
  65. // namespace IAVRA
  66. //=============================================================================
  67.  
  68. var IAVRA = IAVRA || {};
  69.  
  70. //=============================================================================
  71. // module IAVRA.EASING
  72. //=============================================================================
  73.  
  74. IAVRA.EASING = (function($) {
  75.    
  76.     /**
  77.      * Quadratic
  78.      */
  79.     $.quad = {
  80.         in: function(k) {
  81.             return k * k;
  82.         },
  83.         out: function(k) {
  84.             return k * (2 - k);
  85.         },
  86.         inOut: function(k) {
  87.             if ((k *= 2) < 1) {
  88.                 return 0.5 * k * k;
  89.             }
  90.             return - 0.5 * (--k * (k - 2) - 1);
  91.         }
  92.     };
  93.    
  94.     /**
  95.      * Cubic
  96.      */
  97.     $.cubic = {
  98.         in: function(k) {
  99.             return k * k * k;
  100.         },
  101.         out: function(k) {
  102.             return --k * k * k + 1;
  103.         },
  104.         inOut: function(k) {
  105.             if ((k *= 2) < 1) {
  106.                 return 0.5 * k * k * k;
  107.             }
  108.             return 0.5 * ((k -= 2) * k * k + 2);
  109.         }
  110.     };
  111.    
  112.     /**
  113.      * Quartic
  114.      */
  115.     $.quart = {
  116.         in: function(k) {
  117.             return k * k * k * k;
  118.         },
  119.         out: function(k) {
  120.             return 1 - (--k * k * k * k);
  121.         },
  122.         inOut: function(k) {
  123.             if ((k *= 2) < 1) {
  124.                 return 0.5 * k * k * k * k;
  125.             }
  126.             return - 0.5 * ((k -= 2) * k * k * k - 2);
  127.         }
  128.     };
  129.    
  130.     /**
  131.      * Quintic
  132.      */
  133.     $.quint = {
  134.         in: function(k) {
  135.             return k * k * k * k * k;
  136.         },
  137.         out: function(k) {
  138.             return --k * k * k * k * k + 1;
  139.         },
  140.         inOut: function(k) {
  141.             if ((k *= 2) < 1) {
  142.                 return 0.5 * k * k * k * k * k;
  143.             }
  144.             return 0.5 * ((k -= 2) * k * k * k * k + 2);
  145.         }
  146.     };
  147.    
  148.     /**
  149.      * Sinusoidal
  150.      */
  151.     $.sine = {
  152.         in: function(k) {
  153.             return 1 - Math.cos(k * Math.PI / 2);
  154.         },
  155.         out: function(k) {
  156.             return Math.sin(k * Math.PI / 2);
  157.         },
  158.         inOut: function(k) {
  159.             return 0.5 * (1 - Math.cos(Math.PI * k));
  160.         }
  161.     };
  162.    
  163.     /**
  164.      * Exponential
  165.      */
  166.     $.exp = {
  167.         in: function(k) {
  168.             return k === 0.0 ? 0 : Math.pow(1024, k - 1);
  169.         },
  170.         out: function(k) {
  171.             return k === 1.0 ? 1 : 1 - Math.pow(2, - 10 * k);
  172.         },
  173.         inOut: function(k) {
  174.             if (k === 0.0) {
  175.                 return 0;
  176.             }
  177.             if (k === 1.0) {
  178.                 return 1;
  179.             }
  180.             if ((k *= 2) < 1) {
  181.                 return 0.5 * Math.pow(1024, k - 1);
  182.             }
  183.             return 0.5 * (- Math.pow(2, - 10 * (k - 1)) + 2);
  184.         }
  185.     };
  186.    
  187.     /**
  188.      * Circular
  189.      */
  190.     $.circ = {
  191.         in: function(k) {
  192.             return 1 - Math.sqrt(1 - k * k);
  193.         },
  194.         out: function(k) {
  195.             return Math.sqrt(1 - (--k * k));
  196.         },
  197.         inOut: function(k) {
  198.             if ((k *= 2) < 1) {
  199.                 return - 0.5 * (Math.sqrt(1 - k * k) - 1);
  200.             }
  201.             return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
  202.         }
  203.     };
  204.    
  205.     /**
  206.      * Elastic
  207.      */
  208.     $.elastic = {
  209.         in: function(k) {
  210.             var s;
  211.             var a = 0.1;
  212.             var p = 0.4;
  213.  
  214.             if (k === 0.0) {
  215.                 return 0;
  216.             }
  217.             if (k === 1.0) {
  218.                 return 1;
  219.             }
  220.             if (!a || a < 1) {
  221.                 a = 1;
  222.                 s = p / 4;
  223.             } else {
  224.                 s = p * Math.asin(1 / a) / (2 * Math.PI);
  225.             }
  226.             return - (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
  227.         },
  228.         out: function(k) {
  229.             var s;
  230.             var a = 0.1;
  231.             var p = 0.4;
  232.  
  233.             if (k === 0.0) {
  234.                 return 0;
  235.             }
  236.             if (k === 1.0) {
  237.                 return 1;
  238.             }
  239.             if (!a || a < 1) {
  240.                 a = 1;
  241.                 s = p / 4;
  242.             } else {
  243.                 s = p * Math.asin(1 / a) / (2 * Math.PI);
  244.             }
  245.             return (a * Math.pow(2, - 10 * k) * Math.sin((k - s) * (2 * Math.PI) / p) + 1);
  246.         },
  247.         inOut: function(k) {
  248.             var s;
  249.             var a = 0.1;
  250.             var p = 0.4;
  251.  
  252.             if (k === 0.0) {
  253.                 return 0;
  254.             }
  255.             if (k === 1.0) {
  256.                 return 1;
  257.             }
  258.             if (!a || a < 1) {
  259.                 a = 1;
  260.                 s = p / 4;
  261.             } else {
  262.                 s = p * Math.asin(1 / a) / (2 * Math.PI);
  263.             }
  264.             if ((k *= 2) < 1) {
  265.                 return - 0.5 * (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
  266.             }
  267.             return a * Math.pow(2, -10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
  268.         }
  269.     };
  270.    
  271.     /**
  272.      * Back
  273.      */
  274.     $.back = {
  275.         in: function(k) {
  276.             var s = 1.70158;
  277.             return k * k * ((s + 1) * k - s);
  278.         },
  279.         out: function(k) {
  280.             var s = 1.70158;
  281.             return --k * k * ((s + 1) * k + s) + 1;
  282.         },
  283.         inOut: function(k) {
  284.             var s = 1.70158 * 1.525;
  285.             if ((k *= 2) < 1) {
  286.                 return 0.5 * (k * k * ((s + 1) * k - s));
  287.             }
  288.             return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
  289.         }
  290.     };
  291.    
  292.     /**
  293.      * Bounce
  294.      */
  295.     $.bounce = {
  296.         in: function(k) {
  297.             return 1 - IAVRA.EASING.bounce.out(1 - k);
  298.         },
  299.         out: function(k) {
  300.             if (k < (1 / 2.75)) {
  301.                 return 7.5625 * k * k;
  302.             } else if (k < (2 / 2.75)) {
  303.                 return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
  304.             } else if (k < (2.5 / 2.75)) {
  305.                 return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
  306.             } else {
  307.                 return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
  308.             }
  309.         },
  310.         inOut: function(k) {
  311.             if (k < 0.5) {
  312.                 return IAVRA.EASING.bounce.in(k * 2) * 0.5;
  313.             }
  314.             return IAVRA.EASING.bounce.out(k * 2 - 1) * 0.5 + 0.5;
  315.         }
  316.     };
  317.    
  318.     return $;
  319. })(IAVRA.EASING || {});
Add Comment
Please, Sign In to add comment