canvas+js绘制星空页面

canvas+js绘制星空页面

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			body{
				overflow: hidden;
			}
			#canvas{
				display: block;
				background-color: #000000;
			}
		</style>
	</head>
	<body>
		<canvas id="canvas"></canvas>
		<script type="text/javascript">
			var w,h;
			var rainCount = 20;  //流星雨的数量
			var num = 200; //动点数量
			var rains = [];
			var data =[];       
			var cxt;
			init();
			function init(){
				canvas = document.getElementById("canvas");
				w = canvas.width = window.innerWidth;
				h = canvas.height = window.innerHeight;
				cxt = canvas.getContext("2d");
			}
			window.onload = function(){
				init2();AAAAAAAAAAAAAAAAAAA
				for(var i=0;i<rainCount;i++){
					var rain = new Meteor();
					rain.init();
					rain.draw();
					rains.push(rain);
				}
				meteorMove();
			}
			/***********绘制动点*****************/
			function init2(){
				for(var i=0;i<num;i++){
					data[i] = {
						x:Math.random()*w,
						y:Math.random()*h,
						cX:Math.random()*0.6-0.3,
						cY:Math.random()*0.6-0.3
					     };
					cricle(data[i].x,data[i].y);
				}
			}
			function cricle(x,y){
				var c = Math.random();
				cxt.save();
				cxt.beginPath();
				cxt.arc(x,y,1,0,2*Math.PI);
				cxt.fillStyle = "pink";
				cxt.fill();
				cxt.closePath();
				cxt.restore();
			} 
			/*****************************/
			/****流星***/
			var Meteor = function(){
					this.x = -1;
					this.y = -1;
					this.width = -1;
					this.height = -1;
					this.length = -1;
					this.angle = 0;
					this.offset_x =0;
					this.offset_y =0;
					this.speed = 0;
					this.color1 = "";
					this.color2 = "";
				
				this.init = function(){
					this.x = w*Math.random();
					this.y = h*Math.random();
					this.rainColor();
					this.length = Math.ceil(Math.random()*60+170);
					this.angle = 30;
					this.width = this.length*(Math.cos(this.angle*3.14/180));
					this.height = this.length*(Math.sin(this.angle*3.14/180));
				}
					/*****流星颜色****/
				this.rainColor = function(){
					var c =255-parseInt(Math.random()*240);
					this.color1 = "rgba("+c+",20,100,0.5)";
					this.color2 = "#000000";
				}
				/*****绘制流星雨***/
				this.draw = function(){
					cxt.save();
					cxt.beginPath();
					cxt.lineWidth = 1;
					var gradient = cxt.createLinearGradient(this.x,this.y,this.x-this.width,this.y-this.height);
					gradient.addColorStop(0,"white");
					gradient.addColorStop(0.3,this.color1);
					gradient.addColorStop(0.6,this.color2);
					cxt.strokeStyle = gradient;
					cxt.moveTo(this.x,this.y);
					cxt.lineTo(this.x-this.width,this.y-this.height);
					cxt.closePath();
					cxt.stroke();
					cxt.restore();
				}
				/****流星移动***/
				this.move = function(){
					this.speed = Math.random()*2;
					this.offset_x = this.speed*Math.cos(this.angle*3.14/180);
					this.offset_y = this.speed*Math.sin(this.angle*3.14/180);
					//cxt.clearRect(this.x-this.width,this.y-this.height,this.width,this.height);
					/********重新计算坐标点***********/
					this.x = this.x+this.offset_x;
					this.y = this.y+this.offset_y;
					this.draw();
					
				}
				
			}
			/******所有的流星移动*******/
			function meteorMove(){
				for(var i=0;i<rainCount;i++){
					rains[i].move();
					/*****对超出边界的流星就行处理*****/
					if(rains[i].y>=window.innerHeight  || rains[i].y>=window.innerWidth){
						cxt.clearRect(rains[i].x-rains[i].width,rains[i].y-rains[i].height,rains[i].width,rains[i].height);
						rains[i] = new Meteor();
						rains[i].init();
					}
				}
				setTimeout("meteorMove()",2);
			}
		</script>
	</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Canvas绘制星空烟花的示例代码: HTML代码: ``` <canvas id="myCanvas"></canvas> ``` JavaScript代码: ```javascript // 获取canvas和绘图上下文 var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); // 设置布宽度和高度 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 定义星星对象 function Star(x, y, radius, speed) { this.x = x; this.y = y; this.radius = radius; this.speed = speed; } // 定义火花对象 function Spark(x, y, color, radius, speed, direction) { this.x = x; this.y = y; this.color = color; this.radius = radius; this.speed = speed; this.direction = direction; } // 定义一个数组来存储火花 var sparks = []; // 创建星星 var stars = []; for (var i = 0; i < 200; i++) { var x = Math.random() * canvas.width; // 随机x坐标 var y = Math.random() * canvas.height; // 随机y坐标 var radius = Math.random() * 3; // 随机半径 var speed = Math.random() * 2; // 随机速度 stars.push(new Star(x, y, radius, speed)); // 添加到星星数组 } // 动循环 function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); // 清空布 // 绘制星星 ctx.fillStyle = '#ffffff'; for (var i = 0; i < stars.length; i++) { var star = stars[i]; ctx.beginPath(); ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2); ctx.fill(); star.x -= star.speed; // 移动星星 if (star.x < 0) { star.x = canvas.width; // 重置x坐标 } } // 绘制火花 for (var i = 0; i < sparks.length; i++) { var spark = sparks[i]; ctx.fillStyle = spark.color; ctx.beginPath(); ctx.arc(spark.x, spark.y, spark.radius, 0, Math.PI * 2); ctx.fill(); spark.x += Math.cos(spark.direction) * spark.speed; // 计算x方向速度 spark.y += Math.sin(spark.direction) * spark.speed; // 计算y方向速度 spark.radius -= 0.05; // 缩小半径 } // 移除已经消失的火花 for (var i = sparks.length - 1; i >= 0; i--) { if (sparks[i].radius < 0) { sparks.splice(i, 1); } } requestAnimationFrame(animate); // 动循环 } // 鼠标点击事件,添加火花 canvas.addEventListener('click', function(e) { var x = e.clientX; var y = e.clientY; var colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#00ffff', '#ff00ff']; // 颜色数组 for (var i = 0; i < 30; i++) { var color = colors[Math.floor(Math.random() * colors.length)]; var radius = Math.random() * 4 + 1; // 随机半径 var speed = Math.random() * 5 + 1; // 随机速度 var direction = Math.random() * Math.PI * 2; // 随机方向 sparks.push(new Spark(x, y, color, radius, speed, direction)); // 添加到火花数组 } }); animate(); // 开始动循环 ``` 这段代码将创建200个星星,并且在鼠标点击事件触发时添加30个随机颜色的火花。火花会随机移动,并在逐渐缩小半径后消失。同时,星星也会随机移动,创建出星空的效果。您可以根据需要进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值