更改事件类型

发布于 2024-10-14 10:21:09 字数 680 浏览 1 评论 0原文

是否可以更改 JavaScript 中事件的类型

在这种情况下,我在画布上使用 jQuery 触发事件,但在呈现此画布的特定元素上触发事件,例如绘制的形状或图像。

mousemoveevent ---> [canvas] -> [find element] ---> mousemoveoverelementevent

基本上我在整个画布上捕获 onmousemove 事件:

$('canvas').bind('mousemove'........

但是画布内不存在 DOM,所以我希望它转换(在过程中稍后跟进)到:

$('canvas').bind('mousemoveOverElement'........
$('canvas').bind('mouseenterOnElement'........
$('canvas').bind('mouseleaveOnElement'........

然后分配类似

e.element = 'imageAtACertainPositionInTheCanvas'

I 更喜欢传递修改的事件而不是分配新的回调。

Is it possible to change the type of an event in JavaScript?

in this scenario I am triggering events with jQuery on a canvas, but on specific elements in rendered this canvas, like drawn shapes or images.

mousemoveevent ---> [canvas] -> [find element] ---> mousemoveoverelementevent

Basically I catch the onmousemove event over the complete canvas with:

$('canvas').bind('mousemove'........

but the DOM does not exist within the canvas so I want it to transform (to chatch up later in the process) to:

$('canvas').bind('mousemoveOverElement'........
$('canvas').bind('mouseenterOnElement'........
$('canvas').bind('mouseleaveOnElement'........

and then assign something like

e.element = 'imageAtACertainPositionInTheCanvas'

I prefer to pass the modified event instead of assigning a new callback.

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

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

发布评论

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

评论(4

迟到的我 2024-10-21 10:21:09

您可能想阅读Sprite 和 Canvas 的问题及其相对较差的性能。

You might want to read the issue of Sprite and Canvas and their relative bad performances too.

浅沫记忆 2024-10-21 10:21:09

我没听懂你说的,也许你能说得更具体一些。

我知道您可以更改自定义事件的类型。

// Custom mouse events
var myEvent = document.createEvent ("MouseEvents");

// Click
myEvent.initMouseEvent ("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

// Dispatch
link.dispatchEvent (myEvent);

// Changed the type
myEvent.initMouseEvent ("mousemove", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

// Dispatch
link.dispatchEvent (myEvent);

无论如何,这个链接可以帮助你:

DOM Level 3 Events

nsIDOMWindowUtils

I didn't understand what you said, maybe you could be more specific.

I know that you can change the type of a custom event.

// Custom mouse events
var myEvent = document.createEvent ("MouseEvents");

// Click
myEvent.initMouseEvent ("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

// Dispatch
link.dispatchEvent (myEvent);

// Changed the type
myEvent.initMouseEvent ("mousemove", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

// Dispatch
link.dispatchEvent (myEvent);

Anyway this links can help you:

DOM Level 3 Events

nsIDOMWindowUtils

方圜几里 2024-10-21 10:21:09

[示例]

var img = new Image();
img.src = "...";
var $img = $(img);

$img.mousemove(function(_, e){
  console.log(e.pageX);                    
});

$('canvas').mousemove(function(e){
  if (mouse_over_image(e.pageX, e.pageY)) {
    e.target = img;
    $img.trigger("mousemove", e);
  }
});

[Example]

var img = new Image();
img.src = "...";
var $img = $(img);

$img.mousemove(function(_, e){
  console.log(e.pageX);                    
});

$('canvas').mousemove(function(e){
  if (mouse_over_image(e.pageX, e.pageY)) {
    e.target = img;
    $img.trigger("mousemove", e);
  }
});
李不 2024-10-21 10:21:09

我相信您正在寻找的答案是“jQuery 自定义事件”。 jQuery 自定义事件使您能够命名和组织事件处理程序;然后,这些事件处理程序可以由 $(el).trigger('the_event') 触发。只需谷歌搜索该词的用法和示例即可。

我不认为它可以覆盖事件对象,就像您建议的 e.element 一样。您可能需要重新考虑流程以适应它。

(并不是说这个答案值得200声望,但无论如何,希望它对您找到方法有帮助。)

I believe the answer you are seeking for is 'jQuery custom event'. jQuery custom event provides you abilities to name and organize your event handlers; these event handlers can then be triggered by $(el).trigger('the_event'). Just google for the word for usage and example.

I don't it could overwrite the event object, like e.element you suggested though. You may need to rethink the flow to fit it.

(Not the that this answer worth 200 reputation but here it is anyway, hope that it's helpful for you to find a way.)

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