Open In App

Fabric.js easeInOutElastic() Method

In games or animations, there are many moving objects which can move them from point A to B linearly, but after applying an easing, or easing function, it can make it look more natural. An easing function says an animation of how to progress. In this way, a straight motion can take an interesting shape.

Easing functions specify the rate of change of a parameter over time. It is whose equations which make something move slowly at the start and speed up, or slow down near the end. The most common set of easing equations come from Robert Penner’s book and webpage.



The easeInOutElastic() method is used for the effect of easing in and out the object it is used on.

Syntax:



easeInOutElastic(t, b, c, d)

Parameters: This method accepts four parameters as mentioned above and described below:

Return Value: This method returns the eased position of the object i.e., the position of the object at a specific time.

Example 1:




<html>
<head>
  <!-- Adding the FabricJS library -->
  <script src=
  </script>
</head>
<body>
<script type="text/javascript">
  
   // The easeInOutElastic() function
   function easeInOutElastic (t, b, c, d) {
      var s = 1.70158;
      var p = 0;
      var a = c;
      if (t == 0) return b;
      if ((t /= d / 2) == 2) return b + c;
      if (!p) p = d * (.3 * 1.5);
      if (a < Math.abs(c)) {
          a = c;
          var s = p / 4;
      }
      else var s = p / (2 * Math.PI) *
          Math.asin(c / a);
      if (t < 1) 
        return -.5 * (a *
             Math.pow(2, 10 * (t -= 1)) *
             Math.sin((t * d - s) *
                 (2 * Math.PI) / p)) + b;
      return a * Math.pow(2, -10 * (t -= 1)) *
        Math.sin((t * d - s)
          * (2 * Math.PI) / p) *
        .5 + c + b;
  }
  
   // Calling the easeInOutElastic() function
   // over the specified parameter values
   console.log(
     fabric.util.ease.easeInOutElastic(1, 2, 3, 4)
   ); 
</script>
</body>
</html>

Output:

2.0359083332712022

Example 2:




<html>
<head>
  <!-- Adding the FabricJS library -->
  <script src=
  </script>
</head>
<body>
<script type="text/javascript">
   
   // Initializing the parameters
   // with its values
   var t = 5;
   var b = 10;
   var c = 40;
   var d = 12;
  
   // Calling the easeInOutElastic() function
   // over the specified parameter values
   console.log(
     fabric.util.ease.easeInOutElastic(t, b, c, d)
   ); 
</script>
</body>
</html>

Output:

5.676948575674239

Article Tags :