Mercurial > hg > orthanc-stone
diff OrthancStone/Sources/Viewport/WebAssemblyViewport.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 | e4a52cbbdd70 |
children | e8a120dd05bd |
line wrap: on
line diff
--- a/OrthancStone/Sources/Viewport/WebAssemblyViewport.cpp Wed Sep 23 17:25:25 2020 +0200 +++ b/OrthancStone/Sources/Viewport/WebAssemblyViewport.cpp Thu Sep 24 16:40:30 2020 +0200 @@ -38,6 +38,7 @@ #include <boost/make_shared.hpp> #include <boost/enable_shared_from_this.hpp> +#include <boost/math/special_functions/round.hpp> namespace OrthancStone { @@ -112,6 +113,11 @@ { that_.Invalidate(); } + + virtual void RefreshCanvasSize() ORTHANC_OVERRIDE + { + that_.RefreshCanvasSize(); + } }; @@ -137,7 +143,7 @@ if (that->compositor_.get() != NULL) { - that->UpdateSize(*that->compositor_); + that->RefreshCanvasSize(); that->Invalidate(); } @@ -215,7 +221,7 @@ if (compositor_.get() != NULL && controller_ /* should always be true */) { - UpdateSize(*compositor_); + RefreshCanvasSize(); compositor_->FitContent(controller_->GetScene()); OnRequestAnimationFrame(0, reinterpret_cast<void*>(this)); } @@ -248,7 +254,9 @@ canvasCssSelector_(canvasId), #endif interactor_(new DefaultViewportInteractor), - enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents) + enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents), + canvasWidth_(0), + canvasHeight_(0) { } @@ -312,7 +320,7 @@ void WebAssemblyViewport::UpdateCanvasSize() { - UpdateSize(*compositor_); + RefreshCanvasSize(); } WebAssemblyViewport::~WebAssemblyViewport() @@ -358,4 +366,39 @@ interactor_.reset(interactor); } } + + + void WebAssemblyViewport::RefreshCanvasSize() + { + double w, h; + emscripten_get_element_css_size(GetCanvasCssSelector().c_str(), &w, &h); + + /** + * Emscripten has the function emscripten_get_element_css_size() + * to query the width and height of a named HTML element. I'm + * calling this first to get the initial size of the canvas DOM + * element, and then call emscripten_set_canvas_size() to + * initialize the framebuffer size of the canvas to the same + * size as its DOM element. + * https://floooh.github.io/2017/02/22/emsc-html.html + **/ + if (w > 0 && + h > 0) + { + canvasWidth_ = static_cast<unsigned int>(boost::math::iround(w)); + canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h)); + } + else + { + canvasWidth_ = 0; + canvasHeight_ = 0; + } + + emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), canvasWidth_, canvasHeight_); + + if (compositor_.get() != NULL) + { + compositor_->SetCanvasSize(canvasWidth_, canvasHeight_); + } + } }