要使用画布在HTML5中创建逐帧动画,请尝试运行以下代码:
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 );