Html5 canvas animation - Javascript Canvas

Javascript examples for Canvas:Animation

Description

Html5 canvas animation

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>test</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.js"></script> 
      <style id="compiled-css" type="text/css">

#canvas{background:#000;}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){/*from www.  ja  v a2  s  .com*/
var canvas = $("#canvas")[0],
      context = canvas.getContext("2d"),
      bloons = {},
      bloonIndex = 0;
var c_wdh = 360,
      c_hgt = 540;
function createBloon(index, x, y) {
   this.index = index;
  this.x = x;
  this.y = y;
  this.vy = 3;
  this.drawImage = function() {
     context.fillStyle = "#FF0000";
     context.fillRect(this.x, this.y, 50, 50);
   };
}
var i = 1;
var rendorBloon = setInterval(function(){
  bloons[i] = new createBloon(i, Math.random() * (c_wdh - 50), c_hgt);
  i++;
  if(i >= 10){
    clearInterval(rendorBloon);
    rendorBloon = null;
  }
}, 1000);
var animate = setInterval(function () {
  context.clearRect(0, 0, c_wdh, c_hgt);
  for (var i in bloons){
     bloons[i].y -= bloons[i].vy;
    bloons[i].drawImage();
    if(bloons[i].y <= -120){
       delete bloons[i];
    }
  }
  if(rendorBloon === null && Object.keys(bloons).length === 0){
     clearInterval(animate);
  }
}, 30);
    });

      </script> 
   </head> 
   <body> 
      <canvas id="canvas" width="360" height="540"></canvas>  
   </body>
</html>

Related Tutorials