Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>cancelAnimationFrame</code> <em>alone</em> won't necessary stop the animation loop (it turns out).</p> <p>To be absolute sure you will need to use a <em>conditional check</em> as well - a generic example:</p> <pre><code>var isPlaying; /// our condition function loop() { /* funky stuff here */ If (isPlaying === true) requestId = requestAnimationFrame(loop); } </code></pre> <p>Then starting it:</p> <pre><code>functiom start() { isPlaying = true; loop(); } </code></pre> <p>Now when you want to stop the animation you need to do:</p> <pre><code>function stop() { isPlaying = false; /// kill any request in progress if (requestId) cancelAnimationFrame(requestId); } </code></pre> <p>If you don't do this you risk stacking calls to the loop - for example:</p> <p>If you resize and cAF isn't stopping rAF from re-trigger the loop, the old loop will still run in the background and you will start a new loop on top of that.</p> <p>That is why you see speed increases as the old and the new loop will both increment the positions before the stars are drawn to screen.</p> <p>On the third re-size yet another loop is started, and eventually the whole thing will block the browser.</p> <h1>However</h1> <p>Instead of utilizing start and stop of the loop I would recommend you the following approach:</p> <ul> <li>Create canvas once</li> <li>Start the loop only once</li> <li>In this case, a re-factoring of the whole re-size mechanism could be beneficial (for instance, separate needed initializations (width and height of element) from first time initializations that can be re-used later).</li> <li>There is no need to re-init the stars for each re-size as you will use the width and height to check their boundaries (canvas will do the clipping).</li> </ul> <p>When resizing you <em>can</em> consider using a conditional flags to prevent <em>render</em> while re-sizing.</p> <p>Although generally, a condition to block rendering while canvas changes size is really not necessary due to the single-thread nature of JavaScript and in cases such as this you do boundary check on current element size. Canvas itself will take care of the clipping for you.</p> <p>And that being said: there should be no need to <em>re-create</em> the canvas element each time.</p> <p>This creates an unnecessary overhead. Simple set new width and height on its properties if canvas is already created:</p> <pre><code>if (typeof canvas === 'undefined') canvas = /* only create if it doesn't exist */ canvas.width = width; canvas.height = height; </code></pre> <p>PS: <strong><a href="http://codepen.io/anon/pen/CLKhn" rel="nofollow">I "hampered" a version with some brute-force-ish implementations</a></strong> of the above. It's far from complete or of high quality but takes out some of the pain for the sake of example to give you a couple of pointers.</p> <p>Please adopt to suit your needs.</p> <p><strong>Update</strong>:</p> <p>To include more info from the additional comments:</p> <p>when a new size is set on a canvas element its <em>context</em> is reset to default (<code>fillStyle</code> becomes transparent, <code>strokeStyle</code> black, transform is reset and so on).</p> <p>This means that all non-default settings must be set again after each new size is set.</p> <p>Setting a new size may (and typically do) clear the content of the canvas as well so that all pixels becomes transparent black.</p>
    singulars
    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