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 by dividing the width or height of the Mandelbrot by the width or height of the canvas. For example, while the canvas may be set to 640 by 480 pixels, the bounds of the Mandelbrot may be something...