即使经过 360 度后如何获得以度为单位的相对旋转

2024-01-05

如果我有一个旋转的圆,并且达到 360 度的两倍,我会得到 720 度,但是当我旋转到 360 或 720 时,结果会给我 0 度,但我需要相对度数,所以如果圆旋转到 360 度,结果应该是 360 度而不是 0。是否有公式或函数可以在经过 359 度后获得精确的旋转。

这就是我能走多远,但问题是当旋转再次设置回 0 时,黑胶唱片有时会回到曲目播放的开头,这是我卡住的部分:

var detectTap = false;
var detectPlay = false;
var lastDeg = 0;
var currentDragTime = 0;
$(document).ready(function() {

  $("body").height($(document).height());
  $("#services").height($(document).height());
  $(".container").height($(document).height());
  $(".row").height($(document).height());

  $("#audioplay1").click(function() {

    myAudio = document.getElementById('audio2');
    duration = myAudio.duration;
    myAudio.currentTime = 0;
    myAudio.loop = true;
    myAudio.play();
    detectPlay = true;
    offset = $("#round-play1").offset();
  })

})

$(document).on('touchstart click', '#round-play1', function(e) {
  var x = e.touches ? e.touches[0].clientX : e.changedTouches ? e.changedTouches[0].pageX : null;
  var y = e.touches ? e.touches[0].clientY : e.changedTouches ? e.changedTouches[0].pageY : null;

  if (x && y) {
    detectTap = true;
    startDeg = angXY(e);
    myAudio.pause();
    A_x = x;
    A_y = y;
  } else {
    detectTap = true;
    x = 0
    y = 0;
    myAudio.pause();
  }

});

$(document).on('touchmove click', '#round-play1', function(e) {
  var x = e.touches ? e.touches[0].clientX : e.changedTouches ? e.changedTouches[0].pageX : null;
  var y = e.touches ? e.touches[0].clientY : e.changedTouches ? e.changedTouches[0].pageY : null;

  if (x && y) {
    var deg = angXY(e);

    var t2 = globTime;

    if (A_y > y) {


      if (lastDeg < deg) {
        var t3 = (myAudio.duration / ((myAudio.duration / (deg - startDeg)) * 100));
        currentDragTime = t3 + t2;

        myAudio.currentTime = currentDragTime;
      } else {

        var t3 = (myAudio.duration / ((myAudio.duration / (deg - startDeg)) * 100));
        currentDragTime = t2 + t3;

        myAudio.currentTime = currentDragTime;
      }


    } else {

      if (lastDeg > deg) {

        var t3 = (myAudio.duration / ((myAudio.duration / (startDeg - deg)) * 100));
        currentDragTime = t2 - t3;

        myAudio.currentTime = currentDragTime;

      } else {

        if (Math.round(currentDragTime) == 0) {



        }

        var t3 = (myAudio.duration / ((myAudio.duration / (startDeg - deg)) * 100));
        currentDragTime = t2 - t3;

        myAudio.currentTime = currentDragTime;
      }


    }


    myAudio.play();

    $("#round-play1").css('-moz-transform', 'rotate(' + deg + 'deg)');
    $("#round-play1").css('-webkit-transform', 'rotate(' + deg + 'deg)');
    $("#round-play1").css('-o-transform', 'rotate(' + deg + 'deg)');
    $("#round-play1").css('-ms-transform', 'rotate(' + deg + 'deg)');

    A_y = y;
    lastDeg = deg;
  }


});

$(document).on('touchend click', '#round-play1', function(e) {
  var x = e.touches ? e.touches[0] ? e.touches[0].clientX : e.changedTouches ? e.changedTouches[0].pageX : null : null;
  var y = e.touches ? e.touches[0] ? e.touches[0].clientY : e.changedTouches ? e.changedTouches[0].pageY : null : null;


  detectTap = false;
  myAudio.play();
  lockUp = false;
  lockDown = false;
  //r = getCurrentRotation($("#round-play1")[0]);
});

const angXY = (e) => {
  var x = e.touches ? e.touches[0].clientX : e.changedTouches[0].pageX;
  var y = e.touches ? e.touches[0].clientY : e.changedTouches[0].pageY;

  var center_x = (offset.left) + ($("#round-play1").width() / 2);
  var center_y = (offset.top) + ($("#round-play1").height() / 2);
  var mouse_x = x;
  var mouse_y = y;
  var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
  var degree = (radians * (180 / Math.PI) * -1) + 180;

  return degree;
};

