Canvas动态案例

  • Canvas动态案例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    <canvas id="draw" width="220" height="320" style="background-color: skyblue"></canvas>
    </body>
    </html>
    <script type="text/javascript">
    var draw=document.getElementById('draw');
    var cvs=draw.getContext('2d');
    var img=new Image;
    img.src='walkingdead.png';//添加图片地址
    img.onload=function(){//图片上有10张小图横着排列
    var width=this.width/10;
    var height=this.height;
    var i=0;
    window.setInterval(function(){
    cvs.clearRect(0,0,draw.width,draw.height);//清除上一个图片
    cvs.drawImage(img,i*width,0,width,height,0,0,width,height);
    if(i==9){
    i=0
    }else{i++;}
    },200);
    }
    </script>
您的支持将鼓励我继续创作!