-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifsdisplay.html
401 lines (364 loc) · 13.2 KB
/
ifsdisplay.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<html>
<head>
<link rel='stylesheet' href='stylesheet.css'>
</head>
<body onload="startup();">
<div class='col'>
<div class='row align-end'>
<span style="padding-left:10px;">
<label for='iters'>Iterations</label>
<input id='iters' type='number' value=100000 min=0 max=500000 step=1000 oninput="renderFractal(system)"></input>
</span>
<span style="padding-left:10px;">
<label for='probs'>Probabilities:</label>
<select id='probs' name='probs' oninput="renderFractal(system)">
<option value='det'>Determinant</option>
<option value='uniform'>Uniform</option>
</select>
</span>
<span style="padding-left:10px;">
<label for='systems'>Predefined system:</label>
<select id='systems' name='systems' onclick="predefinedSystem(null)">
<option value='dragon'>Dragon</option>
<option value='fern'>Fern</option>
</select>
</span>
<span style="padding-left:10px;">
<label for='color'>Color:</label>
<select id='color' name='color' onclick="renderFractal(system)">
<option value='black'>Black</option>
<option value='white'>White</option>
</select>
</span>
</div>
<div class='row'>
<div class='col'>
<canvas id="ifs-canvas" height='400' width='400'> </canvas>
<div class='light'>Fractal display</div>
</div>
<div class='col'>
<canvas id='decomp-canvas' width=400 height=400></canvas>
<div class='light'>Fractal SVD manipulator</div>
</div>
<div>
<canvas id='info-canvas' class='no-border' width=250 height=400></canvas>
</div>
</div>
<div class='row'>
<div class='col' style="min-width: 400px;">
<div class='header'>System parameters</div>
<div class='row'>
<input id='system-size' type='number' min=2 max=16 value=2 oninput='changeSystemSize()'></input>
<label for='system-size' style="padding-left:10px;">System size</label>
</div>
<!-- <div class='row'>
<input id='step-size' type='number' min='0.01' max='0.5' value='0.1' step='0.01'>
</input>
<label for='step-size' style="padding-left:10px;">Step size</label>
</div> -->
<div id='params' class='col'></div>
<div class='row'>
<button onclick="sampleRandomSystem()">Random</button>
<button class="ml" onclick="copySystemJSON()">Copy</button>
<span class='ml' id='copy-notice'></span>
</div>
</div>
<div class='col'>
<textarea id='entry-field' name='entry-field' cols="48" rows="12"
placeholder="Enter a system (as JSON)"></textarea>
<button id='submit-system' onclick="formSubmitSystem()">Submit</button>
</div>
</div>
<div class='col' style="border-top: solid black 1px">
<div style='font-size: 150%; font-weight: bold;'>Instructions</div>
<div class='col'>
<p style='font-size: 115%;'>
Use the fractal SVD manipulator pane to change the system SVD parameters.
Click and drag to manipulate the parameters.
<br>
The solid circular handles adjust the singular values— <span style='color: red;'>red</span>
is the larger singular value (and associated vectors)
while <span style='color: blue;'>blue</span> is the smaller singular value.
<br>
Holding CTRL and clicking a singular value handle will flip the determinant.
<br>
The hollow circular handles change the singular vector orientation.
<br>
The central black hollow circle adjusts the position (translation vector).
</p>
</div>
</div>
</div>
</body>
<script src=ifs.js></script>
<script src=manipulate.js></script>
<script>
// Global state
var system = null;
var fillRate = 0;
var manipulator;
// -------------------------------------------------------
// Predefined systems
var predefined = {
dragon: [[[0.5, -0.5, 0], [0.5, 0.5, 0]],
[[-0.5, -0.5, 1], [0.5, -0.5, 0]]],
fern: [[[0.95, 0.005, -0.002], [-0.005, 0.93, 0.5]],
[[0.035, -0.2, -0.09], [0.16, 0.04, 0.02]],
[[-0.04, 0.2, 0.083], [0.16, 0.04, 0.12]]],
}
function sampleRandomSystem() {
var n = parseInt(document.getElementById('system-size').value);
system = randomSystem(n);
displayParams();
renderFractal(system);
}
function predefinedSystem(name=null) {
if (name === null) {
name = document.getElementById('systems').value;
}
if (predefined.hasOwnProperty(name)) {
system = deepCopy(predefined[name]);
displayParams();
renderFractal(system);
}
}
function formSubmitSystem() {
let text = document.getElementById('entry-field').value;
try {
let sys = JSON.parse(text);
if (sys.length < 2) throw new SyntaxError('Not enough transforms');
for (let i = 0; i < sys.length; i++) {
if (sys[i].length !== 2 || sys[i][0].length !== 3 || sys[i][1].length !== 3)
throw new SyntaxError('System must consist only of 3x2 matrices');
}
system = sys;
document.getElementById('system-size').value = system.length;
changeSystemSize();
} catch (SyntaxError) {
console.log('Input was not a valid system');
}
}
function renderFractal(sys, force=false) {
var iters = document.getElementById('iters').value;
var probs = document.getElementById('probs').value;
let color = document.getElementById('color').value;
let t1 = performance.now();
var points = iterateIFS(sys, iters, probs=probs);
let t2 = performance.now();
drawPoints(points, color);
let t3 = performance.now();
manipulator.update(sys, force);
showSystemInfo(system);
// console.log('iterate(' + (t2-t1) + '), draw(' + (t3-t2) + ')');
}
function createMatrixCard() {
var mat = [[1, 0, 0], [0, 1, 0]];
var card = document.createElement('div');
card.classList.add('col')
for (let i = 0; i < mat.length; i++) {
var row = document.createElement('span');
for (let j = 0; j < mat[i].length; j++) {
var cell = document.createElement('input');
cell.type = 'number';
// cell.step = document.getElementById('step-size').value;
cell.step = 0.01;
let notlast = j < mat[i].length - 1;
cell.min = notlast ? '-1' : '-10';
cell.max = notlast ? '1' : '10'
cell.value = mat[i][j].toPrecision(3);
cell.oninput = function(ev) {
let sys = readTextParams();
renderFractal(sys);
}
row.appendChild(cell);
}
card.appendChild(row);
}
return card;
}
function updateMatrixCard(mat, card) {
for (let i = 0; i < mat.length; i++) {
var row = card.children[i];
for (let j = 0; j < mat[i].length; j++) {
var cell = row.children[j];
cell.value = mat[i][j].toPrecision(4);
}
}
}
function changeSystemSize() {
let n = parseInt(document.getElementById('system-size').value, 10);
let s = system.length;
if (n > s)
for (let i = s; i < n; i++) {
system.push([[1.0,0.0,0.0],[0.0,1.0,0.0]]);
}
else if (n < s)
for (let i = 0; i < s-n; i++)
system.pop();
displayParams();
renderFractal(system, true);
}
function readTextParams() {
var container = document.getElementById('params');
var sys = [];
for (let i = 0; i < container.children.length; i++) {
var mat = [];
var mat_els = container.children[i];
for (let j = 0; j < mat_els.children.length; j++) {
var row = [];
let row_els = mat_els.children[j];
for (let k = 0; k < row_els.children.length; k++) {
let v = parseFloat(row_els.children[k].value);
row.push(v);
}
mat.push(row);
}
sys.push(mat);
}
return sys;
}
function displayParams(sys=null) {
if (sys === null) {
sys = system;
}
var container = document.getElementById('params');
let nc = container.children.length;
if (nc > sys.length)
for (let i = sys.length; i < nc; i++)
container.removeChild(container.lastChild);
else if (nc < sys.length)
for (let i = nc; i < sys.length; i++) {
let card = createMatrixCard(sys[i]);
container.appendChild(card);
}
for (let i = 0; i < sys.length; i++)
updateMatrixCard(sys[i], container.children[i]);
document.getElementById('system-size').value = sys.length;
}
function drawPoints(points, color='black', region=null) {
var can = document.getElementById('ifs-canvas');
var ctx = can.getContext('2d');
let fg, bg;
if (color === 'black') {
fg = 0;
bg = 255;
} else if (color === 'white') {
fg = 255;
bg = 0;
}
if (region === null)
region = minmax(points);
var w = can.width;
var h = can.height;
ctx.fillStyle = bg === 0 ? 'black' : 'white';
ctx.fillRect(0, 0, w, h);
let id = ctx.getImageData(0 ,0, w, h);
let pix = id.data;
var count = 0;
for (let i = 0; i < points.length; i++) {
let x = Math.floor((points[i][0] - region[0]) / (region[2]-region[0]) * (w-1));
let y = Math.floor((points[i][1] - region[1]) / (region[3]-region[1]) * (h-1));
if (x >= 0 && x < w && y >= 0 && y < h) {
// ar[y][x] = 1;
let k = (y*w + x)*4;
if (pix[k] === bg) {
count++;
pix[k] = fg;
pix[k+1] = fg;
pix[k+2] = fg;
pix[k+3] = 255;
}
}
}
fillRate = count / (w*h);
ctx.putImageData(id, 0, 0);
// coordinates of corners
let fsize = 9;
ctx.fillStyle = '#AAAAAA';
ctx.font = fsize.toString()+'px courier';
let p = 3;
let x1 = region[0].toPrecision(p);
let y1 = region[1].toPrecision(p);
let x2 = region[2].toPrecision(p);
let y2 = region[3].toPrecision(p);
ctx.textBaseline = 'top';
ctx.textAlign = 'left';
let s = '('+x1+','+y1+')';
ctx.fillText(s, 2, 2);
s = '('+x2+','+y1+')';
ctx.textAlign = 'right';
ctx.fillText(s, w-2, 2);
s = '('+x1+','+y2+')';
ctx.textBaseline = 'bottom';
ctx.textAlign = 'left';
s = '('+x2+','+y2+')';
ctx.fillText(s, 2, h-2);
ctx.textAlign = 'right';
ctx.fillText(s, w-2, h-2);
}
function showSystemInfo(sys=null) {
if (sys === null) sys = system;
var can = document.getElementById('info-canvas');
var ctx = can.getContext('2d');
ctx.font = '12px courier';
ctx.fillStyle = 'black';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.clearRect(0, 0, can.width, can.height);
let xoff = 5, yoff = 5, xspace = 65, yspace = 20;
ctx.fillText('\u03c31', xoff, yoff);
ctx.fillText('\u03c32', xoff+xspace, yoff);
ctx.fillText('\u03b8', xoff+2*xspace, yoff);
ctx.fillText('\u03d5', xoff+3*xspace, yoff);
ctx.beginPath();
ctx.moveTo(0, yoff+yspace*2/3);
ctx.lineTo(can.width, yoff+yspace*2/3);
ctx.stroke();
function getAngle(mat) {
return Math.acos(mat[0][0]) * 180.0 / 3.14159 * Math.sign(mat[1][0]);
}
for (let i = 0; i < sys.length; i++) {
let svd = manipulator.svd[i];
let y = yoff+(i+1)*yspace;
ctx.fillText(svd.S[0].toPrecision(4), xoff, y);
ctx.fillText(svd.S[1].toPrecision(4), xoff+xspace, y);
ctx.fillText(getAngle(svd.U).toPrecision(4), xoff+2*xspace, y);
ctx.fillText(getAngle(svd.V).toPrecision(4), xoff+3*xspace, y);
}
}
function copySystemJSON() {
let text = JSON.stringify(system);
navigator.clipboard.writeText(text);
document.getElementById('copy-notice').innerHTML = 'copied';
}
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
function startup() {
manipulator = new ManipulateDisplay(document.getElementById('decomp-canvas'), function(sys) {
renderFractal(sys);
displayParams(sys);
system = sys;
document.getElementById('copy-notice').innerHTML = '';
});
system = deepCopy(predefined.fern);
document.getElementById('systems').value = 'fern';
displayParams();
// var stepper = document.getElementById('step-size');
// stepper.oninput = function(ev) {
// var new_step = stepper.value;
// var container = document.getElementById('params');
// for (let i = 0; i < container.children.length; i++) {
// var mat_els = container.children[i];
// for (let j = 0; j < mat_els.children.length; j++) {
// let row_els = mat_els.children[j];
// for (let k = 0; k < row_els.children.length; k++) {
// row_els.children[k].step = new_step;
// }
// }
// }
// }
renderFractal(system);
}
</script>
</html>