zoukankan      html  css  js  c++  java
  • 利用canvas制作乱跑的小球

    canvas制作乱跑的小球

    说明:将下面的代码放到html的body就可以,键盘控制上(W)下(S)左(A)右(D)  

    <body>
        <canvas id="canvas" style="border: 1px solid #000;display: block;margin:30px auto;"></canvas>
        <script type="text/javascript">
            var myCanvas=document.getElementById('canvas');
            myCanvas.height="500";//背景为500*500
            myCanvas.width="500";
            var ctx=myCanvas.getContext("2d");
            //键盘事件
            document.onkeydown=function(event){
                var e = event || window.event || arguments.callee.caller.arguments[0];
                var x=0,y=0;
                // 上按W
                if(e && e.keyCode==87){ 
                    ctx.clearRect(x-11,y-11,22,22);
                    y-=10;
                    ctx.translate(x,y);
                    ctx.beginPath();
                    ctx.arc(0,0,10,0,Math.PI*2);
                    ctx.fill();
                    ctx.closePath();
                    ctx.restore();
                    };
                 // 左按A
                if(e && e.keyCode==65){
                    ctx.clearRect(x-11,y-11,22,22);
                    x-=10;
                    ctx.translate(x,y);
                    ctx.beginPath();
                    ctx.arc(0,0,10,0,Math.PI*2);
                    ctx.fill();
                    ctx.closePath();
                    ctx.restore();
                   } ; 
                 // 下按S
                if(e && e.keyCode==83){
                    ctx.clearRect(x-11,y-11,22,22);
                    y=10;
                    ctx.translate(x,y);
                    ctx.beginPath();
                    ctx.arc(0,0,10,0,Math.PI*2);
                    ctx.fill();
                    ctx.closePath();
                    ctx.restore();
                }
                // 右按D
                if(e && e.keyCode==68){ 
                    ctx.clearRect(x-11,y-11,22,22);
                    x=10;
                    ctx.translate(x,y);
                    ctx.beginPath();
                    ctx.arc(0,0,10,0,Math.PI*2);
                    ctx.fill();
                    ctx.closePath();
                    ctx.restore();
                }
            }; 
        </script>
    </body>
    我若风光万人陪,一无所有还有谁?
  • 相关阅读:
    unity 用LineRender画四边形并测面积
    unity读取Texture文件并转为Sprit
    unity shader入门(四):高光
    unity shader入门(三)逐像素光照,Blinn-Phong模型
    unity shader入门(二)语义,结构体,逐顶点光照
    unity shader入门(一):基本结构话痨版
    好多坑,好多好多坑(1)
    点击按钮收缩功能
    unity 实现技能释放
    ugui用户定义操作按键
  • 原文地址:https://www.cnblogs.com/wanghongze/p/6237780.html
Copyright © 2011-2022 走看看