H5 canvas小案例-行星旋转

图示

在这里插入图片描述

直接干代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			
			body{
				overflow: hidden;
			}
			
		</style>
	</head>
	<body>
		
		
		<canvas id="galaxy" width="1500" height="1000" ></canvas>
		
		
		<script type="text/javascript">
			const galaxy = document.querySelector("#galaxy");
			
	
		
			const ct = galaxy.getContext("2d");
			
			
			const colors = ct.createLinearGradient(0,200,200,0);
			colors.addColorStop(0,"#000");
			colors.addColorStop(0.5,"#111111");
			colors.addColorStop(1.0,"#000");
			
			var rcolors = null;
			
			var n = 1;
			
			
			// 屏幕高宽
			const w = 1390;
			const h = 680;
			// 圆心坐标
			const rx = Math.floor(w/2);
			const ry = Math.floor(h/2);
			
			ct.translate(rx,ry);
			function drawGalaxy(){
				ct.clearRect(-rx,-ry,w,h);
				// 背景
				
				ct.fillStyle = colors;
				ct.beginPath();
				ct.fillRect(-rx,-ry,w,h);
				ct.closePath()
				
				// 星星
				for (var i = 0; i < 150; i++) {
					ct.beginPath()
					ct.strokeStyle = 'white';
					ct.arc(getRandom(-rx,rx),getRandom(-ry,ry),1,0, getRandom(90,360) ,false);
					ct.stroke();
				}
				
				
				// 旋转标记
				if(n++ == 360){
					n = 1
				}
				
				
				// 太阳
				rcolors = ct.createRadialGradient(0,0,1,0,0,50);
				rcolors.addColorStop(0,'#ec1221');
				rcolors.addColorStop(0.5,'#ec2427');
				rcolors.addColorStop(1.0,'#ec283c');
				ct.save()
				ct.beginPath()
				ct.fillStyle = rcolors;
				ct.arc(0,0,50,0,360,false);
				ct.fill();
				ct.restore()
				
				// 水星
				rcolors = ct.createRadialGradient(0,0,1,0,0,10);
				rcolors.addColorStop(0,'#0c40ff')
				rcolors.addColorStop(0.5,'#2063ff')
				rcolors.addColorStop(1.0,'#3575ff')
				
				ct.beginPath();
				ct.strokeStyle = "#03A9F4"
				ct.arc(0,0,70,0,360,false);
				ct.stroke();
				ct.closePath()
				
				ct.save()
				
				ct.beginPath()
				ct.rotate(n*3*Math.PI/180);
				ct.fillStyle = rcolors;
				ct.arc(0,-70,10,0,360,false);
				ct.fill();
				ct.closePath()
				ct.restore()
				
				
				// 金星
				rcolors = ct.createRadialGradient(0,0,1,0,0,15);
				rcolors.addColorStop(0,'#ff4c06')
				rcolors.addColorStop(0.5,'#ff7818')
				rcolors.addColorStop(1.0,'#ffb726')
				
				ct.beginPath();
				ct.strokeStyle = "#03A9F4"
				ct.arc(0,0,100,0,360,false);
				ct.stroke();
				ct.closePath()
				
				ct.save()
				
				ct.beginPath()
				ct.rotate(n*4*Math.PI/180);
				ct.fillStyle = rcolors;
				ct.arc(0,-100,15,0,360,false);
				ct.fill();
				ct.closePath()
				ct.restore()
				
				
				// 地球
				rcolors = ct.createRadialGradient(0,0,1,0,0,18);
				rcolors.addColorStop(0,'#0caaff')
				rcolors.addColorStop(0.5,'#78beff')
				rcolors.addColorStop(1.0,'#92c7ff')
				
				ct.beginPath();
				ct.strokeStyle = "#03A9F4"
				ct.arc(0,0,140,0,360,false);
				ct.stroke();
				ct.closePath()
				
				ct.save()
				
				ct.beginPath()
				ct.rotate(n*2*Math.PI/180);
				ct.fillStyle = rcolors;
				ct.arc(0,-140,18,0,360,false);
				ct.fill();
				ct.closePath()
				ct.restore()
				
				
				// 火星
				rcolors = ct.createRadialGradient(0,0,1,0,0,16);
				rcolors.addColorStop(0.2,'#ff5500')
				
				
				ct.beginPath();
				ct.strokeStyle = "#03A9F4"
				ct.arc(0,0,180,0,360,false);
				ct.stroke();
				ct.closePath()
				
				ct.save()
				
				ct.beginPath()
				ct.rotate(n*7*Math.PI/180);
				ct.fillStyle = rcolors;
				ct.arc(0,-180,16,0,360,false);
				ct.fill();
				ct.closePath()
				ct.restore()
				
				
			}
			
		
			
			
			
			
			drawGalaxy()
			setInterval(drawGalaxy,60);
			
			
			
			
			function getRandom(min,max){
				const choose = max - min +1;
				return Math.floor(Math.random()*choose +min);
			}
			
			
			
		</script>
	</body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值