Skip to content

Instantly share code, notes, and snippets.

@GerHobbelt
Forked from mbostock/.block
Created July 16, 2012 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GerHobbelt/3125020 to your computer and use it in GitHub Desktop.
Save GerHobbelt/3125020 to your computer and use it in GitHub Desktop.
Drag Multiples
# Editor backup files
*.bak
*~
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
float: left;
border: solid 1px #aaa;
}
</style>
<script src="http://mbostock.github.com/d3/d3.js"></script>
<body>
<script>
var width = 238,
height = 123,
radius = 20;
var drag = d3.behavior.drag()
.origin(Object)
.on("drag", dragmove);
var svg = d3.select("body").selectAll("svg")
.data(d3.range(16).map(function() { return {x: width / 2, y: height / 2}; }))
.enter().append("svg")
.attr("width", width)
.attr("height", height);
svg.append("circle")
.attr("r", radius)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(drag);
function dragmove(d) {
d3.select(this)
.attr("cx", d.x = Math.max(radius, Math.min(width - radius, d3.event.x)))
.attr("cy", d.y = Math.max(radius, Math.min(height - radius, d3.event.y)));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment