小编Had*_*hes的帖子

HTML5 Canvas - 如何沿某个方向移动对象?

我有一个物体想要让它绕恒星运行。我已经设法使物体向恒星移动,但现在我还需要设置横向移动。

显然,这并不像调整 X 那样容易,因为当它移动到星星的一侧时,我还必须调整 Y。我想知道如何使用一些数学来计算出当物体围绕恒星移动时需要调整 X 和 Y 多少。

到目前为止,这是我的代码:

var c = document.getElementById('canvas');
var ctx = c.getContext('2d');

c.width = window.innerWidth;
c.height = window.innerHeight;

var star = {
    x: c.width / 2,
    y: c.height / 2,
    r: 100,
    g: 2,
    draw: function()
    {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.r, 0, 2*Math.PI);
        ctx.fillStyle = 'orange';
        ctx.fill();
        ctx.closePath();
    }
};

var node = {
    x: c.width / 2,
    y: 100,
    r: 20,
    draw: function()
    {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.r, 0, 2*Math.PI);
        ctx.fillStyle = 'blue'; …
Run Code Online (Sandbox Code Playgroud)

html javascript canvas

1
推荐指数
1
解决办法
2728
查看次数

标签 统计

canvas ×1

html ×1

javascript ×1