移动的小球

html文件
<html>
	<head>
	<script type="text/javascript" src="../point/point.js"></script>
	<style type="text/css">
	.canvas{
		background:#000;
		}	
		</style>
	</head>
	<body >
	<canvas class="canvas"></canvas>	
	<script>window.οnlοad=function(){
		var width = window.innerWidth,
				height =window.innerHeight;
				S.init();
		}</script>
	</body>
</html>

js文件

var S = {
	init:function(){
		Drawing.init();
		var d = new Dot(window.innerWidth / 2,window.innerHeight / 2);
		d._draw();
		var d1 = new Dot(window.innerWidth / 2 +200,window.innerHeight / 2+200);
		d1._draw();

		Drawing.loop(function (){
			d.move(d1);d1._draw();
			});
		}
	};

 Point = function(args){
	this.x = args.x;
	this.y = args.y;
	this.z = args.z;
	};

 Dot = function(x,y){
	this.p = new Point({
		x:x,
		y:y,
		z:15
		});
	};
  Dot.prototype = {
 	_draw: function(){
 		Drawing.drawCircle(this.p);
 		},
 	move: function(d){
 		dx = this.p.x - d.p.x;
 		dy = this.p.y - d.p.y;
 		dl = Math.sqrt(dx * dx + dy * dy);
 		e = 0.25;
 		
 		this.p.x -= ((dx / dl) * e * dl);
 		this.p.y -= ((dy / dl) * e * dl);
 		this._draw(this);
 		}
 	}
  Drawing = (function(){
	var canvas,context,renderFn,
	requestFrame  = window.requestAnimateFrame ||
									window.webkitRequestAnimateFrame ||
									window.mozRequestAnimateFrame ||
									window.oRequestAnimateFrame ||	
									window.msRequestAnimateFrame ||
									function(callback){
										window.setTimeout(callback,1000 / 60);
										};									
	return {
		init:function(){
			canvas = document.querySelector('.canvas');
	  	context = canvas.getContext('2d');
	  	this.adjustCanvas();
			},
		adjustCanvas:function(){
			canvas.width = window.innerWidth;
			canvas.height = window.innerHeight;
			},
	  drawCircle: function(p){
		context.fillStyle="#0000ff";
    context.beginPath();
    context.arc(p.x,p.y,p.z,0,2 * Math.PI,true);
    context.closePath();
    context.fill();
		},
	 loop: function(fn){
	 	renderFn = !renderFn?fn:renderFn;
	 	this.clearFrame();
    renderFn();
		requestFrame.call(window, this.loop.bind(this));
		},
	clearFrame: function(){
		context.clearRect(0,0,canvas.width,canvas.height);
		}
		}
		
	}());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值