A Pen by A Non Ymous on CodePen.
Created
April 13, 2014 00:51
-
-
Save naveed-ahmad/10563971 to your computer and use it in GitHub Desktop.
A Pen by Captain Anonymous.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas></canvas> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var c = document.getElementsByTagName('canvas')[0]; | |
var b = document.body; | |
var a = c.getContext('2d'); | |
var W = c.width = window.innerWidth; | |
var H = c.height = window.innerHeight; | |
var max_it = 12; | |
var depths=[]; | |
var pi = Math.PI; | |
function flake(x,y) | |
{ | |
max_it = Math.round(Math.random()*4) + 4; | |
a.strokeStyle = 'rgba(255,255,255,1)'; | |
depths = []; | |
if( !x) | |
{ | |
x =( Math.random() -1/2 )* W/ 40; | |
y = (Math.random() -1/2)* H /40; | |
} | |
for( var i=0; i < max_it;i++) | |
{ | |
depths.push( Math.random()/2 + 1/3); | |
} | |
for( var i=0; i < 6 ; i++) | |
{ | |
blade( i * pi/3, 1, x,y,0, 1) | |
} | |
} | |
function clearScreen() | |
{ | |
a.fillStyle = "black"; | |
a.fillRect(0,0,W,H); | |
a.fill(); | |
} | |
function blade( r, l, x, y, depth, trunk) | |
{ | |
// _log.push( " [ " + r + "," + l + "," + x + "," + y + "," +depth+ "," +trunk+ " ] \n" ); | |
r = (r + 2*pi)%(2*pi); | |
var x2 = Math.cos(r) * l + x; | |
var y2 = Math.sin(r) * l + y; | |
var l2 = l * depths[depth] ; | |
var depth2 = depth + 1; | |
//draw | |
a.beginPath(); | |
a.lineWidth = Math.ceil( l * 10); | |
a.moveTo(40*x + W/2,40*y+H/2); | |
a.lineTo(40*x2 + W/2, 40*y2+H/2); | |
a.closePath(); | |
a.stroke(); | |
//recurse | |
if( depth < max_it && l > 1/40) | |
{ | |
blade( r - pi/3, l2, x2, y2, depth2); | |
blade( r + pi/3, l2, x2, y2, depth2); | |
if(trunk) | |
blade( r, l * 0.8,x2,y2, depth2, trunk); | |
else | |
blade(r,l2,x2,y2,depth2); | |
} | |
} | |
clearScreen(); | |
$('body').click( | |
function(){ | |
clearScreen(); | |
flake(1,0); | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment