Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/wasm-viewport.ts @ 313:8bdc6112bc2e am-2
initial resize of canvas
author | am@osimis.io |
---|---|
date | Wed, 03 Oct 2018 17:01:05 +0200 |
parents | 313903066093 |
children | 97f16214dc5e |
rev | line source |
---|---|
230 | 1 var isPendingRedraw = false; |
229 | 2 |
230 | 3 function ScheduleWebViewportRedraw(cppViewportHandle: any) : void |
4 { | |
5 if (!isPendingRedraw) { | |
6 isPendingRedraw = true; | |
7 console.log('Scheduling a refresh of the viewport, as its content changed'); | |
8 window.requestAnimationFrame(function() { | |
9 isPendingRedraw = false; | |
10 Stone.WasmViewport.GetFromCppViewport(cppViewportHandle).Redraw(); | |
11 }); | |
229 | 12 } |
230 | 13 } |
226 | 14 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
15 declare function UTF8ToString(any): string; |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
16 |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
17 function CreateWasmViewport(htmlCanvasId: string) : any { |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
18 var cppViewportHandle = CreateCppViewport(); |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
19 var canvasId = UTF8ToString(htmlCanvasId); |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
20 var webViewport = new Stone.WasmViewport(StoneFrameworkModule, canvasId, cppViewportHandle); // viewports are stored in a static map in WasmViewport -> won't be deleted |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
21 webViewport.Initialize(); |
313 | 22 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
23 return cppViewportHandle; |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
24 } |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
25 |
226 | 26 module Stone { |
27 | |
28 // export declare type InitializationCallback = () => void; | |
29 | |
30 // export declare var StoneFrameworkModule : any; | |
31 | |
32 //const ASSETS_FOLDER : string = "assets/lib"; | |
33 //const WASM_FILENAME : string = "orthanc-framework"; | |
229 | 34 |
226 | 35 export class WasmViewport { |
36 | |
313 | 37 private static viewportsMapByCppHandle_ : Map<number, WasmViewport> = new Map<number, WasmViewport>(); // key = the C++ handle |
38 private static viewportsMapByCanvasId_ : Map<string, WasmViewport> = new Map<string, WasmViewport>(); // key = the canvasId | |
229 | 39 |
226 | 40 private module_ : any; |
41 private canvasId_ : string; | |
42 private htmlCanvas_ : HTMLCanvasElement; | |
43 private context_ : CanvasRenderingContext2D; | |
44 private imageData_ : any = null; | |
45 private renderingBuffer_ : any = null; | |
46 private touchZoom_ : any = false; | |
47 private touchTranslation_ : any = false; | |
48 | |
49 private ViewportSetSize : Function; | |
50 private ViewportRender : Function; | |
51 private ViewportMouseDown : Function; | |
52 private ViewportMouseMove : Function; | |
53 private ViewportMouseUp : Function; | |
54 private ViewportMouseEnter : Function; | |
55 private ViewportMouseLeave : Function; | |
56 private ViewportMouseWheel : Function; | |
57 private ViewportKeyPressed : Function; | |
58 | |
227 | 59 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object |
60 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
61 public constructor(module: any, canvasId: string, cppViewport: any) { |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
62 |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
63 this.pimpl_ = cppViewport; |
313 | 64 WasmViewport.viewportsMapByCppHandle_[this.pimpl_] = this; |
65 WasmViewport.viewportsMapByCanvasId_[canvasId] = this; | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
66 |
226 | 67 this.module_ = module; |
68 this.canvasId_ = canvasId; | |
69 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement; | |
244 | 70 if (this.htmlCanvas_ == null) { |
71 console.log("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'"); | |
72 } | |
226 | 73 this.context_ = this.htmlCanvas_.getContext('2d'); |
74 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
75 this.ViewportSetSize = this.module_.cwrap('ViewportSetSize', null, [ 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
76 this.ViewportRender = this.module_.cwrap('ViewportRender', null, [ 'number', 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
77 this.ViewportMouseDown = this.module_.cwrap('ViewportMouseDown', null, [ 'number', 'number', 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
78 this.ViewportMouseMove = this.module_.cwrap('ViewportMouseMove', null, [ 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
79 this.ViewportMouseUp = this.module_.cwrap('ViewportMouseUp', null, [ 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
80 this.ViewportMouseEnter = this.module_.cwrap('ViewportMouseEnter', null, [ 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
81 this.ViewportMouseLeave = this.module_.cwrap('ViewportMouseLeave', null, [ 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
82 this.ViewportMouseWheel = this.module_.cwrap('ViewportMouseWheel', null, [ 'number', 'number', 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
83 this.ViewportKeyPressed = this.module_.cwrap('ViewportKeyPressed', null, [ 'number', 'string', 'number', 'number' ]); |
228 | 84 } |
85 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
86 public GetCppViewport() : number { |
228 | 87 return this.pimpl_; |
226 | 88 } |
89 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
90 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport { |
313 | 91 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) { |
92 return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle]; | |
93 } | |
94 console.log("WasmViewport not found !"); | |
95 return undefined; | |
96 } | |
97 | |
98 public static GetFromCanvasId(canvasId: string) : WasmViewport { | |
99 if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) { | |
100 return WasmViewport.viewportsMapByCanvasId_[canvasId]; | |
229 | 101 } |
102 console.log("WasmViewport not found !"); | |
103 return undefined; | |
104 } | |
105 | |
226 | 106 public Redraw() { |
107 if (this.imageData_ === null || | |
108 this.renderingBuffer_ === null || | |
227 | 109 this.ViewportRender(this.pimpl_, |
110 this.imageData_.width, | |
226 | 111 this.imageData_.height, |
112 this.renderingBuffer_) == 0) { | |
113 console.log('The rendering has failed'); | |
114 } else { | |
115 // Create an accessor to the rendering buffer (i.e. create a | |
116 // "window" above the heap of the WASM module), then copy it to | |
117 // the ImageData object | |
118 this.imageData_.data.set(new Uint8ClampedArray( | |
119 this.module_.buffer, | |
120 this.renderingBuffer_, | |
121 this.imageData_.width * this.imageData_.height * 4)); | |
122 | |
123 this.context_.putImageData(this.imageData_, 0, 0); | |
124 } | |
125 } | |
126 | |
127 public Resize() { | |
128 if (this.imageData_ != null && | |
129 (this.imageData_.width != window.innerWidth || | |
130 this.imageData_.height != window.innerHeight)) { | |
131 this.imageData_ = null; | |
132 } | |
133 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
134 // width/height can be defined in percent of window width/height through html attributes like data-width-ratio="50" and data-height-ratio="20" |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
135 var widthRatio = Number(this.htmlCanvas_.dataset["widthRatio"]) || 100; |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
136 var heightRatio = Number(this.htmlCanvas_.dataset["heightRatio"]) || 100; |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
137 |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
138 this.htmlCanvas_.width = window.innerWidth * (widthRatio / 100); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
139 this.htmlCanvas_.height = window.innerHeight * (heightRatio / 100); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
140 |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
141 console.log("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
142 |
226 | 143 if (this.imageData_ === null) { |
144 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
228 | 145 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); |
226 | 146 |
147 if (this.renderingBuffer_ != null) { | |
148 this.module_._free(this.renderingBuffer_); | |
149 } | |
150 | |
151 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4); | |
313 | 152 } else { |
153 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
226 | 154 } |
155 | |
156 this.Redraw(); | |
157 } | |
158 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
159 public Initialize() { |
227 | 160 |
161 // Force the rendering of the viewport for the first time | |
162 this.Resize(); | |
226 | 163 |
228 | 164 var that : WasmViewport = this; |
227 | 165 // Register an event listener to call the Resize() function |
166 // each time the window is resized. | |
228 | 167 window.addEventListener('resize', function(event) { |
168 that.Resize(); | |
169 }, false); | |
226 | 170 |
227 | 171 this.htmlCanvas_.addEventListener('contextmenu', function(event) { |
172 // Prevent right click on the canvas | |
173 event.preventDefault(); | |
174 }, false); | |
175 | |
176 this.htmlCanvas_.addEventListener('mouseleave', function(event) { | |
228 | 177 that.ViewportMouseLeave(that.pimpl_); |
227 | 178 }); |
179 | |
180 this.htmlCanvas_.addEventListener('mouseenter', function(event) { | |
228 | 181 that.ViewportMouseEnter(that.pimpl_); |
227 | 182 }); |
183 | |
184 this.htmlCanvas_.addEventListener('mousedown', function(event) { | |
185 var x = event.pageX - this.offsetLeft; | |
186 var y = event.pageY - this.offsetTop; | |
228 | 187 that.ViewportMouseDown(that.pimpl_, event.button, x, y, 0 /* TODO */); |
227 | 188 }); |
189 | |
190 this.htmlCanvas_.addEventListener('mousemove', function(event) { | |
191 var x = event.pageX - this.offsetLeft; | |
192 var y = event.pageY - this.offsetTop; | |
228 | 193 that.ViewportMouseMove(that.pimpl_, x, y); |
227 | 194 }); |
195 | |
196 this.htmlCanvas_.addEventListener('mouseup', function(event) { | |
228 | 197 that.ViewportMouseUp(that.pimpl_); |
227 | 198 }); |
199 | |
200 window.addEventListener('keydown', function(event) { | |
228 | 201 that.ViewportKeyPressed(that.pimpl_, event.key, event.shiftKey, event.ctrlKey, event.altKey); |
227 | 202 }); |
226 | 203 |
227 | 204 this.htmlCanvas_.addEventListener('wheel', function(event) { |
205 var x = event.pageX - this.offsetLeft; | |
206 var y = event.pageY - this.offsetTop; | |
228 | 207 that.ViewportMouseWheel(that.pimpl_, event.deltaY, x, y, event.ctrlKey); |
227 | 208 event.preventDefault(); |
209 }); | |
210 | |
211 this.htmlCanvas_.addEventListener('touchstart', function(event) { | |
212 that.ResetTouch(); | |
213 }); | |
214 | |
215 this.htmlCanvas_.addEventListener('touchend', function(event) { | |
216 that.ResetTouch(); | |
217 }); | |
218 | |
219 this.htmlCanvas_.addEventListener('touchmove', function(event) { | |
220 if (that.touchTranslation_.length == 2) { | |
221 var t = that.GetTouchTranslation(event); | |
228 | 222 that.ViewportMouseMove(that.pimpl_, t[0], t[1]); |
227 | 223 } |
224 else if (that.touchZoom_.length == 3) { | |
226 | 225 var z0 = that.touchZoom_; |
227 | 226 var z1 = that.GetTouchZoom(event); |
228 | 227 that.ViewportMouseMove(that.pimpl_, z0[0], z0[1] - z0[2] + z1[2]); |
227 | 228 } |
229 else { | |
230 // Realize the gesture event | |
231 if (event.targetTouches.length == 1) { | |
232 // Exactly one finger inside the canvas => Setup a translation | |
233 that.touchTranslation_ = that.GetTouchTranslation(event); | |
228 | 234 that.ViewportMouseDown(that.pimpl_, |
235 1 /* middle button */, | |
227 | 236 that.touchTranslation_[0], |
237 that.touchTranslation_[1], 0); | |
238 } else if (event.targetTouches.length == 2) { | |
239 // Exactly 2 fingers inside the canvas => Setup a pinch/zoom | |
240 that.touchZoom_ = that.GetTouchZoom(event); | |
241 var z0 = that.touchZoom_; | |
228 | 242 that.ViewportMouseDown(that.pimpl_, |
243 2 /* right button */, | |
227 | 244 z0[0], |
245 z0[1], 0); | |
246 } | |
247 } | |
248 }); | |
249 } | |
226 | 250 |
251 public ResetTouch() { | |
227 | 252 if (this.touchTranslation_ || |
253 this.touchZoom_) { | |
228 | 254 this.ViewportMouseUp(this.pimpl_); |
226 | 255 } |
227 | 256 |
257 this.touchTranslation_ = false; | |
258 this.touchZoom_ = false; | |
259 } | |
226 | 260 |
261 public GetTouchTranslation(event) { | |
227 | 262 var touch = event.targetTouches[0]; |
263 return [ | |
264 touch.pageX, | |
265 touch.pageY | |
266 ]; | |
267 } | |
226 | 268 |
269 public GetTouchZoom(event) { | |
227 | 270 var touch1 = event.targetTouches[0]; |
271 var touch2 = event.targetTouches[1]; | |
272 var dx = (touch1.pageX - touch2.pageX); | |
273 var dy = (touch1.pageY - touch2.pageY); | |
274 var d = Math.sqrt(dx * dx + dy * dy); | |
275 return [ | |
276 (touch1.pageX + touch2.pageX) / 2.0, | |
277 (touch1.pageY + touch2.pageY) / 2.0, | |
278 d | |
279 ]; | |
280 } | |
226 | 281 |
227 | 282 } |
226 | 283 } |
284 |