Here is a way it could be solved with some additional JS:
$('.draggable').draggable({
drag: function (aEvent, aUi) {
$(".droppable").each(function() {
var myX = aUi.offset.left;
var myY = aUi.offset.top;
var myR = $(this).outerWidth() / 2;
var myCX = $(this).offset().left + myR;
var myCY = $(this).offset().top + myR;
if (checkIntersection(myX, myY, myCX, myCY, myR))
{
$(this).addClass('touched');
}
});
}
});
$('.droppable').droppable({
tolerance: 'touch'
});
function checkIntersection(aX, aY, aCX, aCY, aR) {
return (Math.pow(aX - aCX, 2) + Math.pow(aY - aCY, 2) < Math.pow(aR, 2));
}
So here is what I did:
I moved the function to the
drag
event of thedraggable
as this gets called over and over and so we can constantly check if we are now in the circle.There is an
.each()
loop as you now have to check everydroppable
in turn.I am checking whether or not the x and y coordinates of the
draggable
overlap with thedroppable
. This is done in the functioncheckIntersection
. For a full explanation of the algorithm used see Equation for testing if a point is inside a circle.
As a note, right now I am only checking the intersection with one of the corners of the little square, you would have to do this for all corners, to be fully precise.
Here is the fiddle: http://jsfiddle.net/V3Hkg/2/