-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (44 loc) · 1.35 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var CANVAS = {
WIDTH: 1000,
HEIGHT: 600
};
var MANDELBROT = {
MIN_X: -2.5,
MAX_X: 1,
MIN_Y: -1.25,
MAX_Y: 1.25
};
var COLOR_PALETTE = ["red", "black", "white"];
var MAX_ITERATIONS = 500;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
canvas.width = CANVAS.WIDTH;
canvas.height = CANVAS.HEIGHT;
function drawPoint (x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, 1, 1);
}
function normalize (value, max, newMax, newMin) {
return (value / max) * (newMax - newMin) + newMin;
}
(function mandelbrot () {
var i, j, x0, y0, x, y, iteration, xTemp, color;
var paletteLength = COLOR_PALETTE.length;
for (i = 0; i < CANVAS.WIDTH; i++) {
for (j = 0; j < CANVAS.HEIGHT; j++) {
x = 0;
y = 0;
iteration = 0;
x0 = normalize(i, CANVAS.WIDTH, MANDELBROT.MAX_X, MANDELBROT.MIN_X);
y0 = normalize(j, CANVAS.HEIGHT, MANDELBROT.MAX_Y, MANDELBROT.MIN_Y);
while (x*x + y*y < 4 && iteration < MAX_ITERATIONS) {
xTemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xTemp;
iteration++;
}
color = COLOR_PALETTE[iteration % paletteLength];
drawPoint(i, j, COLOR_PALETTE[iteration % paletteLength]);
}
}
})();