srobbin/jquery-approach — Issue #7: Approach box rather than centre point
Author: fgpsmith
Created: 2011-11-15 14:42:21+00:00
State: open

I was playing with approach to create a "fisheye" style text menu. However because the animation is based on distance from the centre of an object moving the mouse over the start or end of the text in the menus didn't cause much animation because with medium length text strings it was too far from the centre.

I have hacked the source to calculate distance from the object box rather than the centre (inside the box counts as a distance of zero) and thought that it was an option you might want to consider for future versions.

My distance code has not be optimised in anyway (as it should be for repeated calling) but it works for what I needed in the short term

function getDist(obj, x, y) {
var offset = $(obj).offset();
minX = offset.left;
maxX = minX + $(obj).width();
minY = offset.top;
maxY = minY + $(obj).height();
var dX2
var dY2
if (x >= minX && x <= maxX) {
dX2 = 0;
} else {
d1 = minX - x;
d2 = maxX - x;
dX2 = Math.min( d1*d1, d2*d2);
}
if (y >= minY && y <= maxY) {
dY2 = 0;
} else {
d1 = minY - y;
d2 = maxY - y;
dY2 = Math.min( d1*d1, d2*d2);
}
return parseInt(Math.sqrt(dX2 + dY2));
}