[英]Simple easing function in javascript
I'm having trouble figuring out this simple math problem.我在解决这个简单的数学问题时遇到了麻烦。 I have spent over two hours reading through various related answers on SO and Google, but it seems my high school math knowledge is gone.
我花了两个多小时阅读 SO 和 Google 上的各种相关答案,但似乎我的高中数学知识已经消失了。
On the page I have an element, that, once it passes a threshold, gets scaled down, the closer it gets to the edge of the containing element.在页面上我有一个元素,一旦它通过阈值,就会缩小,它越接近包含元素的边缘。 Right now, it scales in a linear fashion.
现在,它以线性方式扩展。 I calculate the distance to the container's edge, compare it to the threshold value (where the scale is 100%) and calculate a percentage from that, that is used to actually scale the Element (via CSS transform).
我计算到容器边缘的距离,将其与阈值(比例为 100%)进行比较,并从中计算一个百分比,用于实际缩放元素(通过 CSS 转换)。
What I would like, is for the scaling to start slowly for about the first 60-80% and then ramp up considerably.我想要的是,缩放在前 60-80% 时缓慢开始,然后显着增加。
To me it seems I need some sort of inverse exponential or logarithmic function to do this, but I can't figure out exactly how to implement this.对我来说,我似乎需要某种反指数或对数函数来做到这一点,但我无法确切地弄清楚如何实现这一点。 Ideally, the function would return
0.0
for x = threshold
and 1.0
for x = 0
(where x would be the element's current position/percentage).理想情况下,该函数将在
x = threshold
返回0.0
,在x = 0
返回1.0
(其中 x 将是元素的当前位置/百分比)。
Any help is appreciated.任何帮助表示赞赏。 I think this is probably trivial, but I cannot wrap my head around it.
我认为这可能是微不足道的,但我无法理解它。
Here's two you could try:这里有两个你可以尝试:
(cos(pi*x) + 1) / 2
Plot on Wolfram Alpha 在 Wolfram Alpha 上绘图
1 - x^2
Plot on Wolfram Alpha 在 Wolfram Alpha 上绘图
Depending on if you want them to ease out or be steep at the threshold.取决于您是希望它们放松还是在阈值处陡峭。 These are normalized to (0,1), but you can easily scale them to whatever interval by dividing x by your threshold.
这些被归一化为 (0,1),但您可以通过将 x 除以您的阈值轻松地将它们缩放到任何间隔。
Here you go:干得好:
function easeInOutQuad(t, b, c, d) {
t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;
};
where在哪里
it's all supplied from this great list of easing equations: http://gizma.com/easing/这一切都来自这个很棒的缓动方程列表: http : //gizma.com/easing/
If you want a super simple ease-in-out cubic, I think this one does the job如果你想要一个超级简单的缓入出立方体,我认为这个就可以了
function easeInOut(t){
return t > 0.5 ? 4*Math.pow((t-1),3)+1 : 4*Math.pow(t,3);
}
It basically uses the saddle point in x^3
from 0 to 0.5, then takes another x^3 curve and translates it to be the end.它基本上使用
x^3
从 0 到 0.5 的鞍点,然后采用另一条 x^3 曲线并将其转换为终点。 t
needs to be in the domain [0,1]
t
需要在域[0,1]
http://plnkr.co/edit/QiY8RR9KPCvoA8ZXVtPY?p=preview http://plnkr.co/edit/QiY8RR9KPCvoA8ZXVtPY?p=preview
Below you see the code of this easing function written in TypeScript.下面你会看到这个用 TypeScript 编写的缓动函数的代码。
function easeInOutQuad(x: number): number {
return x < 0.5 ? 2 * x * x : 1 - pow(-2 * x + 2, 2) / 2;
}
Bouncing on my comment, you can use jQuery's animate()
like this (example extracted from the doc )在我的评论中弹跳,您可以像这样使用 jQuery 的
animate()
( 从文档中提取的示例)
$( "#clickme" ).click(function() {
$( "#book" ).animate({
width: "toggle",
height: "toggle"
}, {
duration: 5000,
specialEasing: {
width: "linear",
height: "easeOutBounce"
},
complete: function() {
$( this ).after( "<div>Animation complete.</div>" );
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.