How do I get the coordinates of a mouse click on a canvas element?

后端 未结 22 2667
忘掉有多难
忘掉有多难 2020-11-21 23:56

What\'s the simplest way to add a click event handler to a canvas element that will return the x and y coordinates of the click (relative to the canvas element)?

No

22条回答
  •  轮回少年
    2020-11-22 00:42

    Hey, this is in dojo, just cause it's what I had the code in already for a project.

    It should be fairly Obvious how to convert it back to non dojo vanilla JavaScript.

      function onMouseClick(e) {
          var x = e.clientX;
          var y = e.clientY;
      }
      var canvas = dojo.byId(canvasId);
      dojo.connect(canvas,"click",onMouseClick);
    

    Hope that helps.

提交回复
热议问题