如何使用 HTML5 Canvas 为轨道上的物体设置动画?

发布于 2024-11-06 03:09:20 字数 101 浏览 7 评论 0原文

假设我已经画好了所有的物体,中间有一个太阳,还有几个圆圈代表行星。有人能告诉我如何让这些物体以圆形方式移动吗?我不需要任何重力等,但我想这可能是一个甜蜜的奖励!

谢谢 :)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2024-11-13 03:09:20

这是一个粗略的解决方案:

var canvas = document.getElementById('scene');
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;

var circle = function(color, r) {
    ctx.fillStyle = color;

    ctx.beginPath();
    ctx.arc(0, 0, r, 0, 2 * Math.PI, true);
    ctx.closePath();

    ctx.fill();
}

var i = 0;
var redraw = function() {
    ctx.save();

    // paint bg
    ctx.fillStyle = 'black';
    ctx.fillRect(0, 0, w, h);

    // set origin to center
    ctx.translate(w / 2, h / 2);

    // draw sun
    circle('yellow', 20);

    // rotate + move along x
    ctx.rotate(i / 100);
    ctx.translate(100, 0);

    // draw planet
    circle('green', 10);

    ctx.restore();

    i++;

    window.requestAnimationFrame(redraw);
};

window.requestAnimationFrame(redraw);

您可以在此处找到演示。根据需要进行扩展。 :)

Here's a crude solution:

var canvas = document.getElementById('scene');
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;

var circle = function(color, r) {
    ctx.fillStyle = color;

    ctx.beginPath();
    ctx.arc(0, 0, r, 0, 2 * Math.PI, true);
    ctx.closePath();

    ctx.fill();
}

var i = 0;
var redraw = function() {
    ctx.save();

    // paint bg
    ctx.fillStyle = 'black';
    ctx.fillRect(0, 0, w, h);

    // set origin to center
    ctx.translate(w / 2, h / 2);

    // draw sun
    circle('yellow', 20);

    // rotate + move along x
    ctx.rotate(i / 100);
    ctx.translate(100, 0);

    // draw planet
    circle('green', 10);

    ctx.restore();

    i++;

    window.requestAnimationFrame(redraw);
};

window.requestAnimationFrame(redraw);

You can find a demo here. Extend as needed. :)

遇见了你 2024-11-13 03:09:20

为了旋转空间中的点(本例中的点是行星的位置),您需要将该点的坐标与二维变换矩阵相乘,幸运的是,您不需要担心学习如何构建这个矩阵或者如何将点与矩阵相乘。 html5 canvas 上下文有一个 rotate() 方法,它将在调用此函数后绘制的每个点上为您执行此操作。 rotate()方法允许传入一个unic参数,即旋转的角度,但是这个角度需要转换成弧度。要将角度 X 转换为弧度,请执行以下操作:

var X = 10; // 10 degrees
var X_Radians = Math.PI / 180 * X;

// Now you pass this angle to the rotate method
context.rotate(X_Radians);

好的,现在您知道如何旋转画布中的点,只是不要忘记执行保存和恢复操作,以免影响场景中的其他对象。

现在你想要的是围绕另一个点(太阳)旋转一个点,当你在画布上旋转某些东西时,如果你的太阳不在(0,0)点,它会围绕(0,0)点旋转位置,然后您需要平移(移动)要旋转的点,使太阳的位置位于 (0,0) 位置。为此,您只需从旋转点的位置中减去太阳的位置即可。然后应用旋转,然后通过添加之前从中减去的太阳位置,将您的点平移(移动)回其原始位置。

要翻译您的观点,您将使用画布上下文的 translate() 方法,所以我们开始(只是不要介意我的行星和太阳是直角):

var Planet_Position = {X: 400, Y: 250};
var Sun_Position = {X: 250, Y: 250};

// First, let's draw our sun and our planet
context.fillStyle = "#ffff00";
context.fillRect(Sun_Position.X, Sun_Position.Y, 10, 10);

context.fillStyle = "#0000ff";
context.fillRect(Planet_Position.X, Planet_Position.Y, 10, 10);

// Ok, so now we are going to rotate our planet around the sun and draw it again

context.save();
context.translate(Sun_Position.X, Sun_Position.Y);
context.rotate(-Math.PI / 180 * 10);
context.translate(-Sun_Position.X, -Sun_Position.Y);

context.fillRect(Planet_Position.X, Planet_Position.Y, 10, 10);

context.restore();

好的,您可能已经注意到了尴尬的事情:我说你需要先从行星位置中减去太阳位置,然后在旋转后添加它,但在上面的代码中,我显然先添加了太阳位置,然后再进行减法,但这只是因为顺序下进行的矩阵乘法引擎盖,使其反转...

不,我希望我说得很清楚,但是几何变换确实是一个很大的主题,需要在如此简短的答案中解释...

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. The rotate() 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:

var X = 10; // 10 degrees
var X_Radians = Math.PI / 180 * X;

// Now you pass this angle to the rotate method
context.rotate(X_Radians);

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):

var Planet_Position = {X: 400, Y: 250};
var Sun_Position = {X: 250, Y: 250};

// First, let's draw our sun and our planet
context.fillStyle = "#ffff00";
context.fillRect(Sun_Position.X, Sun_Position.Y, 10, 10);

context.fillStyle = "#0000ff";
context.fillRect(Planet_Position.X, Planet_Position.Y, 10, 10);

// Ok, so now we are going to rotate our planet around the sun and draw it again

context.save();
context.translate(Sun_Position.X, Sun_Position.Y);
context.rotate(-Math.PI / 180 * 10);
context.translate(-Sun_Position.X, -Sun_Position.Y);

context.fillRect(Planet_Position.X, Planet_Position.Y, 10, 10);

context.restore();

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文