const angXY2 = (e) => {
  var x = e.touches ? e.touches[0].clientX : e.changedTouches[0].pageX;
  var y = e.touches ? e.touches[0].clientY : e.changedTouches[0].pageY;

  var center_x = (offset.left) + ($("#round-play1").width() / 2);
  var center_y = (offset.top) + ($("#round-play1").height() / 2);
  var mouse_x = x;
  var mouse_y = y;
  var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
  var degree = (radians * (180 / Math.PI) * -1) + 180;

  return degree;
};


function getCurrentRotation(el) {
  var st = window.getComputedStyle(el, null);
  var tm = st.getPropertyValue("-webkit-transform") ||
    st.getPropertyValue("-moz-transform") ||
    st.getPropertyValue("-ms-transform") ||
    st.getPropertyValue("-o-transform") ||
    st.getPropertyValue("transform") ||
    "none";
  if (tm != "none") {
    var values = tm.split('(')[1].split(')')[0].split(',');
    var angle = Math.round(Math.atan2(values[1], values[0]) * (180 / Math.PI));
    return (angle < 0 ? angle + 360 : angle); //adding 360 degrees here when angle < 0 is equivalent to adding (2 * Math.PI) radians before
  }
  return 0;
}
r = 0;
var globTime = 0;
setInterval(() => {
  if (!detectTap) {
    if (detectPlay) {
      r = r + 1;

      if (r > 360) {
        r = 0;
      }

      globTime = myAudio.currentTime;

      $("#round-play1")[0].style.transform = `rotate(${r}deg)`;
    }
  }
}, 10);
#services .block .icon-block {
  border: 4px solid #f9398a;
  width: 250px;
  height: 250px;
  display: flex;
  border-radius: 50%;
  margin: 0 auto;
  float: left;
  background-color: #181818;
  padding: 10px;
}

#services .block .icon-block2 {
  border: 4px solid #1bc5fd;
  width: 250px;
  height: 250px;
  display: flex;
  border-radius: 50%;
  margin: 0 auto;
  float: left;
  background-color: #181818;
  padding: 10px;
}

#services .block .icon-block a {
  font-size: 25px;
}

#services .block .upper-block::before {
  border: 2px solid red;
  content: "";
  position: absolute;
  top: 50%;
  width: 100%;
  z-index: -99;
}

#services .block .upper-block {
  justify-content: center;
  display: flex;
  align-items: center;
}

