当前位置:首页 > VUE

vue实现烟花效果

2026-03-09 16:40:57VUE

Vue实现烟花效果的方法

使用Canvas绘制烟花

在Vue中可以通过Canvas实现烟花效果。创建一个Canvas元素,利用JavaScript绘制烟花爆炸和粒子运动效果。

<template>
  <div>
    <canvas ref="canvas" width="800" height="600"></canvas>
  </div>
</template>

<script>
export default {
  mounted() {
    this.initFireworks();
  },
  methods: {
    initFireworks() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');

      // 烟花粒子数组
      let particles = [];

      // 绘制函数
      function draw() {
        ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        particles.forEach((p, i) => {
          p.x += p.vx;
          p.y += p.vy;
          p.alpha -= 0.01;

          ctx.fillStyle = `rgba(${p.color}, ${p.alpha})`;
          ctx.beginPath();
          ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
          ctx.fill();

          if(p.alpha <= 0) {
            particles.splice(i, 1);
          }
        });

        requestAnimationFrame(draw);
      }

      // 创建烟花
      function createFirework(x, y) {
        const particleCount = 100;
        const color = `${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}`;

        for(let i = 0; i < particleCount; i++) {
          particles.push({
            x,
            y,
            radius: Math.random() * 3 + 1,
            color,
            alpha: 1,
            vx: Math.cos(Math.PI * 2 * i / particleCount) * Math.random() * 6,
            vy: Math.sin(Math.PI * 2 * i / particleCount) * Math.random() * 6
          });
        }
      }

      // 点击触发烟花
      canvas.addEventListener('click', (e) => {
        createFirework(e.offsetX, e.offsetY);
      });

      draw();
    }
  }
}
</script>

使用CSS动画实现简单烟花

对于不需要复杂效果的场景,可以使用纯CSS实现简单的烟花动画。

<template>
  <div class="firework-container" @click="createFirework">
    <div 
      v-for="(firework, index) in fireworks" 
      :key="index" 
      class="firework" 
      :style="firework.style"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fireworks: []
    }
  },
  methods: {
    createFirework(e) {
      const colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff'];
      const color = colors[Math.floor(Math.random() * colors.length)];

      for(let i = 0; i < 12; i++) {
        const angle = (Math.PI * 2 / 12) * i;
        const distance = Math.random() * 100 + 50;

        this.fireworks.push({
          style: {
            left: `${e.clientX}px`,
            top: `${e.clientY}px`,
            backgroundColor: color,
            transform: `translate(${Math.cos(angle) * distance}px, ${Math.sin(angle) * distance}px)`
          }
        });

        setTimeout(() => {
          this.fireworks.shift();
        }, 1000);
      }
    }
  }
}
</script>

<style>
.firework-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.firework {
  position: absolute;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  transition: all 1s ease-out;
}
</style>

使用第三方库

对于更复杂的效果,可以考虑使用第三方库如particles.js或vue-particles。

vue实现烟花效果

安装vue-particles:

npm install vue-particles

使用示例:

vue实现烟花效果

<template>
  <div>
    <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>

性能优化建议

对于大量烟花粒子,建议使用对象池技术重用粒子对象,避免频繁创建和销毁对象。

考虑使用WebGL代替Canvas以获得更好的性能,特别是当需要渲染数千个粒子时。

对于移动设备,减少同时显示的粒子数量以确保流畅的动画效果。

标签: 烟花效果
分享给朋友:

相关文章

vue实现预览效果

vue实现预览效果

Vue 实现预览效果的方法 使用 v-html 指令实现简单预览 在 Vue 中可以通过 v-html 指令直接将 HTML 字符串渲染到页面上,适用于简单的富文本预览场景。 <templat…

js实现滚动条效果

js实现滚动条效果

实现滚动条效果的方法 使用原生JavaScript实现滚动条 通过监听滚动事件,可以动态调整滚动条的位置和样式。以下是一个基础实现示例: // 获取DOM元素 const container = d…

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div>…

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

vue实现凤凰效果

vue实现凤凰效果

Vue 实现凤凰效果 凤凰效果通常指一种视觉动画,模拟凤凰展翅、火焰燃烧等动态效果。在 Vue 中实现这种效果,可以结合 CSS 动画、Canvas 或第三方动画库(如 GSAP)。以下是几种实现方式…

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…