canvas实战之酷炫背景动画(四)

系列文章
canvas实战之酷炫背景动画(一)
canvas实战之酷炫背景动画(二)
canvas实战之酷炫背景动画(三)
canvas实战之酷炫背景动画(四)
canvas实战之酷炫背景动画(五)
canvas实战之酷炫背景动画(六)
canvas实战之酷炫背景动画(七)

既然上一节出现了绘制两次直线以及连接处不在圆点处的bug是由于两次for嵌套后造成二次直线绘制时坐标发生了变化所产生的结果。
那我们就将绘制直线提出来,等待圆点绘制完成后再去实现直线的绘制来解决圆点与直线错位问题。
而二次绘制的情况是由于for嵌套后点1到点2,点2到点1绘制了两次,所以我们可以选择只绘制第一次或则第二次
实现原理

for(var i = 0 ; i < x ; i++){
	for(var j = 0 ; j < i-1 ; j++){
		在这里进行绘制即可实现只有一次绘制 ,既由数组后面的点向前面的点绘线
	}
}

下面时具体代码实现

<script type="text/javascript">
class FwhfPointLine{
	constructor(pointNum,pointR,pointColor,pointSpeed,lineMaxLength,lineColor){
		this.pointNum = pointNum;
		this.pointR = pointR;
		this.pointColor = pointColor;
		this.pointColorLength = pointColor.length;
		this.pointSpeed = pointSpeed;
		this.lineMaxLength = Math.pow(lineMaxLength,2);
		this.lineColor = lineColor;
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		this.pointArr = [];
		this.timer = null;
		this.canvas = '';
		this.context = '';
		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.position = "fixed";
		this.canvas.style.top = 0;
		this.canvas.style.left = 0;
		this.canvas.style.pointerEvents = 'none';
		this.context = this.canvas.getContext('2d');
		for(var i = 0 ; i < this.pointNum ; i++){
			this.pointArr[i] = [this.rand(this.pointR[1],this.width-this.pointR[1]),
								this.rand(this.pointR[1],this.height-this.pointR[1]),
								this.rand(this.pointR[0],this.pointR[1]),
								this.rand(0,this.pointColorLength-1),
								this.rand(this.pointSpeed[0],this.pointSpeed[1]),
								this.rand(this.pointSpeed[0],this.pointSpeed[1])];
		}
		this.Repaint();
	}
	drawPoint(){
		for(var i = 0 ; i < this.pointNum ; i++){
			this.context.beginPath();
			this.context.fillStyle = this.pointColor[this.pointArr[i][3]];
			this.context.arc(this.pointArr[i][0],this.pointArr[i][1],this.pointArr[i][2],0,2*Math.PI);
			this.context.fill();
			this.context.closePath();
			
			if(this.pointArr[i][0] + this.pointArr[i][4] >= this.canvas.width){
				this.pointArr[i][0] = this.canvas.width;
				this.pointArr[i][4] *= -1;
			}else if(this.pointArr[i][0] + this.pointArr[i][4] <= 0){
				this.pointArr[i][0] = 0;
				this.pointArr[i][4] *= -1;
			}else{
				this.pointArr[i][0] += this.pointArr[i][4];
			}

			if(this.pointArr[i][1] + this.pointArr[i][5] >= this.canvas.height){
				this.pointArr[i][1] = this.canvas.height;
				this.pointArr[i][5] *= -1;
			}else if(this.pointArr[i][1] + this.pointArr[i][5] <= 0){
				this.pointArr[i][1] = 0;
				this.pointArr[i][5] *= -1;
			}else{
				this.pointArr[i][1] += this.pointArr[i][5];
			}
		}
	}
	drawLine(){
		for(var i = 0 ; i < this.pointNum ; i++){
			for(var j = 0 ; j < i-1 ; j++){
				var showIndex = ((Math.pow(this.pointArr[i][0]-this.pointArr[j][0],2) + Math.pow(this.pointArr[i][1]-this.pointArr[j][1],2))/this.lineMaxLength).toFixed(1);
				if(showIndex < 1){
					this.context.beginPath();
					this.context.strokeStyle = "rgba("+this.lineColor[0]+","+this.lineColor[1]+","+this.lineColor[2]+","+(1-showIndex)+")";
					this.context.moveTo(this.pointArr[i][0],this.pointArr[i][1]);
					this.context.lineTo(this.pointArr[j][0],this.pointArr[j][1]);
					this.context.stroke();
					this.context.closePath();
				}
			}
		}
	}
	Repaint(){
		this.timer = setInterval(()=>{
			
			this.context.clearRect(0,0,this.width,this.height);
			this.drawPoint();
			this.drawLine();
			
		},30)
	}
	rand(min,max){
		var c = max - min + 1;
		return Math.floor(Math.random() * c + min);
	}
}
/*
*pointeNum 随机的点的个数 number 
*pointR 点的半径 array [minR,maxR] 推荐[0.5,1] 
*pointColor 点的颜色 array [color1,color2,...] 
*pointSpeed 点的速度 array [speedX,speedY] 
*lineMaxLength 线条出现的最大长度 number 
*lineColor 线条的颜色 array [0-255,0-255,0-255] 
*/
new FwhfPointLine(50,[0.5,1],['rgb(200,0,0)','rgb(0,200,0)','rgb(0,0,200)'],[-3,3],100,[222,116,159]); 
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风舞红枫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值