zoukankan      html  css  js  c++  java
  • [Canvas]Running Horse

    下载地址:https://files.cnblogs.com/files/xiandedanteng/52-RunningHorse.rar,下载完毕后请使用Chrome浏览器打开Index.html查看。

    图例:

    代码:

    <!DOCTYPE html>
    <html lang="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <head>
         <title>奔跑的马 19.3.3 13:20 horn19782016@163.com</title>
        </head>
    
         <body onload="draw()">
            <canvas id="myCanvus" width="120px" height="90px" style="border:1px dashed black;">
                出现文字表示你的浏览器不支持HTML5
            </canvas>
         </body>
    </html>
    <script type="text/javascript">
    <!--
    var ctx;// 绘图环境
    var cds;// 从大图中取小图的坐标数组
    var img;// 大图
    
    function draw(){
        var canvas=document.getElementById('myCanvus');
        
        canvas.width=120;
        canvas.height=90;
        
        ctx=canvas.getContext('2d');
    
        img=new Image();
        img.src="runningHorse.jpg";
        
        // 图块坐标
        cds=[
            {'x':'0', 'y':'10','width':'120','height':'80'},
            {'x':'120','y':'10','width':'120','height':'80'},
            {'x':'240','y':'10','width':'120','height':'80'},
            {'x':'360','y':'10','width':'120','height':'80'},
            {'x':'480','y':'10','width':'120','height':'80'},
            
            {'x':'0', 'y':'100','width':'120','height':'80'},
            {'x':'120','y':'100','width':'120','height':'80'},
            {'x':'240','y':'100','width':'120','height':'80'},
            {'x':'360','y':'100','width':'120','height':'80'},
            {'x':'490','y':'100','width':'120','height':'80'},
       ];
        
        animate();
    };
    
    var index=0;
    var i=0;
    
    function animate(){
        ctx.clearRect(0,0,120,90);
        ctx.fillStyle = "rgb(137,201,3)";
        ctx.fillRect(0, 0, 120, 90);
            
        ctx.strokeStyle = "black";
    
        // 绘制地面
        ctx.beginPath();
        ctx.fillStyle = "black";
        ctx.moveTo(0, 80);
        ctx.lineTo(120,80);
        ctx.stroke();
        ctx.closePath();
        
        index++;
        if(index>100){
            index=0;
        }
        i=index % 10;
    
        // 截取一块图贴上
        ctx.drawImage(img,cds[i].x,cds[i].y,cds[i].width,cds[i].height,0,0,cds[i].width,cds[i].height);    
    
        setTimeout( function(){
            window.requestAnimationFrame(animate); /// 让浏览器自行决定帧速率
        }, 0.20 * 1000 );// 延时执行
    }
    
    //-->
    </script>

    2019年3月3日14点38分

  • 相关阅读:
    CSS3 定位| Position研究
    ASP.NET中Partial Class部分类
    如何安全实现“记住我”的功能
    JavaScript后台代码操作HTML TABLE的方法
    强悍的CSS工具组合:Blueprint, Sass, Compass
    CSS的力量:用一个DIV画图
    ASP.NET记录错误日志的方式
    ASP.NET私有构造函数作用
    c#.net实现浏览器端大文件分块上传
    asp.net实现浏览器端大文件分块上传
  • 原文地址:https://www.cnblogs.com/heyang78/p/10465614.html
Copyright © 2011-2022 走看看