【问题标题】:svg transform rotation precisionsvg 变换旋转精度
【发布时间】:2018-03-20 09:10:38
【问题描述】:

我正在尝试使用 svg 创建一个简单的测试,允许我更改质量设置 (FPS) 和 svg 上的旋转速度。

我注意到变换、旋转功能适用于度数。据我所知,使用小数作为度数是不可能提高精度的。

那么我们是不是卡在使用旋转度数了,这给了我们最多 360 块来划分一个圆,或者有没有办法以某种方式提高精度?

我问的原因是因为我创建了一支笔https://codepen.io/KaGys/pen/yKVjrr?editors=1111,我在其中添加了几个质量设置。更高的质量设置会导致更高的每秒帧数。这意味着如果 1 度是我可以旋转的最小单位,我将需要开始延迟动画以低于每帧 1 度,在我的最高质量设置(每秒最快帧)下仍然是一个相当快的旋转.我希望问题很清楚,如果没有,请告诉我,我会尽力澄清。

    var ultra = 1000 / 120;
    var high = 1000 / 60;
    var medium = 1000 / 30;
    var low = 1000 / 10;

    var quality = ultra;

    var speed = 1; // degrees per frame on ultra quality (converts to lower quality settings to maintain consistency)
    // I tried doing this on per second basis, which works but the problem is that degrees can not be decimals which causes inconsistent behavior
    speed = (speed * quality) / ultra; // Recalculate the speed for consistency across quality settings

  var rotateSettings = {
    path: document.getElementById("pathGroup"),
    active: false,
    speed: speed,
    quality: quality
  };

  function rotate() {
    if (this.active) {
      setTimeout(rotate.bind(this), this.quality);
      var currentRotation = Number(
        /\d+/.exec(this.path.getAttributeNS(null, "transform"))
      );
  currentRotation = currentRotation%360;
      console.log(this.speed);
      this.path.setAttributeNS(
        null,
        "transform",
        `rotate(${currentRotation + this.speed}, 60, 60)`
      );
    }
  }

  document.getElementById("btnRotate").addEventListener("click", e => {
    if (!rotateSettings.active) {
      rotateSettings.active = true;
      document.getElementById("btnRotate").style.background = "green";
      document.getElementById("btnRotate").style.color = "white";
      rotate.bind(rotateSettings)();
    } else {
      rotateSettings.active = false;
      document.getElementById("btnRotate").style.background = "white";
      document.getElementById("btnRotate").style.color = "green";
    }
  });

【问题讨论】:

  • 您的知识不正确,您可以通过尝试使用浮点数进行旋转来轻松纠正您的误解。
  • 嗨罗伯特,请检查codepen.io/KaGys/pen/yKVjrr?editors=1111。如果您将那里的速度(var speed = 1;)更改为例如速度 0.5 或 1.5。 0.5 行不通。如果旋转度数接受浮点数,我在哪里出错了? (使用旋转按钮进行测试,控制台会将您的速度设置记录在循环中,以便您可以看到循环仍然有效)提前致谢!
  • 您为分解转换并获取值而编写的逻辑不正确。您会发现 SVG DOM 比自己动手更容易。
  • 您的回复对发现错误非常有帮助,它消除了我对旋转和浮点的误解。谢谢罗伯特。如果有人想知道,错误就在这里: /\d+/.exec(this.path.getAttributeNS(null, "transform")) 正则表达式只提取数字。通过将正则表达式更改为:/\d*\.?\d+/ 我不确定您使用 SVG DOM 是什么意思。

标签: javascript html css svg rotation


【解决方案1】:

您可以通过SVG DOM读取变换角度

var currentRotation = this.path.transform.animVal[0].angle;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2019-02-06
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多