Skip to content

Instantly share code, notes, and snippets.

@hrickards
Created January 30, 2012 16:54
Show Gist options
  • Save hrickards/1705401 to your computer and use it in GitHub Desktop.
Save hrickards/1705401 to your computer and use it in GitHub Desktop.
Mandelbrot Canvas
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var mandelbrotCanvas = document.getElementById('mandelbrotCanvas');
var context = mandelbrotCanvas.getContext('2d');
var width = mandelbrotCanvas.width;
var height = mandelbrotCanvas.height;
var max_iter = 1000;
var interval = 200;
for (i=0;i<width;i++) {
for (j=0;j<height;j++) {
var Za = 0;
var Zb = 0;
var Ca = 3.5*i/(width -1) - 2.5;
var Cb = 2*j/(height-1) - 1;
var iter = 0;
var Zas = 0
while((Zas + Zb*Zb) < 4 && iter < max_iter) {
Zas = Za*Za;
var Zan = Zas - Zb*Zb + Ca;
Zb = 2*Za*Zb + Cb;
Za = Zan;
iter++;
}
if (iter == max_iter) {
colour = 'black';
} else {;
var modulus = Math.round(Math.sqrt(Za*Za + Zb*Zb)) % 5;
switch(modulus) {
case 0: colour = 'FF3300'; break;
case 1: colour = '66CC33'; break;
case 2: colour = '33FFFF'; break;
case 3: colour = '0033FF'; break;
case 4: colour = 'FF00FF'; break;
}
}
context.fillStyle = colour;
context.fillRect(i, j, width/interval, height/interval);
}
}
};
</script>
</head>
<body>
<canvas id="mandelbrotCanvas" width="800" height="400">
</canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment