canvas流星创作小例子

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<canvas id="s"></canvas>
		<script>
			var aa = document.getElementById("s");
			var canvas = aa.getContext("2d");
			aa.width = window.innerWidth;
			aa.height = window.innerHeight;
			aa.style.background = "#000000";
			var n1;
			var n2;
		

			n1 = Math.floor(Math.random() * (window.innerWidth - 100 + 1) + 100);//100-window.innerwidth之间的随机数

			n2 = Math.floor(Math.random() * (window.innerHeight - 60 + 1) + 60);//60-window.innerwidth之间的随机数

			var x = n1; //x
			var y = n2;
			var x1 = n1 - 80;
			var y1 = n2 + 80; //y1	

			//画月亮
			function moon() {

				canvas.fillStyle = "white";
				canvas.strokeStyle = "white";
				canvas.arc(100, 30, 25, 0, Math.PI * 2, false);
				canvas.stroke();
				canvas.fill();

			}
			moon();

			var count = 0

			function me() {
				// aa.style.zIndex="-1";
				canvas.clearRect(0, 60, window.innerWidth, window.innerHeight);
				var col = canvas.createLinearGradient(x, y, x1, y1); //让渐变的颜色跟画出的流星一致
				col.addColorStop(0, "black");
				col.addColorStop(1, "white");
				canvas.beginPath();
				canvas.lineWidth = 2.8;
				canvas.strokeStyle = col;
				canvas.moveTo(x, y);
				canvas.lineTo(x1, y1);
				canvas.stroke();

				//requestAnimationFrame的执行速度
				x = x - 6;
				y = y + 6;
				x1 = x1 - 6;
				y1 = y1 + 6;
				requestAnimationFrame(me); //回调函数,可以解决卡顿						

				if (y > window.innerHeight) {
					var n1 = Math.round(Math.random() * window.innerWidth);
					var n2 = Math.round(Math.random() * window.innerHeight);
					if (n1 > 100 && n2 > 60) {
						x = n1; //x
						y = n2;
						x1 = n1 - 80;
						y1 = n2 + 80; //y1	
					}
				}
			}

			me()
		</script>
	</body>
</html>

觉得有帮助的希望支持一下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值