Illustrates how to detect when and where a mouse button is pressed.

javascript programming
exa_03_mousedown

AخA
 
1
<html>
2
<head>
3
<title>mousedown</title>
4
<script>
5
function pressFn(e) {
6
  // Note that "this" points to the HTML element
7
  console.log("button",e.which,"loc",e.pageX-this.offsetLeft,e.pageY-this.offsetTop);
8
}
9
function setup() {
10
  var canv = document.getElementById("canv");
11
  canv.addEventListener("mousedown",pressFn);
12
}
13
</script>
14
</head>
15
<body onload="setup();">
16
<h3>Open the console to see output!</h3>
17
<canvas id="canv" width="300" height="200" style="border:1px gray solid;"></canvas>
18
</body>
19
</html>