代码 炫酷彩色粒子鼠标跟随动画效果(附源代码)

2024-11-18 05:19:55 +0800 CST views 1156

炫酷彩色粒子鼠标跟随动画效果(附源代码)

这是一个交互式的彩色粒子动画,主要特点是随着鼠标移动产生五彩缤纷的粒子爆炸效果。通过结合Canvas绘图、事件监听和动画循环,创造了一个流畅、互动性强的视觉体验。
images

实现逻辑

  1. 粒子系统

    • 定义Particle类,每个粒子有位置、大小、颜色和速度属性。
    • 粒子会随时间缩小并移动。
  2. 粒子爆炸效果

    • createExplosion函数在鼠标位置创建多个随机颜色和速度的粒子。
    • 使用setInterval定期在鼠标位置创建新的粒子爆炸。
  3. 动画循环

    • 使用requestAnimationFrame持续更新和绘制粒子。
    • 移除过小的粒子以优化性能。

源代码

<style>
  body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
  }

  #confettiCanvas {
    background-color: #1a202c;
    position: absolute;
    top: 0;
    left: 0;
  }

  .instruction {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-family: Arial, sans-serif;
    font-size: 24px;
    font-weight: bold;
    text-align: center;
    pointer-events: none;
  }
</style>

<canvas id="confettiCanvas"></canvas>
<p class="instruction">鼠标轻轻一动,五彩纷飞!</p>

<script>
  const canvas = document.getElementById("confettiCanvas");
  const ctx = canvas.getContext("2d");
  const colors = ["#FF3F8E", "#04C2C9", "#2E55C1", "#F9D423"];

  let particles = [];
  let mousePos = { x: 0, y: 0 };

  class Particle {
    constructor(x, y, size, color, speedX, speedY) {
      this.x = x;
      this.y = y;
      this.size = size;
      this.color = color;
      this.speedX = speedX;
      this.speedY = speedY;
    }

    update() {
      this.x += this.speedX;
      this.y += this.speedY;
      this.size *= 0.98; // 粒子逐渐缩小
    }

    draw() {
      ctx.fillStyle = this.color;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
      ctx.closePath();
      ctx.fill();
    }
  }

  function resizeCanvas() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
  }

  function createExplosion(x, y) {
    const particleCount = 50;
    for (let i = 0; i < particleCount; i++) {
      const size = Math.random() * 5 + 2;
      const color = colors[Math.floor(Math.random() * colors.length)];
      const speedX = (Math.random() * 2 - 1) * 2;
      const speedY = (Math.random() * 2 - 1) * 2;
      particles.push(new Particle(x, y, size, color, speedX, speedY));
    }
  }

  function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    particles = particles.filter((particle) => {
      particle.update();
      particle.draw();
      return particle.size > 0.5;
    });

    requestAnimationFrame(animate);
  }

  function handleMouseMove(event) {
    mousePos.x = event.clientX;
    mousePos.y = event.clientY;
  }

  resizeCanvas();
  animate();

  window.addEventListener("resize", resizeCanvas);
  canvas.addEventListener("mousemove", handleMouseMove);

  setInterval(() => {
    createExplosion(mousePos.x, mousePos.y);
  }, 50);
</script>

使用方式

  1. 复制上方的源代码到一个新的HTML文件中。
  2. 使用浏览器打开该HTML文件即可运行。

功能概述

  • 随着鼠标的移动,粒子爆炸会在鼠标位置产生并逐渐消散,营造出炫酷的动态效果。
  • 该代码优化了粒子管理,粒子一旦缩小至一定大小便会被移除,确保动画流畅且性能较高。
复制全文 生成海报 动画 交互设计 Web开发

推荐文章

使用 Nginx 获取客户端真实 IP
2024-11-18 14:51:58 +0800 CST
Python设计模式之工厂模式详解
2024-11-19 09:36:23 +0800 CST
Golang - 使用 GoFakeIt 生成 Mock 数据
2024-11-18 15:51:22 +0800 CST
智慧加水系统
2024-11-19 06:33:36 +0800 CST
PHP来做一个短网址(短链接)服务
2024-11-17 22:18:37 +0800 CST
Vue 中如何处理父子组件通信?
2024-11-17 04:35:13 +0800 CST
Golang 中应该知道的 defer 知识
2024-11-18 13:18:56 +0800 CST
deepcopy一个Go语言的深拷贝工具库
2024-11-18 18:17:40 +0800 CST
动态渐变背景
2024-11-19 01:49:50 +0800 CST
Vue3中如何处理路由和导航?
2024-11-18 16:56:14 +0800 CST
Vue 中如何处理跨组件通信?
2024-11-17 15:59:54 +0800 CST
Nginx 如何防止 DDoS 攻击
2024-11-18 21:51:48 +0800 CST
XSS攻击是什么?
2024-11-19 02:10:07 +0800 CST
Vue3中如何扩展VNode?
2024-11-17 19:33:18 +0800 CST
php常用的正则表达式
2024-11-19 03:48:35 +0800 CST
git使用笔记
2024-11-18 18:17:44 +0800 CST
Vue3中如何处理SEO优化?
2024-11-17 08:01:47 +0800 CST
PHP 命令行模式后台执行指南
2025-05-14 10:05:31 +0800 CST
Vue3中的v-bind指令有什么新特性?
2024-11-18 14:58:47 +0800 CST
程序员茄子在线接单