-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.htm
368 lines (351 loc) · 11.6 KB
/
index.htm
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Soap Bubbles </title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<link rel="stylesheet" href="theme.css">
<script src="fractals.js"></script>
</head>
<body onload="init()" onresize="init()">
<div>
<table><tr>
<td class="title">
<tt>S</tt><tt>o</tt><tt>a</tt><tt>p</tt>
<tt>B</tt><tt>u</tt><tt>b</tt><tt>b</tt><tt>l</tt><tt>e</tt><tt>s</tt> </td>
<td style="padding-left: 10px; padding-right: 10px;"><table><tr> <!-- Pan -->
<td> Center: </td>
<td><input type="text" id="x" value="0"></td>
<td style="width: 10px;">+</td>
<td><input type="text" id="y" value="0i"></td>
</tr></table></td><!-- Zoom -->
<td style="padding-left: 10px; padding-right: 10px;"><table><tr>
<td> Zoom: </td>
<td><input id="zoom" type="text" value="100%"></td>
<td><table><tr>
<td class="button" id="zin"><i class="material-icons">add_circle</i></td>
<td class="button" id="zout"><i class="material-icons">remove_circle</i></td>
</tr></table></td>
</tr></table></td>
<td style="width: 75px;"><table><tr><!-- Tools -->
<td class="button" id="info_b"><i class="material-icons">info</i></td> <!-- info -->
<td class="button" id="sett_b"><i class="material-icons">settings</i></td> <!-- settings -->
</tr></table></td>
<td><table><tr><!-- Links -->
<td><a href="index.htm">Reset</a></td>
<td><a href="https://github.com/hacktheverse/Soap-Bubbles" target="_blank">Source Code</a></td>
<td><a href="https://en.wikipedia.org/wiki/Fractal" target="_blank">Learn More</a></td>
</tr></table></td>
</tr></table>
</div>
<div id="load" class="float">Loading...
<input id="informer" value="draw" style="display: none;">
</div>
<div id="info" class="float">
<h1> About </h1>
<p> Soap Bubbles is a fractal-graphing website (and work in progress). Currently, you can explore the depths
of a Mandelbrot fractal- just pan by clicking, zoom by double clicking (or using the nifty toolbar) - if you're
very patient. Many features are on their way, so stay tuned for more exciting developments! </p>
<p> P.S. It's called Soap Bubbles on account of the Mandelbrot Set's infamous bulbs reminding me of soap bubbles. </p>
<br><hr><br>
<p> Designed and Implemented by A.C.B. </p>
</div>
<div id="settings" class="float">
<h1> Settings </h1>
<table>
<tr>
<td><p> Number of Iterations: <input type="text" id="iter" style="width: 25px;"></p>
<p class="hint"> * The default is 23, but 50+ iterations provides the fractal with a greater depth/resolution. </p></td>
</tr>
<tr>
<td><p> Enable Smooth Coloring: <input type="checkbox" id="smooth" style="width: 25px;"></p>
<p class="hint"> * Enabled by default, but disabling decreases load-times and shows banding. </p></td>
</tr>
<tr>
<td><p> Escape Radius: <input type="text" id="radius" style="width: 25px;"></p>
<p class="hint"> * Note that the escape radius of a Mandelbrot Set must be at least 2. </p></td>
</tr>
<tr>
<td><p> Loading block size: (as % of page width and height)</p>
<p> Width: <input type="text" id="block_xf" style="width: 25px;"></p>
<p> Height: <input type="text" id="block_yf" style="width: 25px;"></p></td>
</tr>
</table>
<br><hr><br>
</div>
<script>
var divTimer = -1;
var divOn = null;
/**
* Toggle, in an animated fashion, the visibility of a div
* @param {String} div_id - string id of div to be toggled
*/
function toggle_div(div_id){
if(divOn != null && divOn.id != div_id){
divOn.style.opacity = 0;
divOn.style.display = "none";
}
var div = document.getElementById(div_id);
clear_divTimer();
if(div.style.display!="block"){
div.style.display="block";
divTimer = setInterval(function() {
if(Number(div.style.opacity)<1){
div.style.opacity = Number(div.style.opacity) + 0.1;
} else {
clear_divTimer();
}
}, 10);
} else {
divTimer = setInterval(function() {
if(Number(div.style.opacity)>0){
div.style.opacity = Number(div.style.opacity) - 0.1;
} else {
div.style.display = "none";
clear_divTimer();
divOn = null;
}
}, 10);
}
divOn = div;
}
/**
* Clears div timer, i.e. ends animation
*/
function clear_divTimer(){
if(divTimer != -1){
clearInterval(divTimer);
divTimer = -1;
}
}
// Add event listeners
var buttons = [document.getElementById("sett_b"), document.getElementById("info_b")];
buttons[0].addEventListener("click", function(event) {toggle_div("settings");});
buttons[1].addEventListener("click", function(event) {toggle_div("info");});
</script>
<canvas id="map"></canvas>
<script>
// initialize values, set up listeners
var map = document.getElementById("map");
var loading = document.getElementById("load");
// This element exists purely to 'inform' on the fractal's state, so that the loading cue can react appropriately
var informer = document.getElementById("informer");
var zoomInput = document.getElementById("zoom");
var realInput = document.getElementById("x");
var imInput = document.getElementById("y");
var iterInput = document.getElementById("iter");
var smoothCheck = document.getElementById("smooth");
smoothCheck.checked = true;
var smooth = true;
var threshInput = document.getElementById("radius");
threshInput.value = 2;
var blockwInput = document.getElementById("block_xf");
blockwInput.value = 2.5 + "%";
var blockhInput = document.getElementById("block_yf");
blockhInput.value = 5 + "%";
map.addEventListener('dblclick', function() { fractal_zoom(2/3); });
map.addEventListener("click", fractal_move);
document.getElementById("zin").addEventListener("click", function() { fractal_zoom(2/3); });
document.getElementById("zout").addEventListener("click", function() { fractal_zoom(3/2); });
realInput.addEventListener("keyup", fractal_pan);
imInput.addEventListener("keyup", fractal_pan);
zoomInput.addEventListener("keyup", function(event) {
if(event.keyCode == 13){ // enter key
var input = zoomInput.value;
var sym = input.indexOf("%");
if(sym>0){
input = input.substring(0, sym);
}
if(!isNaN(input)){
zoom = 100/Number(input);
}
fractal_zoom(1);
}
});
iterInput.addEventListener("keyup", function(event){
if(event.keyCode == 13){ // enter key
var input = iterInput.value;
if(!isNaN(input)){
max_iter = input;
}
fractal_draw();
}
});
smoothCheck.addEventListener("click", function(event) {
smooth = smoothCheck.checked;
fractal_draw();
});
threshInput.addEventListener("keyup", function(event) {
if(!isNan(threshInput.value) && threshInput.value >= 2) {
threshold = threshInput.value;
} else {
threshInput.value = threshold;
}
fractal_draw();
});
blockwInput.addEventListener("keyup", function(event) {
if(event.keyCode == 13){ // enter key
var wf = blockwInput.value;
var sym = wf.indexOf("%");
if(sym >= 0){
wf = wf.substring(0, sym);
}
if(!isNaN(wf) && wf>0 && wf<=100) {
block_xf = wf/100;
}
blockwInput.value = block_xf*100 + "%";
}
});
blockhInput.addEventListener("keyup", function(event) {
if(event.keyCode == 13){ // enter key
var hf = blockhInput.value;
var sym = hf.indexOf("%");
if(sym >= 0){
hf = hf.substring(0, sym);
}
if(!isNaN(hf) && hf>0 && hf<=100) {
block_yf = hf/100;
}
blockhInput.value = block_yf*100 + "%";
}
});
var timer = -1;
var loadTimer = -1;
var max_iter = 23;
var width = window.innerWidth;
var height = window.innerHeight;
var zoom = 1;
var xmin = -1;
var xmax = 2.5;
var ymin = -1;
var ymax = 1;
var threshold = 2;
var block_xf = 1/40;
var block_yf = 1/20;
/**
* Clears loadTimer, i.e. ends loading animation
*/
function clear_loadTimer(){
if(loadTimer != -1){
clearInterval(loadTimer);
loadTimer = -1;
}
}
/**
* Shows loadTimer, in an animated way
*/
function show_load(){
clear_loadTimer();
loadTimer = setInterval(function(){
if(Number(loading.style.opacity)<1){
loading.style.opacity = Number(loading.style.opacity) + 0.05;
} else {
clear_loadTimer();
}
}, 50);
}
/**
* Hides loadTimer, in an animated way
*/
function hide_load(){
clear_loadTimer();
loadTimer = setInterval(function() {
if(Number(loading.style.opacity)>0){
loading.style.opacity = Number(loading.style.opacity) - 0.05;
} else {
clear_loadTimer();
}
}, 50);
}
/**
* Initializes fractal
*/
function init() {
width = window.innerWidth;
height = window.innerHeight;
map.width = width;
map.height = height;
setInterval(function() {
if(informer.value == "hide"){ // it is time to hide
informer.value="done";
clearInterval(timer);
timer = -1;
hide_load();
}
}, 100);
fractal_draw();
}
/**
* Draws fractal, canceling any other timers drawing on the map canvas
*/
function fractal_draw() {
show_load();
informer.value = "draw";
realInput.value = ((xmax + xmin)/2).toFixed(2);
imInput.value = ((ymax + ymin)/2).toFixed(2) + "i";
zoomInput.value = Math.floor(100/zoom) + "%";
iterInput.value = max_iter;
setTimeout(function(){
clearInterval(timer);
timer = -1;
timer = graph(map, max_iter, zoom*xmin, zoom*xmax, zoom*ymin, zoom*ymax,
informer, smooth, threshold*threshold, Math.floor(width*block_xf), Math.floor(height*block_yf));
}, 1);
}
/**
* Centers the fractal on the point clicked.
* @param {Event} event - event triggering the move
*/
function fractal_move(event) {
if(event != null){
var xrange = xmax - xmin;
var yrange = ymax - ymin;
var xf = (event.clientX-width/2)*xrange/width;
var yf = (event.clientY-height/2)*yrange/height;
xmin -= xf;
xmax -= xf;
ymin -= yf;
ymax -= yf;
}
fractal_draw();
}
/**
* Zooms fractal
* @param {Number} factor - factor by which to decrease zoom
*/
function fractal_zoom(factor) {
if(factor <= 0){
return;
}
zoom*=factor;
fractal_draw();
}
/**
* Pan action triggered by panning text boxes
* @param {Event} event - event that tiggered pan
*/
function fractal_pan(event){
if(event.keyCode == 13){ // enter key
var real = realInput.value;
var imaginary = imInput.value;
var sym = imaginary.indexOf("i");
if(sym>0){
imaginary = imaginary.substring(0, sym);
}
if(!isNaN(imaginary) && !isNaN(real)){
var rdiff = real - (xmin + xmax)/2;
rdiff/=zoom;
var idiff = imaginary - (ymin + ymax)/2;
idiff/=zoom;
xmin -= rdiff;
ymin += idiff;
xmax -= rdiff;
ymax += idiff;
}
fractal_move(null);
}
}
</script>
</body>
</html>