Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created March 13, 2013 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/5148348 to your computer and use it in GitHub Desktop.
Save enjalot/5148348 to your computer and use it in GitHub Desktop.
limit zoom
{"description":"limit zoom","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"test.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/oaqk8dt.png"}
var svg = d3.select("svg");
var box = {
x: 200,
y: 200,
width: 200,
height: 200
};
//cache the values as starting points;
var x = box.x;
var y = box.y;
var bwidth = box.width;
var bheight = box.height;
var rect = svg.append("rect")
.attr({
fill:"#A6E6E9",
"fill-opacity": 0.5,
stroke: "#1F2D92",
"stroke-width":3,
rx: 5
})
.attr(box)
var zoom = d3.behavior.zoom()
.on("zoom", function() {
var translate = d3.event.translate;
var scale = d3.event.scale;
svg.select("text").text(translate + " " + scale)
box.width = scale * bwidth;
box.height = scale * bheight;
//limit x in left direction
var newx = x + translate[0];
if(newx < 100) {
newx = 100;
zoom.translate([0, 0])
}
box.x = newx;
box.y = y + translate[1];
rect.attr(box);
})
svg.call(zoom);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment