changeset 1618:9a52bac0c2a7

Added code to clear pending calls to RequestAnimationFrame
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 02 Nov 2020 17:54:18 +0100
parents 49ba862d3454
children 4c9e68291467
files Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp Applications/Platforms/WebAssembly/WebAssemblyViewport.h
diffstat 2 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp	Fri Oct 30 17:43:29 2020 +0100
+++ b/Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp	Mon Nov 02 17:54:18 2020 +0100
@@ -129,6 +129,7 @@
     if (that->compositor_.get() != NULL &&
         that->controller_ /* should always be true */)
     {
+      that->animationFrameCallbackIds_.clear();
       that->Paint(*that->compositor_, *that->controller_);
     }
       
@@ -213,7 +214,9 @@
 
   void WebAssemblyViewport::Invalidate()
   {
-    emscripten_request_animation_frame(OnRequestAnimationFrame, reinterpret_cast<void*>(this));
+    long id = emscripten_request_animation_frame(OnRequestAnimationFrame, 
+                                                 reinterpret_cast<void*>(this));
+    animationFrameCallbackIds_.push_back(id);
   }
 
   void WebAssemblyViewport::FitForPrint()
@@ -320,6 +323,13 @@
 
   WebAssemblyViewport::~WebAssemblyViewport()
   {
+    // cancel the pending RequestAnimationFrame callbacks
+    for(size_t i = 0; i < animationFrameCallbackIds_.size(); ++i)
+    {
+      long id = animationFrameCallbackIds_[i];
+      emscripten_cancel_animation_frame(id);
+    }
+
     emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
                                    reinterpret_cast<void*>(this),
                                    false,
--- a/Applications/Platforms/WebAssembly/WebAssemblyViewport.h	Fri Oct 30 17:43:29 2020 +0100
+++ b/Applications/Platforms/WebAssembly/WebAssemblyViewport.h	Mon Nov 02 17:54:18 2020 +0100
@@ -41,6 +41,7 @@
 
 #include <memory>
 #include <string>
+#include <vector>
 #include <boost/enable_shared_from_this.hpp>
 
 namespace OrthancStone
@@ -60,6 +61,7 @@
     bool                                  enableEmscriptenMouseEvents_;
     unsigned int                          canvasWidth_;
     unsigned int                          canvasHeight_;
+    std::vector<long>                     animationFrameCallbackIds_;
 
     static EM_BOOL OnRequestAnimationFrame(double time, void *userData);