当前位置:首页 > 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 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依…

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <…

vue实现录播播放

vue实现录播播放

Vue 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…