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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.