-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.html
82 lines (76 loc) · 2.35 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
80
81
82
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go wasm</title>
<style>
.image {
display: block;
max-height: 25rem;
width: 49%;
float: left;
border: 2px solid gray;
margin-right: 2px;
}
.separator {
clear: both;
}
</style>
</head>
<body>
<div id="status"></div>
<input type="file" id="uploader" />
<button id="close">Shutdown app</button>
<br />
<label for="brightness">Brightness</label>
<input type="range" min="-1" max="1" value="0" step="0.1" id="brightness">
<label for="contrast">Contrast</label>
<input type="range" min="-1" max="1" value="0" step="0.1" id="contrast">
<label for="hue">Hue</label>
<input type="range" min="-360" max="360" value="0" step="10" id="hue">
<label for="sat">Saturation</label>
<input type="range" min="-1" max="1" value="0" step="0.1" id="sat">
<div class="separator">Results:</div>
<div>
<image id="sourceImg" class="image" />
<image id="targetImg" class="image" />
</div>
<script src="wasm_loader.js"></script>
<script>
const go = new Go();
// memoryBytes is an Uint8Array pointing to the webassembly linear memory.
let memoryBytes;
let mod, inst, bytes;
let imageType;
document.getElementById('status').innerText = "Initializing wasm...";
WebAssembly.instantiateStreaming(
fetch("shimmer.wasm", {cache: 'no-cache'}), go.importObject).then((result) => {
mod = result.module;
inst = result.instance;
memoryBytes = new Uint8Array(inst.exports.mem.buffer)
document.getElementById('status').innerText = "Initialization complete.";
run();
});
async function run() {
await go.run(inst);
}
// displayImage takes the pointer to the target image in the wasm linear memory
// and its length. Gets the resulting byte slice and creates an image blob.
function displayImage(buf) {
let blob = new Blob([buf], {'type': imageType});
document.getElementById('targetImg').src = URL.createObjectURL(blob);
}
document.getElementById('uploader').addEventListener('change', function() {
let reader = new FileReader();
reader.onload = (ev) => {
bytes = new Uint8Array(ev.target.result);
loadImage(bytes);
let blob = new Blob([bytes], {'type': imageType});
document.getElementById("sourceImg").src = URL.createObjectURL(blob);
};
imageType = this.files[0].type;
reader.readAsArrayBuffer(this.files[0]);
});
</script>
</body>
</html>