• <tfoot id='GZRls'></tfoot>

    <small id='GZRls'></small><noframes id='GZRls'>

    <legend id='GZRls'><style id='GZRls'><dir id='GZRls'><q id='GZRls'></q></dir></style></legend>

        • <bdo id='GZRls'></bdo><ul id='GZRls'></ul>
      1. <i id='GZRls'><tr id='GZRls'><dt id='GZRls'><q id='GZRls'><span id='GZRls'><b id='GZRls'><form id='GZRls'><ins id='GZRls'></ins><ul id='GZRls'></ul><sub id='GZRls'></sub></form><legend id='GZRls'></legend><bdo id='GZRls'><pre id='GZRls'><center id='GZRls'></center></pre></bdo></b><th id='GZRls'></th></span></q></dt></tr></i><div id='GZRls'><tfoot id='GZRls'></tfoot><dl id='GZRls'><fieldset id='GZRls'></fieldset></dl></div>
      2. 如何限制圆区域内的运动

        时间:2023-09-07

            <tbody id='7iPRB'></tbody>
                <bdo id='7iPRB'></bdo><ul id='7iPRB'></ul>

              • <i id='7iPRB'><tr id='7iPRB'><dt id='7iPRB'><q id='7iPRB'><span id='7iPRB'><b id='7iPRB'><form id='7iPRB'><ins id='7iPRB'></ins><ul id='7iPRB'></ul><sub id='7iPRB'></sub></form><legend id='7iPRB'></legend><bdo id='7iPRB'><pre id='7iPRB'><center id='7iPRB'></center></pre></bdo></b><th id='7iPRB'></th></span></q></dt></tr></i><div id='7iPRB'><tfoot id='7iPRB'></tfoot><dl id='7iPRB'><fieldset id='7iPRB'></fieldset></dl></div>
              • <small id='7iPRB'></small><noframes id='7iPRB'>

              • <legend id='7iPRB'><style id='7iPRB'><dir id='7iPRB'><q id='7iPRB'></q></dir></style></legend>
                <tfoot id='7iPRB'></tfoot>

                  本文介绍了如何限制圆区域内的运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这可能更多是与几何相关的问题,但我试图将控制器限制在圆的区域内.我知道我必须接触 Math.sin() 和 Math.cos() 方法,但到目前为止我的尝试都没有结果.

                  This might be more a geometry related question, but I'm trying to constrain a controller within an area of a circle. I know I have to touch the Math.sin() and Math.cos() methods, but my attemps so far have been fruitless so far.

                  这里是 jsfiddle:到目前为止,我已经能够将它限制在一个看不见的正方形上.http://jsfiddle.net/maGVK/

                  Here is the jsfiddle: So far I've been able to constrain it to an invisible square. http://jsfiddle.net/maGVK/

                  推荐答案

                  所以在大家的帮助下,我终于能够完成这个了.

                  So I finally was able to complete this with a bit of everyone's help.

                  var pointerEl = document.getElementById("pointer");
                  var canvasEl = document.getElementById("canvas");
                  var canvas = {
                      width: canvasEl.offsetWidth,
                      height: canvasEl.offsetHeight,
                      top: canvasEl.offsetTop,
                      left: canvasEl.offsetLeft
                  };
                  canvas.center = [canvas.left + canvas.width / 2, canvas.top + canvas.height / 2];
                  canvas.radius = canvas.width / 2;
                  
                  
                  window.onmousemove = function(e) {
                      var result = limit(e.x, e.y);
                          pointer.style.left = result.x + "px";
                          pointer.style.top = result.y + "px";
                  }
                  
                  function limit(x, y) {
                      var dist = distance([x, y], canvas.center);
                      if (dist <= canvas.radius) {
                          return {x: x, y: y};
                      } 
                      else {
                          x = x - canvas.center[0];
                          y = y - canvas.center[1];
                          var radians = Math.atan2(y, x)
                             return {
                                 x: Math.cos(radians) * canvas.radius + canvas.center[0],
                                 y: Math.sin(radians) * canvas.radius + canvas.center[1]
                             }
                          } 
                      }
                  
                  function distance(dot1, dot2) {
                      var x1 = dot1[0],
                          y1 = dot1[1],
                          x2 = dot2[0],
                          y2 = dot2[1];
                      return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
                  }
                  

                  你可以在这里看到结果:

                  You can see the result here:

                  http://jsfiddle.net/7Asn6/

                  这篇关于如何限制圆区域内的运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 HTML5 画布上画一个模糊的圆圈? 下一篇:在谷歌地图上画圆弧

                  相关文章

                  最新文章

                    • <bdo id='Q0oho'></bdo><ul id='Q0oho'></ul>
                    <tfoot id='Q0oho'></tfoot>

                  1. <small id='Q0oho'></small><noframes id='Q0oho'>

                    <legend id='Q0oho'><style id='Q0oho'><dir id='Q0oho'><q id='Q0oho'></q></dir></style></legend>

                    <i id='Q0oho'><tr id='Q0oho'><dt id='Q0oho'><q id='Q0oho'><span id='Q0oho'><b id='Q0oho'><form id='Q0oho'><ins id='Q0oho'></ins><ul id='Q0oho'></ul><sub id='Q0oho'></sub></form><legend id='Q0oho'></legend><bdo id='Q0oho'><pre id='Q0oho'><center id='Q0oho'></center></pre></bdo></b><th id='Q0oho'></th></span></q></dt></tr></i><div id='Q0oho'><tfoot id='Q0oho'></tfoot><dl id='Q0oho'><fieldset id='Q0oho'></fieldset></dl></div>