鼠标坐标似乎错误地放置了 Raphael.js 个对象

Mouse coordinates appears to incorrectly place Raphael.js objects

我有一些拉斐尔的直角画 space。我设置了快速鼠标坐标显示,用于精确放置对象:

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

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

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

            });

但是,矩形似乎比预期的位置大了大约 10 像素。为什么会这样? (我也知道使用 style 而不是 css 文件是不好的做法,但我只是想直接跳到玩拉斐尔而不用担心)

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");

配置它

住link这里有问题:https://jsfiddle.net/oLwydha6/

我该如何解决这个问题?

总的差异是 9px,正文边距 (8px) 和边框 (1px).您可能要考虑使用 offsetX instead of 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>