Frame by frame animation in HTML5 with canvas

JavascriptWeb DevelopmentFront End Scripts

To create a frame by frame animation in HTML5 with canvas, try to run the following code:

var myImageNum = 1;
var lastImage = 5;

var context = canvas.getContext('2d');
var img = new Image;

img.onload = function(){
   context.clearRect( 0, 0, context.canvas.width, context.canvas.height );
   context.drawImage( img, 0, 0 );
};

var timer = setInterval( function(){
   if (myImageNum > lastImage){
      clearInterval( timer );
   }else{
      img.src = "images/left_hnd_"+( myImageNum++ )+".png";
   }
}, 1000/15 );
raja
Updated on 29-Jan-2020 06:36:50

Advertisements