Skip to content

Instantly share code, notes, and snippets.

@kylenstone
Forked from mbostock/.block
Created March 2, 2020 02:45
Show Gist options
  • Save kylenstone/e85bd31a94b4e50e0961edb70a18c95d to your computer and use it in GitHub Desktop.
Save kylenstone/e85bd31a94b4e50e0961edb70a18c95d to your computer and use it in GitHub Desktop.
Force-Directed Symbols
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var w = 960,
h = 500,
nodes = [],
node;
var vis = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
var force = d3.layout.force()
.nodes(nodes)
.links([])
.size([w, h]);
force.on("tick", function(e) {
vis.selectAll("path")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
setInterval(function(){
// Add a new random shape.
nodes.push({
type: d3.svg.symbolTypes[~~(Math.random() * d3.svg.symbolTypes.length)],
size: Math.random() * 300 + 100
});
// Restart the layout.
force.start();
vis.selectAll("path")
.data(nodes)
.enter().append("path")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.attr("d", d3.svg.symbol()
.size(function(d) { return d.size; })
.type(function(d) { return d.type; }))
.style("fill", "steelblue")
.style("stroke", "white")
.style("stroke-width", "1.5px")
.call(force.drag);
}, 1000);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment