当前位置:首页 > VUE

vue实现点击烟花

2026-02-18 18:19:07VUE

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 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…

vue实现网站

vue实现网站

Vue 实现网站的基本步骤 Vue.js 是一个渐进式 JavaScript 框架,适合构建现代化的单页应用(SPA)或动态网站。以下是使用 Vue 实现网站的基本方法。 初始化 Vue 项目 使用…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…

vue实现首页

vue实现首页

Vue 实现首页的基本步骤 安装 Vue.js 和相关依赖,可以通过 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代前端开发。 npm instal…

vue 实现设置密码

vue 实现设置密码

Vue 实现设置密码功能 在 Vue 中实现设置密码功能通常涉及表单输入、验证和提交逻辑。以下是实现的基本步骤和代码示例。 创建密码表单组件 使用 Vue 的单文件组件(SFC)创建一个密码设置表单…

vue 实现跳动爱心

vue 实现跳动爱心

实现跳动爱心的步骤 使用CSS动画和Vue动态绑定 在Vue中创建一个跳动爱心,可以通过CSS动画结合Vue的动态绑定实现。定义一个爱心的形状,使用CSS关键帧动画控制缩放效果。 <templ…