【问题标题】:Mouse coordinates appears to incorrectly place Raphael.js objects鼠标坐标似乎错误地放置了 Raphael.js 对象
【发布时间】:2019-07-25 02:34:36
【问题描述】:

我有一些关于 Raphael 空间的矩形。我设置了一个快速鼠标坐标显示来精确放置对象:

document.addEventListener("mousemove",function(event){

        var x = event.clientX;
        var y = event.clientY;

    document.getElementById('position').innerHTML = " X = "+ x +" Y = "+ y;

            });

但是,矩形似乎与预期的位置相差 10 像素左右。为什么会这样? (我也知道使用 style 而不是 css 文件是不好的做法,但我只是想直接玩弄 Raphael 而不必担心)

var p = Raphael("paper");
let r1, r2, r3, l1;


r1 = p.rect(200, 200, 40, 40)
  .attr({
    'fill': 'red',
    'cursor': 'pointer',
    'href': 'https://www.google.com/',
  });
r2 = p.rect(277, 320, 50, 50)
  .attr({
    'fill': 'blue',
    'cursor': 'pointer',
    'href': 'https://www.google.com/',
  });
p.path("M 200,200 L 240,240");


r3 = p.rect(377, 300, 30, 80)
  .attr({
    'fill': 'yellow',
    'cursor': 'pointer',
    'href': 'https://www.google.com/',
  });




document.addEventListener("mousemove", function(event) {

  var x = event.clientX;
  var y = event.clientY;

  document.getElementById('position').innerHTML = " X = " + x + " Y = " + y;

});
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>tester2</title>
  <script src="raphael-min.js"></script>
</head>

<body>
  <div id="content">
    <div id="paper"></div>
  </div>
  <p id="position">Test</p>
  <style>
    #paper {
      height: 500px;
      width: 500px;
      border: 1px solid black;
    }
  </style>
</body>

我怀疑我的鼠标坐标系只是关闭了,但如果是这样我不知道如何相对于Raphael("paper");配置它

问题的实时链接:https://jsfiddle.net/oLwydha6/

我该如何解决这个问题?

【问题讨论】:

    标签: javascript html raphael


    【解决方案1】:

    总的区别是9px,距主体(8px)和边框(1px)的边距。您可能要考虑使用offsetX 而不是clientX 来获取职位:

    var p = Raphael("paper");
    let r1, r2, r3, l1;
    
    
    r1 = p.rect(200, 200, 40, 40)
      .attr({
        'fill': 'red',
        'cursor': 'pointer',
        'href': 'https://www.google.com/',
      });
    r2 = p.rect(277, 320, 50, 50)
      .attr({
        'fill': 'blue',
        'cursor': 'pointer',
        'href': 'https://www.google.com/',
      });
    p.path("M 200,200 L 240,240");
    
    
    r3 = p.rect(377, 300, 30, 80)
      .attr({
        'fill': 'yellow',
        'cursor': 'pointer',
        'href': 'https://www.google.com/',
      });
    
    document.addEventListener("mousemove", function(event) {
    
      var x = event.clientX;
      var y = event.clientY;
      document.getElementById('position').innerHTML = " clientX = " + x + " clientY = " + y;
      document.getElementById('position_offset').innerHTML = " offsetX = " + event.offsetX + " offsetY = " + event.offsetY;
    });
    #paper {
      height: 500px;
      width: 500px;
      border: 1px solid black;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.8/raphael.min.js"></script>
    <div id="content">
      <div id="paper"></div>
    </div>
    <p id="position">Test</p>
    <p id="position_offset">Test</p>
    <h1 id="h1">hello world</h1>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多