-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·334 lines (254 loc) · 9.13 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mandelbrot Viewer</title>
<link href="css/style.css" rel="stylesheet">
<body>
<div class="container">
<div align="center">
<h1>Mandelbrot Viewer</h1>
<h3>Click to zoom, press Z to toggle zoom direction</h3>
<div id="interactivity-container">
<div id="canvas-container">
<canvas tabindex="1" id="webglCanvas" style="border: none; background-color: black;" width="512" height="512"></canvas>
</div>
<table id="readout">
<tr>
<td class="row1">Zoom factor:</td>
<td class="row2" id="zoom-readout"></td>
</tr>
<tr>
<td class="row1">Center X:</td>
<td class="row2" id="center-x-readout"></td>
</tr>
<tr>
<td class="row1">Center Y:</td>
<td class="row2" id="center-y-readout"></td>
</tr>
</table>
</div>
</div>
</div>
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/webGL_boilerplate.js"></script>
<!-- VERTEX SHADER -->
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec2 position;
attribute vec2 uv;
varying vec2 pos;
void main() {
pos = uv;
gl_Position = vec4(position, 0.0, 1.0);
}
</script>
<!-- FRAGMENT SHADER -->
<script id="fragmentShader" type="x-shader/x-fragment">
precision highp float;
#define ITERS 250
#define COLOR1 vec3(0.0)
#define COLOR2 vec3(1.0, 0.3, 0.05)
#define COLOR3 vec3(1.0, 0.8, 0.1)
#define COLOR4 vec3(0.2, 0.05, 0.4)
#define OFFSET (0.25 / 512.0)
uniform vec2 ll;
uniform vec2 ur;
varying vec2 pos;
vec2 complexSquare(vec2 c) {
return vec2((c.x * c.x) - (c.y * c.y), 2.0 * (c.x * c.y));
}
vec4 iterateMandelbrot(vec2 c) {
vec2 initial = vec2(0.0, 0.0);
int iters = 0;
float dist = 1.0;
for (int i = 0; i < ITERS; i++) {
initial = complexSquare(initial) + c;
iters = i;
// Sqrt-less check for divergence
float square_norm = (initial.x * initial.x) + (initial.y * initial.y);
if (square_norm > 4.0) {
dist = sqrt(square_norm);
break;
}
}
return vec4(initial, float(iters), dist);
}
vec3 colorGradient(vec2 params) {
float mu = params.x - (log(log(params.y)) / log(2.0));
mu = 3.0 * (1.0 - pow(1.0 - (mu / float(ITERS)), 2.0));
if (mu >= 2.0) {
return mix(COLOR3, COLOR4, mu - 2.0);
}
if (mu >= 1.0) {
return mix(COLOR2, COLOR3, mu - 1.0);
}
return mix(COLOR1, COLOR2, mu);
}
void main() {
/* 4 SUPERSAMPLES FOR ANTIALIASING */
vec3 color_total = vec3(0.0);
vec2 complex1 = mix(ll, ur, pos + vec2(-OFFSET));
vec4 result1 = iterateMandelbrot(complex1);
color_total += result1.w > 2.0 ? colorGradient(result1.zw) : vec3(0.0);
vec2 complex2 = mix(ll, ur, pos + vec2(-OFFSET, OFFSET));
vec4 result2 = iterateMandelbrot(complex2);
color_total += result2.w > 2.0 ? colorGradient(result2.zw) : vec3(0.0);
vec2 complex3 = mix(ll, ur, pos + vec2(OFFSET));
vec4 result3 = iterateMandelbrot(complex3);
color_total += result3.w > 2.0 ? colorGradient(result3.zw) : vec3(0.0);
vec2 complex4 = mix(ll, ur, pos + vec2(OFFSET, -OFFSET));
vec4 result4 = iterateMandelbrot(complex4);
color_total += result4.w > 2.0 ? colorGradient(result4.zw) : vec3(0.0);
gl_FragColor = vec4(0.25 * color_total, 1.0);
}
</script>
<!-- WEBGL SETUP -->
<script>
/* PROGRAM INITIALIZATION */
var gl = initializeWebGL("webglCanvas");
var canvasDim = 512;
var program = createGlslProgram(gl, "vertexShader", "fragmentShader");
var fsq = createShape(gl,
[
-1.0, -1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
-1.0, 1.0, 0.0, 1.0
],
[
0, 1, 2,
0, 2, 3
]
);
/* WINDOW BOUND ATTRIBUTES */
var old_width = 4.0;
var old_ll = [-2.0, -2.0];
var old_ur = [2.0, 2.0];
var curr_width = 4.0;
var curr_ll = [-2.0, -2.0];
var curr_ur = [2.0, 2.0];
var new_width = null;
var new_ll = null;
var new_ur = null;
var is_transitioning = false;
var current_frame = 0;
var num_transition_frames = 20;
var min_zoom_width = 0.00002;
var max_zoom_width = 30;
</script>
<!-- FUNCTION FOR DRAWING MANDLEBROT ON FULL SCREEN QUAD -->
<script>
function drawMandelbrot() {
gl.useProgram(program);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
/* SET UNIFORMS */
var lowerLeftLocation = gl.getUniformLocation(program, "ll");
gl.uniform2f(lowerLeftLocation, curr_ll[0], curr_ll[1]);
var upperRightLocation = gl.getUniformLocation(program, "ur");
gl.uniform2f(upperRightLocation, curr_ur[0], curr_ur[1]);
/* SET UP ATTRIBUTES */
gl.bindBuffer(gl.ARRAY_BUFFER, fsq.vertexBuffer);
var vertPosition = gl.getAttribLocation(program, "position");
gl.enableVertexAttribArray(vertPosition);
gl.vertexAttribPointer(vertPosition, 2, gl.FLOAT, false, 4 * 4, 0);
var vertTexCoord = gl.getAttribLocation(program, "uv");
gl.enableVertexAttribArray(vertTexCoord);
gl.vertexAttribPointer(vertTexCoord, 2, gl.FLOAT, false, 4 * 4, 4 * 2);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
/* DRAW */
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, fsq.indexBuffer);
gl.drawElements(gl.TRIANGLES, fsq.size, gl.UNSIGNED_SHORT, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
gl.useProgram(null);
}
</script>
<!-- WEBGL RENDER/UPDATE CODE -->
<script>
/* ZOOM/CURSOR MODE VARIABLES */
var zoom = true;
var cursor_bank = 'zoom-in';
var animationID;
function boundsOk() {
return !((zoom && curr_width < min_zoom_width) || ((!zoom) && curr_width > max_zoom_width));
}
function updateBounds(event) {
/* Change curr values into old values */
old_width = curr_width;
old_ll = curr_ll.slice();
old_ur = curr_ur.slice();
var absoluteX = (event.offsetX < 0) ? 0.0 : event.offsetX / (canvasDim - 1);
var absoluteY = (event.offsetY < 0) ? 1.0 : (canvasDim - 1 - event.offsetY) / (canvasDim - 1);
center = [old_ll[0] + old_width * absoluteX, old_ll[1] + old_width * absoluteY];
/* Set target values */
new_width = old_width * (zoom ? 0.5 : 2.0);
new_ll = [center[0] - new_width/2.0, center[1] - new_width/2.0];
new_ur = [center[0] + new_width/2.0, center[1] + new_width/2.0];
/* Signal start of transition */
$('#webglCanvas').css('cursor', 'default');
is_transitioning = true;
current_frame = 0;
}
function lerp(start, end, amt) {
return start * (1.0 - amt) + end * amt;
}
function doTransition() {
if (current_frame >= num_transition_frames) {
curr_width = new_width;
curr_ll = new_ll.slice();
curr_ur = new_ur.slice();
is_transitioning = false;
return;
}
var progress = current_frame / num_transition_frames;
curr_width = lerp(old_width, new_width, progress);
curr_ll = [
lerp(old_ll[0], new_ll[0], progress),
lerp(old_ll[1], new_ll[1], progress)
];
curr_ur = [
lerp(old_ur[0], new_ur[0], progress),
lerp(old_ur[1], new_ur[1], progress)
];
current_frame++;
}
function updateText() {
var center = [
(curr_ll[0] + curr_ur[0]) / 2.0,
(curr_ll[1] + curr_ur[1]) / 2.0,
];
$('#zoom-readout').text((2.0 / curr_width).toFixed(2));
$('#center-x-readout').text(center[0].toFixed(15));
$('#center-y-readout').text(center[1].toFixed(15));
}
$(window).keydown(function(event){
if (event.which == 90) {
zoom = !zoom;
cursor_bank = zoom ? 'zoom-in' : 'zoom-out';
if (!is_transitioning) {
$('#webglCanvas').css('cursor', cursor_bank);
}
}
});
$('#webglCanvas').click(function(event){
if (!is_transitioning && boundsOk()) {
updateBounds(event);
window.requestAnimationFrame(updateWebGL);
}
});
function updateWebGL(time) {
drawMandelbrot();
updateText();
if (is_transitioning) {
doTransition();
window.requestAnimationFrame(updateWebGL);
}
$('#webglCanvas').css('cursor', cursor_bank);
}
$('#webglCanvas').css('cursor', cursor_bank);
window.requestAnimationFrame(updateWebGL);
</script>
</body>
</html>