-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
342 lines (301 loc) · 8.43 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
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fractals</title>
<meta name="viewport" content="width=520">
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type="text/javascript" src="js/jquery/jquery-2.1.4.js"></script>
<script type="text/javascript" src="js/jquery/jcanvas.min.js"></script>
<script type="text/javascript">
var width = 500;
var height = 500;
var intervalID = -1;
var iterations = 0;
var pos = {x:0,y:0};
var ifs = [];
var colors = [];
colors[0] = "#FF0000";
colors[1] = "#00FF00";
colors[2] = "#0000FF";
colors[3] = "#FFFF00";
colors[4] = "#00FFFF";
colors[5] = "#FF00FF";
function randomRange(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
function uniformToCanvasSpace(vector)
{
return {x:(vector.x * width), y:((1-vector.y) * height)}
}
function iteratedFunction(ifs,pos)
{
var xOffset = (pos.x-ifs.fixed.x);
var yOffset = (pos.y-ifs.fixed.y);
var result = {
x: ifs.scale * (Math.cos(ifs.angle) * xOffset - Math.sin(ifs.angle) * yOffset) + ifs.fixed.x,
y: ifs.scale * (Math.sin(ifs.angle) * xOffset + Math.cos(ifs.angle) * yOffset) + ifs.fixed.y
}
return result;
}
function drawDot(pos, color)
{
var p = uniformToCanvasSpace(pos)
$('#fractal').drawRect({
fillStyle: color,
x: p.x,
y: p.y,
width: 1,
height: 1
});
}
function iterate(steps)
{
for(var i = 0; i<steps; i++)
{
var rnd = Math.floor(Math.random()*ifs.length);
pos = iteratedFunction(ifs[rnd],pos);
drawDot(pos, ifs[rnd].color);
iterations++;
}
$('#iterations').text(iterations);
}
function updateIFSData()
{
ifs = [];
$('.function').each(function()
{
ifs.push({
fixed: {
x: parseFloat($(this).find('.x').val()),
y: parseFloat($(this).find('.y').val())
},
scale: parseFloat($(this).find('.scale').val()),
angle: parseFloat($(this).find('.rotation').val()),
color: $(this).find('.color').val()
});
});
console.log("Updata data: "+ifs.length);
updateURL();
drawFixPoints();
}
function getGetDataFromURL()
{
var s = window.location.search.substring(1).split('&');
if(!s.length) return;
$_GET = {};
for(var i = 0; i < s.length; i++) {
var parts = s[i].split('=');
$_GET[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
}
}
function getIFSDataFromGet()
{
var count = $_GET["l"];
if (typeof count === "undefined") count = 0;
for(var i=0; i<count; ++i)
{
ifs[i] = {
scale: $_GET["s"+i],
angle: $_GET["a"+i],
color: '#'+$_GET["c"+i],
fixed: {
x: $_GET["x"+i],
y: $_GET["y"+i]
}
};
}
updateForm();
}
function updateForm()
{
if(ifs.length <= 0) return;
for(var i=functionCount; i<ifs.length; ++i)
{
var template = $('.function[index=0]').clone();
template.attr('index',functionCount).appendTo('#functions');
template.find('.color').val(colors[functionCount%colors.length]);
functionCount++;
}
var i=0;
$('.function').each(function()
{
$(this).find('.x').val(parseFloat(ifs[i].fixed.x)),
$(this).find('.y').val(parseFloat(ifs[i].fixed.y))
$(this).find('.scale').val(parseFloat(ifs[i].scale)),
$(this).find('.rotation').val(parseFloat(ifs[i].angle)),
$(this).find('.color').val(ifs[i].color)
++i;
});
}
function updateURL()
{
var url = "?l="+ifs.length;
for(var i=0; i<ifs.length; ++i)
{
url += "&"+
"s"+i+"="+encodeURI(ifs[i].scale)+"&"+
"a"+i+"="+encodeURI(ifs[i].angle)+"&"+
"x"+i+"="+encodeURI(ifs[i].fixed.x)+"&"+
"y"+i+"="+encodeURI(ifs[i].fixed.y)+"&"+
"c"+i+"="+encodeURI(ifs[i].color.substr(1,6));
}
window.history.pushState({}, '', "index.html"+url);
//window.location.search = url;
}
function drawFixPoints()
{
$('#fixpoints').clearCanvas();
for(var i=0; i<ifs.length; ++i)
{
var p = uniformToCanvasSpace({x:ifs[i].fixed.x, y: ifs[i].fixed.y})
$('#fixpoints').drawRect({
fillStyle: ifs[i].color,
x: p.x,
y: p.y,
width: 9,
height: 9
});
}
}
function start()
{
updateIFSData();
var fps = 60;
var steps = $("#steps").val();
//console.log("FPS: "+fps);
clearInterval(intervalID);
if(!playing)
{
intervalID = setInterval(iterate,1000/fps,steps/fps);
playing = true;
$(this).css('background-image', 'url("./img/pause.svg")');
}
else
{
playing = false;
$(this).css('background-image', 'url("./img/play.svg")');
}
$('#iterations').text(iterations);
}
function add()
{
var template = $('.function[index=0]').clone();
template.attr('index',functionCount).appendTo('#functions');
template.find('.color').val(colors[functionCount%colors.length]);
functionCount++;
updateIFSData();
}
function remove()
{
if(functionCount <= 1) return;
$('.function').last().remove();
functionCount--;
updateIFSData();
}
function clear()
{
clearInterval(intervalID);
playing = false;
$('#startButton').css('background-image', 'url("./img/play.svg")');
$('#fractal').clearCanvas();
iterations = 0;
$('#iterations').text(iterations);
}
function updateLayers()
{
if($("#showGrid").prop("checked")) $("#grid").show();
else $("#grid").hide();
if($("#showFixpoints").prop("checked")) $("#fixpoints").show();
else $("#fixpoints").hide();
}
function drawGrid()
{
$('#grid').clearCanvas();
for(var i=0; i<10; ++i)
{
var alpha = (1/(1+i%2))-0.4;
var pos = i/10;
drawHorizontalLine(pos,'rgba(0,0,0,'+alpha+')');
drawVerticalLine(pos, 'rgba(0,0,0,'+alpha+')');
}
}
function drawHorizontalLine(val, color)
{
var start = uniformToCanvasSpace({x:0, y: val})
var end = uniformToCanvasSpace({x:1, y: val})
drawUniformLine(start,end,color);
}
function drawVerticalLine(val,color)
{
var start = uniformToCanvasSpace({x:val, y: 0})
var end = uniformToCanvasSpace({x:val, y: 1})
drawUniformLine(start,end,color);
}
function drawUniformLine(start, end, color)
{
$('#grid').drawLine({
strokeStyle: color,
strokeWidth: 2,
x1: start.x, y1: start.y,
x2: end.x, y2: end.y
});
}
var playing = false;
var functionCount = 1;
$(document).ready(function () {
getGetDataFromURL();
getIFSDataFromGet();
updateIFSData();
updateLayers();
drawGrid();
$('#settings').on("change", "input", updateIFSData);
$('#functions').on("change", "input", updateIFSData);
$('#info').on("change", "input", updateLayers);
$('#startButton').click(start);
$('#clearButton').click(clear);
$('#addFunc').click(add);
$('#removeFunc').click(remove);
if($_GET["autoplay"]) start();
});
</script>
</head>
<body>
<div id='result' class="section">
<div id='multicanvas'>
<canvas id='fractal' width='500' height='500'></canvas>
<canvas id='fixpoints' width='500' height='500'></canvas>
<canvas id='grid' width='500' height='500'></canvas>
</div>
<div id='info'>
<label>Iterations: </label></span><span id='iterations'>0</span>
<span style='float:right'>
<input id='showFixpoints' type='checkbox'> Fixpoints</input>
<input id='showGrid' type='checkbox'> Grid</input>
</span>
</div>
</div>
<div id='settings' class="section">
<button id='clearButton'></button>
<button id='startButton'></button>
<label>Iterations/s</label>
<input id='steps' class='compact' type='number' value='10000'></input>
<button id='addFunc'></button>
<button id='removeFunc'></button>
<!--<div style='clear both'></div>-->
</div>
<div id='functions' class="section">
<div class='function' index='0'>
<input type='color' class='color compact' value='#FF0000'></input>
<label>Scale:</label>
<input class='scale compact' type='text' value='1'></input>
<label>Angle:</label>
<input class='rotation compact' type='text' value='0'></input>
<label>Point:</label>
<input class='x compact' type='text' value='1'></input>
<input class='y compact' type='text' value='1'></input>
</div>
</div>
</body>
</html>