Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created January 24, 2013 03:59
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/4617523 to your computer and use it in GitHub Desktop.
Save enjalot/4617523 to your computer and use it in GitHub Desktop.
zoom and drag
{"description":"zoom and drag","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}},"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,"fullscreen":false,"thumbnail":"http://i.imgur.com/vtVA2FS.png"}
var svg = d3.select("svg");
var box = tributary.test;
//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
})
//here we access tributary's supplied data
.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;
box.x = x + translate[0];
box.y = y + translate[1];
//rect.attr(box);
updateBox();
})
svg.call(zoom);
//this saves the current value of the box variable
//into the test.json file
//this causes everything to reexcute and thus
//rerender the box with those dimension
function updateBox() {
var cm = tributary.getCodeEditor("test.json")
cm.setValue(JSON.stringify(box));
}
{"x":337.50517770036964,"y":234.9301214344293,"width":145.36659866795839,"height":132.07087318003533}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment