Tj Holowaychuk
14 years ago
2 changed files with 84 additions and 0 deletions
@ -0,0 +1,83 @@ |
|||
|
|||
/** |
|||
* Module dependencies. |
|||
*/ |
|||
|
|||
var Canvas = require('../lib/canvas') |
|||
, canvas = new Canvas(1150, 230) |
|||
, ctx = canvas.getContext('2d') |
|||
, fs = require('fs'); |
|||
|
|||
|
|||
function render(level){ |
|||
renderLevel(level,81,0); |
|||
} |
|||
|
|||
function renderLevel(minimumLevel,level,y){ |
|||
for (var x=0; x < 243 / level; ++x) { |
|||
drawBlock(x,y,level); |
|||
} |
|||
for (var x=0; x < 243 / level; x+=3){ |
|||
drawBlock(x,y+1,level); |
|||
drawBlock(x+2,y+1,level); |
|||
} |
|||
for (var x=0; x < 243 / level; ++x){ |
|||
drawBlock(x,y+2,level); |
|||
} |
|||
if ((y += 3) >= 243 / level){ |
|||
y=0; |
|||
level /= 3; |
|||
} |
|||
if (level >= minimumLevel){ |
|||
renderLevel(minimumLevel, level, y); |
|||
} |
|||
} |
|||
|
|||
function drawBlock(x,y,level){ |
|||
ctx.fillStyle = getPointColour( |
|||
x * level + (level-1) / 2 |
|||
, y * level + (level-1) / 2); |
|||
|
|||
ctx.fillRect( |
|||
x * level |
|||
, y * level |
|||
, level |
|||
, level |
|||
); |
|||
} |
|||
|
|||
function getPointColour(x,y){ |
|||
x= x / 121.5 - 1; |
|||
y= -y / 121.5 + 1; |
|||
var x2y2 = x * x + y * y; |
|||
if (x2y2 > 1){ |
|||
return '#000'; |
|||
} else { |
|||
var root = Math.sqrt(1 - x2y2); |
|||
var x3d = x * 0.7071067812 + root / 2 - y / 2; |
|||
var y3d = x * 0.7071067812 - root / 2 + y / 2; |
|||
var z3d = 0.7071067812 * root + 0.7071067812 * y; |
|||
var brightness= -x / 2 + root * 0.7071067812 + y / 2; |
|||
if (brightness < 0) brightness = 0; |
|||
return '' |
|||
+ 'rgb(' + Math.round(brightness * 127.5 * (1 - y3d)) |
|||
+ ',' + Math.round(brightness * 127.5 * (x3d + 1)) |
|||
+ ',' + Math.round(brightness * 127.5 * (z3d + 1)) |
|||
+ ')' |
|||
; |
|||
} |
|||
} |
|||
|
|||
var start = new Date; |
|||
render(15); |
|||
ctx.translate(230,0); |
|||
render(10); |
|||
ctx.translate(230,0); |
|||
render(6); |
|||
ctx.translate(230,0); |
|||
render(3); |
|||
ctx.translate(230,0); |
|||
render(1); |
|||
console.log('Rendered in %d seconds', (new Date - start) / 1000); |
|||
|
|||
canvas.savePNG(__dirname + "/ray.png"); |
Loading…
Reference in new issue