-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
226 lines (166 loc) · 7.85 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
<!DOCTYPE html>
<html>
<head>
<title>Fractals</title>
<meta charset="utf-8">
<link rel="stylesheet" href="./webgl.css" type="text/css">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</head>
<body>
<div class="title">
<h1>Mandelbrot/Julia Set Fractal Web Toy</h1>
</div>
<div class="row">
<div class="column">
<p>Left click on canvas to zoom in, right click to zoom out</p>
<canvas id="glcanvas" width="500" height="500" onmousedown="mousedown(event)" oncontextmenu="return false;" onmouseup="mouseup(event)"></canvas>
<h1>ABOUT</h1>
<p></p>
<p>The Mandelbrot set is the set of complex numbers \(c\) for which the iterative function \(f(x) = x^2 + c\) does not diverge,
for \(x\) starting at \(0\).
<p></p>
The Julia set is the set of complex numbers \(x\) for which the iterative function \(f(x) = x^2 + c\) does not diverge when initialized with \(x\),
given a complex number \(c\) (which you can set with the slider below).
<p></p>
Each pixel represents a point in the complex plane, and black points are in the set.
<p></p>
This program uses shaders executed on the GPU to parallize the algorithm. With this method the set renders very quickly,
and can be interacted with in real time, making for a fun web toy.
<h3>The Algorithm:</h3>
<p></p>
<p>The algorithm used is the classic Escape Time Algorithm. The color of a pixel is determined by the number of iterations
the complex number associated with that pixel takes to diverge to infinity. Clearly we cannot actually diverge the value to infinity, so we determine whether
it will or not by checking if the iterated value increases to a certain value in a certain number of iterations. That number of iterations can be changed with the slider below.
A higher value will make for a more detailed fractal but is more computationally intensive.
</p>
</p>
</div>
<div class="column">
<div>
<input type="radio" id="radio-one" name="notaswitch-one" value="yes" checked/>
<label for="radio-one">Mandelbrot</label>
<input type="radio" id="radio-two" name="notaswitch-one" value="maybe" />
<label for="radio-two">Julia</label>
</div>
<div>
<p>Real part:</p>
<input class="slider" id="x-slider" type="range" step="1" min="-100" max="100" value="0">
<p>Imaginary part:</p>
<input class="slider" id="y-slider" type="range" step="1" min="-100" max="100" value="0">
</div>
<div>
<p>\(c =\) <span id="real-1">0</span>\( + \)<span id="imaginary-1">0</span>\(i\)</p>
<p></p>
<p><input id="real" type="number" step="0.01" value="0.0"/> + <input id="imaginary" type="number" step="0.01" value="0.0"/>i</p>
</div>
<div>
<p>Max iterations: <span id="max-it">75</span> (escape time algorithm, higher value may cause lag)</p>
<input class="slider" id="max-iterations" type="range" step="1" min="50" max="1000" value="75">
</div>
</div>
</div>
</body>
<script id="shader-fs-mandel" type="x-shader/x-fragment">
/* Fragment shader that renders Mandelbrot set */
precision mediump float;
/* Width and height of screen in pixels */
uniform vec2 u_resolution;
/*position of mouse on display*/
/* Point on the complex plane that will be mapped to the center of the screen */
uniform vec2 u_zoomCenter;
/* Distance between left and right edges of the screen. This essentially specifies
which points on the plane are mapped to left and right edges of the screen.
Together, u_zoomCenter and u_zoomSize determine which piece of the complex
plane is displayed. */
uniform float u_zoomSize;
/* How many iterations to do before deciding that a point is in the set. */
uniform int u_maxIterations;
//c1 is black, c2 is any colour, c3 is white
vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b*cos( 6.28318*(c*t+d) );
}
vec2 f(vec2 z, vec2 c) {
return mat2(z,-z.y,z.x)*z + c;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
/* Decide which point on the complex plane this fragment corresponds to.*/
vec2 c = u_zoomCenter + (uv * 4.0 - vec2(2.0)) * (u_zoomSize / 4.0);
/* Now iterate the function. */
vec2 z = vec2(0.0);
bool escaped = false;
int iterations;
for (int i = 0; i < 10000; i++) {
iterations = i;
/* Unfortunately, GLES 2 doesn't allow non-constant expressions in loop
conditions so we have to do this ugly thing instead. */
if (i > u_maxIterations) break;
z = f(z, c);
if (length(z) > 2.0) {
escaped = true;
break;
}
}
vec3 col = palette(float(iterations)/float(u_maxIterations), vec3(0.5, 0.5, 0.5), vec3(0.5, 0.5, 0.5), vec3(1.0, 1.0, 1.0),vec3(0.00, 0.33, 0.67));
gl_FragColor = escaped ? vec4(col, 1.0) : vec4(vec3(0.0, 0.0, 0.0), 1.0);
}
</script>
<script id="shader-fs-julia" type="x-shader/x-fragment">
/* Fragment shader that renders Mandelbrot set */
precision mediump float;
/* Width and height of screen in pixels */
uniform vec2 u_resolution;
/*position of mouse on display*/
/* Point on the complex plane that will be mapped to the center of the screen */
uniform vec2 u_zoomCenter;
/* Distance between left and right edges of the screen. This essentially specifies
which points on the plane are mapped to left and right edges of the screen.
Together, u_zoomCenter and u_zoomSize determine which piece of the complex
plane is displayed. */
uniform float u_zoomSize;
/* How many iterations to do before deciding that a point is in the set. */
uniform int u_maxIterations;
uniform vec2 u_c;
vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b*cos( 6.28318*(c*t+d) );
}
vec2 f(vec2 z, vec2 c) {
return mat2(z,-z.y,z.x)*z + c;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
/* Decide which point on the complex plane this fragment corresponds to.*/
vec2 z = u_zoomCenter + (uv * 4.0 - vec2(2.0)) * (u_zoomSize / 4.0);
/* Now iterate the function. */
//vec2 z = vec2(0.0);
bool escaped = false;
int iterations;
for (int i = 0; i < 10000; i++) {
iterations = i;
/* Unfortunately, GLES 2 doesn't allow non-constant expressions in loop
conditions so we have to do this ugly thing instead. */
if (i > u_maxIterations) break;
z = f(z, u_c);
if (length(z) > 2.0) {
escaped = true;
break;
}
}
//vec3 col = colour_picker(float(iterations) / float(u_maxIterations), vec3(0.1,0.0, 0.1), vec3(0.05, 0.34, 0.93), vec3(0.07, 0.89, 0.12), vec3(0.81,0.89, 0.03 ), vec3(0.9, 0.0, 0.0), vec3(0.0, 0.0, 0.0));
vec3 col = palette(float(iterations)/float(u_maxIterations), vec3(0.5, 0.5, 0.5), vec3(0.5, 0.5, 0.5), vec3(1.0, 1.0, 1.0),vec3(0.00, 0.33, 0.67));
gl_FragColor = escaped ? vec4(col, 1.0) : vec4(vec3(0.0, 0.0, 0.0), 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec2 coords;
void main()
{
gl_Position = vec4(coords.xy, 0.0, 1.0);
}
</script>
<script src="./dist/gl-matrix.js"></script>
<script src="./webgl.js"></script>
</html>