当前位置:首页 > VUE

vue实现点击烟花

2026-02-18 18:19:07VUE

Vue实现点击烟花效果

在Vue中实现点击烟花效果,可以通过监听点击事件并动态生成烟花元素。以下是两种实现方式:

vue实现点击烟花

方式一:使用CSS动画和动态元素

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

<script>
export default {
  data() {
    return {
      fireworks: []
    }
  },
  methods: {
    createFirework(e) {
      const colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff']

      for (let i = 0; i < 50; i++) {
        const angle = Math.random() * Math.PI * 2
        const velocity = 5 + Math.random() * 7
        const size = 3 + Math.random() * 5

        this.fireworks.push({
          style: {
            left: `${e.clientX}px`,
            top: `${e.clientY}px`,
            backgroundColor: colors[Math.floor(Math.random() * colors.length)],
            transform: `translate(${Math.cos(angle) * velocity * 10}px, ${Math.sin(angle) * velocity * 10}px)`,
            width: `${size}px`,
            height: `${size}px`,
            opacity: 1
          }
        })
      }

      setTimeout(() => {
        this.fireworks = []
      }, 1000)
    }
  }
}
</script>

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

.firework {
  position: absolute;
  border-radius: 50%;
  transition: transform 1s ease-out, opacity 1s ease-out;
}
</style>

方式二:使用Canvas实现高性能烟花

<template>
  <canvas 
    ref="canvas" 
    @click="launchFirework"
    style="position: fixed; top: 0; left: 0; width: 100%; height: 100%;"
  ></canvas>
</template>

<script>
export default {
  mounted() {
    this.canvas = this.$refs.canvas
    this.ctx = this.canvas.getContext('2d')
    this.fireworks = []
    this.particles = []

    this.canvas.width = window.innerWidth
    this.canvas.height = window.innerHeight

    window.addEventListener('resize', () => {
      this.canvas.width = window.innerWidth
      this.canvas.height = window.innerHeight
    })

    this.animate()
  },
  methods: {
    launchFirework(e) {
      const x = e.clientX
      const y = e.clientY

      this.fireworks.push({
        x: x,
        y: y,
        particles: Array(50).fill().map(() => ({
          x: x,
          y: y,
          vx: Math.random() * 6 - 3,
          vy: Math.random() * 6 - 3,
          color: `hsl(${Math.random() * 360}, 100%, 50%)`,
          size: Math.random() * 3 + 1,
          life: 100
        }))
      })
    },
    animate() {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)

      this.fireworks.forEach(fw => {
        fw.particles.forEach(p => {
          p.x += p.vx
          p.y += p.vy
          p.life--

          this.ctx.fillStyle = p.color
          this.ctx.globalAlpha = p.life / 100
          this.ctx.beginPath()
          this.ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2)
          this.ctx.fill()
        })
      })

      this.fireworks = this.fireworks.filter(fw => 
        fw.particles.some(p => p.life > 0)
      )

      requestAnimationFrame(this.animate)
    }
  }
}
</script>

实现要点

第一种方式使用Vue的动态渲染和CSS动画,适合简单的烟花效果。第二种方式使用Canvas,性能更好,适合复杂的烟花效果和大量粒子。

两种方式都可以通过调整参数来改变烟花效果:

  • 粒子数量影响烟花密度
  • 粒子速度影响烟花扩散范围
  • 粒子大小影响视觉效果
  • 颜色可以随机生成或使用特定配色方案

可以根据实际需求选择适合的实现方式,或结合两者优势创建更丰富的交互效果。

vue实现点击烟花

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

相关文章

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template>…