Skip to content

Instantly share code, notes, and snippets.

@bstaats
Forked from mbostock/.block
Created January 16, 2012 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstaats/1622327 to your computer and use it in GitHub Desktop.
Save bstaats/1622327 to your computer and use it in GitHub Desktop.
Shall we play a game?

#Shall we play a game?#

This is to illustrate (mainly troubleshoot) rapidly animating thousands of data points with chained transitions. The goal is to recreate a similar effect as the computer animation of Joshua playing thermonuclear war. Example is around the 2min mark

Winner None

###Current problems###

  1. Transitions get out of sync
  2. Cant scale to 1000 data points

Both of these are likely due to the easing. However, without the easing they dont look much like explosions. Alternative?

###TODO:###

  • Use real lat/lon coordinates
  • Add the animating trajectories (from point a to b) as in the movie.

Built with D3.js

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Shall we play a game?</title>
<script type="text/javascript" src="https://github.com/mbostock/d3/raw/v2.7.1/d3.js"></script>
<script type="text/javascript" src="https://github.com/simplegeo/polymaps/raw/v2.5.1/polymaps.js"></script>
<style type="text/css">
body {
background: #222;
}
circle {
fill: none;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script type="text/javascript">
var w = 960,
h = 500,
z = d3.scale.category20c(),
n = 100,
duration = 1000 * 60 * 1, // 2min
po = org.polymaps,
loci = d3.range(n).map(function() {
return {
x: Math.random() * w,
y: Math.random() * h,
count: Math.random() * 21
};
});
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var map = po.map()
.container(svg.node())
.zoom(1.9)
.center({lat: 0, lon: 0})
.add(po.interact())
.add(po.hash());
map.add(po.image()
.url(po.url("http://{S}tile.cloudmade.com"
+ "/1a1b06b230af4efdbb989ea99e9841af"
+ "/33025/256/{Z}/{X}/{Y}.png")
.hosts(["a.", "b.", "c.", ""])));
var layer = svg.append('g')
.attr('id','data-layer');
layer.selectAll('circle')
.data(loci)
.enter().append('circle')
.attr("cx", function(d){return d.x})
.attr("cy", function(d){return d.y})
.attr("r", function(d){return d.count*10})
.style("fill", '#ffffff')
.style("stroke", '#ffffff')
.style("fill-opacity", 0)
.style("stroke-opacity", 0)
.transition()
.duration(0)
.delay(function(d,i){return i*(duration/n)})
.style("fill-opacity", 1)
.style("stroke-opacity", 1)
.transition()
.duration(1000)
.ease('circle')
.delay(function(d,i){return i*(duration/n)})
.style("fill", function(d,i){return z(i);})
.style("stroke", function(d,i){return z(i);})
.transition()
.duration(1000)
.delay(function(d,i){return i*(duration/n)})
.ease('circle')
.style("fill-opacity", 0)
.attr("r", 1)
.transition()
.duration(2000)
.delay(function(d,i){return 1000+(i*(duration/n))})
.ease(Math.sqrt)
.style("fill-opacity", 1)
.transition()
.duration(2000)
.delay(function(d,i){return 2000+(i*(duration/n))})
.ease(Math.sqrt)
.style("stroke-opacity", 0)
.transition()
.duration(4000)
.delay(function(d,i){return 1000+(i*(duration/n))})
.ease(Math.sqrt)
.style("fill-opacity", 0)
.attr("r", function(d){return d.count*10})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment