在uniapp中的 canvas的粒子自由扩散背景效果

html部分

<canvas id="myCanvas" style="width:100%; height: 100%;background='rgba(0,0,0,0)'"></canvas>

 方法部分

<script  lang="renderjs"  module="myCanvas">
			console.log('进来了');
			export default {
				mounted (){
					const canvas = document.getElementById("myCanvas").childNodes[0];
					// const canvas = document.getElementsByTagName('canvas')[0];
					// console.log(canvas2);
					// console.log(myClassElements[0]);
					const ctx = canvas.getContext("2d");
					console.log(ctx,'xxxxxxx');
					canvas.width = window.innerWidth;
					canvas.height = window.innerHeight;
					// console.log(canvas.width,canvas.height);
					// 粒子构造函数
					function Particle() {
					  this.x = Math.random() * canvas.width; // x坐标
					  this.y = Math.random() * canvas.height; // y坐标
					  this.vx = (Math.random() - 0.5) * 2; // x轴方向速度
					  this.vy = (Math.random() - 0.5) * 2; // y轴方向速度
					  this.radius = Math.random() * 3; // 粒子大小
					  this.opacity = Math.random(); // 粒子透明度
					}
							
					// 绘制粒子
					Particle.prototype.draw = function() {
					  ctx.beginPath();
					  ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
					  ctx.fillStyle = `rgba(255,255,255, ${this.opacity})`;
					  ctx.fill();
					}
							
					const particles = []; // 粒子集合
					const particleNum = 200; // 粒子数量
							
					// 初始化粒子
					for (let i = 0; i < particleNum; i++) {
					  particles.push(new Particle());
					}
							
					// 动画
					function animate() {
					  ctx.clearRect(0, 0, canvas.width, canvas.height); // 清空画布
							
					  particles.forEach((particle, index) => {
					    particle.draw(); // 绘制粒子
							
					    particle.x += particle.vx;
					    particle.y += particle.vy;
							
					    // 碰撞检测,边缘反弹
					    if (particle.x - particle.radius < 0 || particle.x + particle.radius > canvas.width) {
					      particle.vx = -particle.vx;
					    }
					    if (particle.y - particle.radius < 0 || particle.y + particle.radius > canvas.height) {
					      particle.vy = -particle.vy;
					    }
							
					    // 粒子之间连线
					    for (let i = index + 1; i < particleNum; i++) {
					      const distance = Math.sqrt((particles[i].x - particle.x) ** 2 + (particles[i].y - particle.y) ** 2);
					      if (distance < 100) {
					        ctx.beginPath();
					        ctx.moveTo(particle.x, particle.y);
					        ctx.lineTo(particles[i].x, particles[i].y);
					        ctx.strokeStyle = `rgba(255,255,255,${(100 - distance) / 100})`;
					        ctx.stroke();
					      }
					    }
					  });
					  
					  // 标注文本
					  // ctx.font = "18px Arial";
					  // ctx.fillStyle = "white";
					  // ctx.fillText("自由扩散的粒子特效", canvas.width / 2 - 100, 50);
					  
					  requestAnimationFrame(animate);
					}
							
					animate();
				}
			}
			
	
			  
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值