当前位置:首页 > VUE

vue实现点击红包雨

2026-02-25 19:58:17VUE

Vue 实现点击红包雨效果

实现思路 红包雨效果通常包含随机位置生成红包、动画下落、点击交互和计分功能。Vue的响应式特性可以方便地管理红包状态和用户交互。

核心代码实现

<template>
  <div class="red-envelope-rain" @click="handleCanvasClick">
    <canvas ref="canvas"></canvas>
    <div class="score">得分: {{ score }}</div>
    <button @click="startRain">开始红包雨</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      envelopes: [],
      score: 0,
      animationId: null,
      canvas: null,
      ctx: null
    }
  },
  mounted() {
    this.canvas = this.$refs.canvas
    this.ctx = this.canvas.getContext('2d')
    this.resizeCanvas()
    window.addEventListener('resize', this.resizeCanvas)
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
    window.removeEventListener('resize', this.resizeCanvas)
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = window.innerWidth
      this.canvas.height = window.innerHeight
    },
    startRain() {
      this.score = 0
      this.envelopes = []
      this.createEnvelopes()
      this.animate()
    },
    createEnvelopes() {
      for (let i = 0; i < 30; i++) {
        this.envelopes.push({
          x: Math.random() * this.canvas.width,
          y: -50,
          speed: 2 + Math.random() * 3,
          width: 40,
          height: 50,
          color: `hsl(${Math.random() * 30 + 0}, 100%, 50%)`,
          clicked: false
        })
      }
    },
    animate() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)

      this.envelopes.forEach(env => {
        if (!env.clicked) {
          env.y += env.speed
          this.drawEnvelope(env)

          if (env.y > this.canvas.height) {
            env.y = -50
            env.x = Math.random() * this.canvas.width
          }
        }
      })

      this.animationId = requestAnimationFrame(this.animate)
    },
    drawEnvelope(env) {
      this.ctx.fillStyle = env.color
      this.ctx.fillRect(env.x, env.y, env.width, env.height)

      this.ctx.fillStyle = 'gold'
      this.ctx.font = '16px Arial'
      this.ctx.fillText('¥', env.x + 15, env.y + 30)
    },
    handleCanvasClick(e) {
      const rect = this.canvas.getBoundingClientRect()
      const x = e.clientX - rect.left
      const y = e.clientY - rect.top

      this.envelopes.forEach(env => {
        if (!env.clicked &&
            x > env.x && x < env.x + env.width &&
            y > env.y && y < env.y + env.height) {
          env.clicked = true
          this.score += Math.floor(Math.random() * 10) + 1
        }
      })
    }
  }
}
</script>

<style>
.red-envelope-rain {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

canvas {
  display: block;
  background: linear-gradient(to bottom, #87CEEB, #E0F7FA);
}

.score {
  position: absolute;
  top: 20px;
  right: 20px;
  font-size: 24px;
  color: white;
  text-shadow: 1px 1px 2px black;
}

button {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  font-size: 18px;
  background: #FF5252;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
</style>

功能优化建议

增加视觉效果

  • 添加红包打开动画效果,点击时显示金额弹窗
  • 实现红包旋转摆动效果增强真实感
  • 添加背景音乐和点击音效

性能优化

  • 使用对象池管理红包对象,避免频繁创建销毁
  • 对于不可见区域的红包暂停渲染
  • 使用CSS transform代替直接修改位置属性

游戏机制扩展

  • 添加倒计时功能限制游戏时间
  • 实现不同红包类型和不同分值
  • 添加最高分记录功能

实现要点说明

Canvas渲染 使用Canvas而非DOM元素渲染红包,能获得更好的性能表现,特别是在大量红包同时下落时。

响应式设计 通过监听resize事件确保Canvas始终适应窗口尺寸,保证在各种设备上正常显示。

动画循环 使用requestAnimationFrame实现平滑动画效果,在组件销毁时记得取消动画循环防止内存泄漏。

vue实现点击红包雨

碰撞检测 通过简单的矩形碰撞检测判断点击位置是否落在红包上,这是实现交互功能的关键。

标签: 红包vue
分享给朋友:

相关文章

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue实现列表

vue实现列表

Vue 实现列表的方法 在 Vue 中实现列表渲染通常使用 v-for 指令,可以动态生成多个元素。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 遍历数组,生成列表项。v-for 需要…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue实现项目

vue实现项目

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 创建 Vue 项目 使用 Vue CLI 快…

vue滚动加载实现

vue滚动加载实现

vue滚动加载实现 滚动加载是一种常见的优化手段,通过监听滚动事件动态加载数据,减少初始渲染压力。以下是几种实现方式: 使用IntersectionObserver API Intersection…