Skip to content

Instantly share code, notes, and snippets.

Created Sep 10, 2012
Embed
What would you like to do?
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