Mercurial > hg > orthanc-stone
annotate Applications/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js @ 1827:21ccc00839f7 StoneWebViewer-2.0
upgrade to Emscripten 2.0.23
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 May 2021 11:05:59 +0200 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
rev | line source |
---|---|
1589
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
1 /** |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
2 * Stone of Orthanc |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
1589
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
6 * |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
10 * the License, or (at your option) any later version. |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
11 * |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
15 * Affero General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1589
diff
changeset
|
16 * |
1589
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
19 **/ |
8c5f9864545f
adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1538
diff
changeset
|
20 |
1393
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
21 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
22 // This object wraps the functions exposed by the wasm module |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
23 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
24 const WasmModuleWrapper = function() { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
25 this._InitializeViewport = undefined; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
26 }; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
27 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
28 WasmModuleWrapper.prototype.Setup = function(Module) { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
29 this._SetArgument = Module.cwrap('SetArgument', null, [ 'string', 'string' ]); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
30 this._Initialize = Module.cwrap('Initialize', null, [ 'string' ]); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
31 }; |
1384
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
32 |
1393
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
33 WasmModuleWrapper.prototype.SetArgument = function(key, value) { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
34 this._SetArgument(key, value); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
35 }; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
36 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
37 WasmModuleWrapper.prototype.Initialize = function(canvasId) { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
38 this._Initialize(canvasId); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
39 }; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
40 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
41 var wasmModuleWrapper = new WasmModuleWrapper(); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
42 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
43 $(document).ready(function() { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
44 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
45 window.addEventListener('WasmModuleInitialized', function() { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
46 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
47 // bind the C++ global functions |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
48 wasmModuleWrapper.Setup(Module); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
49 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
50 console.warn('Native C++ module initialized'); |
1384
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
51 |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
52 // Loop over the GET arguments |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
53 var parameters = window.location.search.substr(1); |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
54 if (parameters != null && parameters != '') { |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
55 var tokens = parameters.split('&'); |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
56 for (var i = 0; i < tokens.length; i++) { |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
57 var arg = tokens[i].split('='); |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
58 if (arg.length == 2) { |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
59 // Send each GET argument to WebAssembly |
1393
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
60 wasmModuleWrapper.SetArgument(arg[0], decodeURIComponent(arg[1])); |
1384
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
61 } |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
62 } |
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
63 } |
1393
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
64 wasmModuleWrapper.Initialize(); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
65 }); |
1384
24bcff8ea58f
RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
66 |
1393
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
67 window.addEventListener('StoneException', function() { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
68 alert('Exception caught in C++ code'); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
69 }); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
70 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
71 var scriptSource; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
72 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
73 if ('WebAssembly' in window) { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
74 console.warn('Loading WebAssembly'); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
75 scriptSource = 'RtViewerWasm.js'; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
76 } else { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
77 console.error('Your browser does not support WebAssembly!'); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
78 } |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
79 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
80 // Option 1: Loading script using plain HTML |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
81 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
82 /* |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
83 var script = document.createElement('script'); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
84 script.src = scriptSource; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
85 script.type = 'text/javascript'; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
86 document.body.appendChild(script); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
87 */ |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
88 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
89 // Option 2: Loading script using AJAX (gives the opportunity to |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
90 // report explicit errors) |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
91 |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
92 axios.get(scriptSource) |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
93 .then(function (response) { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
94 var script = document.createElement('script'); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
95 script.innerHTML = response.data; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
96 script.type = 'text/javascript'; |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
97 document.body.appendChild(script); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
98 }) |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
99 .catch(function (error) { |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
100 alert('Cannot load the WebAssembly framework'); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
101 }); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
102 }); |
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
103 |
1517
307a805d0587
rtviewer --> stone-rtviewer
Benjamin Golinvaux <bgo@osimis.io>
parents:
1512
diff
changeset
|
104 // 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 |
1393
27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents:
1384
diff
changeset
|
105 |