-
Notifications
You must be signed in to change notification settings - Fork 4
/
dragtest.html
43 lines (35 loc) · 1.06 KB
/
dragtest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
float: left;
border: solid 1px #aaa;
}
</style>
<script src="http://mbostock.github.com/d3/d3.js?3.3.3"></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) {
console.log(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>
</body>