Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created June 26, 2020 07:44
Show Gist options
  • Save feliwir/9cebe8941ee8f88895a5c9bfec574ca1 to your computer and use it in GitHub Desktop.
Save feliwir/9cebe8941ee8f88895a5c9bfec574ca1 to your computer and use it in GitHub Desktop.
var Module = {
preRun: [],
postRun: [],
print: (function() {
return function(text) {
if (arguments.length > 1)
{
text = Array.prototype.slice.call(arguments).join(' ');
}
console.log(text);
};
})(),
printErr: function(text) {
if (arguments.length > 1)
{
text = Array.prototype.slice.call(arguments).join(' ');
}
console.error(text);
},
canvas: (function() {
var canvas = document.getElementById('canvas');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
spinnerElement.hidden = false;
}
else {
if (!text) spinnerElement.hidden = true;
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
window.ondragover = function(ev) {ev.preventDefault()}
window.ondrop = function (ev) {
ev.preventDefault();
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
for (var i = 0; i < ev.dataTransfer.items.length; i++) {
// If dropped items aren't files, reject them
if (ev.dataTransfer.items[i].kind === 'file') {
var file = ev.dataTransfer.items[i].getAsFile();
console.log('... file[' + i + '].name = ' + file.name);
Module.FS.writeFile(file.name,arrayBuf)
}
}
} else {
// Use DataTransfer interface to access the file(s)
for (var i = 0; i < ev.dataTransfer.files.length; i++) {
var file = ev.dataTransfer.files[i];
var arrayBuf = file.arrayBuffer();
//TODO: get this into C++
Module.FS.writeFile(file.name,arrayBuf)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment