小编VSt*_*ate的帖子

我将如何旋转画布上的图像以面对它的移动方向?

这里有一个小提琴。我要问的是太空船必须面向运动的方向,因为不这样做是没有意义的。无论如何,我认为我遇到的问题是找到一个角度,并在船以不同方式移动时让船旋转该角度。

function draw () { //Create and animates 
var angle = Math.atan2(clickedChords.y - player.y, clickedChords.x - player.x);
 ctx.clearRect(0,0,canvas.width,canvas.height);  

  ctx.fillStyle= 'white'; 
  ctx.beginPath();
  //Creates crossHair/nav marker
  ctx.arc(clickedChords.x,clickedChords.y,3,0,360);
  ctx.rect(clickedChords.x - 8,clickedChords.y - 1,16,2);     
  ctx.fill();

  ctx.drawImage(ships.blueBeginers,player.x-15,player.y-15);  

  player.x += Math.cos(angle); 
  player.y += Math.sin(angle);

  requestAnimationFrame(draw); //God  
  }
 draw();
Run Code Online (Sandbox Code Playgroud)

我在翻译图像然后从图像中间旋转时遇到问题。这可能很简单,但我被卡住了。

感谢你们对我的帮助。

javascript math canvas angle rotation

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

标签 统计

angle ×1

canvas ×1

javascript ×1

math ×1

rotation ×1