-
Notifications
You must be signed in to change notification settings - Fork 21
/
script.js
138 lines (93 loc) · 2.41 KB
/
script.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
document.addEventListener("DOMContentLoaded", main, false);
function wasmLoad(fileName) {
var memory = new WebAssembly.Memory({ initial: 100, maximum: 1000 });
heap = new Uint8Array(memory.buffer);
var imports = {
env: {
console_log: function(arg) { console.log(arg); },
memory: memory,
}
};
var request = new XMLHttpRequest();
request.open("GET", fileName);
request.responseType = "arraybuffer";
request.send();
request.onload = function() {
var wasmSource = request.response;
var wasmModule = new WebAssembly.Module(wasmSource);
var wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
wasmLoadDone();
} // XMLHttpRequest.onload()
} // loadWasm()
function wasmSimpleTest() {
console.log( wasm.inc(99) );
heap[0] = 99; heap[1] = 99; heap[2] = 99;
wasm.incmem();
console.log( heap[0], heap[1], heap[2] );
} // wasmSimpleTest()
function imageLoad() {
img = new Image();
img.crossOrigin = 'anonymous';
img.src = "volt.jpg";
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
img.onload = function() {
ctx.drawImage(img, 0, 0);
img.style.display = "none";
imageLoadDone();
};
} // imageLoad()
function prepareCanvas() {
var title = document.getElementById("title");
title.innerHTML = img.width + " x " + img.height;
ctx.width = img.width;
ctx.width = img.height * 2;
imageProcMode = 0;
}
function copyTopImageToHeap() {
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
count = canvas.width * canvas.height * 4;
for (var i = 0; i < count; i++) {
heap[i] = imageData.data[i];
}
}
function copyHeapToBottomImage() {
for (var i = 0; i < count; i++) {
imageData.data[i] = heap[i];
}
ctx.putImageData(imageData, 0, img.height);
}
function imageProc() {
copyTopImageToHeap();
wasm.swap_red(canvas.width, canvas.height, imageProcMode);
imageProcMode++;
copyHeapToBottomImage();
}
//===============================================================
function main() {
wasmLoad("inc.wasm");
}
function wasmLoadDone() {
wasmSimpleTest();
imageLoad();
}
function imageLoadDone() {
prepareCanvas();
carousel();
}
function carousel() {
imageProc();
blurCount = 0;
setTimeout(blur, 1000);
}
function blur() {
wasm.blur(canvas.width, canvas.height);
copyHeapToBottomImage();
blurCount++;
if (blurCount > 60) {
setTimeout(carousel, 100);
} else {
setTimeout(blur, 10);
}
}