I have a graph that allows for zooming in and out on different nodes. To aid in selecting multiple nodes, I have integrated a drag-selection box shown here: http://bl.ocks.org/lgersman/5370827. This works perfectly when the user has not zoomed out of the graph, but once they do, everything becomes skewed.
I know that I need to take the scale into account, but I am not sure to properly factoring it in. The scale value goes from 1 (no zoom) to .1 (zoomed all the way out). Below is the snippet where the scale adjustment should go. I have tried multiplying it, adding it to both the size of the box and the X/Y, but have not identified the best spot.
var d = {
x : parseInt(s.attr("x"), 10),
y : parseInt(s.attr("y"), 10),
width : parseInt(s.attr("width"), 10),
height : parseInt(s.attr("height"), 10)
},
move = {
x : p[0] - d.x,
y : p[1] - d.y
}
;
if( move.x < 1 || (move.x*2<d.width)) { //dragging to the left
d.x = p[0];
d.width -= move.x;
} else { //dragging to the right
d.width = move.x;
}
if( move.y < 1 || (move.y*2<d.height)) { //dragging up
d.y = p[1];
d.height -= move.y;
} else { //dragging down
d.height = move.y;
}
s.attr(d);