Renders the Mandelbrot fractal at any size, position and zoom level you want in 133 bytes.
-
-
Save p01/1267983 to your computer and use it in GitHub Desktop.
Mandelbrot fractal with zoom & panning in 133 bytes
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
function | |
( | |
d, // ImageData object of width and height 'w' | |
w, // width ( and height ) of 'd' | |
x, // center x coordinate of the fractal | |
y, // center y coordinate of the fractal | |
z, // zoom ratio | |
// placeholder arguments | |
n, // pixel index | |
r, // real part of the complex number | |
i, // imaginary part of the complex number | |
k, // iteration count/pixel alpha | |
t // temporary variable | |
) | |
{ | |
for | |
( | |
n=w*w*4; // loop through every pixels | |
n-=4; | |
d.data[n+3]=k // set the alpha of the current pixel to the number of iterations | |
) | |
for | |
( | |
r=i=0, | |
k=256; // we go for no more than 256 iterations | |
--k/r; // or until we exit the mandelbrot set | |
i=t | |
) | |
t=2*r*i-y-(2-n/w/w)*z, // compute the next iteration of 'i' | |
r=r*r-i*i+x-(2-n/w%4)*z // and 'r' | |
} |
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
function(d,w,x,y,z,n,r,i,k,t){for(n=w*w*4;n-=4;d.data[n+3]=k)for(r=i=0,k=256;--k/r;i=t)t=2*r*i-y-(2-n/w/w)*z,r=r*r-i*i+x-(2-n/w%4)*z} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Mathieu 'p01' Henri - http://www.p01.org/releases/ | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "mandelbrotFractalWithZoomAndPanning", | |
"description": "Mandelbrot fractal with zoom & panning in 133 bytes.", | |
"keywords": [ | |
"mandelbrot", | |
"fractal", | |
"canvas", | |
"zoom", | |
"pan" | |
] | |
} |
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
<!doctype html> | |
<title>Mandelbrot fractal with zoom & panning in 133 bytes.</title> | |
<h1>Mandelbrot fractal with zoom & panning in 133 bytes.</h1> | |
<p>You should see a static rendering of the classic Mandelbrot set, and one zooming and panning.</p> | |
<canvas id=c></canvas> | |
<script> | |
var ctx = document.getElementById('c').getContext('2d'); | |
var mandelbrot = function(d,w,x,y,z,n,r,i,k,t){for(n=w*w*4;n-=4;d.data[n+3]=k)for(r=i=0,k=256;--k/r;i=t)t=2*r*i-y-(2-n/w/w)*z,r=r*r-i*i+x-(2-n/w%4)*z}; | |
// static rendering of the classic Mandelbrot set | |
var imageData = ctx.getImageData(0,0,150,150); | |
mandelbrot( imageData, 150, 0, 0, 1 ); | |
ctx.putImageData( imageData, 0, 0 ); | |
imageData = ctx.getImageData(0,0,100,100); | |
setInterval | |
( | |
function() | |
{ | |
var a=new Date()/4000; | |
mandelbrot( imageData, 100, Math.cos(a*3)/4-1.01, .45-Math.sin(a*3)/4, 1.001+Math.cos(a) ); | |
ctx.putImageData( imageData, 175, 25 ); | |
}, | |
50 | |
); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fwiw it could be 128 bytes if we pass directly the
data
property of the ImageData.Ideally I would like to avoid passing the
w
and get it from eitherd.width
ord.data.length
( via something likew=Math.sqrt(d.data.length)>>2
) but I haven't figured a way that fits in 140bytes