Fabric.js easeInOutQuad() 方法
在游戏或动画中,有许多移动物体可以直线地从点A移动到点B,但是应用缓动函数后,可以使其看起来更加自然。缓动函数描述了动画的进展方式。这样,直线运动可以采用有趣的形状呈现。
缓动函数 指定参数随时间的变化速率。它的方程可以使某个物体在起始时慢慢移动,然后加速,或者在接近结束时减速。最常见的一组缓动方程来源于Robert Penner的书和网页。
easeInOutQuad() 方法 用于实现二次缓动效果。可以应用在某个对象上,使其在缓动中加速和减速。
语法:
easeInOutQuad(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 easeInOutQuad() function
function easeInOutQuad (t, b, c, d) {
if ((t /= d / 2) < 1)
return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t - 2) - 1) + b;
}
// Calling the easeInOutQuad() function over
// the specified parameter values
console.log(
fabric.util.ease.easeInOutQuad(1, 2, 3, 4)
);
</script>
</body>
</html>
输出:
2.375
示例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 easeInOutQuad() function over
// the specified parameter values
console.log(
fabric.util.ease.easeInOutQuad(t, b, c, d)
);
</script>
</body>
</html>
输出:
23.888888888888893