Javascript 如何使用“AddEventListener”方法向 Canvas 元素添加双击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:52:31  来源:igfitidea点击:

How to add doubleclick event to Canvas element using the "AddEventListener" method?

javascripthtmlcanvasdom-events

提问by marcio

I'm just trying to add a double click event to a HTML5 Canvas element. It works fine with:

我只是想向 HTML5 Canvas 元素添加双击事件。它适用于:

myCanvas.ondbclick

However, I want to use the addEventListener method to do that. I guess it might be a simple task but I googled everywhere and could not find it. What's the name of the event I should be using?

但是,我想使用 addEventListener 方法来做到这一点。我想这可能是一项简单的任务,但我到处搜索,但找不到。我应该使用的事件名称是什么?

myCanvas.addEventListener('doubleclick?', function(){ 

  // Some dazzling stuff happens be here

});

Hope it's possible, don't wanted to "break" my coding consistency.

希望这是可能的,不想“破坏”我的编码一致性。

回答by Pablo Fernandez

The event name is dblclick:

事件名称是dblclick

myCanvas.addEventListener('dblclick', function(){ 

  // Some dazzling stuff happens be here

});

Also your first example is wrong, it should say:

你的第一个例子也是错误的,它应该说:

myCanvas.ondblclick