I have a function that calculates the next frame in an animation of various objects moving in both X and Y axis [ I call it frameRender()
] and a function that applies that resulting frame to the objects [ I call that frameDisplay()
]. The objects don't just move from point A to B, they move constantly, always receiving new target coords. I use a setInterval()
with a 1000/frameRate
interval but that doesn't seem to work at all as browsers don't have accurate timings.
The question is: How can I make sure the animation has a constant frame rate, and will run at the same speed on all browsers, on all systems? I have tried everything and can't seem to get an accurate result even on just different browsers (I test on Firefox and Chrome, Chrome usually displays a lot faster).
The result should be: When it plays slow, the animation interval should decrease at first, and then try to skip some frames [ by skipping frameDisplay()
] if it the DOM displays slow, until it plays correctly. When it plays fast, the animation interval should increase, making the animation play at the correct speed.
But how do you keep consistency in all of this, since you can't always be sure when the browsers will become slow, or when they will perform fast. For example, if there is huge spike of movements, and we decrease the interval to keep the frame rate steady, and then suddenly most of the moving objects stop or don't move much, it will suddenly perform really fast!
Any ideas?