当前位置:首页 > VUE

vue实现粒子动画特效

2026-01-20 20:31:51VUE

Vue 实现粒子动画特效的方法

粒子动画通常通过 Canvas 或第三方库实现,以下是几种常见实现方式:

使用 Canvas 原生实现

通过 Vue 封装 Canvas 粒子动画组件:

<template>
  <canvas ref="canvas" class="particle-canvas"></canvas>
</template>

<script>
export default {
  mounted() {
    this.initCanvas();
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;

      // 粒子配置
      const particles = [];
      const particleCount = 100;

      for (let i = 0; i < particleCount; i++) {
        particles.push({
          x: Math.random() * canvas.width,
          y: Math.random() * canvas.height,
          size: Math.random() * 5 + 1,
          speedX: Math.random() * 3 - 1.5,
          speedY: Math.random() * 3 - 1.5
        });
      }

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

        particles.forEach(particle => {
          particle.x += particle.speedX;
          particle.y += particle.speedY;

          // 边界检测
          if (particle.x < 0 || particle.x > canvas.width) particle.speedX *= -1;
          if (particle.y < 0 || particle.y > canvas.height) particle.speedY *= -1;

          ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
          ctx.beginPath();
          ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
          ctx.fill();
        });

        requestAnimationFrame(animate);
      }

      animate();
    }
  }
};
</script>

<style>
.particle-canvas {
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
}
</style>

使用 particles.js 库

安装 particles.js 的 Vue 封装版本:

npm install particles.js vue-particles

组件使用方式:

<template>
  <div id="app">
    <vue-particles
      color="#ffffff"
      :particleOpacity="0.7"
      :particlesNumber="80"
      shapeType="circle"
      :particleSize="4"
      linesColor="#ffffff"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    />
  </div>
</template>

<script>
import VueParticles from 'vue-particles'
export default {
  components: {
    VueParticles
  }
}
</script>

使用 tsParticles 库

安装 tsParticles(更现代的替代方案):

npm install tsparticles-slim

Vue 组件实现:

vue实现粒子动画特效

<template>
  <div id="tsparticles"></div>
</template>

<script>
import { loadSlim } from "tsparticles-slim";

export default {
  name: "ParticlesBackground",
  mounted() {
    this.initParticles();
  },
  methods: {
    async initParticles() {
      await loadSlim((engine) => {
        return engine.load("tsparticles", {
          particles: {
            number: { value: 80 },
            move: { enable: true },
            opacity: { value: 0.5 },
            size: { value: 3 },
            links: {
              enable: true,
              distance: 150
            }
          }
        });
      }).then(({ container }) => {
        container.loadCanvas("tsparticles", {
          background: {
            color: "#0d47a1"
          }
        });
      });
    }
  }
};
</script>

性能优化建议

  • 对于大量粒子(>1000),考虑使用 WebGL 渲染(如 three.js)
  • 粒子数量根据设备性能动态调整
  • 组件销毁时清除动画循环
  • 移动设备减少粒子数量

以上方法可根据项目需求选择,原生 Canvas 适合定制化需求,而 particles.js/tsParticles 适合快速实现标准粒子效果。

标签: 粒子特效
分享给朋友:

相关文章

jquery 特效

jquery 特效

jQuery 特效实现方法 jQuery 提供了丰富的特效方法,可以轻松实现动画、渐变、滑动等效果。以下是一些常见的 jQuery 特效实现方式: 淡入淡出效果 使用 fadeIn() 和 fade…

vue有没有实现特效

vue有没有实现特效

Vue 可以通过多种方式实现特效,包括内置过渡动画、第三方动画库、CSS 动画和 JavaScript 动画库。以下是一些常见方法: 使用 Vue 内置过渡动画 Vue 提供了 <transi…

vue跳页面实现特效

vue跳页面实现特效

Vue 页面跳转实现特效的方法 在 Vue 中实现页面跳转特效可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 的过渡动画 Vue Router 与 Vue 的过渡系统结合,可以…

css制作特效

css制作特效

CSS 制作特效的方法 CSS 提供了多种方式实现动态和视觉特效,以下是一些常见方法: 动画效果 通过 @keyframes 和 animation 属性创建复杂动画: @keyframes fa…

css文字制作特效

css文字制作特效

文字阴影效果 使用 text-shadow 属性为文字添加阴影,支持多阴影叠加。参数依次为水平偏移、垂直偏移、模糊半径和颜色。 .text-shadow { text-shadow: 2px…

css能制作特效

css能制作特效

CSS 制作特效的方法 CSS 提供了多种方式来实现网页特效,包括动画、过渡、变形和滤镜等。以下是一些常见的 CSS 特效实现方法: 动画(Animation) 通过 @keyframes 规则定义…