@@ -63,6 +63,11 @@ class Oscilloscope {
6363 this . close ( ) ;
6464 } ;
6565 widgetWindow . onmaximize = this . _scale . bind ( this ) ;
66+ widgetWindow . onrollup = this . _throttle . bind ( this ) ;
67+ widgetWindow . onunroll = this . _wakeUp . bind ( this ) ;
68+
69+ this . _handleVisibilityChange = this . _handleVisibilityChange . bind ( this ) ;
70+ document . addEventListener ( "visibilitychange" , this . _handleVisibilityChange ) ;
6671
6772 // UI state
6873 this . zoomFactor = 40.0 ;
@@ -106,6 +111,7 @@ class Oscilloscope {
106111
107112 if ( this . _rafId !== null ) {
108113 cancelAnimationFrame ( this . _rafId ) ;
114+ clearTimeout ( this . _rafId ) ;
109115 this . _rafId = null ;
110116 }
111117
@@ -123,9 +129,34 @@ class Oscilloscope {
123129 this . draw ( ) ;
124130 }
125131
132+ _throttle ( ) {
133+ if ( ! this . _running || this . _rafId === null ) return ;
134+ cancelAnimationFrame ( this . _rafId ) ;
135+ clearTimeout ( this . _rafId ) ;
136+ this . _rafId = setTimeout ( this . draw , 1000 ) ;
137+ }
138+
139+ _wakeUp ( ) {
140+ if ( ! this . _running || this . _rafId === null ) return ;
141+ cancelAnimationFrame ( this . _rafId ) ;
142+ clearTimeout ( this . _rafId ) ;
143+ this . _rafId = null ;
144+ this . draw ( ) ;
145+ }
146+
147+ _handleVisibilityChange ( ) {
148+ if ( document . visibilityState === "visible" ) {
149+ this . _wakeUp ( ) ;
150+ } else {
151+ this . _throttle ( ) ;
152+ }
153+ }
154+
126155 close ( ) {
127156 this . _stopAnimation ( ) ;
128157
158+ document . removeEventListener ( "visibilitychange" , this . _handleVisibilityChange ) ;
159+
129160 this . drawVisualIDs = { } ;
130161 this . _canvasState = { } ;
131162 this . pitchAnalysers = { } ;
@@ -214,6 +245,11 @@ class Oscilloscope {
214245 draw ( ) {
215246 if ( ! this . _running ) return ;
216247
248+ if ( document . visibilityState === "hidden" || this . widgetWindow . _rolled ) {
249+ this . _rafId = setTimeout ( this . draw , 1000 ) ;
250+ return ;
251+ }
252+
217253 this . _renderFrame ( ) ;
218254
219255 this . _rafId = requestAnimationFrame ( this . draw ) ;
0 commit comments