HTML5---Canvas-模拟9星球运行轨道

规则:

星球  英文名     公转周期         光色     暗色
水星  Mercury     87.70#A69697 #5C3E40 
金星  Venus       224.701#C4BBAC #1F1315
地球  Earth       365.2422#78B1E8 #050C12
火星  Mars        686.98#CEC9B6 #76422D
木星  Jupiter     4332.589#C0A48E #322222
土星  Saturn      10759.5#F7F9E3 #5C4533
天王星 Uranus      30799.095#A7E1E5 #19243A
海王星 Neptune     164.8*365天  #0661B2 #1E3B73

html:

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

js:

<script>
    var cxt=document.getElementById('canvas').getContext('2d');

    //轨道
    function drawTrack(){
        for(var i=0;i<8;i++){
            cxt.beginPath();
            cxt.arc(500,500,(i+1)*50,0,Math.PI*360,false);
            cxt.closePath();
            //设置笔触的颜色
            cxt.strokeStyle="#fff";
            cxt.stroke();
        }

    }
    drawTrack();

    //星球--画出星球需要哪些属性
    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(){                   
            cxt.save();//保存之前的画布内容                  
            cxt.translate(500,500);//重置0,0坐标点                   
            cxt.rotate(this.time*(360/this.cycle)*Math.PI/180);//设置旋转角度                 
            cxt.beginPath();//画星球
            cxt.arc(this.x,this.y,this.radius,0,360,false);
            cxt.closePath();
            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.color;   //将渐变对象复制给填充画笔
            cxt.fill();                 
            cxt.restore();  //恢复之前保存的画布内容                   
            this.time+=1;//执行完毕之后时间增加
        }
    }

    //创建一个太阳对象的构造函数
    function Sun(){ Star.call(this,0,0,20,0,"#F00","#f90")}

    //水星
    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.224,"#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 water=new Mercury();
    var gold=new Venus();
    var diqiu=new Earth();
    var fire=new Mars();
    var wood=new Jupiter();
    var soil=new Saturn();
    var god=new Uranus();
    var sea=new Neptune();

    function move(){                
        cxt.clearRect(0,0,1000,1000); //清除画布                
        drawTrack();    //画出轨道      

        sun.draw();     //调用-画出每个星球
        water.draw();
        gold.draw();
        diqiu.draw();
        fire.draw();
        wood.draw();
        soil.draw();
        god.draw();
        sea.draw();
    }

    //使个星球进行运动
    setInterval(move,10);
</script>

github
效果图:
这里写图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uni-app中使用html2canvas生成图片并点击保存,你可以按照以下示例代码进行操作: 1. 首先,安装html2canvas依赖: ``` npm install html2canvas --save ``` 2. 在需要使用html2canvas的页面中引入html2canvas库: ```html <template> <view> <!-- 页面内容 --> </view> </template> <script> import html2canvas from 'html2canvas'; export default { methods: { saveImage() { // 获取需要生成图片的DOM元素 const target = document.getElementById('target'); // 使用html2canvas生成图片 html2canvas(target).then(canvas => { // 将canvas转换为图片地址 const imgUrl = canvas.toDataURL('image/png'); // 创建a标签并设置下载属性 const link = document.createElement('a'); link.href = imgUrl; link.download = 'image.png'; // 模拟点击下载 link.click(); }); }, }, }; </script> <style> /* 样式 */ </style> ``` 3. 在需要保存图片的地方调用`saveImage`方法: ```html <template> <view> <!-- 页面内容 --> <button @click="saveImage">保存图片</button> </view> </template> <script> export default { methods: { saveImage() { // 调用保存图片方法 }, }, }; </script> <style> /* 样式 */ </style> ``` 在以上代码中,我们使用`html2canvas`库将指定的DOM元素(id为`target`)生成为一个canvas对象,然后将canvas转换为图片地址,并创建一个带有下载属性的a标签,模拟点击该标签实现下载保存功能。你可以根据自己的需求进行相应的样式和DOM元素的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值