Skip to content

Instantly share code, notes, and snippets.

@tmcw
Last active December 11, 2015 21:18
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 tmcw/4661421 to your computer and use it in GitHub Desktop.
Save tmcw/4661421 to your computer and use it in GitHub Desktop.
Tiny things
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font:normal 12px/20px 'Georgia';
}
path {
fill:none;
stroke:#720551;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var g = d3.select('body').append('svg')
.attr({ width: 400, height: 400 })
.append('g')
.attr({ width: 400, height: 400 }),
txt = g.append('text').attr('transform', 'translate(10,20)');
ctl = d3.select('body').append('input').attr({
'type': 'range', min: 2, max: 50 }),
line = d3.svg.line()
.x(function(d) { return s(d[0]); })
.y(function(d) { return s(d[1]); }),
s = d3.scale.linear().domain([-1, 1]).range([0, 400]);
function getpts(n) {
var trans = [],
pts = d3.range(0, n + 1).map(function(d) {
return [Math.cos(Math.PI * 2 * d/n), Math.sin(Math.PI * 2 * d/n)];
});
pts.forEach(function(p, i) {
for (var j = i + 1; j < pts.length; j++) {
trans.push(p); trans.push(pts[j]);
}
});
return trans;
}
g.selectAll('path').data([[]]).enter().append('path');
ctl.on('change', function() {
if (ti) { window.clearTimeout(ti); ti = null; }
draw(+d3.select(this).node().value);
});
var n = 2;
function draw(n) {
g.selectAll('path').data([getpts(n)]).attr('d', line);
txt.text(n);
}
var ti = window.setInterval(function() {
draw(n);
if (++n > 50) n = 2;
}, 200);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment