addEventListener在Canvas标记中 [英] addEventListener in Canvas tag
问题描述
我有一个canvas标签来创建图形。在我的行的每个部分,我有一个点来启用鼠标悬停并显示更多细节。
I have a canvas tag to create a graphic. At each section of my line, I have a "dot" to enable a 'mouseover' and display more detail.
当我不添加事件侦听器到此点。
Everything works fine when I don't add an event listener to this dot.
Firebug警告我:s.addEventListener不是函数...
Firebug is warning me : s.addEventListener is not a function...
是否可以创建动态事件侦听器? (我是新的Javascript)
Is it possible to create dynamic event listener? (I'm new to Javascript)
推荐答案
不能将DOM事件附加到DOM对象(元素)之外的东西。 canvas
是一个DOM元素,你正在绘制到画布上的东西不是。他们成为画布的一部分作为img的像素。
You cannot attach DOM events to things other than DOM objects (elements). The canvas
is a DOM element, the things you are drawing to the canvas are not. They become a part of the canvas as pixels of an img.
为了检测对画布上的特定点的点击,您必须在画布上附加点击事件元素,然后将点击事件的x / y坐标与画布的坐标进行比较。
In order to detect a click on a specific point on your canvas you must attach the click event on the canvas element, and then compare the x/y coordinates of the click event with the coordinates of your canvas.
这篇关于addEventListener在Canvas标记中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!