如何使用 HTML5 Canvas 为轨道上的物体设置动画?
假设我已经画好了所有的物体,中间有一个太阳,还有几个圆圈代表行星。有人能告诉我如何让这些物体以圆形方式移动吗?我不需要任何重力等,但我想这可能是一个甜蜜的奖励!
谢谢 :)
Lets say I've already drawn all my objects, a sun in the middle and a couple of circles for planets. Can someone enlighten me as to how I make these objects move around in a circular fashion? I don't need any gravity etc but I suppose that could be a sweet bonus!
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个粗略的解决方案:
您可以在此处找到演示。根据需要进行扩展。 :)
Here's a crude solution:
You can find a demo here. Extend as needed. :)
为了旋转空间中的点(本例中的点是行星的位置),您需要将该点的坐标与二维变换矩阵相乘,幸运的是,您不需要担心学习如何构建这个矩阵或者如何将点与矩阵相乘。 html5 canvas 上下文有一个
rotate()
方法,它将在调用此函数后绘制的每个点上为您执行此操作。rotate()
方法允许传入一个unic参数,即旋转的角度,但是这个角度需要转换成弧度。要将角度 X 转换为弧度,请执行以下操作:好的,现在您知道如何旋转画布中的点,只是不要忘记执行保存和恢复操作,以免影响场景中的其他对象。
现在你想要的是围绕另一个点(太阳)旋转一个点,当你在画布上旋转某些东西时,如果你的太阳不在(0,0)点,它会围绕(0,0)点旋转位置,然后您需要平移(移动)要旋转的点,使太阳的位置位于 (0,0) 位置。为此,您只需从旋转点的位置中减去太阳的位置即可。然后应用旋转,然后通过添加之前从中减去的太阳位置,将您的点平移(移动)回其原始位置。
要翻译您的观点,您将使用画布上下文的
translate()
方法,所以我们开始(只是不要介意我的行星和太阳是直角):好的,您可能已经注意到了尴尬的事情:我说你需要先从行星位置中减去太阳位置,然后在旋转后添加它,但在上面的代码中,我显然先添加了太阳位置,然后再进行减法,但这只是因为顺序下进行的矩阵乘法引擎盖,使其反转...
不,我希望我说得很清楚,但是几何变换确实是一个很大的主题,需要在如此简短的答案中解释...
in order to rotate a point in space (the point in this case is the position of the planet) you need to multiply the point's coordinates with a 2D transformation matrix, luckly, you don't need to worry about learning how to build this matrix or how to multiply a point with a matrix. The html5 canvas context has a
rotate()
method which is gonna do it for you in every point that you draw after calling this function. Therotate()
method allows you to pass in an unic parameter which is the angle of rotation, but this angle needs to be converted into Radians. To converte an angle X into Radians you do the following:Ok, so now you know how to rotate a point in you canvas just don't forget to do the save and restore thing to not affect the others objects in the scene.
Now what you want here is to rotate a point around another point (the sun), when you rotate something on the canvas it is gonna orbitate around the (0,0) point, if your sun is not at the (0,0) position, then you need to translate (move) the point that you want to rotate in a way that the position of the sun is gonna be at the (0,0) position. To do this you only have to subtract the position of the sun from the position of the rotating point. Then apply the rotation, and then, translate (move) your point back to it's original position by adding the sun position that you subtracted from it before.
To translate your point you gonna use the
translate()
method of the canvas context, so here we go (just don't mind that my planet and sun are rects):Ok, you might have noticed one awkward thing: That I said you needed to subtract the sun position from the planet position first and add it after rotating, but in the code above I apparentely added the sun position first and did the subtraction after, but that is just because of the order of the matrix multiplications that are going on under the hood, makes it reversed...
Nah, I hope I was clear, but geometric transformation is really a big subject to explain in such a short answer...