当前位置:首页 > VUE

vue实现烟花

2026-02-10 15:55:47VUE

Vue实现烟花效果的方法

在Vue中实现烟花效果可以通过Canvas绘制或CSS动画两种方式。以下是具体实现方法:

使用Canvas绘制烟花

在Vue组件中创建Canvas元素并绘制烟花动画:

<template>
  <div class="fireworks-container">
    <canvas ref="canvas"></canvas>
  </div>
</template>

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

      class Particle {
        constructor(x, y, color) {
          this.x = x;
          this.y = y;
          this.color = color;
          this.velocity = {
            x: (Math.random() - 0.5) * 8,
            y: (Math.random() - 0.5) * 8
          };
          this.alpha = 1;
          this.decay = Math.random() * 0.015 + 0.01;
        }

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

        update() {
          this.velocity.y += 0.05;
          this.x += this.velocity.x;
          this.y += this.velocity.y;
          this.alpha -= this.decay;
          this.draw();
          return this.alpha > 0;
        }
      }

      let particles = [];

      function createFirework(x, y) {
        const color = `hsl(${Math.random() * 360}, 100%, 50%)`;
        const particleCount = 150;

        for (let i = 0; i < particleCount; i++) {
          particles.push(new Particle(x, y, color));
        }
      }

      function animate() {
        requestAnimationFrame(animate);
        ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        particles = particles.filter(particle => particle.update());
      }

      animate();

      window.addEventListener('click', (e) => {
        createFirework(e.clientX, e.clientY);
      });

      // 自动发射烟花
      setInterval(() => {
        const x = Math.random() * canvas.width;
        const y = Math.random() * canvas.height / 2;
        createFirework(x, y);
      }, 1000);
    }
  }
}
</script>

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

使用CSS动画实现简单烟花

对于简单的烟花效果,可以使用纯CSS实现:

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

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

      this.fireworks.push({
        style: {
          left: `${Math.random() * 100}%`,
          top: `${Math.random() * 100}%`,
          '--color': color
        }
      });

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

<style>
.firework {
  position: fixed;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background-color: var(--color);
  animation: explode 1s ease-out forwards;
  transform: translate(-50%, -50%);
}

@keyframes explode {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(20);
    opacity: 0;
  }
}
</style>

使用第三方库

对于更复杂的烟花效果,可以使用第三方库如canvas-confetti

<template>
  <button @click="fireConfetti">发射烟花</button>
</template>

<script>
import confetti from 'canvas-confetti';

export default {
  methods: {
    fireConfetti() {
      confetti({
        particleCount: 100,
        spread: 70,
        origin: { y: 0.6 }
      });
    }
  }
}
</script>

性能优化建议

对于Canvas实现的烟花效果,需要注意性能优化:

vue实现烟花

  • 限制同时显示的粒子数量
  • 使用requestAnimationFrame进行动画循环
  • 避免频繁的DOM操作
  • 对于移动端,适当减少粒子数量

以上方法可以根据实际需求选择使用,Canvas实现的效果更加丰富,CSS实现更加简单,第三方库则提供了开箱即用的解决方案。

标签: 烟花vue
分享给朋友:

相关文章

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…