Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Drawing onto a separate canvas is a good way to have multiple layers, but there are several ways to do what you would like:</p> <ol> <li>If your PNG image is pre-defined, you can just render it directly onto the canvas with <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage" rel="nofollow"><code>context.drawImage()</code></a>. The transparency will be preserved. As long as your image is loaded onto the page before it is rendered, there will be little overhead in re-rendering it every frame.</li> <li>You can draw to a hidden canvas and then draw that canvas onto your main canvas. You can do this by creating a canvas in JavaScript and never writing it to the page. For example: <code>var layer = document.createElement('canvas');</code> and then call <code>context.drawImage()</code> with <code>layer</code> as the <code>image</code> parameter. I have written an implementation of a <a href="https://github.com/IceCreamYou/HTML5-Canvas-Game-Boilerplate/blob/gh-pages/js/boilerplate/drawing.js#L9" rel="nofollow">Layer class</a> you can use to make this easier.</li> <li>You can draw directly onto your main canvas using transparency by setting the <a href="http://www.html5canvastutorials.com/advanced/html5-canvas-global-alpha-tutorial/" rel="nofollow"><code>globalAlpha</code></a> property of your graphics context object. (Just remember to set it back to 1 after you're done drawing your transparent stuff.)</li> <li>You can draw onto a secondary, visible canvas that is absolutely positioned over your primary canvas. To do this you need to set the CSS <code>background-color</code> of the secondary canvas to <code>transparent</code>.</li> </ol> <p>Similarly, there are two good ways to wait one second:</p> <ol> <li>Use <code>setTimeout</code> or <code>setInterval</code>. This will wait as close to your delay period as possible (in this case one second) and then execute the callback asynchronously. Use <code>setTimeout</code> if you want to execute something once and <code>setInterval</code> if you want to execute something at a predefined interval indefinitely (i.e. every second).</li> <li><p>Keep a variable that holds the last time you executed the function you want to run, and check whether you've waited long enough before running the code again. This will run the code synchronously. For example:</p> <pre><code>// Outside of your animation loop var lastRun = Date.now(); // Inside your animation loop if (lastRun + 1000 &lt; Date.now()) { /* Run you callback code */ } </code></pre> <p>Since you are using <code>requestAnimationFrame</code> to run your main animation loop, this will run at the soonest time after your delay (e.g. 1 second) at which the browser is ready to paint a new frame. Note that if you want code using this method to run asynchronously, you can also use <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers" rel="nofollow">web workers</a>.</p></li> </ol> <p>Unless you're drawing text onto a canvas without caching it or drawing thousands of objects onto your canvas, chances are pretty good that your performance is not draw-bound, so I would opt for the simplest solution even if it's not asynchronous. You can use the <a href="http://github.com/mrdoob/stats.js" rel="nofollow">stats.js</a> library to test your canvas performance.</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. This table or related slice is empty.
    1. This table or related slice is empty.
    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