canvas绘制星空

无图不欢,先上图

<!DOCTYPE html>
    <html>
    <head>
    	<title></title>
    	<style>
    		*{
    			margin:0;
    			padding:0;
    		}
    	</style> 
    </head>
    <body>
    </body>
    <script type="text/javascript">
    class FwhfStarrySky{
    	constructor(){
    		this.canvas = '';
    		this.context = '';
    		this.timer = null;
    		this.mountainArr = [];
    		this.starArr = [];
    		this.meteorArr = [];
    		this.width = window.innerWidth;
    		this.height = window.innerHeight;
    		this.init();
    	}
    	init(){
    		document.body.innerHTML += "<canvas id='fwhfCanvas'></canvas>";
    		this.canvas = document.getElementById('fwhfCanvas');
    		this.canvas.width = this.width;
    		this.canvas.height = this.height;
    		this.canvas.style.display = 'block';
    		this.context = this.canvas.getContext('2d');
    		
    		var drawMountainX = 0;
    		while(drawMountainX < this.width){
    			if(this.rand(1,2) == 1){
    				this.mountainArr.push([drawMountainX,this.rand(this.height-70,this.height-50),drawMountainX-this.rand(15,25),this.height-60]);
    			}else{
    				this.mountainArr.push([drawMountainX,this.rand(this.height-70,this.height-50)]);
    			}
    			drawMountainX += this.rand(10,30);
    		}
    		
    		var ladder = 0;
    		while(ladder < this.height-300){
    			for(var i = 0 ; i < (this.height-ladder)/100 ; i++){
    				this.starArr.push([this.rand(0,this.width),this.rand(ladder,ladder+20),this.rand(0,10),0.1]);
    			}
    			ladder += 20;
    		}
    		
    		this.drawTimer();
    	}
    	drawSky(){
    		this.context.beginPath();
    		var skyStyle = this.context.createLinearGradient(0,0,0,this.canvas.height);
    		skyStyle.addColorStop(0,"#000211");
    		skyStyle.addColorStop(0.3,"#080d23");
    		skyStyle.addColorStop(0.7,"#18203d");
    		skyStyle.addColorStop(1,"#293756");
    		this.context.fillStyle = skyStyle;
    		this.context.fillRect(0,0,this.width,this.height);
    		this.context.closePath();
    	}
    	drawMountain(){
    		this.context.beginPath();
    		this.context.fillStyle = '#111';
    		this.mountainArr.forEach((v)=>{
    			if(v.length == 4){
    				this.context.quadraticCurveTo(v[2],v[3],v[0],v[1])
    			}else{
    				this.context.lineTo(v[0],v[1]);
    			}
    		});
    		this.context.lineTo(this.width,this.height-60);
    		this.context.lineTo(this.width,this.height);
    		this.context.lineTo(0,this.height);
    		this.context.fill();
    		this.context.closePath();
    	}
    	darwStar(){
    		this.starArr.forEach((v)=>{
    			this.context.beginPath();
    			this.context.fillStyle = "rgba(255,255,255,"+v[2]/10+")"; 
    			this.context.arc(v[0],v[1],1,0,2*Math.PI);
    			this.context.fill();
    			this.context.closePath();
    		});
    	}
    	drawMoon(){
    		this.context.beginPath();
    		var MoonStyle = this.context.createRadialGradient(300,150,38,300,150,50);
    		MoonStyle.addColorStop(0,'rgba(255,255,255,1)');
    		MoonStyle.addColorStop(1,'rgba(255,255,255,0)');
    		this.context.fillStyle = MoonStyle; 
    		this.context.arc(300,150,50,0,2*Math.PI);
    		this.context.fill();
    		this.context.closePath();
    	}
    	drawMeteor(){
    		var meteorNum = this.rand(-9,9);
    		if(meteorNum == 1){
    			this.meteorArr.push([this.rand(0,this.width+this.height),0,this.rand(1,3)]);
    		}
    		this.meteorArr.forEach((v)=>{
    			this.context.beginPath();
    			this.context.fillStyle = "rgba(255,255,255,1)";
    			if(v[0] > this.width){
    				this.context.arc(v[0],v[1]+(v[0]-this.width),1,0,2*Math.PI);
    			}else{
    				this.context.arc(v[0],v[1],1,0,2*Math.PI);
    			}
    			this.context.fill();
    			if(v[0] > this.width){
    				var meteorStyle = this.context.createLinearGradient(v[0],v[1],v[0]+v[2]*20,v[1]+(v[0]-this.width)-v[2]*20);
    				meteorStyle.addColorStop(0,"rgba(255,255,255,1)");
    				meteorStyle.addColorStop(1,"rgba(255,255,255,0)");
    				this.context.strokeStyle = meteorStyle;
    				this.context.lineTo(v[0],v[1]+(v[0]-this.width));
    				this.context.lineTo(v[0]+v[2]*20,v[1]+(v[0]-this.width)-v[2]*20);
    			}else{
    				var meteorStyle = this.context.createLinearGradient(v[0],v[1],v[0]+v[2]*20,v[1]-v[2]*20);
    				meteorStyle.addColorStop(0,"rgba(255,255,255,1)");
    				meteorStyle.addColorStop(1,"rgba(255,255,255,0)");
    				this.context.strokeStyle = meteorStyle;
    				this.context.lineTo(v[0],v[1]);
    				this.context.lineTo(v[0]+v[2]*20,v[1]-v[2]*20);
    			}
    			this.context.stroke();
    			this.context.closePath();
    		})
    		this.meteorArr.forEach((v,index)=>{
    			v[0] -= v[2];
    			v[1] += v[2];
    			if(v[0] < -20 || v[1] > this.height){
    				this.meteorArr.splice(index,1);
    			}
    		})
    	}
    	drawTimer(){
    		this.drawSky();
    		this.darwStar();
    		this.drawMoon();
    		this.drawMeteor();
    		this.drawMountain();
    		this.timer = setInterval(()=>{
    			this.starArr.forEach((v)=>{
    				if(v[2] + v[3] < 0 || v[2] + v[3] > 10){
    					v[3] *= -1;
    				}
    				v[2] += v[3];
    			});
    			this.drawSky();
    			this.darwStar();
    			this.drawMoon();
    			this.drawMeteor();
    			this.drawMountain();
    		},20)
    	}
    	rand(min,max){
    		var c = max - min + 1;
    		return Math.floor(Math.random() * c + min);
    	}
    } 
    new FwhfStarrySky(); 
    </script>
</html> 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风舞红枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值