特殊旋转计算

问题描述 投票:0回答:1

我正在研究某种特殊指南针。

该项目工作得非常好,在这个问题中也没有那么重要,因为我只是在项目的一部分遇到了大问题:


我的代码返回的计算角度可能是(-360到360之间的值):

-318°, -29°, 223°, -163°, ... 

由于我的奇怪(但工作)计算,我没有从0°到360°的角度,但是例如我得到的值在42°到-317°或25°到-334°之间。

为了旋转特定旋转的图像,这种行为不会引起问题,因为相对于圆形旋转,-317°几乎与42°相同。


我的问题:对于我的项目,现在重要的是从0% to 100%(如0°-360°)获得默认角度的旋转。我怎样才能获得这些角度范围?

function rotate(val) {
  div.style.transform = "rotate("+val+"deg)"
  div.innerHTML = val + "°"
}
rotate(26)
div {
  position: fixed;
  left: 50%;
  top: 10%;
  margin-left:-30px;
  background: black;
  font-family: Helvetica Neue;
  font-size: 30px;
  height: 60px;
  width: 60px;
  color: white;
  line-height: 160%;
  text-align: center;
}
<p><b>(!) Try</b>: Input "25" and "-334"</p>

<input type="number" value="25" onchange="rotate(this.value)" placeholder="input degree">
<div id="div">[]</div>

我的问题:如何以百分比(从0%-100%)计算图像的旋转?

编辑:只是使用

var percentageRotation = myweirdrotation/3.6

根本不会解决问题。它不会输出0到100之间的值。内部也有这种奇怪的“跳跃”。

javascript html math rotation compass
1个回答
0
投票

我发现了自己的解决方案

使用此代码将完成工作:

var rot = (myweirdrotation%360) / 3.6
© www.soinside.com 2019 - 2024. All rights reserved.