-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.html
215 lines (186 loc) · 6.43 KB
/
mandelbrot.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mandelbrot Set with WebGL</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gl = canvas.getContext("webgl2");
if (!gl) {
alert(
"WebGL 2 is not available in your browser. Please try another browser."
);
}
const vertexShaderSource = `
precision mediump float;
attribute vec2 a_position;
varying vec2 v_position;
void main() {
v_position = a_position;
gl_Position = vec4(a_position, 0.0, 1.0);
}
`;
const fragmentShaderSource = `
precision highp float;
uniform vec2 resolution;
uniform vec2 offset;
uniform float zoom;
uniform int maxIterations;
uniform float hueOffset;
vec3 hsl2rgb(float h, float s, float l) {
vec3 rgb = clamp(abs(mod(h * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
return l + s * (rgb - 0.5) * (1.0 - abs(2.0 * l - 1.0));
}
void main() {
vec2 coord = (gl_FragCoord.xy - 0.5 * resolution) / zoom + offset;
vec2 z = coord;
int iterations = 0;
for (int i = 0; i < 1000; i++) {
if (iterations >= maxIterations) break;
float x = (z.x * z.x - z.y * z.y) + coord.x;
float y = (z.y * z.x + z.x * z.y) + coord.y;
if (x * x + y * y > 4.0) break;
z = vec2(x, y);
iterations++;
}
float normalizedIterations = float(iterations) / float(maxIterations);
float h = mod(normalizedIterations * 720.0 + hueOffset, 360.0);
float s = 1.0;
float l = mix(0.5, 1.0, normalizedIterations);
vec3 color = hsl2rgb(h / 360.0, s, l);
if (iterations == maxIterations) {
color = vec3(0.0);
}
gl_FragColor = vec4(color, 1.0);
}
`;
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.error(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
}
function createProgram(gl, vertexShader, fragmentShader) {
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return program;
}
console.error(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
}
const vertexShader = createShader(
gl,
gl.VERTEX_SHADER,
vertexShaderSource
);
const fragmentShader = createShader(
gl,
gl.FRAGMENT_SHADER,
fragmentShaderSource
);
const program = createProgram(gl, vertexShader, fragmentShader);
const positionAttributeLocation = gl.getAttribLocation(
program,
"a_position"
);
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
const positions = [-1, -1, 1, -1, -1, 1, 1, 1];
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(positions),
gl.STATIC_DRAW
);
let offset = [0, 0];
let zoom = 200.0;
let maxIterations = 1000;
let hueOffset = Math.random() * 360.0;
function drawScene() {
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(program);
gl.enableVertexAttribArray(positionAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.vertexAttribPointer(
positionAttributeLocation,
2,
gl.FLOAT,
false,
0,
0
);
const hueOffsetUniformLocation = gl.getUniformLocation(program, "hueOffset");
gl.uniform1f(hueOffsetUniformLocation, hueOffset);
const resolutionUniformLocation = gl.getUniformLocation(
program,
"resolution"
);
gl.uniform2f(resolutionUniformLocation, canvas.width, canvas.height);
const offsetUniformLocation = gl.getUniformLocation(program, "offset");
gl.uniform2f(offsetUniformLocation, offset[0], offset[1]);
const zoomUniformLocation = gl.getUniformLocation(program, "zoom");
gl.uniform1f(zoomUniformLocation, zoom);
const maxIterationsUniformLocation = gl.getUniformLocation(
program,
"maxIterations"
);
gl.uniform1i(maxIterationsUniformLocation, maxIterations);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(drawScene);
}
drawScene();
let mouseDown = false;
let prevMousePos = [0, 0];
canvas.addEventListener("mousedown", (event) => {
mouseDown = true;
prevMousePos = [event.clientX, event.clientY];
});
canvas.addEventListener("mousemove", (event) => {
if (!mouseDown) return;
const dx = (event.clientX - prevMousePos[0]) / zoom;
const dy = (event.clientY - prevMousePos[1]) / zoom;
offset[0] -= dx;
offset[1] += dy;
prevMousePos = [event.clientX, event.clientY];
});
canvas.addEventListener("mouseup", () => {
mouseDown = false;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
const scaleFactor = 1 - event.deltaY * 0.001;
zoom *= scaleFactor;
});
window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
hueOffset = Math.random() * 360.0;
});
</script>
</body>
</html>