Skip to content

Instantly share code, notes, and snippets.

@p01 p01/LICENSE.txt forked from 140bytes/LICENSE.txt
Created Oct 6, 2011

Embed
What would you like to do?
Mandelbrot fractal with zoom & panning in 133 bytes

Mandelbrot fractal with zoom & panning in 133 bytes

Renders the Mandelbrot fractal at any size, position and zoom level you want in 133 bytes.

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'
}
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}
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.
{
"name": "mandelbrotFractalWithZoomAndPanning",
"description": "Mandelbrot fractal with zoom & panning in 133 bytes.",
"keywords": [
"mandelbrot",
"fractal",
"canvas",
"zoom",
"pan"
]
}
<!doctype html>
<title>Mandelbrot fractal with zoom &amp; panning in 133 bytes.</title>
<h1>Mandelbrot fractal with zoom &amp; 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>
@p01

This comment has been minimized.

Copy link
Owner Author

commented Oct 7, 2011

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 either d.width or d.data.length ( via something like w=Math.sqrt(d.data.length)>>2 ) but I haven't figured a way that fits in 140bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.