当前位置:首页 > 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>

自定义参数调整

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

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

性能优化方案

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

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

实现字符颜色变化:

代码雨实现vue

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

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

相关文章

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…