Fabric.js easeInOutElastic() 方法
在游戏或动画中,有许多移动的物体可以在点A和点B之间线性移动,但是在应用缓动或缓动函数之后,可以使其看起来更自然。缓动函数描述了动画的进展方式。通过这种方式,直线运动可以变得更有趣。
缓动函数 指定一个参数随时间变化的速率。它的方程使得物体在开始时移动缓慢,然后在结束时加速或减速。最常见的一组缓动方程来自Robert Penner的书籍和网站。
easeInOutElastic() 方法 用于对其应用的对象的缓动效果。
语法:
easeInOutElastic(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 easeInOutElastic() function
function easeInOutElastic (t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d / 2) == 2) return b + c;
if (!p) p = d * (.3 * 1.5);
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
}
else var s = p / (2 * Math.PI) *
Math.asin(c / a);
if (t < 1)
return -.5 * (a *
Math.pow(2, 10 * (t -= 1)) *
Math.sin((t * d - s) *
(2 * Math.PI) / p)) + b;
return a * Math.pow(2, -10 * (t -= 1)) *
Math.sin((t * d - s)
* (2 * Math.PI) / p) *
.5 + c + b;
}
// Calling the easeInOutElastic() function
// over the specified parameter values
console.log(
fabric.util.ease.easeInOutElastic(1, 2, 3, 4)
);
</script>
</body>
</html>
输出:
2.0359083332712022
示例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 easeInOutElastic() function
// over the specified parameter values
console.log(
fabric.util.ease.easeInOutElastic(t, b, c, d)
);
</script>
</body>
</html>
输出:
5.676948575674239
极客教程