HTML5的Canvas画图模拟太阳系运转

今天研究的是利用HTML5的Canvas画图来模拟太阳系运转,首先,在这个太阳系里分为画轨道和画星球两个部分,

于每一个星球我们要知道它的颜色和公转周期,如下图。

 

 

采用面向对象编程的思想,代码如下

 

stars.html

<!DOCTYPE HTML>

<html>
	<head></head>
	<body>
    	<canvas id="canvas" width="1000" height="1000" style="background:#000">
            你的浏览器不支持canvas标签!
    	</canvas>

    	<script src="stars.js">
    	</script>

	</body>
</html>

 

stars.js

/******************************************/
/*                                        */
/*   本节代码体现了用JavaScript编写面向对 */
/*   象程序的思想,希望能认真阅读理解。   */
/*                                        */
/******************************************/


//设置2d绘图环境
var ctx = document.getElementById("canvas").getContext("2d");

//画轨道
function drawTrack(){
	for(var i = 0; i < 8; i++){
		ctx.beginPath();
		ctx.arc(500, 500, (i + 1) * 50, 0, 360, false);
		ctx.closePath();
		ctx.strokeStyle = "#fff";
		ctx.stroke();
	}
}

//画星球的类
function Star(x, y, radius, cycle, sColor, eColor){

	//设置星球类的属性
	this.x = x;             //星球的坐标点
	this.y = y;

	this.radius = radius;   //星球的半径
	this.cycle = cycle;     //设置周期
	this.sColor = sColor;   //星球的颜色,起始颜色和结束颜色
	this.eColor = eColor;

	this.color = null;
	//设置一个计时器
	this.time = 0;

	//给星球类定义一个方法
	this.draw = function(){
		ctx.save();               //保存之前的内容
		ctx.translate(500, 500);  //重置0,0坐标     
		ctx.rotate(this.time * (360 / this.cycle) * Math.PI / 180);  //旋转角度

		//画星球
		ctx.beginPath();
		ctx.arc(this.x, this.y, this.radius, 0, 360, false);
		ctx.closePath();
		//设置星球的填充颜色
		this.color = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius);
		this.color.addColorStop(0, this.sColor);
		this.color.addColorStop(1, this.eColor);
		ctx.fillStyle = this.color;
		ctx.fill();
		//恢复之前画布的内容
		ctx.restore();
		this.time += 1;
	}
}

//创建一个太阳的构造函数
function Sun(){
	Star.call(this, 0, 0, 20, 0, "#FFFF00", "#FF9900");
}

//创建一个水星的构造函数
function Mercury(){
	Star.call(this, 0, -50, 10, 87.70, "#A69697", "#5C3E40");
}

//创建一个金星的构造函数
function Venus(){
	Star.call(this, 0, -100, 10, 224.701, "#C4BBAC", "#1F1315");
}

//创建一个地球的构造函数
function Earth(){
	Star.call(this, 0, -150, 10, 365.2422, "#78B1E8", "#050C12");
}

//创建一个火星的构造函数
function Mars(){
	Star.call(this, 0, -200, 10, 686.98, "#CEC9B6", "#76422D");
}

//创建一个木星的构造函数
function Jupiter(){
	Star.call(this, 0, -250, 10, 4332.589, "#C0A48E", "#322222");
}
//创建一个土星的构造函数
function Saturn(){
	Star.call(this, 0, -300, 10, 10759.5, "#F7F9E3", "#5C4533");
}
//创建一个天王星的构造函数
function Uranus(){
	Star.call(this, 0, -350, 10, 30799.095, "#A7E1E5", "#19243A");
}
//创建一个海王星的构造函数
function Neptune(){
	Star.call(this, 0, -400, 10, 60152, "#0661B2", "#1E3B73");
}

var sun = new Sun();
var mercury = new Mercury();
var venus = new Venus();
var earth = new Earth();
var mars = new Mars();
var jupiter = new Jupiter();
var saturn = new Saturn();
var uranus = new Uranus();
var neptune = new Neptune();

function Move(){
	ctx.clearRect(0, 0, 1000, 1000);
	drawTrack();
	sun.draw();
	mercury.draw();
	venus.draw();
	earth.draw();
	mars.draw();
	jupiter.draw();
	saturn.draw();
	uranus.draw();
	neptune.draw();
}

setInterval(Move,10);


 

运行效果:

 

 

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值