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

    • <bdo id='ePhbQ'></bdo><ul id='ePhbQ'></ul>

    1. <legend id='ePhbQ'><style id='ePhbQ'><dir id='ePhbQ'><q id='ePhbQ'></q></dir></style></legend>
        <tfoot id='ePhbQ'></tfoot>

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

        基于鼠标移动的 HTML5 平移

        时间:2023-06-21
      2. <small id='tbubv'></small><noframes id='tbubv'>

        • <legend id='tbubv'><style id='tbubv'><dir id='tbubv'><q id='tbubv'></q></dir></style></legend>
              <bdo id='tbubv'></bdo><ul id='tbubv'></ul>
                <tbody id='tbubv'></tbody>
              <tfoot id='tbubv'></tfoot>
                1. <i id='tbubv'><tr id='tbubv'><dt id='tbubv'><q id='tbubv'><span id='tbubv'><b id='tbubv'><form id='tbubv'><ins id='tbubv'></ins><ul id='tbubv'></ul><sub id='tbubv'></sub></form><legend id='tbubv'></legend><bdo id='tbubv'><pre id='tbubv'><center id='tbubv'></center></pre></bdo></b><th id='tbubv'></th></span></q></dt></tr></i><div id='tbubv'><tfoot id='tbubv'></tfoot><dl id='tbubv'><fieldset id='tbubv'></fieldset></dl></div>
                  本文介绍了基于鼠标移动的 HTML5 平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 HTML5 的画布内实现平移"功能,但我不确定实现它的最佳方法.

                  I'm trying to implement functionality to "pan" inside a canvas in HTML5 and I am unsure about the best way to go about accomplishing it.

                  目前 - 我正在尝试检测鼠标在画布上的位置,如果它在边缘的 10% 范围内,它将朝那个方向移动,如图所示:

                  Currently - I am trying to detect where the mouse is on the canvas, and if it is within 10% of an edge, it will move in that direction, as shown:

                  当前边缘检测:

                  canvas.onmousemove = function(e)
                  {
                      var x = e.offsetX;
                      var y = e.offsetY;
                      var cx = canvas.width;
                      var cy = canvas.height;
                      if(x <= 0.1*cx && y <= 0.1*cy)
                      {
                           alert("Upper Left"); 
                           //Move "viewport" to up and left (if possible)
                      }
                      //Additional Checks for location
                  }
                  

                  我知道我可以通过在画布中创建路径并将事件附加到它们来实现这一点,但我没有经常使用它们,所以我想我会在这里问.另外 - 如果一个包装"平底锅是可能的,那就太棒了(向左平移最终会向右).

                  I know I could probably accomplish this by creating paths within the canvas and attaching events to them, but I haven't worked with them much, so I thought I would ask here. Also - if a "wrapping" pan would be possible that would be awesome (panning to the left will eventually get to the right).

                  总结:我想知道在 HTML5 Canvas 内完成平移"的最佳途径是什么.这不会使用图像,而是使用实际绘制的对象(如果这有什么不同的话).如果可以的话,我很乐意回答任何问题.

                  Summary: I am wondering what the best route is to accomplish "panning" is within the HTML5 Canvas. This won't be using images but actual drawn objects (if that makes any difference). I'll be glad to answer any questions if I can.

                  演示:

                  演示

                  推荐答案

                  这取决于您希望如何实现鼠标移动平移,但今天它通常是实时"平移,因为您可以四处拖动.我试着稍微更新一下你的小提琴:http://jsfiddle.net/pimvdb/VWn6t/3/.

                  It depends on how you want panning with mouse movement to be implemented, but today it's often 'realtime' panning in that you can drag around. I tried to update your fiddle a little: http://jsfiddle.net/pimvdb/VWn6t/3/.

                  var isDown = false; // whether mouse is pressed
                  var startCoords = []; // 'grab' coordinates when pressing mouse
                  var last = [0, 0]; // previous coordinates of mouse release
                  
                  canvas.onmousedown = function(e) {
                      isDown = true;
                  
                      startCoords = [
                          e.offsetX - last[0], // set start coordinates
                          e.offsetY - last[1]
                     ];
                  };
                  
                  canvas.onmouseup   = function(e) {
                      isDown = false;
                  
                      last = [
                          e.offsetX - startCoords[0], // set last coordinates
                          e.offsetY - startCoords[1]
                      ];
                  };
                  
                  canvas.onmousemove = function(e)
                  {
                      if(!isDown) return; // don't pan if mouse is not pressed
                  
                      var x = e.offsetX;
                      var y = e.offsetY;
                  
                      // set the canvas' transformation matrix by setting the amount of movement:
                      // 1  0  dx
                      // 0  1  dy
                      // 0  0  1
                  
                      ctx.setTransform(1, 0, 0, 1,
                                       x - startCoords[0], y - startCoords[1]);
                  
                      render(); // render to show changes
                  
                  }
                  

                  这篇关于基于鼠标移动的 HTML5 平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:HTML5、JavaScript 和在画布中绘制多个矩形 下一篇:如果多次应用,带有 alpha 的 rgba fillStyle 不会变得完全不透明

                  相关文章

                  1. <legend id='J2Yzr'><style id='J2Yzr'><dir id='J2Yzr'><q id='J2Yzr'></q></dir></style></legend>

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

                  2. <tfoot id='J2Yzr'></tfoot>

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