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