jquery 鼠标在画布中的位置,以及 DOM 中的活动元素?
我的问题是我有一个色谱画布,并且 onclick 它将单击的颜色分配给页面的各个部分。当用户单击画布时,我会得到这样的位置。
$('#colorPicker').click(function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var canvas1 = colorPicker.getContext('2d');
var data = canvas1.getImageData(x, y, 1, 1).data;
var rgb = 'rgb(' + data[0] + ',' + data[1] + ',' + data[2] + ')';
$('a').css({color: rgb});
}).disableSelection();
唯一的问题是我的画布位于 jquery 手风琴中,因此当手风琴发生变化时,画布位置也会发生变化,从而使附加到画布的单击处理程序的坐标出现很大变化。
我尝试向单击添加实时事件,但这没有改变任何内容,如何获取画布内指针的正确坐标,是否有获取 x 和 y 值的实时方法?
My problem is I have a canvas of a colour spectrum and onclick it assigns the color clicked to various parts of the page. When the user clicks the canvas I get the position like so.
$('#colorPicker').click(function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var canvas1 = colorPicker.getContext('2d');
var data = canvas1.getImageData(x, y, 1, 1).data;
var rgb = 'rgb(' + data[0] + ',' + data[1] + ',' + data[2] + ')';
$('a').css({color: rgb});
}).disableSelection();
Only problem being that my canvas is in a jquery accordion, so the canvas position changes when the accordion changes, making my co-ordinates for the click handler attached to the canvas out by quite alot.
I tried adding a live event to the click but that changed nothing, how can I get the correct coordinates for the pointer within the canvas, is there a live way of getting the x and y values??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取相对于
的点击位置(假设
#colorPicker
是):
To get the click position relative to the
<canvas>
(assuming#colorPicker
is the<canvas>
):