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

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

自实现vue

自实现vue

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

vue 实现点击选中

vue 实现点击选中

实现点击选中效果 在Vue中实现点击选中效果可以通过多种方式完成,以下是几种常见的方法: 方法一:使用v-bind和v-on 通过绑定class和监听click事件来实现选中状态切换。 <…

vue 中实现拖拽

vue 中实现拖拽

Vue 中实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop…