画布动画
我正在使用画布制作图形脚本,我正在向图表添加动画,但我不喜欢它的外观,例如,我使用 setInterval for X 函数向矩形添加高度来制作条形图,但是我想要一个更流畅的动画,还有另一种制作动画的方法吗?
I'm making a graph script using canvas, i'm adding animation to a chart but i don't like the way that it's look, i use setInterval for X function adding height to a rectangle to make a bar chart for example, but i wanna an animation more fluid, is another way to do an animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不不不。使用 JS 创建动画有三种方法:
选项 #1 和 #2 是创建动画的老式方法。选项 #3 是较新的规范,可产生最流畅的动画,因为浏览器本身会动态控制 FPS。这是 Paul Irish 创建的一个很棒的垫片,它创建了一个 requestAnimFrame 包装器来处理所有浏览器实现:
no no no. There are three ways to create an animation with JS:
options #1 and #2 are old-school ways of creating animations. option #3 is a newer spec and yields the smoothest animations because the browser itself is dynamically controlling the FPS. Here's an awesome shim created by Paul Irish that creates a requestAnimFrame wrapper to handle all browser implementations:
我假设您有初始高度为 0 的矩形,并且每个间隔都增加高度,直到达到设定点......并且您想让动画“更平滑”?
为了使其更加流畅,您只需降低 setInterval [delay] 的第二个参数,以便第一个参数 [要调用的函数] 被更多地调用...
此外,您可以使用公式
rect.h = (rect.h*N+targetHeight)/(N+1)
... 其中 N > 1...因此,最初,条形会增长很多,然后最终减慢增长到目标高度。
I'm assuming that you have rectangles of initial height 0, and you're increasing height per interval until you reach a set point... and that you want to make the animation "smoother"?
To make it more fluid, you just lower the 2nd parameter of setInterval [delay] so that the first parameter [function to call] is called more...
In addition, you can add a tween with a slowdown at the end by using the formula
rect.h = (rect.h*N+targetHeight)/(N+1)
... where N > 1...So that initially, the bar grows by a lot and then eventually slows down growth to the target height.