[ Launch: Dragging a rectangle ] 5559807 by RobinL
[ Launch: Dragging a rectangle ] 5559579 by RobinL
-
-
Save RobinL/5559807 to your computer and use it in GitHub Desktop.
Dragging a rectangle,simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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