diff --git a/srcjs/output_binding_image.js b/srcjs/output_binding_image.js index e5f440f8f..bca921331 100644 --- a/srcjs/output_binding_image.js +++ b/srcjs/output_binding_image.js @@ -312,18 +312,55 @@ imageutils.initCoordmap = function($el, coordmap) { return newvals; }; - // Given an offset, return an object representing which panel it's in. + // Given an offset, return an object representing which panel it's in. The + // `expand` argument tells it to expand the panel area by that many pixels. + // It's possible for an offset to be within more than one panel, because + // of the `expand` value. If that's the case, find the nearest panel. coordmap.getPanel = function(offset, expand) { expand = expand || 0; - var bounds; - for (var i=0; i= bounds.left - expand && - offset.y <= bounds.bottom + expand && - offset.y >= bounds.top - expand) { - return coordmap[i]; + var x = offset.x; + var y = offset.y; + + var matches = []; // Panels that match + var dists = []; // Distance of offset to each matching panel + var b; + for (var i=0; i= b.left - expand && + y <= b.bottom + expand && + y >= b.top - expand) + { + matches.push(coordmap[i]); + + // Find distance from edges for x and y + var xdist = 0; + var ydist = 0; + if (x > b.right && x <= b.right + expand) { + xdist = x - b.right; + } else if (x < b.left && x >= b.left - expand) { + xdist = x - b.left; + } + if (y > b.bottom && y <= b.bottom + expand) { + ydist = y - b.bottom; + } else if (y < b.top && y >= b.top - expand) { + ydist = y - b.top; + } + + // Cartesian distance + dists.push(Math.sqrt( Math.pow(xdist, 2) + Math.pow(ydist, 2) )); + } + } + + if (matches.length) { + // Find shortest distance + var min_dist = Math.min.apply(null, dists); + for (i=0; i