Events in elements within a canvas

前端 未结 3 1610
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 13:37

I\'ve draw an image inside a canvas tag. How could I add events to it? I want to do something when user clicks in that image.

character.addEventListener(\'cl         


        
3条回答
  •  独厮守ぢ
    2021-01-15 14:18

    There's no way to add event handlers to actual drawings on the canvas. You can handle events for the entire canvas - that's it.

    Your options at this point is to either add an abstraction over the canvas, and lookup drawings you have there based on coordinates from canvas click events - or to drop canvas altogether and go for e.g. svg. The Raphaël library (http://raphaeljs.com/) can help you with the latter.

提交回复
热议问题