Fabric.js easeOutCirc() 方法
在游戏或动画中,有许多移动的物体可以以线性方式将它们从点A移动到点B,但是应用缓动或缓动函数后,可以使其看起来更加自然。缓动函数描述了动画的进展方式。通过这种方式,直线运动可以呈现出有趣的形状。
缓动函数 指定了参数随时间变化的速率。缓动函数的方程使得某物在开始时慢慢移动,然后加速或者在结束时放慢速度。最常见的一组缓动方程来自罗伯特·彭纳(Robert Penner)的书和网页。
easeOutCirc() 方法 用于圆形缓动效果。
语法:
easeOutCirc(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 easeOutCirc() function
function easeOutCirc (t, b, c, d) {
return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
}
// Calling the easeOutCirc() function over
// the specified parameter values
console.log(fabric.util.ease.easeOutCirc(1, 2, 3, 4));
</script>
</body>
</html>
输出:
3.9843134832984433
示例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 easeOutCirc() function
function easeOutCirc (t, b, c, d) {
return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
}
// Initializing the parameters with its values
var t = 5;
var b = 10;
var c = 40;
var d = 12;
// Calling the easeOutCirc() function over
// the specified parameter values
console.log(fabric.util.ease.easeOutCirc(t, b, c, d));
</script>
</body>
</html>
输出:
42.48931448269655