comparison OrthancStone/Sources/Viewport/WebAssemblyCairoViewport.cpp @ 1576:92fca2b3ba3d

sanitizing the handling of canvas size
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 24 Sep 2020 16:40:30 +0200
parents 314b6dc507d9
children
comparison
equal deleted inserted replaced
1575:e4a52cbbdd70 1576:92fca2b3ba3d
32 #endif 32 #endif
33 33
34 34
35 #include <Images/Image.h> 35 #include <Images/Image.h>
36 36
37 #include <boost/math/special_functions/round.hpp>
38
39 namespace OrthancStone 37 namespace OrthancStone
40 { 38 {
41 void WebAssemblyCairoViewport::GetCanvasSize(unsigned int& width,
42 unsigned int& height)
43 {
44 double w, h;
45 emscripten_get_element_css_size(GetCanvasCssSelector().c_str(), &w, &h);
46
47 /**
48 * Emscripten has the function emscripten_get_element_css_size()
49 * to query the width and height of a named HTML element. I'm
50 * calling this first to get the initial size of the canvas DOM
51 * element, and then call emscripten_set_canvas_size() to
52 * initialize the framebuffer size of the canvas to the same
53 * size as its DOM element.
54 * https://floooh.github.io/2017/02/22/emsc-html.html
55 **/
56 if (w > 0 &&
57 h > 0)
58 {
59 width = static_cast<unsigned int>(boost::math::iround(w));
60 height = static_cast<unsigned int>(boost::math::iround(h));
61 }
62 else
63 {
64 width = 0;
65 height = 0;
66 }
67 }
68
69
70 void WebAssemblyCairoViewport::Paint(ICompositor& compositor, 39 void WebAssemblyCairoViewport::Paint(ICompositor& compositor,
71 ViewportController& controller) 40 ViewportController& controller)
72 { 41 {
73 compositor.Refresh(controller.GetScene()); 42 compositor.Refresh(controller.GetScene());
74 43
121 javascript_->GetWidth(), // $2 90 javascript_->GetWidth(), // $2
122 javascript_->GetHeight()); // $3 91 javascript_->GetHeight()); // $3
123 } 92 }
124 93
125 94
126 void WebAssemblyCairoViewport::UpdateSize(ICompositor& compositor)
127 {
128 unsigned int width, height;
129 GetCanvasSize(width, height);
130 emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), width, height);
131
132 dynamic_cast<CairoCompositor&>(compositor).UpdateSize(width, height);
133 }
134
135
136 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvasId, 95 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvasId,
137 bool enableEmscriptenMouseEvents) : 96 bool enableEmscriptenMouseEvents) :
138 WebAssemblyViewport(canvasId,enableEmscriptenMouseEvents) 97 WebAssemblyViewport(canvasId,enableEmscriptenMouseEvents)
139 { 98 {
140 unsigned int width, height; 99 RefreshCanvasSize();
141 GetCanvasSize(width, height); 100 AcquireCompositor(new CairoCompositor(GetCanvasWidth(), GetCanvasHeight()));
142 emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), width, height);
143
144 AcquireCompositor(new CairoCompositor(width, height));
145 } 101 }
146 102
147 103
148 boost::shared_ptr<WebAssemblyCairoViewport> WebAssemblyCairoViewport::Create( 104 boost::shared_ptr<WebAssemblyCairoViewport> WebAssemblyCairoViewport::Create(
149 const std::string& canvasId, bool enableEmscriptenMouseEvents) 105 const std::string& canvasId, bool enableEmscriptenMouseEvents)