.round-play {
  width: 225px;
  height: 225px;
  border-radius: 50%;
  display: flex;
  background-color: white;
  margin: 0 auto;
  margin-top: -1.5px;
  animation-duration: 100s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

.record {
  border-radius: 50%;
  background-color: #000;
  content: "";
  display: block;
  width: 225px;
  height: 225px;
  background-image: -webkit-radial-gradient( rgba(0, 0, 0, 1) 0, rgba(0, 0, 0, 1) 1%, rgba(206, 70, 38, 1) 2%, rgba(206, 70, 38, 1) 21%, rgba(206, 70, 38, 1) 22%, rgba(206, 70, 38, 1) 23%, rgba(206, 70, 38, .5) 24%, rgba(255, 255, 255, 0) 25%, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, .1) 41%, rgba(255, 255, 255, 0) 43%, rgba(255, 255, 255, .1) 45%, rgba(255, 255, 255, 0) 46%, rgba(255, 255, 255, 0) 53%, rgba(255, 255, 255, .1) 54%, rgba(255, 255, 255, 0) 55%, rgba(255, 255, 255, 0) 60%, rgba(255, 255, 255, .1) 61%, rgba(255, 255, 255, 0) 62%, rgba(255, 255, 255, .1) 63%, rgba(255, 255, 255, 0) 64%, rgba(255, 255, 255, .1) 65%, rgba(255, 255, 255, 0) 66%), -webkit-linear-gradient( 45deg, #000 0%, #000 30%, rgba(47, 47, 47, 1) 50%, #000 70%, #000 100%);
  background-image: radial-gradient( rgba(0, 0, 0, 1) 0, rgba(0, 0, 0, 1) 1%, rgba(206, 70, 38, 1) 2%, rgba(206, 70, 38, 1) 21%, rgba(206, 70, 38, 1) 22%, rgba(206, 70, 38, 1) 23%, rgba(206, 70, 38, .5) 24%, rgba(255, 255, 255, 0) 25%, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, .1) 41%, rgba(255, 255, 255, 0) 43%, rgba(255, 255, 255, .1) 45%, rgba(255, 255, 255, 0) 46%, rgba(255, 255, 255, 0) 53%, rgba(255, 255, 255, .1) 54%, rgba(255, 255, 255, 0) 55%, rgba(255, 255, 255, 0) 60%, rgba(255, 255, 255, .1) 61%, rgba(255, 255, 255, 0) 62%, rgba(255, 255, 255, .1) 63%, rgba(255, 255, 255, 0) 64%, rgba(255, 255, 255, .1) 65%, rgba(255, 255, 255, 0) 66%), linear-gradient( 45deg, #000 0%, #000 30%, rgba(47, 47, 47, 1) 50%, #000 70%, #000 100%);
}

.divbar1 {
  width: 10px;
  height: 100px;
  background-color: #f9398a;
  position: relative;
}

.divcircle {
  z-index: 100;
  border-radius: 15px;
  position: absolute;
  top: 46%;
  width: 30px;
  height: 30px;
  background-color: white;
  margin-left: -10px;
}

.divbar2 {
  width: 10px;
  height: 100px;
  background-color: #1bc5fd;
  position: relative;
  margin-top: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<section id="services">
  <div class="container block" style="width: 100%;">
    <div class="row">
      <div class="col-lg-6 upper-block">
        <div class="icon-block">
          <div id="round-play1" class="round-play record"></div>
          <audio id="audio2" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3">
                                </audio>
        </div>

        <div style="width:10px;" class="">

          <div class="divbar1"></div>
          <div class="divcircle"></div>
          <div class="divbar2"></div>

        </div>

        <div class="icon-block2" style="">
          <div id="round-play1" class="round-play record"></div>
        </div>
      </div>
      <button id="audioplay1">Audio</button>
    </div>
  </div>
</section>

你可以设置一个大于 360 度的角度,它会很高兴地旋转这么多度。只需跟踪您正在使用的值,以便您actually设置正确的角度,以获得正确的 CSS 属性。

每次单击“go”时,以下代码片段将在 0 到 900 度之间保持 div 旋转,并且在任何时候都不会应用该角度“模 360”:

go.addEventListener(`click`, () => {
  const { style } = box;
  const deg = parseFloat(style.getPropertyValue(`--deg`) || 0);
  const update = (Math.random() * 900)|0;
  style.setProperty(`--update`, update);
  style.setProperty(`--deg`, deg + update);
  degrees.textContent = `added ${update} degrees`;
});
div {
  --deg: 0;
  --update: 0;
  --speed: calc(var(--update) / 360 * 5s);
  background: red;
  width: 1em;
  height: 1em;
  transform: rotate(calc(var(--deg) * 1deg));
  transition: transform var(--speed) linear;
  margin-bottom: 0.4em;
  border-top: 1px solid black;
}
<div id="box"></div>
<button id="go">go</button>
<span id="degrees"></span>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

即使经过 360 度后如何获得以度为单位的相对旋转 的相关文章

随机推荐

  • 使用 .htaccess 删除 .php 并重定向到其非 .php 版本

    我有一个网站 需要删除每个文件的 php 扩展名 然后将指向包含 php 扩展名的任何文件的任何链接重定向到同一文件 但不包含 php 我找到了以下代码 它非常有用 但是文件夹中包含的任何内容都将被重定向到根目录 如下例所示 http ww
  • 如何仅在 div 加载时运行函数?

    我只想在加载 div 时运行函数 当我加载页面时 会加载许多文件 在列表的末尾 PHP 回显一个 div 当显示这个时 jQuery 应该运行一个函数 我可以通过点击事件来完成此操作 但我希望它能够自动工作 而无需按下按钮 单击后 它的工作
  • 尝试了解 Pandoc 如何从 Markdown 转换为 Latex

    如果这是重复的话 我深表歉意 我有一个 Markdown 文件test md https www akshaygaur org test md它有两个乳胶数学模式部分 第一个乳胶块 在 md 文件中 begin flalign P 1
  • VScode:如何更改 HTML 打开和关闭标记的颜色

    如何更改 VScode 中 HTML 打开 关闭标签的颜色以匹配下图 我尝试过使用Highlight Matching Tag扩展名和以下设置 但这仅适用于选择 onFocus 标签 我希望开放标签的实际字体颜色与所有结束标签不同 谢谢你
  • 是否可以在不使用 ID 的情况下实现自增编号?

    我继续谷歌搜索并发现唯一的方法是使用 Id GeneratedValue strategy GenerationType Identity 但我已经有一个主键 我只需要另一个自动递增的字段 通过手动计算来编码确实很困难 我看到以下选项 1
  • iOS10 上的地址簿崩溃

    在 iOS10 0 中 从联系人选择器中选择联系人会使应用程序崩溃 联系人选择器显示使用ABPeoplePickerNavigationController像这样 let contactsPicker ABPeoplePickerNavig
  • maven 使用简单的命令行安装和部署第 3 方依赖项

    我们有许多未在任何地方托管的第三方依赖项 对于每一个 我们都有一个 jar 文件 我们希望能够将其安装和 或部署到我们的存储库 一些 jar 文件有自己的依赖项 我们也需要声明它们 我们为每个 jar 文件创建了 pom xml 文件 声明
  • ANR 错误 - 屏幕关闭 - 我该如何处理它们?

    我在开发人员控制台上收到此消息 指出我的应用程序已冻结 因为 ANR 意图广播 act android intent action SCREEN OFF flg 0x40000000 没有堆栈跟踪 因为这是由 Froyo 之前的用户提出的
  • 如何从 Sails JS 中的现有数据库生成模型?

    我首先从SailsJS and MySQL 我的数据库中有很多表 所以 我不知道在SailsJS有一个从数据库生成模型的工具 例如Database First in Entity Framework ASP 您应该使用 自动生成现有模型库数
  • 带有自定义适配器的 ListView

    我遵循了几个教程 但仍然无法填充我的列表视图 我究竟做错了什么 这是布局 spaced list xml
  • 在 iPhone 中计算行驶距离

    我需要找到两个地点之间的行驶距离 我不需要在地图中显示方向 只需要计算距离 我需要在我的应用程序中使用它 MapKit 允许这样做吗 有没有可以使用的替代方案 我可以使用 CloudMade 进行前向地理编码 但似乎没有获取行驶距离的选项
  • Haskell 智能构造函数的编译时检查

    我正在学习 Haskell 通过讲座 http www cis upenn edu cis194 spring13 http www cis upenn edu cis194 spring13 我有 module HanoiDisk Han
  • 如果未输入文本,则保持按钮禁用

    我想实现这样的目标 我有 4 个文本字段和 1 个选择字段 这是提交表单所需的 我想保持按钮禁用 除非文本字段具有值并且从下拉列表中选择了一个选项 我的代码是here http jsfiddle net C2mRd 3 我遇到的问题是 每当
  • 获得一副牌中的牌的得分值的最佳方法是什么

    我想知道创建一副带有分数的纸牌或计算分数的最佳方法是什么 正如您将在下面看到的 我有一个计算类来处理卡片分数 检查卡片是什么并给它一个值并计算获胜者 我正在创建一个二十一点游戏 我已经随机生成了卡牌 但现在每张卡牌的分数都遇到了麻烦 我只是
  • 如何重新打开 OS X 中情节提要中创建的关闭窗口

    我的问题很重要 但答案似乎不适用于 Swift Storyboards Cocoa 用 X 关闭后以编程方式显示主窗口 https stackoverflow com questions 6201958 cocoa programmatic
  • 查找列中的 n 个最大值

    我试图找到 SQL Server 中特定列中的 n 个最大数字 我们可以轻松找到一列中的最大值和第二大值 但是我如何找到一列中的 5 个最大值呢 您为 MySQL 和 SQL Server 都标记了它 在 SQL Server 中您可以使用
  • Python,向目录字符串添加尾部斜杠,操作系统独立

    如何添加尾部斜杠 对于 nix 对于 win32 到目录字符串 如果尾部斜杠尚未存在 谢谢 os path join path 如果尾部斜杠尚不存在 则会添加尾部斜杠 你可以做os path join path or os path joi
  • 如何在代码中访问x:Name-property?

    我将 XAML 文件中的 x Name 分配给了一个可以触发 MouseDown 事件的对象 在这种情况下 我想再次获取发件人的 x name attribute 我怎么做 该对象看起来像这样
  • float 的访问说明符的行为不符合预期

    include
  • 即使经过 360 度后如何获得以度为单位的相对旋转

    如果我有一个旋转的圆 并且达到 360 度的两倍 我会得到 720 度 但是当我旋转到 360 或 720 时 结果会给我 0 度 但我需要相对度数 所以如果圆旋转到 360 度 结果应该是 360 度而不是 0 是否有公式或函数可以在经过