使用 JavaScript 制作图像序列动画
我有一个包含不同角度物体图像的序列。我希望当用户拖动鼠标时对象被假旋转,我已经实现了这一点。
但我想要的是,当鼠标离开图像区域时,它会将图像序列动画回到默认位置。
例如,我有 30 个 JPEG,其中 1.jpg 是 -180°,30.jpg 是 180°。当然,15.jpg 是 0° 居中的默认图像。
因此,如果用户一直旋转到 (-)180°,3 秒后它将旋转回 0°。但我希望动画尽可能流畅。我该怎么做呢?
I have a sequence with images of an object from different angles. I want the object to be faux rotated when the user drags its mouse, and this I have implemented.
But what I want, is when the mouse leaves the image area, that it animates the image sequence back to the default position.
For instance, I have 30 JPEGs, where 1.jpg is -180° and 30.jpg is 180°. Naturally, 15.jpg is the centered default image at 0°.
So, if the user rotates all the way to (-)180° it will rotate back to 0° after say 3 seconds. But I want the animation to be as smooth as possible. How would I go about to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使动画尽可能流畅,您应该使用 CSS 精灵,这是一个包含所有其他图像的图像,这样动画开始时所有帧都会被加载。
然后,您只需要根据您希望动画的平滑程度在给定的时间内调用一个函数,并更改图像的背景属性。或者,如果不使用精灵,请为其分配不同的 src。
我认为你应该选择不小于25的每秒帧数。更高的帧速率意味着更流畅的动画,但使用更多的CPU。
这是基本方法
如果您希望图像以动画方式返回,您只需应用相同的方法,但帧编号向后。
For the animation to be as smooth as possible, you should use a CSS sprite, an image containing all your other images, so the frames will all be loaded when the animation start.
Then you only need to call a function in a given amount of time, based on how smooth you want the animation to be, and change the background property of your image. Or, if not using sprite, assign a different src to it.
I think you should choose a frame-per-second value of no less than 25. Higher frame rate means more smooth animation, but more CPU used.
This is the basic approach
If you want the image to animate back, you just need to apply the same approach but with the frame numbers going backwards.