Canvas How to - Paint out full image in animation








Question

We would like to know how to paint out full image in animation.

Answer


<!--  w ww.  ja va 2 s. c o m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function(){
var canv = document.getElementById('myCanvas');
var ctx = canv.getContext('2d');
var img=new Image();
img.src='http://www.java2s.com/style/download.png';
img.onload= function(){
    for(var i=0;i<img.width;i+=10){
       for(var j=0;j<img.height;j+=10){
           (function(){
              var _i = i; // capture the current value of i
              var _j = j;
              setTimeout(function(){
                  ctx.drawImage(img,_i,_j,10,10,_i,_j,10,10)},(i+j)*2);
           })();
       }
     }
}
});
});//]]>  
</script>
</head>
<body>
  <canvas id='myCanvas'>

</body>
</html>

The code above is rendered as follows: