HTML点击鼠标动态特效,屏幕生成特效火花特效

效果视频:

火花视频html+js

直接上代码:

html 复制代码
<!DOCTYPE html>
<html>
<head>
	<title>Mouse Click Effect</title>
	<style>
		body {
			margin: 0;
			padding: 0;
			background-color: #000;
		}
		canvas {
			position: absolute;
			top: 0;
			left: 0;
			z-index: -1;
		}
	</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");
		var particles = [];
		var particleCount = 100;
		var particleRadius = 3;
		var colors = ["#f44336", "#e91e63", "#9c27b0", "#673ab7", "#3f51b5", "#2196f3", "#03a9f4", "#00bcd4", "#009688", "#4caf50", "#8bc34a", "#cddc39", "#ffeb3b", "#ffc107", "#ff9800", "#ff5722", "#795548", "#9e9e9e", "#607d8b"];
		canvas.addEventListener("mousedown", function(e) {
			for (var i = 0; i < particleCount; i++) {
				particles.push(new createParticle(e.clientX, e.clientY));
			}
		});
		function createParticle(x, y) {
			this.x = x;
			this.y = y;
			this.vx = Math.random() * 10 - 5;
			this.vy = Math.random() * 10 - 5;
			this.color = colors[Math.floor(Math.random() * colors.length)];
			this.radius = particleRadius;
			this.alpha = 1;
			this.draw = function() {
				ctx.globalAlpha = this.alpha;
				ctx.beginPath();
				ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
				ctx.fillStyle = this.color;
				ctx.fill();
			}
		}
		function draw() {
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			for (var i = 0; i < particles.length; i++) {
				particles[i].x += particles[i].vx;
				particles[i].y += particles[i].vy;
				particles[i].alpha -= 0.01;
				if (particles[i].alpha <= 0) {
					particles.splice(i, 1);
				} else {
					particles[i].draw();
				}
			}
			requestAnimationFrame(draw);
		}
		draw();
	</script>
</body>
</html>
相关推荐
光影少年9 分钟前
react的hooks优缺点、底层实现及hooks参数
前端·react.js·前端框架
weixin_4569074121 分钟前
2026+:html+css 生态的成型之年与平台化跃迁
前端·css·html
上海合宙LuatOS23 分钟前
LuatOS框架的使用(2)
java·服务器·开发语言·前端·数据库·嵌入式硬件·php
江湖有缘34 分钟前
Docker部署NextTrace Web路由工具
前端·docker·容器
wljt1 小时前
游标分页原理
java·前端·数据库
weixin_456907411 小时前
【html+Tss 故障排查】链20230304 最详细解析之像素已拉取,容器仍起不来(含命令清单)
前端·html
Ulyanov1 小时前
基于Impress.js的3D概念地图设计与实现
开发语言·前端·javascript·3d·ecmascript
jiayong231 小时前
Vue 3 面试题 - TypeScript 与工程化
前端·vue.js·typescript
小白菜学前端1 小时前
Git 推送 Vue 项目到远程仓库完整流程
前端·git
A南方故人1 小时前
一个用于实时检测 web 应用更新的 JavaScript 库
开发语言·前端·javascript