-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.js
149 lines (127 loc) · 3.12 KB
/
mandelbrot.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var DIVERGE_LIMIT = 4;
var ITERATION_LIMIT = 16;
var ZOOM_FACTOR = 1;
var X = -0.5;
var Y = 0;
var STEP = 0.1;
var mandelbrot = function(a, b) {
var time = new Date();
var zA = 0;
var zB = 0;
var zA2 = 0;
var zB2 = 0;
for (var i = 0; i < ZOOM_FACTOR*ITERATION_LIMIT; i++) {
if (zA2 + zB2 > DIVERGE_LIMIT*ZOOM_FACTOR*ZOOM_FACTOR) {
return ~~(i/ZOOM_FACTOR) || ITERATION_LIMIT;
} else {
var tempZA = zA2 - zB2 + a;
zA += zA;
zB = zA * zB + b;
zA = tempZA;
zA2 = zA * zA;
zB2 = zB * zB;
}
}
calcTime += new Date() - time;
return ITERATION_LIMIT;
};
var makeSquare = function(type, x, y) {
var square = $('<div class="square' + (type === ITERATION_LIMIT ? ' black' : '') + '"></div>');
if (type < ITERATION_LIMIT) {
var img = $("<img src='img/"+type+".gif'>");
$(square).append(img);
$(img).attr('x', x).attr('y', y);
}
$(square).attr('x', x).attr('y', y);
return square;
};
var render = function() {
calcTime = 0;
var vis = $('.visualizer');
$('.row').remove();
var halfXCount = ~~($(window).width()/64);
var halfYCount = ~~($(window).height()/64);
// var rotate = halfYCount > halfXCount;
for (var i = -1*halfYCount - 1; i < halfYCount + 1; i++) {
row = $('<div class="row"></div>');
for (var j = -1*halfXCount - 1; j < halfXCount + 1; j++) {
var square_x = X + j*STEP;
var square_y = Y + i*STEP;
var iters = (mandelbrot(square_x,square_y));
row.append(makeSquare(iters, square_x, square_y));
}
vis.append(row);
}
};
var zoomIn = function(click_x, click_y) {
X = click_x;
Y = click_y;
STEP /= 2;
ZOOM_FACTOR++;
if (ZOOM_FACTOR > 1) {
$('.zoomout').show();
} else {
$('.zoomout').hide();
}
render();
};
var zoomOut = function() {
STEP *= 2;
ZOOM_FACTOR--;
if (ZOOM_FACTOR > 1) {
$('.zoomout').show();
} else {
$('.zoomout').hide();
}
render();
};
var move = function(direction) {
if (direction === 'up') {
Y -= 5*STEP;
} else if (direction === 'down') {
Y += 5*STEP;
} else if (direction === 'right') {
X += 5*STEP;
} else if (direction === 'left') {
X -= 5*STEP;
}
render();
};
var flashEyes = function() {
var eyes = $('.eyes');
eyes.animate({
'opacity': 0.15
}, 500, 'swing', function() {
eyes.animate({
'opacity': 0
}, 500, 'swing', function() {
setTimeout(flashEyes, Math.random()*15000);
});
});
};
$(document).on('ready', function() {
render();
$(document).on('click', '.square', function(e) {
var x = parseFloat($(e.target).attr("x"));
var y = parseFloat($(e.target).attr("y"));
zoomIn(x, y);
});
$(document).on('click', '.zoomout', function(e) {
zoomOut();
});
$(document).on('click', '.nav', function(e) {
move($(e.target).attr('direction'));
});
$(document).on('click', '.explanation', function() {
$('.explanation').hide();
$('.nav').show();
$('.zoomout').hide();
});
$(document).on('click', 'a', function(e) {
$(e).stopPropagation();
});
$(window).resize(function() {
render();
});
setTimeout(flashEyes, Math.random()*15000);
});