Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're confused between whether the <em>logic</em> is running and whether the <em>rendering</em> is happening. Many browsers now hardware-accelerate their canvases so all rendering happens on the GPU, so actual pixel pushing takes no CPU time anyway. However your tick function has non-trivial code to generate random noise on the CPU. So you're only really concerned over whether the tick function is running. If the canvas is offscreen, it certainly won't be rendered to the display (it's not visible). As for the canvas draw calls, it probably depends on the browser. It could render all draw calls to an off-screen canvas in case you suddenly scroll it back in to view, or it could just queue up all the draw calls and not actually do anything with them until you scroll the canvas in to view. I'm not sure what each browser does there.</p> <p>However, <strong>you shouldn't use <code>setInterval</code> or <code>setTimeout</code> for animating Canvas</strong>. Use the new <a href="https://developer.mozilla.org/en/DOM/window.requestAnimationFrame"><code>requestAnimationFrame</code></a> API. Browsers don't know what you do in a timer call so will always call the timer. <code>requestAnimationFrame</code> on the other hand is designed specifically for visual things, so the browser has the opportunity to not call the tick function, or to reduce the rate it's called at, if the canvas or page is not visible.</p> <p>As for how browsers <em>actually</em> handle it, I'm not sure. However, you should definitely prefer it since future browsers may be able to better optimise <code>requestAnimationFrame</code> in ways they cannot optimise <code>setInterval</code> or <code>setTimeout</code>. I think modern browsers also reduce the ordinary timers to 1 Hz if the page is not visible, but it's definitely much easier for the browser to optimise <code>requestAnimationFrame</code>, plus some browsers get you V-syncing and other niceness with it.</p> <p>So I'm not certain <code>requestAnimationFrame</code> will mean your tick function is not called if the canvas is scrolled out of view. So I'd recommend using <em>both</em> <code>requestAnimationFrame</code> and the existing visibility check. That should guarantee you the most efficient rendering.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload