Fabric.js easeInOutCirc() 方法
在游戏或动画中,有许多移动的对象可以在线性地从点A移动到点B,但在应用缓动或缓动函数后,它可以使运动看起来更自然。缓动函数告诉动画如何进展。这样,直线运动可以采取有趣的形状。
缓动函数 指定了一个参数随时间变化的速率。它的方程使某些东西在开始时慢慢移动,然后加速,或者在结束时减速。最常见的一组缓动方程式来自Robert Penner的书和网页。
easeInOutCirc() 方法 用于在所用对象上实现圆形缓动效果的淡入淡出效果。
语法:
easeInOutCirc(t, b, c, d)
参数: 此方法接受上述提到的四个参数,分别如下所述:
- t: 此参数保存动画开始的指定时间。例如,如果t的值为0,表示动画刚刚开始。
- b: 此参数保存对象在x轴上的指定起始位置。例如,如果b的值为10,表示对象在x坐标上的起始位置为10。
- c: 此参数保存对象的指定值变化量。例如,如果c的值为30,表示对象必须向右移动30,最终停在40。
- d: 此参数保存整个过程的指定持续时间。例如,如果d的值为2,表示对象有2秒的时间从10移动到40执行此动作。
返回值: 此方法返回对象的缓动位置,即在特定时间的对象位置。
示例1:
<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">
// The easeInOutCirc() function
function easeInOutCirc (t, b, c, d) {
if ((t /= d / 2) < 1)
return -c / 2 *
(Math.sqrt(1 - t * t) - 1) + b;
return c / 2 *
(Math.sqrt(1 - (t -= 2) * t) + 1) + b;
}
// Calling the easeInOutCirc() function
// over the specified parameter values
console.log(
fabric.util.ease.easeInOutCirc(1, 2, 3, 4)
);
</script>
</body>
</html>
输出:
2.200961894323342
示例2:
<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 the parameters with its values
var t = 5;
var b = 10;
var c = 40;
var d = 12;
// Calling the easeInOutCirc() function
// over the specified parameter values
console.log(
fabric.util.ease.easeInOutCirc(t, b, c, d)
);
</script>
</body>
</html>
输出:
18.94458403214867
极客教程