Fabric.js easeInOutExpo()方法

Fabric.js easeInOutExpo()方法

在游戏或动画中,有许多移动的物体可以以线性的方式从点A移动到点B,但是如果应用缓动或缓动函数,可以使其看起来更自然。缓动函数描述了动画如何进行。通过这种方式,直线运动可以变成有趣的形状。

缓动函数 指定了参数随时间变化的速率。它的方程使得某物在开始时慢慢移动并加速,或者在接近结束时减速。最常见的一组缓动方程来自Robert Penner的书和网页。

easeInOutExpo()方法 用于指数缓动效果。

语法:

easeInOutExpo(t, b, c, d)

参数: 此方法接受四个参数,如上所述和下面所述:

  • t: 此参数保存动画开始时的指定时间。例如,如果t的值为0,则表示动画刚刚开始。
  • b: 此参数保存对象在x轴上的指定起始位置。例如,如果b的值为10,则表示对象在x坐标上的起始位置为10。
  • c: 此参数保存对象的值变化的指定量。例如,如果c的值为30,则表示对象需要向右移动30个单位,最后停在40。
  • d: 此参数保存整个过程的指定持续时间。例如,如果d的值为2,则表示对象从10到40的运动有2秒钟的时间。

返回值: 此方法返回对象的缓动位置,即指定时间的对象位置。

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
  <!-- Adding the FabricJS library -->
  <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
  </script> 
</head> 
  
<body> 
    <script type="text/javascript"> 
       // Initializing easeInOutExpo() function 
       function easeInOutExpo (t, b, c, d) { 
          if (t == 0) return b; 
          if (t == d) return b + c; 
          if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; 
          return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b; 
       } 
  
       // Calling the easeInOutExpo() function over 
       // the specified parameter values 
       console.log(fabric.util.ease.easeInOutExpo(1, 2, 3, 4));  
    </script> 
</body> 
  
</html>

输出:

2.046875

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
  <!-- Adding the FabricJS library -->
  <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
  </script> 
</head> 
  
<body> 
    <script type="text/javascript"> 
      // Initializing easeInOutExpo() function 
      function easeInOutExpo (t, b, c, d) { 
          if (t == 0) return b; 
          if (t == d) return b + c; 
          if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; 
          return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b; 
      } 
        
      // Initializing the parameters with its values 
      var t = 5; 
      var b = 10; 
      var c = 40; 
      var d = 12; 
  
      // Calling the easeInOutExpo() function over 
      // the specified parameter values 
      console.log(fabric.util.ease.easeInOutExpo(t, b, c, d));  
    </script> 
</body> 
  
</html>

输出:

16.299605249474368

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程