comparison 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
comparison
equal deleted inserted replaced
312:a91ad36b684c 313:8bdc6112bc2e
17 function CreateWasmViewport(htmlCanvasId: string) : any { 17 function CreateWasmViewport(htmlCanvasId: string) : any {
18 var cppViewportHandle = CreateCppViewport(); 18 var cppViewportHandle = CreateCppViewport();
19 var canvasId = UTF8ToString(htmlCanvasId); 19 var canvasId = UTF8ToString(htmlCanvasId);
20 var webViewport = new Stone.WasmViewport(StoneFrameworkModule, canvasId, cppViewportHandle); // viewports are stored in a static map in WasmViewport -> won't be deleted 20 var webViewport = new Stone.WasmViewport(StoneFrameworkModule, canvasId, cppViewportHandle); // viewports are stored in a static map in WasmViewport -> won't be deleted
21 webViewport.Initialize(); 21 webViewport.Initialize();
22 22
23 return cppViewportHandle; 23 return cppViewportHandle;
24 } 24 }
25 25
26 module Stone { 26 module Stone {
27 27
32 //const ASSETS_FOLDER : string = "assets/lib"; 32 //const ASSETS_FOLDER : string = "assets/lib";
33 //const WASM_FILENAME : string = "orthanc-framework"; 33 //const WASM_FILENAME : string = "orthanc-framework";
34 34
35 export class WasmViewport { 35 export class WasmViewport {
36 36
37 private static cppWebViewportsMaps_ : Map<number, WasmViewport> = new Map<number, WasmViewport>(); 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
38 39
39 private module_ : any; 40 private module_ : any;
40 private canvasId_ : string; 41 private canvasId_ : string;
41 private htmlCanvas_ : HTMLCanvasElement; 42 private htmlCanvas_ : HTMLCanvasElement;
42 private context_ : CanvasRenderingContext2D; 43 private context_ : CanvasRenderingContext2D;
58 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object 59 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object
59 60
60 public constructor(module: any, canvasId: string, cppViewport: any) { 61 public constructor(module: any, canvasId: string, cppViewport: any) {
61 62
62 this.pimpl_ = cppViewport; 63 this.pimpl_ = cppViewport;
63 WasmViewport.cppWebViewportsMaps_[this.pimpl_] = this; 64 WasmViewport.viewportsMapByCppHandle_[this.pimpl_] = this;
65 WasmViewport.viewportsMapByCanvasId_[canvasId] = this;
64 66
65 this.module_ = module; 67 this.module_ = module;
66 this.canvasId_ = canvasId; 68 this.canvasId_ = canvasId;
67 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement; 69 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement;
68 if (this.htmlCanvas_ == null) { 70 if (this.htmlCanvas_ == null) {
84 public GetCppViewport() : number { 86 public GetCppViewport() : number {
85 return this.pimpl_; 87 return this.pimpl_;
86 } 88 }
87 89
88 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport { 90 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport {
89 if (WasmViewport.cppWebViewportsMaps_[cppViewportHandle] !== undefined) { 91 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) {
90 return WasmViewport.cppWebViewportsMaps_[cppViewportHandle]; 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];
91 } 101 }
92 console.log("WasmViewport not found !"); 102 console.log("WasmViewport not found !");
93 return undefined; 103 return undefined;
94 } 104 }
95 105
137 if (this.renderingBuffer_ != null) { 147 if (this.renderingBuffer_ != null) {
138 this.module_._free(this.renderingBuffer_); 148 this.module_._free(this.renderingBuffer_);
139 } 149 }
140 150
141 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4); 151 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4);
152 } else {
153 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height);
142 } 154 }
143 155
144 this.Redraw(); 156 this.Redraw();
145 } 157 }
146 158