-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
68 lines (55 loc) · 1.8 KB
/
main.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
import * as cm from "./editor.js";
function resizeFromMouse(event) {
document.getElementById("text").style.minWidth = (event.screenX) + "px";
}
window.onload = async function() {
let game = document.getElementById("game");
let resize = document.getElementById("resize");
resize.addEventListener("mousedown", (e) => {
e.preventDefault();
function remove() {
document.removeEventListener("mousemove", resizeFromMouse);
game.contentWindow.removeEventListener("mousemove", resizeFromMouse);
}
document.addEventListener("mousemove", resizeFromMouse);
game.contentWindow.addEventListener("mousemove", resizeFromMouse);
document.addEventListener("mouseup", remove);
game.contentWindow.addEventListener("mouseup", remove);
});
top.addEventListener("keydown", (e) => {
e.stopPropagation();
}, true);
var app = null;
function compileWAT(wabt, text) {
try {
let result = wabt.parseWat("", text);
let bin = result.toBinary({
// In case anyone in the console wants to pause and debug:
write_debug_names: true,
});
if (bin.log !== "") {
alert(bin.log);
console.error(bin.log);
}
let wasm4Cart = game.contentDocument.getElementById("wasm4-cart-json");
let json = `{"WASM4_CART":"${encode(bin.buffer)}","WASM4_CART_SIZE":${bin.buffer.length}}`;
wasm4Cart.innerText = json;
if (app !== null) {
game.contentDocument.body.innerHTML = "";
}
app = new game.contentWindow.wasm4.App();
game.contentDocument.body.appendChild(app);
} catch (error) {
alert(error);
console.error(error);
}
}
let wabt = await new WabtModule();
compileWAT(wabt, cm.editor.state.doc.toString());
document.getElementById("compile").onclick = function() {
game.src = "player.html";
game.onload = function() {
compileWAT(wabt, cm.editor.state.doc.toString());
}
}
}