当前位置:首页 > 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实现的烟花效果,需要注意性能优化:

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

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

vue实现烟花

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

相关文章

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在 V…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HT…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…