-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoomDots.html
81 lines (72 loc) · 2.06 KB
/
zoomDots.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
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
77
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Zooming Dots with D3</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script type="text/javascript">
var w = 1200;
var h = 800;
var numDots = 1000;
function randRGB(){
var color = "";
var rVal = Math.floor(Math.random()*255);
var gVal = Math.floor(Math.random()*255);
var bVal = Math.floor(Math.random()*255);
color = "rgb(" + rVal + "," + gVal + "," + bVal + ")";
return color;
}
var dots = [];
for (var i = 0; i < numDots; i++){
var dot = {};
dot.x = Math.floor(Math.random() * w);
dot.y = Math.floor(Math.random() * h);
dots.push(dot);
}
var svg = d3.select("body") // Creating SVG element on which to draw
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("circle")
.data(dots)
.enter()
.append("circle")
.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
})
.attr("r", function(){
return Math.floor(Math.random() * 20);
})
.attr("fill",function(){
return randRGB();
});
d3.select("svg")
.on("click",function(){
svg.selectAll("circle")
.transition().delay(function(d,i){
return i / dots.length * 200;
}).duration(1000)
.attr("cx",function(){
return Math.floor(Math.random() * w);
})
.attr("cy",function(){
return Math.floor(Math.random() * h);
})
.on("end",function(){
d3.select(this)
.transition()
.duration(1000)
.attr("r", function(){
return Math.floor(Math.random() * 20);
})
})
});
</script>
<p> Keep clicking! Click fast and watch them move towards the middle. </p>
</body>
</html>