-
-
Save wimdows/1502762 to your computer and use it in GitHub Desktop.
Force-Directed Symbols (D3)
This file contains 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> | |
<html> | |
<head> | |
<title>Force-Directed Symbols</title> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.25.0"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js?1.25.0"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.25.0"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var w = 960, | |
h = 500, | |
nodes = [], | |
node; | |
var vis = d3.select("body").append("svg:svg") | |
.attr("width", w) | |
.attr("height", h); | |
createDefs(vis.append('svg:defs')); | |
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("svg: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", "rgb(" + Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + ")") | |
.style("stroke", "steelblue") | |
.style("stroke-width", "1.5px") | |
.attr('filter', 'url(#dropShadow)') | |
.call(force.drag); | |
}, 1000); | |
function createDefs(defs) { | |
var dropShadowFilter = defs.append('svg:filter') | |
.attr('id', 'dropShadow') | |
.attr('filterUnits', "userSpaceOnUse") | |
.attr('width', '250%') | |
.attr('height', '250%'); | |
dropShadowFilter.append('svg:feGaussianBlur') | |
.attr('in', 'SourceGraphic') | |
.attr('stdDeviation', 2) | |
.attr('result', 'blur-out'); | |
dropShadowFilter.append('svg:feColorMatrix') | |
.attr('in', 'blur-out') | |
.attr('type', 'hueRotate') | |
.attr('values', 180) | |
.attr('result', 'color-out'); | |
dropShadowFilter.append('svg:feOffset') | |
.attr('in', 'color-out') | |
.attr('dx', 3) | |
.attr('dy', 3) | |
.attr('result', 'the-shadow'); | |
dropShadowFilter.append('svg:feBlend') | |
.attr('in', 'SourceGraphic') | |
.attr('in2', 'the-shadow') | |
.attr('mode', 'normal'); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment