js写一个烟花效果

30 篇文章 0 订阅
25 篇文章 0 订阅

调用一个startMove的js文件,这个封装用的原生js,包括获取样式,高度,宽度和透明度等。引用js地址如下https://blog.csdn.net/weixin_40196539/article/details/98477850

如有错误,欢迎指正,不胜感激!

烟花完整代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			body,
			html {
				height: 100%;
				width: 100%;
				background: #000;
				overflow: hidden;
			}
			
			* {
				margin: 0;
				padding: 0;
			}
			
			.boom {
				height: 6px;
				width: 6px;
				position: absolute;
			}
			
			.star {
				height: 50px;
				width: 6px;
				position: absolute;
			}
		</style>
		<script src="js/startMove.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			//产生范围随机数
			function randomInt(min, max) {   
				return Math.round(Math.random() * (max - min)) + min;
			}
			//随机颜色
			function randomcolor() {  
				var r = Math.round(Math.random() * 255).toString(16);//十六进制
				var g = Math.round(Math.random() * 255).toString(16);   
				var b = Math.round(Math.random() * 255).toString(16);   
				return(r.length < 2 ? "0" + r : r) + (g.length < 2 ? "0" + g : g) + (b.length < 2 ? "0" + b : b)
			}
			document.onclick = function(e) {
				e = e || event;
				
				var fire = new Fire({
					x: e.clientX,
					y: e.clientY
				})
				fire.launch();
			}

			function Fire(point) { //发射过程
				this.point = point;//位置
				this.init = function() { //发射的烟花初始化 设置从屏幕最低端发射 随机颜色
					this.ele = document.createElement("div");
					this.ele.className = "star";
					this.ele.style.backgroundColor = "#" + randomcolor();
					this.ele.style.left = this.point.x + "px";
					this.ele.style.top = document.body.offsetHeight + "px";
					document.body.appendChild(this.ele);
				}
				this.init();
			}
			Fire.prototype.launch = function() { //发射
					startMove(this.ele, {
						top: this.point.y,
						height: 6
					}, ()=>{ //移动完成的回调函数
						this.over(); //删除烟花
						var r = randomInt(30, 50);
						for(var i = 0; i < r; i++) { //创建爆炸需要的烟花
							new Spark(this.point).init().go(); //调用爆炸方法 链式调用
						}
					});
				}
			Fire.prototype.over = function() {
					this.ele.remove();
				}

			function Spark(e) {
				var self = this;//除了使用箭头函数外,也可以将this存到一个变量里
				this.init = function() { //爆炸的烟花初始化
					this.ele = document.createElement("div");
					this.ele.className = "boom";
					this.ele.style.backgroundColor = "#" + randomcolor();
					this.ele.style.left = e.x + "px";
					this.ele.style.top = e.y + "px";
					this.ele.speedX = randomInt(-20, 20);
					this.ele.speedY = randomInt(-20, 10);
					document.body.appendChild(this.ele);
					return this; //为了链式调用
				}
				this.go = function() { //爆炸
					var t = setInterval(function() { //定时器控制爆炸出来的每一个烟花的运动
						if(self.ele.offsetTop > document.body.offsetHeight) { //掉出屏幕删除 并停止定时器
							self.ele.remove();
							clearInterval(t);
						}
						self.ele.style.left = self.ele.offsetLeft + self.ele.speedX + "px"; //x轴 左右随机速度
						self.ele.style.top = self.ele.offsetTop + self.ele.speedY++ + "px"; //y轴随机速度并且速度一增加 模拟抛物线
					}, 30);
				}
			}
		</script>
	</head>

	<body>
	</body>

</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的用 JavaScript 实现烟花效果的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>烟花效果</title> <style> canvas { display: block; margin: 0 auto; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ctx = canvas.getContext('2d'); function random(min, max) { return Math.random() * (max - min) + min; } function Particle() { this.x = canvas.width / 2; this.y = canvas.height / 2; this.vx = random(-5, 5); this.vy = random(-15, -5); this.color = 'rgb(' + Math.floor(random(0, 255)) + ',' + Math.floor(random(0, 255)) + ',' + Math.floor(random(0, 255)) + ')'; this.size = random(5, 10); this.alpha = 1; this.gravity = 0.2; } Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.vy += this.gravity; this.alpha -= 0.01; }; Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.globalAlpha = this.alpha; ctx.closePath(); ctx.fill(); }; var particles = []; function createParticles(x, y) { for (var i = 0; i < 100; i++) { var p = new Particle(); p.x = x; p.y = y; particles.push(p); } } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { var p = particles[i]; p.update(); p.draw(); } particles = particles.filter(function(p) { return p.alpha > 0; }); requestAnimationFrame(draw); } canvas.addEventListener('click', function(event) { createParticles(event.clientX, event.clientY); }); draw(); </script> </body> </html> ``` 这段代码实现了点击 canvas 时在点击处创建一组烟花粒子,随着时间的推移,粒子逐渐消失。粒子的位置、速度、颜色、大小和透明度都是随机生成的,从而形成了烟花效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值