Advertisement
Guest User

Untitled

a guest
May 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <body>
  4. <script src="//d3js.org/d3.v3.min.js"></script>
  5. <script>
  6.  
  7. var width = 960,
  8. height = 500;
  9.  
  10. var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4}; }),
  11. root = nodes[0]
  12. color = d3.scale.ordinal()
  13. .domain([1,2,3])
  14. .range(['#103138','#324452','#ad3000','#3a6575','#ae6e62','#e79353']);
  15.  
  16. root.radius = 0;
  17. root.fixed = true;
  18.  
  19. var force = d3.layout.force()
  20. .gravity(0.1)
  21. .charge(function(d, i) { return i ? 0 : -4000; })
  22. .friction(0.95)
  23. .nodes(nodes)
  24. .size([width, height]);
  25.  
  26. force.start();
  27.  
  28. var svg = d3.select("body").style('background-color','#091113')
  29. .append("svg")
  30. .attr("width", width)
  31. .attr("height", height)
  32. .append("g")
  33. .style("filter", "url(#gooey)") //Set the filter on the container svg
  34.  
  35. var defs = svg.append('defs');
  36. var filter = defs.append('filter').attr('id','gooey')
  37. filter.append('feGaussianBlur')
  38. .attr('in','SourceGraphic')
  39. .attr('stdDeviation','10')
  40. .attr('result','blur');
  41. filter.append('feColorMatrix')
  42. .attr('in','blur')
  43. .attr('mode','matrix')
  44. .attr('values','1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9')
  45. .attr('result','gooey');
  46.  
  47.  
  48. svg.selectAll("circle")
  49. .data(nodes.slice(1))
  50. .enter().append("circle")
  51. .attr("r", function(d) { return d.radius; })
  52. .style("fill", function(d, i) { return color(i % 7); });
  53.  
  54. force.on("tick", function(e) {
  55. var q = d3.geom.quadtree(nodes),
  56. i = 0,
  57. n = nodes.length;
  58.  
  59. while (++i < n) q.visit(collide(nodes[i]));
  60.  
  61. svg.selectAll("circle")
  62. .attr("cx", function(d) { return d.x; })
  63. .attr("cy", function(d) { return d.y; });
  64. });
  65.  
  66. svg.on("mousemove", function() {
  67. var p1 = d3.mouse(this);
  68. root.px = p1[0];
  69. root.py = p1[1];
  70. force.resume();
  71. });
  72.  
  73. function collide(node) {
  74. var r = node.radius + 16,
  75. nx1 = node.x - r,
  76. nx2 = node.x + r,
  77. ny1 = node.y - r,
  78. ny2 = node.y + r;
  79. return function(quad, x1, y1, x2, y2) {
  80. if (quad.point && (quad.point !== node)) {
  81. var x = node.x - quad.point.x,
  82. y = node.y - quad.point.y,
  83. l = Math.sqrt(x * x + y * y),
  84. r = node.radius + quad.point.radius;
  85. if (l < r) {
  86. l = (l - r) / l * .5;
  87. node.x -= x *= l;
  88. node.y -= y *= l;
  89. quad.point.x += x;
  90. quad.point.y += y;
  91. }
  92. }
  93. return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
  94. };
  95. }
  96.  
  97. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement