canvas学习 太阳轨道

原文地址:https://blog.csdn.net/u012468376/article/details/73350998

代码笔记

<body>
<canvas id="can" width="300" height="300" style="background-color: black;"></canvas>
<script>
    function draw(){
        // sun()
        var can = document.getElementById("can");
        if(!can.getContext) return;
        var can = can.getContext("2d")
        can.save()
        // 保存最初的原点位置
        // 方便在后面恢复
        can.clearRect(0,0,300,300)
        // 清除页面
        // 啦啦啦,种太阳
        can.beginPath();
        can.fillStyle = "red"
        can.arc(150,150,20,0,Math.PI*2)
        can.fill()
        // 地球轨道
        can.moveTo(250,150)
        can.strokeStyle = "red"
        can.arc(150,150,100,0,Math.PI*2)
        can.stroke()
        // 地球
        can.translate(150,150)
        let time = new Date()
        can.rotate(2*Math.PI/60*time.getSeconds()+2*Math.PI/60000*time.getMilliseconds())
        // 转动x坐标轴
        // 地球转动的关键
        can.translate(100,0)
        can.beginPath()
        can.fillStyle = "blue"
        can.arc(0,0,10,0,Math.PI*2)
        can.fill()
        // 月球轨道
        can.beginPath()
        can.strokeStyle = "blue"
        can.arc(0,0,40,0,Math.PI*2)
        can.stroke()
        // 月球
        can.rotate(2*Math.PI/60*time.getSeconds()+2*Math.PI/60000*time.getMilliseconds())
        can.translate(40,0)
        can.beginPath()
        can.fillStyle = "white"
        can.arc(0,0,5,0,Math.PI*2)
        can.fill()
        can.restore()
        requestAnimationFrame(draw)
        // 屏幕刷新
    }
    draw()
</script>
</body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值