|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>太陽系 | 斌果部落格</title>
<style type="text/css">
body{background:#000;}canvas{margin:0 auto;display:block;background:#000;}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
<script type="text/javascript">
var suns = document.getElementById('canvas');
var obj = suns.getContext('2d');
//畫四條軌道 drawTrack()
function drawTrack(){
for(var i=0;i<4;i++){
obj.beginPath();
obj.arc(300,300,(i+1)*70,0,360,false);
obj.closePath();
obj.strokeStyle="#ccc";
obj.stroke();
}
}
//畫球 www.111cn.net
var time=0;
//星球方法
function Star(x,y,r,sColor,eColor,cycle){
this.x=x;
this.y=y;
this.r=r;
this.sColor=sColor;
this.eColor=eColor;
this.cycle=cycle;
this.draw=function(){
obj.save();
obj.translate(300,300);
obj.rotate(time*360/this.cycle*Math.PI/180);
obj.beginPath();
obj.arc(this.x,this.y,this.r,0,360,false);
obj.closePath();
var color = obj.createRadialGradient(this.x,this.y,0,this.x,this.y,this.r);
color.addColorStop(0,sColor);
color.addColorStop(1,eColor);
obj.fillStyle=color;
obj.fill();
obj.restore();
time++;
}
}
//-----------------------------------------------
function Sun(){ //建立太陽構造方法
Star.call(this,0,0,50,"#f00","#f90",0);
}
var sun=new Sun(); //建立太陽執行個體
//-----------------------------------------------
function Mercury(){ //建立水星構造方法
Star.call(this,0,-70,10,"#A69697","#5C3E40",87.70);
}
var sx=new Mercury(); //建立水星執行個體
//-----------------------------------------------
function Venus(){ //建立金星構造方法
Star.call(this,0,-140,30,"#C4BBAC","#1F1315",224.701);
}
var jx=new Venus(); //建立金星執行個體
//-----------------------------------------------
function Earth(){ //建立地球構造方法
Star.call(this,0,-210,25,"#78B1E8","#050C12",365.2422);
}
var dq=new Earth(); //建立地球執行個體
//-----------------------------------------------
function Mars(){ //建立火星構造方法
Star.call(this,0,-280,10,"#CEC9B6","#76422D",686.98);
}
var hx=new Mars(); //建立火星執行個體
//動起來
function move(){
obj.clearRect(0,0,600,600);
drawTrack();
sun.draw();
sx.draw();
jx.draw();
dq.draw();
hx.draw();
}
move();
setInterval("move()",100);
</script>
</body>
</html> |