Mercurial > hg > orthanc-stone
diff Applications/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js @ 1538:d1806b4e4839
moving OrthancStone/Samples/ as Applications/Samples/
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Aug 2020 13:24:38 +0200 |
parents | OrthancStone/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js@307a805d0587 |
children | 8c5f9864545f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Applications/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js Tue Aug 11 13:24:38 2020 +0200 @@ -0,0 +1,85 @@ + +// This object wraps the functions exposed by the wasm module + +const WasmModuleWrapper = function() { + this._InitializeViewport = undefined; +}; + +WasmModuleWrapper.prototype.Setup = function(Module) { + this._SetArgument = Module.cwrap('SetArgument', null, [ 'string', 'string' ]); + this._Initialize = Module.cwrap('Initialize', null, [ 'string' ]); +}; + +WasmModuleWrapper.prototype.SetArgument = function(key, value) { + this._SetArgument(key, value); +}; + +WasmModuleWrapper.prototype.Initialize = function(canvasId) { + this._Initialize(canvasId); +}; + +var wasmModuleWrapper = new WasmModuleWrapper(); + +$(document).ready(function() { + + window.addEventListener('WasmModuleInitialized', function() { + + // bind the C++ global functions + wasmModuleWrapper.Setup(Module); + + console.warn('Native C++ module initialized'); + + // Loop over the GET arguments + var parameters = window.location.search.substr(1); + if (parameters != null && parameters != '') { + var tokens = parameters.split('&'); + for (var i = 0; i < tokens.length; i++) { + var arg = tokens[i].split('='); + if (arg.length == 2) { + // Send each GET argument to WebAssembly + wasmModuleWrapper.SetArgument(arg[0], decodeURIComponent(arg[1])); + } + } + } + wasmModuleWrapper.Initialize(); + }); + + window.addEventListener('StoneException', function() { + alert('Exception caught in C++ code'); + }); + + var scriptSource; + + if ('WebAssembly' in window) { + console.warn('Loading WebAssembly'); + scriptSource = 'RtViewerWasm.js'; + } else { + console.error('Your browser does not support WebAssembly!'); + } + + // Option 1: Loading script using plain HTML + + /* + var script = document.createElement('script'); + script.src = scriptSource; + script.type = 'text/javascript'; + document.body.appendChild(script); + */ + + // Option 2: Loading script using AJAX (gives the opportunity to + // report explicit errors) + + axios.get(scriptSource) + .then(function (response) { + var script = document.createElement('script'); + script.innerHTML = response.data; + script.type = 'text/javascript'; + document.body.appendChild(script); + }) + .catch(function (error) { + alert('Cannot load the WebAssembly framework'); + }); +}); + +// http://localhost:9979/stone-rtviewer/index.html?loglevel=trace&ctseries=a04ecf01-79b2fc33-58239f7e-ad9db983-28e81afa&rtdose=830a69ff-8e4b5ee3-b7f966c8-bccc20fb-d322dceb&rtstruct=54460695-ba3885ee-ddf61ac0-f028e31d-a6e474d9 +