I use the following script in order to get a list of overlapping DIV elements at mouse click coordinates.
In this example script works fine if the DIV are not rotated:
http://jsfiddle.net/eyxt2tt1/2/
If I apply a rotation
on a DIV and user click (near red mark in this example)
http://jsfiddle.net/eyxt2tt1/3/
The script will always return two DIV when instead it should consider only one.
I need to know:
- How to consider the rotation in my calculation?
- Do you know a better approach/alternatives?
Notes:
- I cannot use Document.elementFromPoint()
- They are similar question on SO, but I consider this one different as I need to solve the "rotation issue" in particular.
- I would appreciate your ideas in the jsfiddle
$(document).click(function (e) {
var hitElements = getHitElements(e);
var output = $('#output');
output.html('');
for (var i = 0; i < hitElements.length; ++i) {
output.html(output.html() + '<br />' + hitElements[i][0].tagName + ' ' + hitElements[i][0].id);
};
});
var getHitElements = function (e) {
var x = e.pageX;
var y = e.pageY;
var hitElements = [];
$(':visible').each(function () {
console.log($(this).attr("id"), $(this).outerWidth());
var offset = $(this).offset();
console.log('+++++++++++++++++++++');
console.log('pageX: ' + x);
console.log('pageY: ' + y);
console.log($(this).attr("id"), $(this).offset());
console.log('+++++++++++++++++++++');
if (offset.left < x && (offset.left + $(this).outerWidth() > x) && (offset.top < y && (offset.top + $(this).outerHeight() > y))) {
console.log('included: ', $(this).attr("id"));
console.log('from 0p far x: ', $(this).attr("id"), offset.left + $(this).outerWidth());
console.log('from 0p far y: ', $(this).attr("id"), offset.top + $(this).outerHeight());
hitElements.push($(this));
}
});
return hitElements;
}