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

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

hello,今天赶了一天的路,刚刚闲停下来,本节带着大家梳理一下上一节的实现思路

首先分析博客背景效果,其中无非就是点与线,再加了一些运动效果,所以我们自然要从点入手,无点何以有线。
再分析每次刷新界面时点的位置是随机的,个数也应当是用户所设定的,点的大小应该也由用户设定。所以我们应该随机到一个这样的数据格式

[
	[x1,y1,r1],
	[x2.y2,r2],
	....
	[xn,yn,rn]
]

或者json格式的数据也可以,个人习惯了二维数组。

下面就是根据现有分析编写的代码

<script type="text/javascript">
class FwhfPointLine{
	constructor(pointNum,pointR){
		this.pointNum = pointNum;
		this.pointR = pointR;
		this.width = window.innerWidth;
		this.height = window.innerHeight;
		this.pointArr = [];
		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])];
		}
		for(var i = 0 ; i < this.pointNum ; i++){
			this.context.beginPath();
			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();
		}
	}
	rand(min,max){
		var c = max - min + 1;
		return Math.floor(Math.random() * c + min);
	}
}
/*
*pointeNum 随机的点的个数 number 
*pointR 点的半径 array [minR,maxR] 推荐[0.5,1] 
*/
new FwhfPointLine(50,[0.5,1]); 
</script>

init()中首先创建canvas画布,并配置canvas画布的基本样式(this.canvas.style.pointerEvents = ‘none’;实现点击穿透,防止canvas遮挡其他页面元素造成事件失效),及获取canvas绘图环境。 最后就是循环遍历生成需要的数据格式并绘制在canvas上。
rand函数是随机数的封装,不做过多解释。

ok,接下来就是实现给点添加颜色。并让点动起来。
需求数据格式

[
	[x1,y1,r1,colorIndex1,speedX1,speedY1],
	[x2.y2,r2,colorIndex2,speedX2,speedY2],
	....
	[xn,yn,rn,colorIndexn,speedXn,speedYn]
]
<script type="text/javascript">
class FwhfPointLine{
	constructor(pointNum,pointR,pointColor,pointSpeed){
		this.pointNum = pointNum;
		this.pointR = pointR;
		this.pointColor = pointColor;
		this.pointColorLength = pointColor.length;
		this.pointSpeed = pointSpeed;
		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();
	}
	draw(){
		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];
			}
		}
	}
	Repaint(){
		this.timer = setInterval(()=>{
			this.context.clearRect(0,0,this.width,this.height);
			this.draw();
		},20)
	}
	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] 
*/
new FwhfPointLine(50,[0.5,1],['rgb(200,0,0)','rgb(0,200,0)','rgb(0,0,200)'],[-3,3]); 
</script>

主要利用定时器定时清空画布并重新绘制来实现远点运动效果
this.context.clearRect(0,0,this.width,this.height);
this.draw();

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个基于 HTML5 Canvas 的全屏酷星光闪烁 3D 视差背景动画特效的 HTML 代码: ```html <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Canvas Star Field</title> <style> canvas { background-color: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), stars = [], particles = [], maxStars = 1300, maxParticles = 100; // 创建星星 for (var i = 0; i < maxStars; i++) { stars.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height, radius: Math.random() * 1.5, depth: Math.random() * 2000 + 500 }); } // 创建粒子 function emitParticle() { if (particles.length < maxParticles) { var particle = { x: canvas.width / 2, y: canvas.height / 2, vx: Math.random() * 2 - 1, vy: Math.random() * 2 - 1, radius: Math.random() * 2 + 2, alpha: Math.random() * 0.5 + 0.5, life: Math.random() * 200 + 100 }; particles.push(particle); } } // 更新粒子 function updateParticles() { particles.forEach(function(particle, index) { particle.x += particle.vx; particle.y += particle.vy; particle.life--; particle.alpha -= 0.01; if (particle.life <= 0 || particle.alpha <= 0) { particles.splice(index, 1); } }); } // 绘制星星 function drawStar(star) { var x = (star.x - canvas.width / 2) * (star.depth / canvas.width), y = (star.y - canvas.height / 2) * (star.depth / canvas.width), radius = star.radius * (star.depth / canvas.width); context.beginPath(); context.arc(x + canvas.width / 2, y + canvas.height / 2, radius, 0, Math.PI * 2); context.fillStyle = '#fff'; context.fill(); } // 绘制粒子 function drawParticle(particle) { context.beginPath(); context.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2); context.fillStyle = 'rgba(255, 255, 255, ' + particle.alpha + ')'; context.fill(); } // 绘制 function draw() { context.clearRect(0, 0, canvas.width, canvas.height); // 绘制星星 stars.forEach(function(star) { drawStar(star); }); // 绘制粒子 particles.forEach(function(particle) { drawParticle(particle); }); } // 循环 function loop() { emitParticle(); updateParticles(); draw(); requestAnimationFrame(loop); } // 初始化画布 function initCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; window.addEventListener('resize', function() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); } // 初始化 function init() { initCanvas(); loop(); } // 执行初始化 init(); </script> </body> </html> ``` 这段代码创建了一个全屏的 Canvas 画布,并在其中绘制了星星和粒子。通过调整粒子的数量和大小,以及星星的深度和大小,可以得到不同的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风舞红枫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值