Javascript x和y的画布旋转效果

Javascript x和y的画布旋转效果,javascript,html,html5-canvas,Javascript,Html,Html5 Canvas,我已经得到了这样的效果,我可以使图像朝着它所面对的方向移动。但现在当我尝试时,图像会旋转,但不会朝正确的方向移动。 如何使画布中的图像沿其所面对的方向移动 我的代码: ctx.save(); ctx.translate(bulletArray[i].x, bulletArray[i].y); ctx.rotate(bulletArray[i].degree); ctx.translate(-bulletArray[i].x, -bulletArray[i].y);

我已经得到了这样的效果,我可以使图像朝着它所面对的方向移动。但现在当我尝试时,图像会旋转,但不会朝正确的方向移动。 如何使画布中的图像沿其所面对的方向移动

我的代码:

    ctx.save();
    ctx.translate(bulletArray[i].x, bulletArray[i].y);
    ctx.rotate(bulletArray[i].degree);
    ctx.translate(-bulletArray[i].x, -bulletArray[i].y);
    bulletArray[i].x++;
    ctx.drawImage(bullet, bulletArray[i].x, bulletArray[i].y, 10, 10);
    ctx.restore();

(这是一个游戏循环,所以它会重复)

如果你不想旋转,那么不要使用

ctx.rotate(bulletArray[i].degree);

在您的代码中

确定。它不旋转的原因是因为旋转点需要是屏幕的中心,而不是子弹。这将使子弹正确移动,而不仅仅是旋转。

我确实需要旋转。我想知道的是为什么子弹不会沿着旋转的方向移动。(顺便说一句)我可以在其他事情上这样做,因此这种代码安排的某些方面是错误的。然后使用
sin
cos
函数来计算移动的直角。注意:角度不是度,而是辐射度!我已经弄明白了,并且已经发布了答案。此外,度特性将转换为弧度。