Last active
August 29, 2015 14:21
-
-
Save whityiu/fb9f138a4a91032fa9ec to your computer and use it in GitHub Desktop.
D3: Node Pulse
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 xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<title>D3: Force Layout</title> | |
<style> | |
.visual-area { | |
border: 2px solid #aa88aa; | |
} | |
</style> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script> | |
var width = 500, | |
height = 450, | |
radius = 12, | |
outlineColor = "green", | |
pulseLineColor = "#ffffff", | |
bgColor = "#45818e", | |
pulseAnimationIntervalId; | |
var nodesArray = [ | |
{ "x": 75, "y": 25 }, | |
{ "x": 200, "y": 55 }, | |
{ "x": 400, "y": 150 }, | |
{ "x": 200, "y": 300 }, | |
{ "x": 350, "y": 350 } | |
]; | |
// Set up the SVG display area | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("fill", bgColor) | |
.classed('visual-area', true); | |
var bgRect = d3.select("svg").append("rect") | |
.attr("width", width) | |
.attr("height", height); | |
var linkSet = null, | |
nodeSet = null; | |
// Create data object sets | |
nodeSet = svg.selectAll(".node").data(nodesArray) | |
.enter().append("g") | |
.attr("class", "node") | |
.on('click', function() { | |
// Clear the pulse animation | |
clearInterval(pulseAnimationIntervalId); | |
}); | |
// Draw outlines | |
nodeSet.append("circle") | |
.classed("outline pulse", true) | |
.attr("cx", function(d) { return d.x;}) | |
.attr("cy", function(d) { return d.y;}) | |
.attr("fill", "none") | |
.attr("stroke", pulseLineColor) | |
.attr("r", radius); | |
// Draw nodes on top of outlines | |
nodeSet.append("circle") | |
.attr("cx", function(d) { return d.x;}) | |
.attr("cy", function(d) { return d.y;}) | |
.attr("fill", "gray") | |
.attr("stroke", outlineColor) | |
.attr("r", radius); | |
// Set pulse animation on interval | |
pulseAnimationIntervalId = setInterval(function() { | |
var times = 5, | |
distance = 8, | |
duration = 600; | |
var outlines = svg.selectAll(".pulse"); | |
// Function to handle one pulse animation | |
function repeat(iteration) { | |
if (iteration < times) { | |
outlines.transition() | |
.duration(duration) | |
.each("start", function() { d3.select(".outline").attr("r", radius).attr("stroke", pulseLineColor); }) | |
.attrTween("r", function() { return d3.interpolate(radius, radius + distance); }) | |
.styleTween("stroke", function() { return d3.interpolate(pulseLineColor, bgColor); }) | |
.each("end", function() {repeat(iteration+1);}); | |
} | |
} | |
if (!outlines.empty()) { | |
repeat(0); | |
} | |
}, 6000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment