I would warn against this, you might end up requesting more frames than your screen can render. If you want to go down the route of “on-demand-rendering” - I suggest you throttle your rendering to at most once per screen refresh cycle. You can do that with requestAnimatioFrame
and a bit of logic. I have similar mechanism in meep:
this code is under MIT, so feel free to use it.