Last active
August 29, 2015 14:22
-
-
Save nivas8292/a8adc5bd1ae9cdf72a85 to your computer and use it in GitHub Desktop.
Force Layout - Positive Charge (Attract each other)
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
<html> | |
<head> | |
<title>D3 Particle System</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
</head> | |
<body> | |
<script> | |
var w = '100%'; | |
var h = '100%'; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("id", "chart") | |
.attr("height", h) | |
.attr("width", w); | |
var colorScale = d3.scale.category20(); | |
var force = d3.layout.force() | |
.size([w, h]) | |
.charge(10) | |
.gravity(0) | |
.on("tick", tick); | |
function tick() { | |
svg.selectAll("circle").attr("cx", function (d) { | |
return d.x; | |
}) | |
.attr("cy", function (d) { | |
return d.y; | |
}); | |
} | |
var prevNode; | |
svg.on("mousemove", function () { | |
node = {x: d3.event.x, y: d3.event.y}; | |
node.px = prevNode != undefined ? prevNode.x : node.x; | |
node.py = prevNode != undefined ? prevNode.y : node.y; | |
prevNode = node; | |
svg.append("circle") | |
.data([node]) | |
.attr("cx", node.x) | |
.attr("cy", node.y) | |
.attr("r", 5) | |
.attr("fill", colorScale(node.x + node.y)) | |
.transition() | |
.delay(2000) | |
.attr("r", 0) | |
.each("end", function () { | |
force.nodes().shift(); | |
}) | |
.remove(); | |
force.nodes().push(node); | |
force.start(); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment