Skip to content

Commit 775256d

Browse files
requestFrame
1 parent 4e3e740 commit 775256d

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

apps/desktop/src/renderer/windows.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const sendOutput = (display: Display): void => {
1010

1111
outputWin.document.write('<div style="width:100vw;height:100vh;"></div>')
1212
outputWin.document.body.style.margin = '0'
13+
outputWin.document.body.style.background = '#222'
1314
outputWin.document.body.style.cursor = 'none'
1415

1516
const { x, y } = display.bounds
@@ -21,7 +22,7 @@ export const sendOutput = (display: Display): void => {
2122
})
2223

2324
setTimeout(() => {
24-
engine.setOutput(outputWin.document.querySelector('div') as HTMLDivElement)
25+
engine.setOutput(outputWin.document.querySelector('div') as HTMLDivElement, outputWin)
2526
}, 1000)
2627
}
2728

packages/engine/src/HedronEngine/HedronEngine.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,8 @@ export class HedronEngine {
551551
return this.renderer.createCanvas(containerEl)
552552
}
553553

554-
public setOutput(container: HTMLElement) {
555-
this.renderer.setOutput(container)
554+
public setOutput(container: HTMLElement, outputWindow: Window) {
555+
this.renderer.setOutput(container, outputWindow)
556556
}
557557

558558
public stopOutput() {
@@ -706,7 +706,7 @@ export class HedronEngine {
706706
}
707707

708708
if (this.paused) {
709-
requestAnimationFrame(loop)
709+
this.renderer.requestFrame(loop)
710710
return
711711
}
712712

@@ -723,7 +723,7 @@ export class HedronEngine {
723723

724724
this.advanceFrame(deltaTime)
725725

726-
requestAnimationFrame(loop)
726+
this.renderer.requestFrame(loop)
727727
this.onFrameEnd?.()
728728
}
729729

packages/engine/src/world/Renderer.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)