@@ -17,12 +17,15 @@ export class Renderer {
1717 private outputContainer : HTMLElement | undefined | null
1818 private previewCanvas : HTMLCanvasElement | undefined
1919 private outputCanvas : HTMLCanvasElement | undefined
20+ private outputWindow : Window = window
2021 private canvas : HTMLCanvasElement | undefined
2122 private previewContext : CanvasRenderingContext2D | undefined | null
2223 public aspectRatio : number = 1
2324 public passesNeedUpdate_webGPU : boolean = true
2425 private isSendingOutput = false
2526 private canvasSizeMode : CanvasSizeMode
27+ private frameCallback : ( ( ) => void ) | null = null
28+ private rafId : number | null = null
2629
2730 constructor ( {
2831 rendererType,
@@ -161,10 +164,16 @@ export class Renderer {
161164 this . aspectRatio = ratio
162165 }
163166
167+ public requestFrame ( callback : ( ) => void ) : void {
168+ this . rafId = this . outputWindow . requestAnimationFrame ( callback )
169+ this . frameCallback = callback
170+ }
171+
164172 // Set the output to a second canvas (e.g. a separate window for making full screen)
165- public setOutput ( container : HTMLElement ) : void {
173+ public setOutput ( container : HTMLElement , outputWindow : Window ) : void {
166174 this . stopOutput ( )
167175 this . outputContainer = container
176+ this . outputWindow = outputWindow
168177
169178 if ( ! this . outputContainer ) throw new Error ( "Can't find container" )
170179 if ( ! this . canvas ) throw new Error ( "Can't find canvas" )
@@ -202,11 +211,21 @@ export class Renderer {
202211 if ( ! this . canvas ) throw new Error ( "Can't find canvas" )
203212 if ( ! this . viewerContainer ) throw new Error ( "Can't find viewerContainer" )
204213
214+ this . outputWindow = window
205215 this . viewerContainer . innerHTML = ''
206216 this . canvas . setAttribute ( 'style' , '' )
207217 this . viewerContainer . appendChild ( this . canvas )
208218 this . isSendingOutput = false
209219
220+ // Keep frame loop going when switching back to the main window
221+ if ( this . frameCallback ) {
222+ if ( this . rafId !== null ) {
223+ this . outputWindow . cancelAnimationFrame ( this . rafId )
224+ }
225+ this . outputWindow . requestAnimationFrame ( this . frameCallback )
226+ this . frameCallback = null
227+ }
228+
210229 this . setSize ( )
211230 }
212231
0 commit comments