Mercurial > hg > orthanc-stone
comparison 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 |
comparison
equal
deleted
inserted
replaced
1537:de8cf5859e84 | 1538:d1806b4e4839 |
---|---|
1 | |
2 // This object wraps the functions exposed by the wasm module | |
3 | |
4 const WasmModuleWrapper = function() { | |
5 this._InitializeViewport = undefined; | |
6 }; | |
7 | |
8 WasmModuleWrapper.prototype.Setup = function(Module) { | |
9 this._SetArgument = Module.cwrap('SetArgument', null, [ 'string', 'string' ]); | |
10 this._Initialize = Module.cwrap('Initialize', null, [ 'string' ]); | |
11 }; | |
12 | |
13 WasmModuleWrapper.prototype.SetArgument = function(key, value) { | |
14 this._SetArgument(key, value); | |
15 }; | |
16 | |
17 WasmModuleWrapper.prototype.Initialize = function(canvasId) { | |
18 this._Initialize(canvasId); | |
19 }; | |
20 | |
21 var wasmModuleWrapper = new WasmModuleWrapper(); | |
22 | |
23 $(document).ready(function() { | |
24 | |
25 window.addEventListener('WasmModuleInitialized', function() { | |
26 | |
27 // bind the C++ global functions | |
28 wasmModuleWrapper.Setup(Module); | |
29 | |
30 console.warn('Native C++ module initialized'); | |
31 | |
32 // Loop over the GET arguments | |
33 var parameters = window.location.search.substr(1); | |
34 if (parameters != null && parameters != '') { | |
35 var tokens = parameters.split('&'); | |
36 for (var i = 0; i < tokens.length; i++) { | |
37 var arg = tokens[i].split('='); | |
38 if (arg.length == 2) { | |
39 // Send each GET argument to WebAssembly | |
40 wasmModuleWrapper.SetArgument(arg[0], decodeURIComponent(arg[1])); | |
41 } | |
42 } | |
43 } | |
44 wasmModuleWrapper.Initialize(); | |
45 }); | |
46 | |
47 window.addEventListener('StoneException', function() { | |
48 alert('Exception caught in C++ code'); | |
49 }); | |
50 | |
51 var scriptSource; | |
52 | |
53 if ('WebAssembly' in window) { | |
54 console.warn('Loading WebAssembly'); | |
55 scriptSource = 'RtViewerWasm.js'; | |
56 } else { | |
57 console.error('Your browser does not support WebAssembly!'); | |
58 } | |
59 | |
60 // Option 1: Loading script using plain HTML | |
61 | |
62 /* | |
63 var script = document.createElement('script'); | |
64 script.src = scriptSource; | |
65 script.type = 'text/javascript'; | |
66 document.body.appendChild(script); | |
67 */ | |
68 | |
69 // Option 2: Loading script using AJAX (gives the opportunity to | |
70 // report explicit errors) | |
71 | |
72 axios.get(scriptSource) | |
73 .then(function (response) { | |
74 var script = document.createElement('script'); | |
75 script.innerHTML = response.data; | |
76 script.type = 'text/javascript'; | |
77 document.body.appendChild(script); | |
78 }) | |
79 .catch(function (error) { | |
80 alert('Cannot load the WebAssembly framework'); | |
81 }); | |
82 }); | |
83 | |
84 // 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 | |
85 |