-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.html
79 lines (61 loc) · 2.47 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>WebAssembly WebGL Shaders</title>
<script src="./wasm-arrays.js"></script>
<script src="./dist/appWASM.js"></script>
<script>
"use strict"
window.addEventListener("wasmLoaded", () => {
console.log("wasmLoaded")
let previewCanvasContext
const createCanvas = (id, index, width, height) => {
const canvas = document.createElement("canvas")
canvas.id = id
canvas.width = width
canvas.height = height
canvasContainer.appendChild(canvas)
const context = canvas.getContext("webgl2")
const idBuffer = Module._malloc(id.length+1)
Module.stringToUTF8(id, idBuffer, id.length+1)
Module.ccall("createContext", null, ["number", "number", "number", "number"], [width, height, idBuffer, index])
}
const loadImage = src => {
Module.ccall("clearContexts", null, null, null)
const img = new Image()
img.addEventListener("load", () => {
const canvas = document.createElement("canvas")
canvas.id = "previewCanvas"
canvas.height = img.height
canvas.width = img.width
canvasContainer.innerHTML = ""
previewCanvasContext = canvas.getContext("2d")
previewCanvasContext.drawImage(img, 0, 0)
canvasContainer.appendChild(canvas)
createCanvas("textureLoad", 0, img.width, img.height)
createCanvas("edgeDetect", 1, img.width, img.height)
})
img.src = src
}
// Default image
loadImage("image.png")
// File input
fileInput.addEventListener("change", event => loadImage(URL.createObjectURL(event.target.files[0])))
convert.addEventListener("click", () => {
// Get imageData from the image
const imageData = previewCanvasContext.getImageData(0, 0, previewCanvas.width, previewCanvas.height).data
// Pass the imageData to the C++ code
ccallArrays("loadTexture", null, ["array"], [imageData], {heapIn: "HEAPU8"})
ccallArrays("detectEdges", null, ["array"], [imageData], {heapIn: "HEAPU8"})
})
})
</script>
</head>
<body>
<input id="fileInput" type="file" accept="image/*" value="./image.png">
<button id="convert">Convert</button>
<br>
<br>
<span id="canvasContainer"></span>
</body>
</html>