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>
相关推荐
微臣愚钝3 小时前
前端【8】HTML+CSS+javascript实战项目----实现一个简单的待办事项列表 (To-Do List)
前端·javascript·css·html
lilu88888884 小时前
AI代码生成器赋能房地产:ScriptEcho如何革新VR/AR房产浏览体验
前端·人工智能·ar·vr
LCG元4 小时前
Vue.js组件开发-实现对视频预览
前端·vue.js·音视频
阿芯爱编程4 小时前
vue3 react区别
前端·react.js·前端框架
烛.照1034 小时前
Nginx部署的前端项目刷新404问题
运维·前端·nginx
YoloMari4 小时前
组件中的emit
前端·javascript·vue.js·微信小程序·uni-app
浪浪山小白兔5 小时前
HTML5 Web Worker 的使用与实践
前端·html·html5
疯狂小料5 小时前
React 路由导航与传参详解
前端·react.js·前端框架
追光少年33226 小时前
Learning Vue 读书笔记 Chapter 2
前端·javascript·vue.js·vue3
前端熊猫6 小时前
JavaScript 的 Promise 对象和 Promise.all 方法的使用
开发语言·前端·javascript