本文主要是介绍Html Canva之多行星环绕动态图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
小行星围绕中心体进行动态环绕,并且离中心体越近,环绕速度越快。
效果图:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>SolarySystem</title>
</head>
<body><canvas id="mycanvas" width="1000" height="1000" style="background: #000"></canvas><script>var cxt=document.getElementById("mycanvas").getContext("2d")//行星运行轨道function drawTrack(){for(var i=0;i<8;i++){cxt.beginPath()cxt.arc(500,500,(i+1)*50,0,2*Math.PI,false)cxt.strokeStyle="#fff"cxt.stroke()cxt.closePath()} }drawTrack()//模拟一个类function Star(x,y,radius,cycle,sColor,eColor){this.x=xthis.y=y this.radius=radiusthis.cycle=cyclethis.sColor=sColorthis.eColor=eColorthis.color=nullthis.time=0this.draw=function(){cxt.beginPath()cxt.save()cxt.translate(500,500)cxt.rotate(this.time*360/this.cycle*Math.PI/180)cxt.arc(this.x,this.y,this.radius,0,2*Math.PI,false)this.color=cxt.createRadialGradient(this.x,this.y,0,this.x,this.y,this.radius)this.color.addColorStop(0,this.sColor)this.color.addColorStop(1,this.eColor)cxt.fillStyle=this.colorcxt.fill()cxt.restore()cxt.closePath()this.time+=1}}//创建各个星球类,通过伪继承function Sun(){Star.call(this,0,0,20,0,"#f00","#f00")}function Mercury(){Star.call(this,0,-50,10,88,"#A69888","#5c3666")}function Venus(){Star.call(this,0,-100,10,225,"#C4BBAC","#1F1666")}function Earth(){Star.call(this,0,-150,10,365,"#78b1eb","#050c12")}function Mars(){Star.call(this,0,-200,10,687,"#cec999","#76422d")}function Jupiter(){Star.call(this,0,-250,10,4333,"#c0a48e","#322222")}function Saturn(){Star.call(this,0,-300,10,10766,"#f7f9e3","#1F1666")}function Uranus(){Star.call(this,0,-350,10,30799,"#a7e1e8","#19243a")}function Neptune(){Star.call(this,0,-400,10,60129,"#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(){cxt.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)function anim(){requestAnimationFrame(function(){move()anim()})}anim()</script>
</body>
</html>
这篇关于Html Canva之多行星环绕动态图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!