This is an example of pan limiting, trapping for zoom.translate() by passing an array, which is determined by a simple boolean network for each of x and y of the translation to ensure that dragging is constrained within the panExtent.
It is a fork of Pan+Zoom demonstrating an example of d3.behavior.zoom applied using x- and y-scales.
Requirements
-
create an object variable
var panExtent = {x: [0,600], y: [-200,Infinity] };
to set the limits of the pan -
when setting the domain test for compliance with the panExtent (the domain should be no bigger than panExtent)
-
add function
panLimit()
to do the limiting Math which returns an array used byzoom.translate()
-
in the listener for the zoom (in this case the function
zoomed()
) set thezoom.translate(panLimit())
vector.
note: (fixed) I just noticed this only works effectively for the lower bound when the zoom.scale() is 1 or the lower Y bound is 0, I'll fix this soon.