| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2139 人关注过本帖
标题:HTML5 之 太阳系2,这次连卫星都有了
取消只看楼主 加入收藏
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
结帖率:98.25%
收藏
 问题点数:0 回复次数:2 
HTML5 之 太阳系2,这次连卫星都有了
<!DOCTYPE html>

<html lang="en" xmlns="http://www.
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <canvas width="1600" height="1600" style="background-color:#000;" id="em"></canvas>
    <script>
        var time = 0, span = 50, gc = document.getElementById('em').getContext('2d'), x = 800, y = 800;
        //行星参数,days公转周期,incolor渐变内色,outcolor渐变外色,orbit轨道半径,planet行星半径,start起始位置(弧度)
        var planets = [
            {
                days: 87.7, incolor: 'rgb(166,150,151)', outcolor: 'rgb(93,62,64)', orbit: 65, planet: 3, start: 0,
                moon: []
            }, {
                days: 224.7, incolor: 'rgb(196,187,172)', outcolor: 'rgb(31,19,21)', orbit: 90, planet: 4, start: 0,
                moon: []
            }, {
                days: 365.24, incolor: 'rgb(120,177,232)', outcolor: 'rgb(5,12,18)', orbit: 130, planet: 5, start: 0,
                moon: [
                    { days: 27.3, color: 'rgb(200, 200, 200)', orbit: 10, r: 2, start: 0 }
                ]
            }, {
                days: 686.98, incolor: 'rgb(206,201,182)', outcolor: 'rgb(118,66,45)', orbit: 180, planet: 3, start: 0,
                moon: []
            }, {
                days: 4332.59, incolor: 'rgb(192,164,143)', outcolor: 'rgb(50,34,34)', orbit: 300, planet: 15, start: 0,
                moon: [
                    { days: 1.77, color: 'rgb(200, 200, 200)', orbit: 20, r: 2, start: 0 },
                    { days: 3.55, color: 'rgb(100, 132, 76)', orbit: 30, r: 2, start: 0 },
                    { days: 7.15, color: 'rgb(45, 213, 95)', orbit: 40, r: 2, start: 0 },
                    { days: 16.69, color: 'rgb(156, 30, 247)', orbit: 50, r: 2, start: 0 }
                ]
            }, {
                days: 10759.5, incolor: 'rgb(247,249,227)', outcolor: 'rgb(92,69,51)', orbit: 450, planet: 12, start: 0,
                moon: [
                    { days: 1.88, color: 'rgb(156, 30, 247)', orbit: 17, r: 2, start: 0 },
                    { days: 2.74, color: 'rgb(200, 200, 200)', orbit: 27, r: 2, start: 0 },
                    { days: 4.51, color: 'rgb(100, 132, 76)', orbit: 37, r: 2, start: 0 },
                    { days: 15.95, color: 'rgb(45, 213, 95)', orbit: 47, r: 2, start: 0 },
                    { days: 79.33, color: 'rgb(250, 6, 14)', orbit: 57, r: 2, start: 0 }
                ]
            }, {
                days: 30799.1, incolor: 'rgb(167,225,228)', outcolor: 'rgb(25,36,58)', orbit: 600, planet: 10, start: 0,
                moon: [
                    { days: 4.14, color: 'rgb(156, 30, 247)', orbit: 17, r: 2, start: 0 },
                    { days: 8.71, color: 'rgb(200, 200, 200)', orbit: 27, r: 2, start: 0 },
                    { days: 13.46, color: 'rgb(100, 132, 76)', orbit: 37, r: 2, start: 0 }
                ]
            }, {
                days: 60152, incolor: 'rgb(6,97,178)', outcolor: 'rgb(30,59,115)', orbit: 750, planet: 9, start: 0,
                moon: [
                    { days: 5.88, color: 'rgb(200, 200, 200)', orbit: 15, r: 2, start: 0 }
                ]
            }
        ];
        function drawSun() {
            //绘制太阳
            gc.beginPath();
            gc.arc(x, y, 40, 0, 2 * Math.PI);
            gc.closePath();
            grad = gc.createRadialGradient(x, y, 0, x, y, 35);
            grad.addColorStop(0, 'rgb(255,0,0)');
            grad.addColorStop(1, 'rgb(255,153,0)');
            gc.fillStyle = grad;
            gc.fill();

            for (var i = 0, len = planets.length; i < len; i++) {
                drawPlanet(i);
            }
        };
        function drawPlanet(i) {
            var grad = null, pl = planets[i];

            if (pl.start == 0) {
                pl.start = Math.floor(Math.random() * 360);
            }

            //绘制行星轨道
            gc.beginPath();
            gc.arc(x, y, pl.orbit, 0, 2 * Math.PI);
            gc.closePath();
            gc.strokeStyle = 'rgba(50,50,50,0.5)';
            gc.stroke();

            //绘制行星
            gc.save();
            gc.translate(x, y);
            gc.rotate((time * 360 / pl.days + pl.start) * Math.PI / 180);
            gc.beginPath();
            gc.arc(0, -pl.orbit, pl.planet, 0, 2 * Math.PI);
            gc.closePath();
            grad = gc.createRadialGradient(0, -pl.orbit, 0, 0, -pl.orbit, pl.planet);
            grad.addColorStop(0, pl.incolor);
            grad.addColorStop(1, pl.outcolor);
            gc.fillStyle = grad;
            gc.fill();

            for (var j = 0, leg = pl.moon.length; j < leg; j++) {
                drowMoon(pl, j);
            }

            gc.restore();
        };
        function drowMoon(pl, j) {
            var sl = pl.moon[j];
            if (sl.start == 0) {
                sl.start = Math.floor(Math.random() * 360);
            }
            //月球轨道
            gc.beginPath();
            gc.arc(0, -pl.orbit, sl.orbit, 0, 2 * Math.PI);
            gc.closePath();
            gc.stroke();
            //绘制月球
            gc.save();
            gc.translate(0, -pl.orbit);
            gc.rotate((time * 360 / sl.days + sl.start) * Math.PI / 180);
            gc.beginPath();
            gc.arc(0, -sl.orbit, sl.r, 0, 2 * Math.PI);
            gc.closePath();

            gc.fillStyle = sl.color;
            gc.fill();
            gc.restore();
        };

        function drawSolar() {
            gc.clearRect(0, 0, 1600, 1600);
            drawSun();
            time += span / 1000;
        }

        setInterval(drawSolar, 25);
    </script>
</body>
</html>

[ 本帖最后由 冰镇柠檬汁儿 于 2015-5-30 09:52 编辑 ]
收到的鲜花
  • 林月儿2015-05-29 22:52 送鲜花  49朵   附言:大神就是不一样
搜索更多相关主题的帖子: 太阳系 planet HTML5 style title 
2015-05-29 22:49
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
银河系包括一千二百亿颗恒星和大量的星团、星云,还有各种类型的星际气体和星际尘埃
这么庞大的数据量,你让我怎么实现?
不过反过来想想也到是很容易,大不了用图片旋转就可以了,这样代码连10句都用不了

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2015-05-31 10:25
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
我自己精简了一下代码,把行星和卫星的绘制,精简到了一个函数里,代码懒得放了
谁打算拜师就私信找我吧

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2015-05-31 17:09
快速回复:HTML5 之 太阳系2,这次连卫星都有了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029497 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved