如何获取鼠标点击画布元素的坐标? [英] How do I get the coordinates of a mouse click on a canvas element?
本文介绍了如何获取鼠标点击画布元素的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将点击事件处理程序添加到canvas元素,并返回点击的x和y坐标(相对于canvas元素)最简单的方法是什么?
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)?
推荐答案
如前所述在这里:
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
}
else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
x -= gCanvasElement.offsetLeft;
y -= gCanvasElement.offsetTop;
对我来说完全正常。
这篇关于如何获取鼠标点击画布元素的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文