-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (51 loc) · 1.7 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
wasm_bindgen("pkg/webgl_bg.wasm").catch(console.error)
.then(show_logo);
//display("Drop a PDF here");
function show_logo() {
fetch("Ghostscript_Tiger.svg")
.then(r => r.arrayBuffer())
.then(buf => init_view(new Uint8Array(buf)));
}
function set_scroll_factors() {}
function drop_handler(e) {
e.stopPropagation();
e.preventDefault();
show(e.dataTransfer.files[0]);
}
function dragover_handler(e) {
e.stopPropagation();
e.preventDefault();
}
let view;
function init_view(data) {
let canvas = document.getElementById("canvas");
view = wasm_bindgen.view(canvas, data);
let requested = false;
function animation_frame(time) {
requested = false;
view.animation_frame(time);
}
function check(request_redraw) {
if (request_redraw && !requested) {
window.requestAnimationFrame(animation_frame);
requested = true;
}
}
window.addEventListener("keydown", e => check(view.key_down(e)), {capture: true});
window.addEventListener("keyup", e => check(view.key_up(e)), {capture: true});
canvas.addEventListener("mousemove", e => check(view.mouse_move(e)));
canvas.addEventListener("mouseup", e => check(view.mouse_up(e)));
canvas.addEventListener("mousedown", e => check(view.mouse_down(e)));
window.addEventListener("resize", e => check(view.resize(e)));
view.render();
}
function show(file) {
let reader = new FileReader();
reader.onload = function() {
let data = new Uint8Array(reader.result);
init_view(data);
};
reader.readAsArrayBuffer(file);
}
document.addEventListener("drop", drop_handler, false);
document.addEventListener("dragover", dragover_handler, false);