Josh asked (some time ago) how the browser knows “when the drawing process is over” to avoid flickering. I would immediately comment on his post, but my representative is not high enough. And this is just my opinion. I have no facts to support this, but I feel pretty confident about it, and may be useful to others reading this in the future.
I assume that the browser does not “know” when you are finished drawing. But, like most javascript, as long as your code works without giving up managing the browser, the browser is essentially blocked and cannot / cannot update / respond to its user interface. I assume that if you clear the canvas and draw your entire frame without giving up on the browser, it will not draw your canvas until you finish.
If you set up a situation where your rendering spans multiple calls to setTimeout / setInterval / requestAnimationFrame, where you clear the canvas in one call and draw elements on your canvas in the next few calls, repeating a loop (for example) every 5 calls, I bet you You will see flickering, as the canvas will be updated after each call.
However, I'm not sure I will trust this. We are already at a point where javascript is compiled to native machine code before execution (at least what the Chrome V8 engine does from what I understand). I would not be surprised if there wasn’t too much time before browsers ran javascript in a separate thread from the user interface and synchronized any access to the user interface elements, allowing the user interface to update / respond during javascript execution that did not have access to the user interface to the interface. When / if this happens (and I understand that there are many obstacles to overcome, such as event handlers when you are still running other code), we will probably see flickering on canvas animations that do not use some double buffering.
Personally, I like the idea of two canvas elements located on top of each other and alternating, which are shown / drawn on each frame. Pretty non-intrusive and probably pretty easy to add to an existing application with a few lines of code.
Lee Sep 25 2018-12-12T00: 00Z
source share