【发布时间】:2020-12-12 15:33:47
【问题描述】:
我不明白为什么 clientX 和 clientY 属性会偏移到它们的实际位置。 这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<p>The mouse is at</p><p id="mousecoords"> "hello" </p>
<canvas id="myCanvas" width="500" height="500" style="border:1px solid #000000;"></canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var coords = document.getElementById("mousecoords");
c.onmousemove = function(event){
var x = event.clientX;
var y = event.clientY;
coords.innerHTML= x + "," + y;
ctx.beginPath();
ctx.arc(x, y, 2, 0, Math.PI * 2, true);
ctx.fill();
};
</script>
</body>
</html>
【问题讨论】:
标签: javascript html onmousemove