-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
264 lines (244 loc) · 7.63 KB
/
index.html
File metadata and controls
264 lines (244 loc) · 7.63 KB
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Julia Fractal</title>
<style>
body {
background: black;
color: white;
}
button {
margin-right: 10px;
}
</style>
</head>
<body>
<h1>Click to zoom</h1>
Re:
<input type="text" id="re" value="-0.835" />
Im:
<input type="text" id="im" value="-0.2321"/>
<button id="redrawButton">Redraw</button><br /><br />
<div id="presetButtons"></div>
<canvas id="canvas" width="640" height="480" style="display: flex;">
</canvas><br />
<a href="https://github.com/DonKarlssonSan/JuliaJs">GitHub page</a>
<script>
(function() {
var presets = [ {
name: "z^2 + (-0.835-0.2321i)",
re: -0.835,
im: -0.2321,
fz: "z2"
} , {
name: "z^2 + (-0.70176-0.3842i)",
re: -0.70176,
im: -0.3842,
fz: "z2"
} , {
name: "z^2 + (0.285+0.01i)",
re: 0.285,
im: 0.01,
fz: "z2"
} , {
name: "z^2 + (-0.8+0.156i)",
re: -0.8,
im: 0.156,
fz: "z2"
} , {
name: "z^2 + (-0.4+0.6i)",
re: -0.4,
im: 0.6,
fz: "z2"
} , {
name: "z^2 + 0.279",
re: 0.279,
im: 0,
fz: "z2"
} , {
name: "z^3 + 0.4",
re: 0.4,
im: 0,
fz: "z3"
} , {
name: "z^3 + (0.4993-0.09i)",
re: 0.4993,
im: -0.09,
fz: "z3"
}
];
findPresetByName = function(presetsArray, name) {
for(var i = 0; i < presetsArray.length; i++) {
if(presetsArray[i].name === name) {
return presetsArray[i];
}
}
// Not found
return null;
};
var buttonsDiv = document.getElementById("presetButtons");
presets.forEach(function(item){
var button = document.createElement("button");
button.innerHTML = item.name;
// This will cause a re-flow of the page but we don't care
buttonsDiv.appendChild(button);
});
presetClicked = function(event) {
event = event || window.event;
var target = event.target || event.srcElement;
if(target.nodeName === "BUTTON") {
var preset = findPresetByName(presets, target.innerHTML);
if(preset) {
document.getElementById("re").value = preset.re;
document.getElementById("im").value = preset.im;
fz = preset.fz;
resetZoom();
drawJulia();
}
}
};
buttonsDiv.addEventListener("click", presetClicked, false);
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
var id = ctx.createImageData(width, height);
var d = id.data;
putPixel = function (x, y, r, g, b, a) {
var index = (x + y * width) * 4;
d[index+0] = r;
d[index+1] = g;
d[index+2] = b;
d[index+3] = a;
}
function zoom(event) {
var xPos = event.x;
var yPos = event.y;
xPos -= canvas.offsetLeft;
yPos -= canvas.offsetTop;
var xSize = (xmax-xmin) * 0.7;
var ySize = (ymax-ymin) * 0.7;
xmin = (xmin + xPos * xDelta) - xSize / 2;
xmax = (xmin + xPos * xDelta) + xSize / 2;
ymin = (ymin + yPos * yDelta) - ySize / 2;
ymax = (ymin + yPos * yDelta) + ySize / 2;
drawJulia();
}
canvas.addEventListener("mousedown", zoom, false);
resetZoom = function () {
xmin = -1.6;
ymin = -1.15;
xmax = 1.6;
ymax = 1.15;
}
var xmin;
var ymin;
var xmax;
var ymax;
var xDelta;
var yDelta;
var fz = "z2";
var redArray = new Array(256);
var greenArray = new Array(256);
var blueArray = new Array(256);
drawJulia = function() {
xDelta = (xmax-xmin) / width;
yDelta = (ymax-ymin) / height;
var x;
var y;
var iteration;
var max_iteration = 256;
var x0 = Number(document.getElementById("re").value);
var y0 = Number(document.getElementById("im").value);
for (var Px = 0; Px < width; Px++) {
for (var Py = 0; Py < height; Py++) {
x = xmin + Px * xDelta;
y = ymin + Py * yDelta;
iteration = 0;
while ( x*x + y*y < 4 && iteration < max_iteration ) {
// Multiplication of complex numbers:
// (a+bi) (c+di) = (ac-bd) + (bc+ad)i
// c^2 = (x+yi) (x+yi) = (xx-yy) + (yx+yx)i = (xx-yy) + (2xy)i
// c^3 = (x+yi) ((xx-yy) + (2xy)i) = (x(xx-yy)-y2xy) +(y(xx-yy)+x2xy)i
if(fz === "z2") {
xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
} else if(fz === "z3") {
xtemp = x*(x*x-y*y)-y*2*x*y + x0;
y = y*(x*x-y*y)+x*2*x*y + y0;
}
x = xtemp;
iteration = iteration + 1;
}
if(iteration === max_iteration) {
putPixel(Px, Py, 0, 0, 0, 255);
} else {
putPixel(Px, Py, redArray[iteration], greenArray[iteration], blueArray[iteration], 255);
}
}
}
//calculatePalette();
ctx.putImageData(id, 0, 0);
console.log("xmin: " + xmin + " xmax: " + xmax + " ymin: " + ymin + " ymax: " + ymax);
}
calculatePalette = function () {
// r g b
// 0 0 1
// 0 1 0
// 0 1 1
// 1 0 0
// 1 0 1
// 1 1 0
// 6 combinations
// 256 / 6 = 42.67 steps for each transition
var r, g, b;
var xx = 0;
r = 0; g = 0; b = 255;
for(var combo = 0; combo < 6; combo++) {
for(var step = 0; step < 42; step++) {
if(combo % 2 === 0) {
b = step * 6;
} else {
b = 256 - step * 6;
}
if(combo === 1 || combo === 5) {
g = step * 6;
} else if(combo === 3) {
g = 256 - step * 6;
}
if(combo === 3) {
r = step * 6;
} else if(combo === 100) {
r = 256 - step * 6;
}
redArray[xx] = r;
greenArray[xx] = g;
blueArray[xx] = b;
/*
// This is for drawing the palette when debugging
console.log(r + " " + g + " " + b);
putPixel(xx, 100, r, g, b, 255);
putPixel(xx, 101, r, g, b, 255);
putPixel(xx, 102, r, g, b, 255);
putPixel(xx, 103, r, g, b, 255);
putPixel(xx, 104, r, g, b, 255);
*/
xx++;
}
}
}
document.getElementById("redrawButton").addEventListener("click", drawJulia, false);
resetZoom();
calculatePalette();
drawJulia();
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-47202309-3', 'auto');
ga('send', 'pageview');
})();
</script>
</body>
</html>