-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
337 lines (299 loc) · 8.41 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
var Main = function() {
var _interval = 66;
var _pause = 0;
var _showStats = false;
var _counter = 0;
var _renderTime = 0;
function toggleFullscreen()
{
if (document.fullscreenElement) {
return document.exitFullscreen();
}
else {
return document.documentElement.requestFullscreen();
}
}
function importFile(f)
{
const fileReader = new FileReader();
fileReader.onload = (event) => {
const imageData = new Uint8Array(event.target.result);
const numBytes = imageData.length * imageData.BYTES_PER_ELEMENT;
const dataPtr = Module._malloc(numBytes);
const dataOnHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, numBytes);
dataOnHeap.set(imageData);
Main.encode(f.name, dataOnHeap);
Module._free(dataPtr);
Main.setHTML("current-file", f.name);
};
fileReader.onerror = () => {
console.error('Unable to read file ' + f.name + '.');
};
fileReader.readAsArrayBuffer(f);
}
// public interface
return {
init : function(canvas)
{
Module._initialize_GL(1040, 1040);
Main.resize();
Main.check_GL_enabled(canvas);
},
check_GL_enabled : function(canvas)
{
if (canvas.getContext("2d")) {
var elem = document.getElementById('dragdrop');
elem.classList.add("error");
}
},
resize : function()
{
// reset zoom
var canvas = document.getElementById('canvas');
var width = window.innerWidth - 10;
var height = window.innerHeight - 10;
Main.scaleCanvas(canvas, width, height);
Main.alignInvisibleClick(canvas);
},
toggleFullscreen : function()
{
toggleFullscreen().then(Main.resize);
Main.togglePause(true);
},
togglePause : function(pause)
{
// pause is a cooldown. We pause to help autofocus, but we don't want to do it forever...
if (pause === undefined) {
pause = !Main.isPaused();
}
_pause = pause? 15 : 0;
},
isPaused : function()
{
return _pause > 0;
},
scaleCanvas : function(canvas, width, height)
{
var dim = width;
if (height < dim) {
dim = height;
}
console.log(dim + "x" + dim);
canvas.style.width = dim + "px";
canvas.style.height = dim + "px";
},
alignInvisibleClick : function(canvas)
{
canvas = canvas || document.getElementById('canvas');
var cpos = canvas.getBoundingClientRect();
var invisible_click = document.getElementById("invisible_click");
invisible_click.style.width = canvas.style.width;
invisible_click.style.height = canvas.style.height;
invisible_click.style.top = cpos.top + "px";
invisible_click.style.left = cpos.left + "px";
invisible_click.style.zoom = canvas.style.zoom;
},
encode : function(filename, data)
{
console.log("encoding " + filename);
var res = Module._encode(data.byteOffset, data.length, -1);
console.log(res);
Main.setTitle(filename);
Main.setActive(true);
},
dragDrop : function(event)
{
console.log("drag drop?");
console.log(event);
const files = event.dataTransfer.files;
if (files && files.length === 1) {
importFile(files[0]);
}
},
clickNav : function()
{
document.getElementById("nav-button").focus();
},
blurNav : function(pause)
{
if (pause === undefined) {
pause = true;
}
document.getElementById("nav-button").blur();
document.getElementById("nav-content").blur();
document.getElementById("nav-find-file-link").blur();
Main.togglePause(pause);
},
clickFileInput : function()
{
document.getElementById("file_input").click();
},
fileInput : function(ev)
{
console.log("file input: " + ev);
var file = document.getElementById('file_input').files[0];
if (file)
importFile(file);
Main.blurNav(false);
},
nextFrame : function()
{
_counter += 1;
if (_pause > 0) {
_pause -= 1;
}
var start = performance.now();
if (!Main.isPaused()) {
Module._render();
var frameCount = Module._next_frame();
}
var elapsed = performance.now() - start;
var nextInterval = _interval>elapsed? _interval-elapsed : 0;
setTimeout(Main.nextFrame, nextInterval);
if (_showStats && frameCount) {
_renderTime += elapsed;
Main.setHTML( "status", elapsed + " : " + frameCount + " : " + Math.ceil(_renderTime/frameCount));
}
if ( !(_counter & 31) ) {
Main.resize();
}
},
setActive : function(active)
{
// hide cursor when there's a barcode active
var invisi = document.getElementById("invisible_click");
invisi.classList.remove("active");
invisi.classList.add("active");
},
setMode : function(mode_str)
{
var is_4c = (mode_str == "4C");
Module._configure(2, 255, 255, is_4c);
var nav = document.getElementById("nav-container");
if (is_4c) {
nav.classList.remove("mode-b");
nav.classList.add("mode-4c");
} else if (mode_str == "B") {
nav.classList.add("mode-b");
nav.classList.remove("mode-4c");
} else {
nav.classList.remove("mode-b");
nav.classList.remove("mode-4c");
}
},
setHTML : function(id, msg)
{
document.getElementById(id).innerHTML = msg;
},
setTitle : function(msg)
{
document.title = "Cimbar: " + msg;
}
};
}();
window.addEventListener('keydown', function(e) {
e = e || event;
if (e.target instanceof HTMLBodyElement) {
if (e.key == 'Enter' || e.keyCode == 13 ||
e.key == 'Tab' || e.keyCode == 9 ||
e.key == 'Space' || e.keyCode == 32
) {
Main.clickNav();
e.preventDefault();
}
else if (e.key == 'Backspace' || e.keyCode == 8) {
Main.togglePause(true);
e.preventDefault();
}
}
else {
if (e.key == 'Escape' || e.keyCode == 27 ||
e.key == 'Backspace' || e.keyCode == 8 ||
e.key == 'End' || e.keyCode == 35 ||
e.key == 'Home' || e.keyCode == 36
) {
Main.blurNav();
}
else if (e.key == 'Tab' || e.keyCode == 9 ||
e.key == 'ArrowDown' || e.keyCode == 40
) {
var nav = document.getElementById('nav-button');
var links = document.getElementById('nav-content').getElementsByTagName('a');
if (nav.classList.contains('attention')) {
nav.classList.remove('attention');
links[0].classList.add('attention');
return;
}
for (var i = 0; i < links.length; i++) {
if (links[i].classList.contains('attention')) {
var next = i+1 == links.length? nav : links[i+1];
links[i].classList.remove('attention');
next.classList.add('attention');
break;
}
}
}
else if (e.key == 'ArrowUp' || e.keyCode == 38)
{
var nav = document.getElementById('nav-button');
var links = document.getElementById('nav-content').getElementsByTagName('a');
if (nav.classList.contains('attention')) {
nav.classList.remove('attention');
links[links.length-1].classList.add('attention');
return;
}
for (var i = 0; i < links.length; i++) {
if (links[i].classList.contains('attention')) {
var next = i == 0? nav : links[i-1];
links[i].classList.remove('attention');
next.classList.add('attention');
break;
}
}
}
else if (e.key == 'Enter' || e.keyCode == 13 ||
e.key == ' ' || e.keyCode == 32
) {
var nav = document.getElementById('nav-button');
if (nav.classList.contains('attention')) {
Main.blurNav();
return;
}
var links = document.getElementById('nav-content').getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (links[i].classList.contains('attention')) {
links[i].click();
}
}
}
}
}, true);
window.addEventListener("touchstart", function(e) {
e = e || event;
Main.togglePause(true);
}, false);
window.addEventListener("touchend", function(e) {
e = e || event;
Main.togglePause(false);
}, false);
window.addEventListener("touchcancel", function(e) {
e = e || event;
Main.togglePause(false);
}, false);
window.addEventListener("dragover", function(e) {
e = e || event;
e.preventDefault();
document.body.style["opacity"] = 0.5;
}, false);
window.addEventListener("dragleave", function(e) {
e = e || event;
e.preventDefault();
document.body.style["opacity"] = 1.0;
}, false);
window.addEventListener("drop", function(e) {
e = e || event;
e.preventDefault();
e.stopPropagation();
Main.dragDrop(e);
document.body.style["opacity"] = 1.0;
}, false);