-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandel.js
63 lines (45 loc) · 1.71 KB
/
mandel.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
"use strict";
import init, {Mandel} from "./pkg/julia.js";
const runWasm = async () => {
await init("./pkg/julia_bg.wasm");
const canvasElement = document.querySelector("canvas");
const info = document.querySelector("p");
const canvasContext = canvasElement.getContext("2d");
const canvasImageData = canvasContext.createImageData(
canvasElement.width,
canvasElement.height
);
var mandel = new Mandel(canvasElement.width, canvasElement.height, 4096);
const imageData = mandel.image_buffer();
canvasImageData.data.set(imageData);
const updateCoords = () => {
info.innerHTML = `centre=${mandel.mid_r()}+${mandel.mid_i()}i scale=${mandel.scale()}`;
};
const render = () => {
mandel.render();
updateCoords();
canvasContext.clearRect(0, 0, canvasElement.width, canvasElement.height);
const imageData = mandel.image_buffer();
canvasImageData.data.set(imageData);
canvasContext.putImageData(canvasImageData, 0, 0);
canvasElement.style.cursor = "zoom-in";
};
document.onmousedown = (event) => {
var factor = 0.5;
if (event.button == 0) { // left click zooms in
factor = 2.0;
} else if (event.button != 1) { // wheel click zooms out, anything else ignored
return;
}
const rect = canvasElement.getBoundingClientRect();
const x = (event.clientX - rect.left) * canvasElement.width / canvasElement.clientWidth;
const y = (event.clientY - rect.top) * canvasElement.height / canvasElement.clientHeight;
if (x >= 0 && y >= 0 && x <= canvasElement.width && y <= canvasElement.height) {
canvasElement.style.cursor = "progress"; // no discernible effect
mandel.zoom(y, x, factor);
render();
}
}
render();
}
runWasm();