【发布时间】:2013-05-20 13:41:47
【问题描述】:
我目前有一个检测鼠标进入方向的功能。它为每个部分返回一个整数 (0-3),如下所示。
我正在尝试弄清楚如何更改此函数以根据此插图生成输出:
基本上,我只是想弄清楚用户是从左侧输入图像还是从右侧输入图像。我已经尝试过几次尝试和错误,但我不太擅长这种类型的数学。
我以JSFiddle with a console output 为例。
判断方向的函数是:
function determineDirection($el, pos){
var w = $el.width(),
h = $el.height(),
x = (pos.x - $el.offset().left - (w/2)) * (w > h ? (h/w) : 1),
y = (pos.y - $el.offset().top - (h/2)) * (h > w ? (w/h) : 1);
return Math.round((((Math.atan2(y,x) * (180/Math.PI)) + 180)) / 90 + 3) % 4;
}
其中pos 是一个对象:{x: e.pageX, y: e.pageY},$el 是包含目标元素的 jQuery 对象。
【问题讨论】:
标签: javascript math angle