phoboslab / Impact

HTML5 Game Engine
MIT License
2k stars 204 forks source link

re: "misuse of requestanimationframe" #9

Closed Phildo closed 5 years ago

Phildo commented 5 years ago

this is regarding your (very old) blog post: https://impactjs.com/forums/impact-engine/misuse-of-requestanimationframe (it says to bring questions to this github repo)

I'm actually not using impact, but have a slow-running (on android, specifically) canvas game that I'm looking to fix up.

My current loop looks like this:

var tick = function()
{
  requestAnimationFrame(tick,canvas);
  current_scene.tick();
  current_scene.draw();
}

I'm confused what your blog post is suggesting- should I instead architect it like this:

var tick = function()
{
  current_scene.tick();
  requestAnimationFrame(draw,canvas);
}
var draw = function()
{
  current_scene.draw();
  setTimeout(tick,0);
}

?

I've tried this, and it hasn't seemed to effect performance (which is fine- my problem is likely elsewhere), but it's also irrecoverably stalled the game where it previously would just lag a bit.

I just want to make sure I'm even taking your suggestion correctly before digging more deeply into it.

Thanks!

gamecubate commented 5 years ago

I'd take whatever advice you can extract from that old thread with a grain of salt. Time did pass. Also, Dominic -- the creator of Impact -- himself did say that the engine performed well, at 60 fps, in a thread-related test. So, if it ain't broken... OTOH, had your game used Impact's un-modified core and exhibited significant performance bottlenecks, that would have made for a very interesting use case worthy of further checking.