Skip to content

Instantly share code, notes, and snippets.

@chrisprice
Forked from ColinEberhardt/README.md
Last active January 27, 2016 22:52
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 chrisprice/61d6460ff53eea60ac0c to your computer and use it in GitHub Desktop.
Save chrisprice/61d6460ff53eea60ac0c to your computer and use it in GitHub Desktop.
A tweetable d3 animation
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.13/d3.js"></script>
<style>
circle {
fill:none;
stroke: #222;
}
</style>
</head>
<body>
<script src="scripts.js"></script>
</body>
</html>
var w = 500, // width
h = 500, // height
n = 100, // just some random number
o = {}, p = {}; // a few objects, remove the need for 'var'
// create a canvas
var i = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h)
.style("background", "#d1d1d1")
.append("g")
.attr("transform", "translate(" + [w / 2, h / 2] + ")");
// aliases
var s = Math.sin, c = Math.cos, π = Math.PI;
// datajoin utility function
function j(data, element) {
var update = i.selectAll(element)
.data(data);
//TODO: exit
update.enter = update.enter().append(element);
return update;
}
// render loop
d3.timer(tweet);
function tweet(t) {
// 126 chars, just enough room for a hastag!
o=j([d3.range(0,w).map(i=>[i,s(i/w*π)/2+0.5*h])],"path");o.enter.attr("d",d3.svg.line()});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment