Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In javascript you can use the setInterval and setTimeout functions to create delays and throttle the frame rate.</p> <p>for instance if you wanted to make your drawing loop approximately 30 FPS you could have some code that looks like this:</p> <pre><code>function draw(){ var canvas = document.getElementById('myCanvas'); //create the image object var img = new Image(); //set the image object's image file path var img.src = "images/myImage.png" //check to see that our canvas exists if( canvas.getContext ) { //grab the context to draw to. var ctx = cvs.getContext('2d'); //clear the screen to a white background first //NOTE to make this faster you can clear just the parts //of the canvas that are moving instead of the whole thing //every time. Check out 'Improving HTML5 Canvas Performance' //link mention in other post ctx.fillStyle="rgb(255,255,255)"; ctx.fillRect (0, 0,512, 512); //DO ALL YOUR DRAWING HERE.... //draw animation ctx.drawImage(img, 0, 0); } //Recalls this draw update 30 times per second setTimeout(draw,1000/30); } </code></pre> <p>This will help you control how much processing time the animation is taking. Thus if you find that your animation is going too slow you can increase the frame rate by changing the denominator '30' to something like '60' fps or anything that really works well for your program. </p> <p>As far as optimizing goes in addition to setTimeout() the way you draw your animations is critical too. Try to load all your images before you render them this is called "Preloading". That is if you have a bunch of different images for each animated cell then before you call your draw function load all the images into an array of images like so:</p> <pre><code>var loadedImages = new Array(); loadedImages[0] = new Image(); loadedImages[0].src = "images/animationFrame1.png"; loadedImages[1] = new Image(); loadedImages[1].src = "images/animationFrame2.png"; loadedImages[2] = new Image(); loadedImages[2].src = "images/animationFrame3.png"; loadedImages[3] = new Image(); loadedImages[3].src = "images/animationFrame4.png"; </code></pre> <p>you could even put them in a hash if it makes sense for you app where instead of </p> <pre><code>loadedImages[0] = new Image(); loadedImages[0].src = "images/animationFrame1.png"; </code></pre> <p>you do this</p> <pre><code>loadedImages['frame1'] = new Image(); loadedImages['frame1'].src = "images/animationFrame1.png"; </code></pre> <p>once you have all your images loaded you references them for drawing by doing calling them like so:</p> <pre><code>//Using the integer array ctx.drawImage(loadedImages[0], 0, 0); //OR //Using the stringed hash ctx.drawImage(loadedImages['frame1'], 0, 0); </code></pre> <p>You want to separate your loading process from your rendering process because loading images is process intensive thus will slow your animations down if you are loading things while rendering.</p> <p>That is not to say that you can't ever load anything while rendering, but instead just be conscience that this will slow animation speed down.</p> <p><a href="http://www.techrepublic.com/article/preloading-and-the-javascript-image-object/5214317" rel="nofollow noreferrer">Here</a> is an article on preloading images.</p> <p>There is another post on here which talks about consistent animation speed on all browsers <a href="https://stackoverflow.com/questions/3136644/how-do-i-make-a-javascript-animation-play-at-the-same-speed-on-all-browsers-on-al/3140559#3140559">here</a></p> <p>Note the <a href="http://gafferongames.com/game-physics/fix-your-timestep/" rel="nofollow noreferrer">link</a> posted by the green checked answer</p> <p>Other things to be noted are making sure to only clearing and redrawing bounding boxes as mentioned in the post with HTML 5 canvas optimization. That link has some really good techniques to be conscience of while developing canvas animations.</p> <p><a href="http://www.admixweb.com/2010/01/07/17-javascript-animation-frameworks-worth-to-seing/" rel="nofollow noreferrer">Here</a> are some frame works as well which might come in handy to cross compare what you are doing to what other engines are doing.</p> <p>Hope some of this helps. I am new to javascript (only started coding with it about 2 weeks ago) and so there could be better ways to do things but these are the things I have found thus far.</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.
    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