【问题标题】:How to rotate one object at another (slowly)如何将一个对象旋转到另一个对象(慢慢地)
【发布时间】:2018-08-06 21:08:40
【问题描述】:

我一直在寻找这个功能,但到目前为止我找不到任何我能理解的东西。我已经有一个旋转函数来使它等于位置,但慢慢地证明使用 0-360 和所有的位置有点困难。

我正在使用 html canvas 2d 上下文在笛卡尔坐标系上渲染对象。

我希望 object1 以转弯速率 (R) 面对 positionX 和 positionY,相当简单。

我无需提供任何代码,因为无论如何您都可能自己编写代码。但无论如何我都会给你:

let faceAt = function (thisObject,positionX,positionY) {
  let desiredLocationX = positionX - thisObject.transform.x;
  let desiredLocationY =  positionY -thisObject.transform.y;
  thisObject.transform.rotation = Math.degrees(Math.atan2(desiredLocationY, desiredLocationX));
};

(Math.degrees) 函数将弧度转换为度数。

这个帖子说明了一切:https://www.google.ca/amp/s/jibransyed.wordpress.com/2013/09/05/game-maker-gradually-rotating-an-object-towards-a-target/amp/

【问题讨论】:

  • 我不确定您在该示例代码中做了什么。 Math.degrees 甚至是一个函数吗?我在 MDN 中没有看到它(我必须确保我没有发疯)。如果您正在寻找完整的教程,您应该研究如何操作元素的位置。然后不难拼凑出一种方法,将元素逐帧移动到您想要的位置。
  • 哦,抱歉,Math.degrees 是我自己实现的,用于将弧度转换为度数。
  • 那么,您使用的是 HTML5 画布(您提到了 2d 上下文)?或者你想旋转一个 DOM 元素?
  • 我正在使用 html 渲染上下文 (2d) 在笛卡尔平原上渲染对象
  • 是的,涉及到一个画布元素,但考虑到这个问题,我不明白为什么这些都是相关的。

标签: javascript html


【解决方案1】:

这个问题很不清楚。但是,我假设您实际上只是想围绕 HTML5 画布上的任意点旋转元素。

在画布上,一次只能绘制一个元素。您无法真正操纵单个元素 - 例如,您无法自行旋转元素。相反,您需要旋转整个画布。这将始终围绕画布的中心旋转,但如果您移动画布原点,那么您将在画布的不同部分进行绘制;从而允许您围绕一个点旋转。

查看以下示例。您可以单击画布上的任意位置,使正方形围绕该点旋转。希望这就是你所追求的:

let cv = document.getElementById("cv");
let ctx = cv.getContext("2d");
let angle = 0;

//Variables you can change:
let speed = 1; //Degrees to rotate per frame
let pointX = 250; //The x-coord to rotate around
let pointY = 250; //The y-coord to rotate around

ctx.fillStyle = "#000";

setInterval(()=>{ //This code runs every 40ms; so that the animation looks smooth
  angle = (angle + speed) % 360; //Increment the angle. Bigger changes here mean that the element will rotate faster. If we go over 360deg, reset back to 0.
  
  ctx.clearRect(0, 0, 400, 400); //Clear away the previous frame.
  
  //Draw the point we are rotating around
  ctx.beginPath();
  ctx.arc(pointX,pointY,5,0,2*Math.PI);
  ctx.fill();
  ctx.closePath();
  
  ctx.save(); //Save the state before we transform and rotate the canvas; so we can go back to the unrotated canvas for the next frame
  
  ctx.translate(pointX, pointY); //Move the origin (0, 0) point of the canvas to the point to rotate around. The canvas always rotates around the origin; so this will allow us to rotate around that point
  ctx.rotate(angle*Math.PI/180); //Rotate the canvas by the current angle. You can use your Math.degrees function to convert between rads / degs here.
  
  ctx.fillStyle = "#f00"; //Draw in red. This is also restored when ctx.restore() is called; hence the point will always be black; and the square will always be red.
  
  ctx.fillRect(0, 0, 50, 50); //Draw the item we want rotated. You can draw anything here; I just draw a square.
  
  ctx.restore(); //Restore the canvas state
  
}, 40);





//Boring event handler stuff
//Move the point to where the user clicked
//Not too robust; relys on the body padding not changing
//Really just for the demo


cv.addEventListener("click", (event)=>{
    pointX = event.clientX - 10;
    pointY = event.clientY - 10;
});
#cv {
  border:solid 1px #000; /*Just so we can see the bounds of the canvas*/
  padding:0;
  margin:0;
}

body {
  padding:10px;
  margin:0;
}
<canvas id="cv" width="400" height="400"></canvas><br>
Click on the canvas above to make the rectangle rotate around the point that was clicked.

【讨论】:

  • 标题说明了一切,我的渲染一切都很好,我知道如何围绕一个对象制作圆圈,我主要是在谈论一个对象在另一个(或一个位置)(慢慢地) - 所有相关的。不过,我非常感谢您的回答:) 谢谢伙计
  • 没问题!如果你愿意,也许可以用你需要的快速图表来编辑你的问题,我们可能会帮助你获得你想要的效果:)
  • 等等,抱歉,没有看到您最近的编辑。我会看看那个。