我正在创建一个旋转轮子游戏,我想让轮子开始变慢,以一定的旋转速度达到峰值,然后再次减速。
对这个指数转速有什么想法吗?谢谢!
发布于 2014-10-21 08:31:20
线性加速度会引起二次增加的位移,这在自然界中很常见。作为候选人,你应该考虑这一点。
无论如何,我会设置您的程序,以便您可以插入不同的函数来查看它们的外观。请查看jQuery easings了解这会是什么样子。
对于初学者,我会尝试这个角位移函数:
a = ct^2, for t < 3
= c(9 - t^2), for 3 <= t < 6
= 0, otherwise其中c是某个常量。
发布于 2014-10-21 08:52:08
一个简单的CSS/JS解决方案:
首先,您需要使用@keyframes创建一些CSS3动画
@keyframes spinningWheel{
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
/* Chrome, Safari, Opera */
@-webkit-keyframes spinningWheel{
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
/* Firefox */
@-moz-keyframes spinningWheel{
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}现在创建一个实现动画的CSS类:
.mySpinningWheel{
animation-name: spinningWheel;
-webkit-animation-name: spinningWheel;
-moz-animation-name: spinningWheel;
animation-timing-function: ease-in-out; /* ease-in-out is slow at the beginning and the end */
-webkit-animation-timing-function: ease-in-out;
-moz-animation-timing-function: ease-in-out;
animation-iteration-count: infinite; /* this causes your wheel to spin indefinitely */
-webkit-animation-iteration-count: infinite;
-moz-animation-iteration-count: infinite;
animation-duration: 5s; /* One iteration of this animation will take 5s */
-webkit-animation-duration: 5s;
-moz-animation-duration: 5s;
}这个动画的一次迭代将需要5秒,它将缓慢旋转,中间旋转更快,最后再次缓慢旋转。
要在JS中动态添加CSS类,请使用jQuery的.addClass()函数,或者如果不使用jQuery,只需执行document.querySelector(".selectorForYourWheel").classList.add(".mySpinningWheel")
如果你想自己动态地触发轮子的速度(不让它在一次迭代中减速和提速,而是让它保持一个可以改变的恒定速度),那么将CSS类中的animation-timing-function块从ease-in-out更改为linear。然后,使用jQuery的.css()方法更改动画的持续时间(这样动画的速度就会变快或变慢)。
https://stackoverflow.com/questions/26477025
复制相似问题