首页 > 其他 > 详细

canvas制作运动的小球

时间:2017-02-01 19:44:28      阅读:113      评论:0      收藏:0      [点我收藏+]
技术分享
<!DOCTYPE html>
<head>
<title>canvas</title>
<style>
.canvas{
   border: 1px solid #000;
}
</style>
</head>
<body>
<canvas class="canvas" id="canvas" width="400px" height="400px"></canvas>
<script>
window.onload=function(){
   var can=document.getElementById("canvas");
   var cxt=can.getContext("2d");
   var ball={
      x:100,
      y:100,
      vx:4,
      vy:2,
      radius:20,
      color:"blue",
      draw:function(){
        cxt.beginPath();
        cxt.arc(this.x,this.y,this.radius,0,Math.PI*2,true);
        cxt.closePath();
        cxt.fillStyle=this.color;
        cxt.fill();
      }
   };   
   var draw=function(){
       cxt.clearRect(0,0,canvas.width,canvas.height);
       ball.draw();
       ball.x+=ball.vx;
       ball.y+=ball.vy;
       if(ball.vy+ball.y>canvas.height-15 || ball.vy+ball.y<15){
           ball.vy=-ball.vy;
       }
       if(ball.vx+ball.x>canvas.width-15 || ball.vx+ball.x<15){
           ball.vx=-ball.vx;
       }
       window.requestAnimationFrame(draw);
       //window.setTimeout(function(){
       //      draw();
       //},100);
   };
   
   draw();
   
};
</script>
</body>
</html>
View Code

技术分享

 

canvas制作运动的小球

原文:http://www.cnblogs.com/aliwa/p/6360223.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!