Skip to content

Commit

Permalink
Add maximum FPS feature
Browse files Browse the repository at this point in the history
  • Loading branch information
webfiltered committed Nov 28, 2024
1 parent b8dbca5 commit dda2a1e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/LGraphCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ export class LGraphCanvas {
return `normal ${LiteGraph.NODE_SUBTEXT_SIZE}px Arial`
}

#maximumFrameGap = 0
/** Maximum frames per second to render. 0: unlimited. Default: 0 */
public get maximumFps() {
return this.#maximumFrameGap > Number.EPSILON ? this.#maximumFrameGap / 1000 : 0
}

public set maximumFps(value) {
this.#maximumFrameGap = value > Number.EPSILON ? 1000 / value : 0
}

options: {
skip_events?: any
viewport?: any
Expand Down Expand Up @@ -1864,14 +1874,22 @@ export class LGraphCanvas {
this.is_rendering = true
renderFrame.call(this)

/** Render loop */
function renderFrame(this: LGraphCanvas) {
if (!this.pause_rendering) {
this.draw()
}

const window = this.getCanvasWindow()
if (this.is_rendering) {
window.requestAnimationFrame(renderFrame.bind(this))
if (this.#maximumFrameGap > 0) {
// Manual FPS limit
const gap = this.#maximumFrameGap - (LiteGraph.getTime() - this.last_draw_time)
setTimeout(renderFrame.bind(this), Math.max(1, gap))
} else {
// FPS limited by refresh rate
window.requestAnimationFrame(renderFrame.bind(this))
}
}
}
}
Expand Down

0 comments on commit dda2a1e

Please sign in to comment.