css3旋转过渡,不走捷径

elb*_*132 5 css transition rotation css3 cordova

我想使用css3过渡来使用phonegap平滑指南针移动.我计算所需的旋转角度为0到359.

问题是,当它应该从例如359变为0时,它不会顺时针旋转1度,而是逆时针旋转359度.

有没有办法告诉CSS总是采用最短的方式进行轮换?

thi*_*ate 18

变换正是你所说的.

它从359deg开始并且达到1deg.你期待'翻转'360deg回到1deg,真的是361deg.变换转换的工作方式是在值之间进行插值.

您的问题的解决方案是创建一个包含旋转度的计数器变量:

var rot = 0;  // lets start at zero, you can apply whatever later
Run Code Online (Sandbox Code Playgroud)

要应用旋转,请更改值:

rot = 359;
// note the extra brackets to ensure the expression is evaluated before
//   the string is assigned this is require in some browsers
element.style.transform = ("rotate( " + rot + "deg )");
Run Code Online (Sandbox Code Playgroud)

所以如果你这样做:

rot = 1;
element.style.transform = ("rotate( " + rot + "deg )");
Run Code Online (Sandbox Code Playgroud)

它回去了.因此,无论经过多少次旋转,您都需要查看它是否接近360或0.您可以通过检查其值element.style.transform只是当前rot值然后与新rot值进行比较来完成此操作.但是,您需要针对可能存在的旋转次数执行此操作,因此:

var apparentRot = rot % 360;
Run Code Online (Sandbox Code Playgroud)

现在无论它有多少次旋转,你知道它周围有多远,负值等于值+ 360:

if ( apparentRot < 0 ) { apparentRot += 360; } 
Run Code Online (Sandbox Code Playgroud)

现在你已经规范化了任何负值,并且可以询问是否需要正旋转(通过360deg)或负值.由于您似乎将新的旋转值设置为0-360deg,这可以简化您的问题.您可以询问新旋转+ 360是否比新旋转本身更接近旧值:

var aR,          // what the current rotation appears to be (apparentRot shortened)
    nR,          // the new rotation desired (newRot)
    rot;         // what the current rotation is and thus the 'counter'

// there are two interesting events where you have to rotate through 0/360
//   the first is when the original rotation is less than 180 and the new one
//   is greater than 180deg larger, then we go through the apparent 0 to 359...
if ( aR < 180 && (nR > (aR + 180)) ) {
    // rotate back
    rot -= 360;
} 

//   the second case is when the original rotation is over 180deg and the new
//   rotation is less than 180deg smaller
if ( aR >= 180 && (nR <= (aR - 180)) ) {
    // rotate forward
    rot += 360;
}
Run Code Online (Sandbox Code Playgroud)

除此之外,只需添加新旋转的值rot就可以了:

rot += (nR - aR); //  if the apparent rotation is bigger, then the difference is
                  //  'negatively' added to the counter, so the counter is
                  //  correctly kept, same for nR being larger, the difference is
                  //  added to the counter
Run Code Online (Sandbox Code Playgroud)

清理一下:

var el, rot;

function rotateThis(element, nR) {
    var aR;
    rot = rot || 0; // if rot undefined or 0, make 0, else rot
    aR = rot % 360;
    if ( aR < 0 ) { aR += 360; }
    if ( aR < 180 && (nR > (aR + 180)) ) { rot -= 360; }
    if ( aR >= 180 && (nR <= (aR - 180)) ) { rot += 360; }
    rot += (nR - aR);
    element.style.transform = ("rotate( " + rot + "deg )");
}

// this is how to intialize  and apply 0
el = document.getElementById("elementYouWantToUse");
rotateThis(el, 0);

// now call function
rotateThis(el, 359);
rotateThis(el, 1);
Run Code Online (Sandbox Code Playgroud)

计数器可以是正数或负数,无关紧要,只需使用0-359之间的值进行新的旋转.


Red*_*att 8

正如你所提到的,CSS 过渡没有找到开始和结束旋转之间的最短路径,而是在开始和结束旋转之间完全动画。例如,从 0 到 720 将导致指南针动画旋转两次,而不是按照您的意愿静止不动。

解决方案是在每次更新时将旋转角度设置为与当前旋转角度最接近的等效角度。

计算这个角度的函数需要处理大角度和负角度,因为罗盘可以很容易地在任一方向旋转多次。

诀窍是计算旧角度和请求角度之间的差异并将其转换为范围 ( -180, 180),以便动画始终在正确的方向上采用最短路径。您只需将此差异添加到旧角度即可获得新角度。

function closestEquivalentAngle(from, to) {
    var delta = ((((to - from) % 360) + 540) % 360) - 180;
    return from + delta;
}
Run Code Online (Sandbox Code Playgroud)

除了将差异限制为 ( -180, 180)之外,上述函数非常简单。它利用模数算法来做到这一点:

  1. 差角只是一个角度,所以我们不再考虑 from 或 to
  2. 使用余数运算符获取 ( -360, 360) 中的角度;一个问题:余数与负数的数学模数不同
  3. 添加540,这会将角度移动到 ( 180, 900); 这摆脱了负值并按180度移动角度
  4. 再次取余数使我们的180-shifted 值变为 ( 0, 360); 这相当于数学模数,因为我们已经消除了负值
  5. 减去180以取消将值移动到 ( -180, 180) 中的原始角度

  • 一行 `closestAngle(from, to) =&gt; from + ((((to - from) % 360) + 540) % 360) - 180;` :) (2认同)