当前位置:首页 > VUE

代码雨实现vue

2026-01-18 00:35:53VUE

实现代码雨效果(Vue版)

使用Vue实现代码雨效果可以通过Canvas绘制结合动画帧实现。以下是完整实现方案:

核心代码结构

<template>
  <div class="code-rain-container">
    <canvas ref="canvas"></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chars: "01",
      fontSize: 16,
      columns: 0,
      drops: [],
      animationFrame: null
    }
  },
  mounted() {
    this.initCanvas()
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
    cancelAnimationFrame(this.animationFrame)
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')
      canvas.width = window.innerWidth
      canvas.height = window.innerHeight

      this.columns = Math.floor(canvas.width / this.fontSize)
      this.drops = Array(this.columns).fill(1)

      this.draw()
    },
    draw() {
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')

      ctx.fillStyle = 'rgba(0, 0, 0, 0.05)'
      ctx.fillRect(0, 0, canvas.width, canvas.height)

      ctx.fillStyle = '#0F0'
      ctx.font = `${this.fontSize}px monospace`

      for (let i = 0; i < this.drops.length; i++) {
        const text = this.chars[Math.floor(Math.random() * this.chars.length)]
        ctx.fillText(text, i * this.fontSize, this.drops[i] * this.fontSize)

        if (this.drops[i] * this.fontSize > canvas.height && Math.random() > 0.975) {
          this.drops[i] = 0
        }
        this.drops[i]++
      }

      this.animationFrame = requestAnimationFrame(this.draw)
    },
    handleResize() {
      cancelAnimationFrame(this.animationFrame)
      this.initCanvas()
    }
  }
}
</script>

<style>
.code-rain-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #000;
  z-index: -1;
}
canvas {
  display: block;
}
</style>

自定义参数调整

修改数据属性可调整效果:

代码雨实现vue

data() {
  return {
    chars: "01ABCDEFGHIJKLMNOPQRSTUVWXYZ", // 可显示字符
    fontSize: 18,                          // 字体大小
    speed: 1,                              // 下落速度系数
    opacity: 0.05                          // 背景透明度
  }
}

性能优化方案

对于大屏幕设备,可添加节流处理:

代码雨实现vue

handleResize: _.throttle(function() {
  cancelAnimationFrame(this.animationFrame)
  this.initCanvas()
}, 200)

组件化使用方式

  1. 创建CodeRain.vue文件并粘贴上述代码
  2. 在需要使用的组件中引入:
    import CodeRain from './components/CodeRain.vue'
    export default {
    components: { CodeRain }
    }
  3. 在模板中使用:
    <code-rain />

效果增强技巧

添加渐变颜色效果:

const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height)
gradient.addColorStop(0, '#0F0')
gradient.addColorStop(0.5, '#0C0')
gradient.addColorStop(1, '#090')
ctx.fillStyle = gradient

实现字符颜色变化:

ctx.fillStyle = `hsl(${Math.random() * 120 + 60}, 100%, 50%)`

标签: 代码vue
分享给朋友:

相关文章

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

vue实现 页面

vue实现 页面

Vue 实现页面的核心方法 创建 Vue 项目 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 是官方脚手架工具,适合传统项目;Vite 提供更快的现代构建体验。 npm cre…