确保每次鼠标按下时都会收到鼠标按下事件

发布于 2025-01-05 06:56:37 字数 1531 浏览 4 评论 0原文

下面的代码在按下鼠标按钮后每次鼠标移动时都会绘制一个矩形。当用户释放鼠标时,它应该停止绘制。

我试图弄清楚如果用户在画布元素之外释放鼠标,如何确保绘图停止。

我认为我可以通过在 onmousedown 内的事件上设置 onmouseup 事件处理程序来实现此目的,如下所示: <代码>

canvas.onmousedown = function (e) {
  drawing = true;
  e.onmouseup = function (v) { drawing = false; }
};
but it did not work because e.onmouseup is never called. So I ended up setting window.onmouseup instead.

问题:

  1. 为什么我的 e.onmouseup 从未被调用过?
  2. 我的最终解决方案是最好的方法吗?

<代码>

<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script type="text/javascript">
      drawing = false;
function on_load(e) { var canvas = document.getElementById("canvas");
canvas.onmousedown = function (e) { drawing = true; };
canvas.onmousemove = function (e) { if (drawing) { var x = e.pageX - canvas.offsetLeft; var y = e.pageY - canvas.offsetTop; var context = canvas.getContext("2d"); context.strokeRect(x, y, 10, 10); } };
window.onmouseup = function (e) { drawing = false; }; } </script> </head> <body onload="on_load()"> <canvas id="canvas" width="500" height="500" style="border: 1px solid black;"> </canvas> </body> </html>

The code below draws a rectangle on every mouse move after the mouse button is pressed. When the user releases the mouse it should stop drawing.

I am trying to figure out how to make sure that drawing stops if the user releases the mouse outside the canvas element.

I thought that I could accomplish this by setting onmouseup event handler on my event inside onmousedown like this:

canvas.onmousedown = function (e) {
  drawing = true;
  e.onmouseup = function (v) { drawing = false; }
};


but it did not work because e.onmouseup is never called. So I ended up setting window.onmouseup instead.

Questions:

  1. why was my e.onmouseup never called?
  2. is my final solution the best way to do it?

<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script type="text/javascript">
      drawing = false;
function on_load(e) { var canvas = document.getElementById("canvas");
canvas.onmousedown = function (e) { drawing = true; };
canvas.onmousemove = function (e) { if (drawing) { var x = e.pageX - canvas.offsetLeft; var y = e.pageY - canvas.offsetTop; var context = canvas.getContext("2d"); context.strokeRect(x, y, 10, 10); } };
window.onmouseup = function (e) { drawing = false; }; } </script> </head> <body onload="on_load()"> <canvas id="canvas" width="500" height="500" style="border: 1px solid black;"> </canvas> </body> </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2025-01-12 06:56:37
  1. 为什么我的 e.onmouseup 从未被调用?

e 是一个 Event 对象。它没有为 onmouseup 属性定义行为

  2.我的最终解决方案是最好的方法吗?

是的,但有一些调整。首先,考虑您是否真的需要全局变量。无需全局变量也可以实现相同的效果,但出于调试目的将其保持为全局可能有用。

其次,不需要您的不工作代码。最好拥有一个始终存在的事件侦听器,该侦听器仅更改无害的变量,而不是为每个 mouseup 事件构造一个新的事件侦听器。此外,在您的“想要的”代码中,之前的 mouseup 事件永远不会被显式删除。

 function on_load(e) {
    var drawing = false; // <-- Always declare variables
    var canvas = document.getElementById("canvas");
    canvas.onmousedown = function (e) { drawing = true; };
    canvas.onmousemove = function (e) {
      if (drawing) {
        var x = e.pageX - canvas.offsetLeft;
        var y = e.pageY - canvas.offsetTop;
        var context = canvas.getContext("2d");
        context.strokeRect(x, y, 10, 10);
      }
    };
    window.onmouseup = function (e) { drawing = false; };
}
  1. Why was my e.onmouseup never called?

e is an Event object. It doesn't have a defined behaviour for an onmouseup property

  2. Is my final solution the best way to do it?

Yes, but with some adjustments. First, consider if you really need the global variable. The same effect can be achieved without the global variable, but it might useful to keep it global for debugging purposes.

Second, your not-working code is not needed. It's better to have one always-existing event listener which only changes a harmless variable, than constructing a new event listener for each mouseup event. Besides, in your "wanted" code, the previous mouseup event is never explicitly removed.

 function on_load(e) {
    var drawing = false; // <-- Always declare variables
    var canvas = document.getElementById("canvas");
    canvas.onmousedown = function (e) { drawing = true; };
    canvas.onmousemove = function (e) {
      if (drawing) {
        var x = e.pageX - canvas.offsetLeft;
        var y = e.pageY - canvas.offsetTop;
        var context = canvas.getContext("2d");
        context.strokeRect(x, y, 10, 10);
      }
    };
    window.onmouseup = function (e) { drawing = false; };
}
放赐 2025-01-12 06:56:37

您可能需要考虑使用 onmouseout 处理程序来在鼠标离开元素时停止绘制。

You may want to consider an onmouseout handler to stop drawing when the mouse leaves your element.

空城旧梦 2025-01-12 06:56:37

操作系统如何保证某些小部件中的平衡鼠标向下/向上。那么,如果您让它在操作系统/浏览器“X”上工作,您如何知道操作系统/浏览器“i”或操作系统/浏览器“W”都会以相同的方式工作?有很多原因导致您需要做其他事情,甚至是计时器。与您想要做的事情相比,计时器的开销可能会很小。

How can an OS guarantee anything like a balanced mouse down/up in some widget. Then if you get it working on OS/browser 'X' how do you know that OS/browser 'i' or OS/browser 'W' will all work the same way? There are a lot of reasons why you need to do something else, like even a timer. Likely the overhead of a timer would be small compared to what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文