 The Mandelbrot set is one of the pieces of mathematics behind the iconic fractal images of the 80s and 90s, and here I present how to render it on an html5 canvas, entirely in the browser.

The full mathematics of the Mandelbrot set are beyond the scope of this post, but can be found here. For our purposes, all we need to know is that we are dealing with a set of numbers – that is, every possible number either belongs to, or does not belong to, the set.

The Mandelbrot set deals with complex numbers – that is, numbers which have both a real and imaginary component. When we plot a fractal 2 dimensional image, each point represents a number, with a real component plotted in the horizontal axis, and the imaginary component plotted in the vertical axis (both purely by convention).

So, to render the image, we need to take every point within the image, determine which complex number it represents, determine whether or not that complex number belongs to the set, and colour it accordingly.

The definition of the Mandelbrot set includes the concept of iteration – performing the same function to that number over and over – and seeing whether or not the chain of numbers that results is bounded or not. In some cases, that is immediately obvious from the first iteration. In other cases closer to the edge of the set, it may take many more iterations to determine.

One characteristic feature of the Mandelbrot set (and fractals by definition) is that it is nowhere differentiable – on inspection, more detail appears, and a straight line can never be found. This means that the only way to determine if a point lies within the set is to calculate for that specific point, and the closer that point lies to the boundary of the set, the more iterations are required. So, in order to avoid having to compute potentially infinite iterations for points that lie on the edge, we instead assume that each number is in the set, and if after a set number of iterations we have not proved a number is outside the set, we stick with that assumption.

First we define an html5 canvas:

 1234 Mandelbrot Canvas

We then set up some initial parameters.

We are going to limit each calculation to 30 iterations, as this gives a good level of detail at the screen resolution we are dealing with:

 56

The initial set of complex numbers we are going to use runs from -2+1j at the top left of the image down to 1-1j at the bottom right. This encloses the Mandelbrot set nicely, giving us the stereotypical view of the set. As we are going to keep the aspect ratio fixed (i.e. 1 unit in the x axis will always equal 1 unit in the y axis) the 4th limit is calculated from the other 3:

 78910 var MinRe = -2.0; var MaxRe = 1.0; var MinIm = -1.0; var MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;

It is important to note that we are dealing with 2 sets of co-ordinates: the 2 dimensional matrix of complex numbers defined above, as well as the matrix of pixels in the canvas. We retrieve the height and width of the canvas element and calculate (in units per pixel) the scaling factors between the two sets of co-ordinates:

 1112131415 var element = document.getElementById("mandelbrot"); var ImageHeight=element.height; var ImageWidth=element.width; var Re_factor = (MaxRe-MinRe)/(ImageWidth-1); var Im_factor = (MaxIm-MinIm)/(ImageHeight-1);

The canvas element’s pixel data is stored in a one-dimensional array of rgb and alpha values. As we will be manipulating the canvas at a pixel level, it will be simpler to define a function which amends the array for a given pixel position:

 1617181920212223 //sets pixel at co-ordinate x,y to rgb and alpha values given function setPixel(imageData, x, y, r, g, b, a) {     index = (x + y * imageData.width) * 4;     imageData.data[index+0] = r;     imageData.data[index+1] = g;     imageData.data[index+2] = b;     imageData.data[index+3] = a; }

Next we create a function which performs the iterative calculation for each pixel, and colours it either black or white depending on whether it belongs to the set or not:

 242526272829303132333435363738394041424344454647484950515253545556575859 function reDraw(){     c1 = element.getContext("2d");     c1Data = c1.getImageData(0, 0, ImageWidth, ImageHeight);     for(var y=0; y 4)                 {                     isInside = false;                     break mainloop;                 }                 Z_im = 2*Z_re*Z_im + c_im;                 Z_re = Z_re2 - Z_im2 + c_re;             }             if(isInside){                 r=0;                 g=0;                 b=0;             }             else{                 r=255;                 g=255;                 b=255;             }             setPixel(c1Data, x, y, r, g, b, 0xff);         }     }     c1.putImageData(c1Data, 0, 0); }

We then call the function to draw the set according to our initial parameters:

 63 reDraw();

This gives us a basic black and white image according to set membership: While nice enough, it’s missing the well-known colouration we expect from our fractals. So we instead define the colour of each number not only according to set membership, but also by the number of iterations used to determine membership, and whether the maximum nummber of iterations were reached without determination. Lines 48 to 57 above now become:

 4849505152535455565758596061626364656667 if (n

A full working example of this can be seen here, also incorporating a basic zoom function using jquery.

1. Otter Fan says:

I’m disappointed by the lack of otters in this post.

2. graeme says:

They’re in there, if you zoom in far enough.

Thank you. This helped me a lot. Very good Explanation!

• Johnie says:

But you didn’t answer the question. When a government takes it upon itself to decide the fate of their citizens through “special pr#;samr&o8221g, tell me what makes it right or wrong? God? Zeus? The Orange Blossom Special?

4. Give me show a demo!

5. virginia says:

good

6. Roma says:

The Mandelbrot set is a standout amongst the most unpredictable and lovely numerical articles known. Utilizing HTML5 Canvas, File API, touch (Pointer and motion occasions), and Web Workers.
https://sovinco.com/

7. My Friend Recommended This Blog And He Was Totally Right Keep Up The Fantastic Work!

8. things to do says:

It was thinking about whether I could utilize this review on my other site, I will connect it back to your site though.Great Thanks.