Skip to content

Instantly share code, notes, and snippets.

Created September 10, 2012 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/3692572 to your computer and use it in GitHub Desktop.
Save anonymous/3692572 to your computer and use it in GitHub Desktop.
Phyllotaxis pinwheel
// View live at http://enjalot.com/cypress/3114111
// Original at http://www.jasondavies.com/sunflower-phyllotaxis/
var w = tributary.sw,
h = tributary.sh,
p = 5,
n = 760,
k = 1/4000;
scale = 10;
tributary.init = function(ctx) {
d3.select('#display').style('background', '#111');
};
tributary.run = function(ctx,t) {
tributary.clear(); //helper function to clear the canvas
GOLDEN_ANGLE = Math.PI * (t/40000 + Math.sqrt(k*t));
var points = d3.range(1, n).map(place);
points.forEach(circle);
function circle(p,i) {
ctx.fillStyle = 'hsla(' + Math.round(310+50*i/n) + ',50%,60%,' + Math.sqrt((i/n)) + ')'
ctx.beginPath();
ctx.arc(p[0],p[1],4,0,2*Math.PI);
ctx.fill();
}
function place(n) {
var r = Math.sqrt(n),
a = n * GOLDEN_ANGLE;
return [scale * r * Math.cos(a)+w/2, scale * r * Math.sin(a)+h/2];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment