Time for action – implementing the algorithm

Let's create a MandelbrotGenerator object in a new file named mandelbrotGenerator.js. This object will implement the algorithm that generates the Mandelbrot. The constructor takes the canvas width and height, and the bounds of the Mandelbrot:

function MandelbrotGenerator(canvasWidth, canvasHeight, left, top,right, bottom)
    {

Next we define the variables that the algorithm uses:

    var scalarX = (right - left) / canvasWidth,
        scalarY = (bottom - top) / canvasHeight,
        maxIterations = 1000,
        abort = false,
        inSetColor = { r: 0x00, g: 0x00, b: 0x00 },
        colors = [ /* array of color objects */ ];

The scalarX and scalarY variables are used to convert the Mandelbrot coordinates to canvas coordinates. They are computed ...

Get HTML5 Web Application Development By Example Beginner's guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.