Skip to content

Instantly share code, notes, and snippets.

@RobinL
Created May 11, 2013 12:13
Show Gist options
  • Save RobinL/5559807 to your computer and use it in GitHub Desktop.
Save RobinL/5559807 to your computer and use it in GitHub Desktop.
Dragging a rectangle,simple
{"description":"Dragging a rectangle,simple","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":true,"loop":false,"restart":true,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
//The starting location of the block
blocks = [
{ x: 0, y: 0 },
{ x: 100, y: 100 }
];
drag1 = d3.behavior.drag()
.origin(Object)
.on("drag", function(d) {
//Update the data which is bound to the object
d.x = d3.event.x;
d.y = d3.event.y;
redraw1();
//Note that the 'this' context in here is the object that has been dragged
});
svg = d3.select("svg");
g = svg.selectAll(".rect1")
.data(blocks);
gEnter = g.enter().append("rect")
.attr("x", function(d) {return d.x})
.attr("y", function(d) {return d.y})
.attr("height", 100)
.attr("width", 100)
.attr("fill","#003399")
.attr("class", "rect1")
.call(drag1); //Attach the drag behaviour to the object
function redraw1() {
var a =svg.selectAll(".rect1");
a.attr("x", function(d) {return d.x})
.attr("y", function(d) {return d.y})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment