Created
September 6, 2011 14:44
-
-
Save biovisualize/1197731 to your computer and use it in GitHub Desktop.
Drag and drop with D3
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script> | |
<title>Drag And Drop</title> | |
</head> | |
<body> | |
<div id="viz"></div> | |
<script type="text/javascript"> | |
var vizSVG = d3.select("#viz") | |
.append("svg:svg") | |
.attr("width", 400) | |
.attr("height", 300); | |
vizSVG.append("svg:circle") | |
.attr("id", "blueCircle") | |
.attr("cx", 50) | |
.attr("cy", 140) | |
.attr("r", 40) | |
.attr("fill", "blue") | |
.call(d3.behavior.drag().on("drag", move)); | |
vizSVG.append("svg:circle") | |
.attr("id", "redCircle") | |
.attr("cx", 100) | |
.attr("cy", 140) | |
.attr("r", 40) | |
.attr("fill", "red") | |
.call(d3.behavior.drag().on("drag", move)); | |
function move(){ | |
this.parentNode.appendChild(this); | |
var dragTarget = d3.select(this); | |
dragTarget | |
.attr("cx", function(){return d3.event.dx + parseInt(dragTarget.attr("cx"))}) | |
.attr("cy", function(){return d3.event.dy + parseInt(dragTarget.attr("cy"))}); | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was left as an exercise ;) Sorry I wrote this code 2 years ago when I was learning. I would be glad to help if you need, on the D3 Google Groups