[英]Compute easing duration as a function of distance
I'd like some function to compute the duration for an animation such that even when the distance changes, the speed remains constant. 我想要一些函数来计算动画的持续时间,这样即使距离改变,速度也保持不变。
====================================
| |
|<============ width =============>|
| |
| <====== distance1 ======>|
| |
| <=== distance2 ===>|
| |
===================================
This is trivial to accomplish when dealing with linear easing, 在处理线性缓和时,这是微不足道的,
function getDuration (width, distance, duration) {
return duration * (1 - distance / width);
}
However, this becomes much more complex when using non-linear easing functions, such as easeInOutQuad
(An illustration of this problem: http://jsfiddle.net/Wexcode/rdrbm8et/3/ ). 但是,当使用非线性缓动函数时,这变得更加复杂,例如easeInOutQuad
(此问题的一个例子: http : //jsfiddle.net/Wexcode/rdrbm8et/3/ )。
Given an easing function, such as easeInOutQuad
, how do I compute the duration such that the speed for any distance remains constant? 给定缓动函数,例如easeInOutQuad
,如何计算持续时间,使得任何距离的速度保持不变?
easeInOutQuad: function (t) { return t<.5 ? 2*t*t : -1+(4-2*t)*t },
Source: https://gist.github.com/gre/1650294 资料来源: https : //gist.github.com/gre/1650294
Source: http://api.jqueryui.com/easings/ 资料来源: http : //api.jqueryui.com/easings/
Given a function y = f(t)
, where y is the distance and t is the time simply solve for t, and use the t = g(y)
function to determine how long it takes to get to a given distance. 给定函数y = f(t)
,其中y是距离,t是简单求解t的时间,并使用t = g(y)
函数来确定到达给定距离所需的时间。
For instance, in the easeInOutQuad
easing, -1+(4-2*t)*t
can be solved: 例如,在easeInOutQuad
缓动中,可以求解-1+(4-2*t)*t
:
Source: http://www.wolframalpha.com/input/?i=Solve%28y+%3D+-1%2B%284-2*t%29*t%2C+t%29 资料来源: http : //www.wolframalpha.com/input/? i = Solve%28y +%3D + -1%2B%284-2 * t%29 * t%2C + t%29
In practice, you would multiply t
times your duration
to get your new duration. 在练习中,您将乘以duration
t
倍来获得新的持续时间。
var ratio = distance / width;
if (ratio <= .5) {
return duration * Math.sqrt(ratio * 1/2);
} else if (ratio <= 1) {
return duration * (1 - Math.sqrt(1-ratio) / Math.sqrt(2));
}
http://jsfiddle.net/Wexcode/rdrbm8et/6/ http://jsfiddle.net/Wexcode/rdrbm8et/6/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.