canvas画太阳系八大行星

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>太阳系八大行星</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    body{
        width: 100%;
        height: 100%;
        
        background: url(./btg.jpg)no-repeat;
        background-size: 100% 100%;
    }
</style>

<body>
    <canvas width="1920" height="1080"  id="canvas">
        您的浏览器不支持canvas
    </canvas>
    <script>
        //获取画布
        var canvas = document.getElementById('canvas');
        //获取绘图环境
        var cxt = canvas.getContext('2d');
        //d单位时间time (1:1天)
        var time = 0;
        // 水 金 地 火 木 土 天 海
        function draw() {
            //清除画布
            cxt.clearRect(0, 0, 1920, 1080);
            //画水星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 200, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画金星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 230, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画地球轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 260, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画火星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 290, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画木星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 350, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画土星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 440, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画天王星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 520, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画海王星轨道
            cxt.strokeStyle = "#FFF";
            cxt.beginPath();
            //路径函数
            cxt.arc(960, 500, 570, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //画小星星
            function xiaoxx() {
                
                cxt.beginPath();
                cxt.arc(960, 200, 1, 0, 360, false);
                cxt.closePath();
                var sunColor = cxt.createRadialGradient(960, 200, 0, 960, 200, 180);
                sunColor.addColorStop(0, "#fff");
                sunColor.addColorStop(1, "#fff");
                cxt.fillStyle = sunColor;
                cxt.fill();


            }
            xiaoxx()


            //画太阳
            cxt.beginPath();
            //路径函数 x,y,r,角度范围,顺时针/逆时针
            cxt.arc(960, 500, 180, 0, 360, false);
            cxt.closePath();
            //填充(渐变色)
            //cxt.createLinearGradient(内圆心x,内圆心y,内半径r,外圆心x,外圆心y,外圆半径r);
            var sunColor = cxt.createRadialGradient(960, 500, 0, 960, 500, 180);
            sunColor.addColorStop(0, "#F00");
            sunColor.addColorStop(1, "#F90");
            cxt.fillStyle = sunColor;
            cxt.fill();

            //画水星
            cxt.save();
            //异次元空间00点
            cxt.translate(960, 500);
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(200, 0, 8, 0, 360, false);
            var earthColor = cxt.createRadialGradient(180, 0, 0, 180, 0, 10);
            cxt.strokeStyle = "#050c12";
            earthColor.addColorStop(0, "#0097e2"); //#&8B1Eb
            earthColor.addColorStop(1, "#007dbc"); //#050c12
            cxt.fillStyle = earthColor;
            cxt.fill();
            cxt.closePath();

            // //画金星
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(230, 0, 10, 0, 360, false);
            var moonColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#e7e700";
            moonColor.addColorStop(0, "#ffff00");
            moonColor.addColorStop(1, "#e7e700");
            cxt.fillStyle = moonColor;
            cxt.fill();

            // //画地球
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(260, 0, 15, 0, 360, false);
            var earthColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#55aaff";
            earthColor.addColorStop(0, "#55aaff");
            earthColor.addColorStop(1, "#80b1ff");
            cxt.fillStyle = earthColor;
            cxt.fill();

            // //画火星
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(290, 0, 5, 0, 360, false);
            var marsColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#3E2715";
            marsColor.addColorStop(0, "#915634");
            marsColor.addColorStop(1, "#673B22");
            cxt.fillStyle = marsColor;
            cxt.fill();

            // //画木星
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(350, 0, 40, 0, 360, false);
            var marsColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#84703c";
            marsColor.addColorStop(0, "#84703c");
            marsColor.addColorStop(1, "#ba9d54");
            cxt.fillStyle = marsColor;
            cxt.fill();

            // //画土星
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(440, 0, 35, 0, 360, false);
            var marsColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#dcaf85";
            marsColor.addColorStop(0, "#F1C385");
            marsColor.addColorStop(1, "#C8A07D");
            cxt.fillStyle = marsColor;
            cxt.fill();

            // 天王星
            cxt.rotate(time * 365 / 360 * Math.PI / 960);
            cxt.beginPath();
            cxt.arc(520, 0, 20, 0, 360, false);
            var marsColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#6083D4";
            marsColor.addColorStop(0, "#4C64A4");
            marsColor.addColorStop(1, "#415E9A");
            cxt.fillStyle = marsColor;
            cxt.fill();

            // 海王星
            cxt.rotate(time * 365 / 1000 * Math.PI / 1200);
            cxt.beginPath();
            cxt.arc(570, 0, 22, 0, 360, false);
            var marsColor = cxt.createRadialGradient(30, 0, 0, 30, 0, 5);
            cxt.strokeStyle = "#6881C0";
            marsColor.addColorStop(0, "#6C8BC8");
            marsColor.addColorStop(1, "#414A6D");
            cxt.fillStyle = marsColor;
            cxt.fill();



            cxt.closePath();
            cxt.restore();
            cxt.restore();


            //每画一次图像,时间参数加1
            time += 1;
        }
        draw();
        //通过修改第二个参数课调整速度
        setInterval(draw, 50);
    </script>
</body>

</html>
posted @ 2020-09-21 17:36  Mr-Qin  阅读(942)  评论(0编辑  收藏  举报