-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbles.js
More file actions
76 lines (63 loc) · 1.93 KB
/
Copy pathBubbles.js
File metadata and controls
76 lines (63 loc) · 1.93 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var Bubbles = module.exports = {};
Bubbles.d3 = function () {
console.log('IM D3!');
var d3 = require("d3")
var nodes = d3.range(7).map(function(i) { return { radius: Math.random() * 12 + 4} })
var color = d3.scale.category10();
var cy = 25;
var width = 1500,
height = 500;
var drag = d3.behavior.drag()
.origin(function() {
var t = d3.select(this);
return {
x: t.attr("x") + d3.transform(t.attr("transform")).translate[0],
y: t.attr("y") + d3.transform(t.attr("transform")).translate[1]};
})
.on("drag", dragmove)
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height)
.on("click", click);
var circles = svg.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("cy", cy)
.attr("cx", function(d, i) { return i * 100 + 30})
.attr("r", 20)
.style("fill", function(d, i) { return color(i); })
.call(drag);
function dragmove(d) {
var x = d3.event.x;
var y = d3.event.y;
d3.select(this)
.attr("transform", "translate(" + x + "," + y + ")")
d3.select(this).transition()
.ease("elastic")
.duration(500)
.attr("r", 32);
}
function click(){
if (d3.event.defaultPrevented) return;
var point = d3.mouse(this)
p = {x: point[0], y: point[1] };
svg.append("circle")
.attr("transform", "translate(" + p.x + "," + p.y + ")")
.attr("r", "10")
.style("fill", function(d, i) { return color(i); })
.call(drag);
}
var click = 0;
document.getElementById('button').addEventListener('click', function(){
click += 1;
if (click % 2 === 0) {
circles.transition()
.attr("cy", cy-=200).duration(1000)
.attr("r", function(d) { return d.radius; })
} else {
circles.transition()
.attr("cy", cy+=200).duration(1000)
.attr("r", function(d) { return d.radius; })
}
})
}