Created
December 25, 2017 11:07
-
-
Save dushmis/40193cc5d214561ead08ae2d9e0af027 to your computer and use it in GitHub Desktop.
Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = window.innerWidth, | |
height = window.innerHeight; | |
var nodes = d3.range(100).map(function () { return { radius: Math.random() * 15 + 10 }; }), | |
root = nodes[0]; | |
root.radius = 0; | |
root.fixed = true; | |
var force = d3.layout.force() | |
.gravity(0.05) | |
.charge(function (d, i) { return i ? 0 : -2000; }) | |
.nodes(nodes) | |
.size([width, height]); | |
force.start(); | |
var canvas = d3.select("body").append("canvas") | |
.attr("width", width) | |
.attr("height", height); | |
var context = canvas.node().getContext("2d"); | |
force.on("tick", function (e) { | |
var q = d3.geom.quadtree(nodes), | |
i, | |
d, | |
n = nodes.length; | |
for (i = 1; i < n; ++i) q.visit(collide(nodes[i])); | |
context.clearRect(0, 0, width, height); | |
context.fillStyle = "red"; | |
context.beginPath(); | |
for (i = 1; i < n; ++i) { | |
d = nodes[i]; | |
context.moveTo(d.x, d.y); | |
context.arc(d.x, d.y, d.radius, 0, 2 * Math.PI); | |
} | |
context.fill(); | |
}); | |
canvas.on("mousemove", function () { | |
var p1 = d3.mouse(this); | |
root.px = p1[0]; | |
root.py = p1[1]; | |
force.resume(); | |
}); | |
function collide(node) { | |
var r = node.radius + 16, | |
nx1 = node.x - r, | |
nx2 = node.x + r, | |
ny1 = node.y - r, | |
ny2 = node.y + r; | |
return function (quad, x1, y1, x2, y2) { | |
if (quad.point && (quad.point !== node)) { | |
var x = node.x - quad.point.x, | |
y = node.y - quad.point.y, | |
l = Math.sqrt(x * x + y * y), | |
r = node.radius + quad.point.radius; | |
if (l < r) { | |
l = (l - r) / l * .5; | |
node.x -= x *= l; | |
node.y -= y *= l; | |
quad.point.x += x; | |
quad.point.y += y; | |
} | |
} | |
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1; | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment