Fabric.js easeInElastic() 方法
在游戏或动画中,有许多移动的物体可以线性地从点A移动到点B,但是应用缓动函数后,可以使其看起来更加自然。缓动函数描述了动画的进展方式。这样,直线运动可以呈现出有趣的形状。
缓动函数 指定了参数随时间的变化率。它的方程使得物体在开始时慢慢地加速,或者在接近结尾时减速。最常见的一组缓动方程来自Robert Penner的书和网站。
easeInElastic() 方法 用于弹性缓动。
语法:
easeInElastic(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 easeInElastic() function
function easeInElastic(t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (!p) p = d * .3;
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
}
else var s = p / (2 * Math.PI)
* Math.asin(c / a);
return -(a * Math.pow(2, 10
* (t -= 1)) * Math.sin((t * d
- s) * (2 * Math.PI) / p)) + b;
}
// Calling the easeInElastic() function
// over the specified parameter values
console.log(fabric.util.ease
.easeInElastic(1, 2, 3, 4));
</script>
</body>
</html>
输出:
1.9834271848159404
示例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 easeInElastic() function
function easeInElastic(t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (!p) p = d * .3;
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
}
else var s = p / (2 * Math.PI)
* Math.asin(c / a);
return -(a * Math.pow(2, 10 *
(t -= 1)) * Math.sin((t * d - s)
* (2 * Math.PI) / p)) + b;
}
// Initializing the parameters
// with its values
var t = 5;
var b = 10;
var c = 40;
var d = 12;
// Calling the easeInElastic() function
// over the specified parameter values
console.log(fabric.util.ease
.easeInElastic(t, b, c, d));
</script>
</body>
</html>
输出:
10.659230814942847