Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
HTML5 applications are event driven. You register event listeners with HTML elements and implement code that responds to those events. Nearly all Canvas-based applications handle either mouse or touch events—or both—and many applications also handle various events such as keystrokes and drag and drop.
Detecting mouse events in a canvas is simple enough: You add an event listener to the canvas, and the browser invokes that listener when the event occurs. For example, you can listen to mouse down events, like this:
canvas.onmousedown = function (e) {
// React to the mouse down event
};
Alternatively, you can use the more generic addEventListener() method:
canvas.addEventListener('mousedown', function (e) {
// React to the mouse down event
});