当前位置:首页 > VUE

vue实现子弹循环

2026-01-15 07:52:20VUE

Vue实现子弹循环动画

在Vue中实现子弹循环动画可以通过CSS动画或JavaScript动画库实现。以下是几种常见方法:

方法1:使用CSS动画和Vue过渡

通过Vue的<transition><transition-group>组件结合CSS动画实现子弹移动效果:

<template>
  <div class="bullet-container">
    <div 
      v-for="(bullet, index) in bullets" 
      :key="index"
      class="bullet"
      :style="{ left: bullet.x + 'px', top: bullet.y + 'px' }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bullets: []
    }
  },
  mounted() {
    setInterval(() => {
      this.bullets.push({
        x: 0,
        y: Math.random() * 300
      })
    }, 500)

    setInterval(() => {
      this.bullets = this.bullets.map(bullet => ({
        ...bullet,
        x: bullet.x + 5
      })).filter(bullet => bullet.x < 800)
    }, 16)
  }
}
</script>

<style>
.bullet {
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
  transition: left 0.1s linear;
}
.bullet-container {
  position: relative;
  width: 800px;
  height: 400px;
  border: 1px solid #ccc;
}
</style>

方法2:使用GSAP动画库

GSAP提供更强大的动画控制能力:

<template>
  <div ref="container" class="bullet-container">
    <div 
      v-for="(bullet, index) in bullets" 
      :key="index"
      ref="bullets"
      class="bullet"
    ></div>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      bullets: []
    }
  },
  mounted() {
    setInterval(() => {
      this.bullets.push({ id: Date.now() })
      this.$nextTick(() => {
        const bullet = this.$refs.bullets[this.$refs.bullets.length - 1]
        const containerWidth = this.$refs.container.offsetWidth

        gsap.fromTo(bullet, 
          { x: 0, y: Math.random() * 300 },
          { 
            x: containerWidth,
            duration: 2,
            onComplete: () => {
              this.bullets.shift()
            }
          }
        )
      })
    }, 500)
  }
}
</script>

方法3:使用Canvas绘制

对于大量子弹效果,Canvas性能更好:

<template>
  <canvas ref="canvas" width="800" height="400"></canvas>
</template>

<script>
export default {
  data() {
    return {
      bullets: [],
      ctx: null
    }
  },
  mounted() {
    this.ctx = this.$refs.canvas.getContext('2d')

    setInterval(() => {
      this.bullets.push({
        x: 0,
        y: Math.random() * 400,
        speed: 2 + Math.random() * 3
      })
    }, 300)

    requestAnimationFrame(this.animate)
  },
  methods: {
    animate() {
      this.ctx.clearRect(0, 0, 800, 400)

      this.bullets.forEach((bullet, index) => {
        bullet.x += bullet.speed
        this.ctx.beginPath()
        this.ctx.arc(bullet.x, bullet.y, 5, 0, Math.PI * 2)
        this.ctx.fillStyle = 'red'
        this.ctx.fill()

        if(bullet.x > 800) {
          this.bullets.splice(index, 1)
        }
      })

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

性能优化建议

  • 对于少量子弹元素,使用CSS动画或GSAP更简单
  • 对于大量子弹(50+),考虑使用Canvas或WebGL方案
  • 使用对象池技术复用DOM元素或Canvas绘制对象
  • 适当使用requestAnimationFrame进行动画循环
  • 在组件销毁时清除定时器和动画帧请求

以上方法可根据实际需求选择,CSS方案适合简单场景,GSAP提供更多动画控制,Canvas则适合高性能需求。

vue实现子弹循环

标签: 子弹vue
分享给朋友:

相关文章

订单 vue实现

订单 vue实现

Vue 实现订单功能 数据模型设计 订单功能通常需要设计以下数据结构: // 订单数据结构 const order = { id: '', // 订单ID userId…

vue实现前端分离

vue实现前端分离

Vue 实现前后端分离 前后端分离是一种架构模式,前端负责页面渲染和交互逻辑,后端提供数据接口。Vue.js 作为前端框架,非常适合实现前后端分离的开发模式。 核心实现步骤 创建 Vue 项目 使…

vue实现文档导入

vue实现文档导入

Vue 实现文档导入的方法 在 Vue 中实现文档导入功能通常涉及文件上传、解析和处理。以下是几种常见的方法: 使用原生文件输入和 FileReader 通过 HTML 的原生 <input…

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键…