-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (75 loc) · 2.48 KB
/
index.js
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
import MandelbrotViewer from './lib/MandelbrotViewer';
import * as Gradients from './lib/Gradients';
const canvas = document.getElementById('canvas');
const reset = document.getElementById('reset');
const form = document.getElementById('form');
const zoom = document.getElementById('zoom');
const precision = document.getElementById('precision');
const download = document.getElementById('download');
const colorscheme = document.getElementById('colorscheme');
const canvasSize = document.getElementById('canvasSize');
const centerR = document.getElementById('centerR');
const centerI = document.getElementById('centerI');
const width = document.getElementById('width');
document.querySelectorAll('input[type="number"]').forEach((i) => {
i.step = Number.MIN_VALUE;
});
Object.keys(Gradients).forEach((scheme) => {
const option = document.createElement('option');
option.value = option.innerHTML = scheme;
colorscheme.appendChild(option);
});
let view = new MandelbrotViewer();
const update = () => {
view.precision = Number.parseInt(precision.value);
canvas.width = canvas.height = Number.parseInt(canvasSize.value);
view.render(canvas, colorscheme.value);
centerR.value = view.center.r;
centerI.value = view.center.i;
width.value = view.width;
};
const updateSettings = () => {
view.width = view.height = Number.parseFloat(width.value);
view.center.r = Number.parseFloat(centerR.value);
view.center.i = Number.parseFloat(centerI.value);
update();
};
const getMousePos = (canvas, event) => {
let rect = canvas.getBoundingClientRect();
let x = event.clientX - rect.left;
let y = event.clientY - rect.top;
return {x, y};
};
const magnify = (invert = false) => {
let z = Number.parseInt(zoom.value);
let value = invert ? 1/z : z;
return (e) => {
view.moveTo(canvas, getMousePos(canvas, e));
view.magnify(value);
update();
}
}
canvas.addEventListener('click', magnify());
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
magnify(true)(e);
});
reset.addEventListener('click', (e) => {
view = new MandelbrotViewer();
update();
});
form.addEventListener('submit', (e) => {
e.preventDefault();
update();
});
form.addEventListener('change', updateSettings);
download.addEventListener('click', (e) => {
const img = canvas
.toDataURL("image/png")
.replace('image/octet-stream');
download.href = img;
const filename =
`mandelbrot_${centerR.value}_${centerI.value}_${width.value}.png`;
download.download = filename;
});
update();