0

这是代码,我正在尝试获取旋转角度。我正在围绕表盘旋转图像。我把所有的角度都弄对了,除了当它达到 270 度时,它会做一些时髦的事情。角度变为负值。它从 0 到 270 工作正常,但我无法让它显示 270 到 360 之间的角度。请给我一些建议

    this.rotate = function(x){
    this.node.style.MozTransform="rotate("+x+"deg)";
    this.node.style.WebkitTransform="rotate("+x+"deg)";
    this.node.style.OTransform="rotate("+x+"deg)";
    this.node.style.msTransform="rotate("+x+"deg)";
    this.node.style.Transform="rotate("+x+"deg)";

myintID = setInterval(function(){
    //Math!
    angleFromEye = Math.atan2((cursorLocation.y-self.my_y), cursorLocation.x-  self.my_x)*(180/Math.PI)+ 90;
    //Rotate
    self.rotate(angleFromEye);
4

3 回答 3

0

实际上,对于 -90 到 0 的值,角度也是负数,但是您将 90 添加到角度,所以您看不到。

当它是负数时,只需将 360 添加到角度:

if (angleFromEye < 0) angleFromEye += 360;
于 2012-08-13T13:47:09.363 回答
0

我认为您的问题在于您从返回值范围atan2到所需范围的转换。

atan2以弧度为单位给出角度,在 range[-pi,pi]中,您希望角度在 range 中以度为单位[0,360]

您的转换为您提供角度范围内的度数[-180+90,180+90] = [-90,270]

atan2(...)*(180/Math.PI) + 90

试试这个:

atan2(...)*(180/Math.PI) + 180
于 2012-08-13T13:50:42.810 回答
0

我最近创建了一个 jQuery 插件,它允许您获取元素的旋转,也可以旋转元素。它也适用于 ie7 和 ie8。这里:http ://wp-dreams.com/jquery-element-rotation-plugin/

于 2012-10-16T20:34:41.413 回答