当前位置:首页 > JavaScript

js实现雨滴

2026-04-06 03:43:25JavaScript

实现雨滴效果的JavaScript方法

使用HTML5 Canvas和JavaScript可以轻松创建雨滴效果。以下是一个简单的实现方法:

const canvas = document.getElementById('rainCanvas');
const ctx = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const rainDrops = [];
const rainCount = 100;

class RainDrop {
  constructor() {
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * -canvas.height;
    this.speed = 2 + Math.random() * 5;
    this.length = 10 + Math.random() * 20;
    this.opacity = Math.random() * 0.6;
  }

  update() {
    this.y += this.speed;
    if (this.y > canvas.height) {
      this.y = Math.random() * -100;
      this.x = Math.random() * canvas.width;
    }
  }

  draw() {
    ctx.beginPath();
    ctx.moveTo(this.x, this.y);
    ctx.lineTo(this.x, this.y + this.length);
    ctx.strokeStyle = `rgba(174, 194, 224, ${this.opacity})`;
    ctx.lineWidth = 1;
    ctx.stroke();
  }
}

for (let i = 0; i < rainCount; i++) {
  rainDrops.push(new RainDrop());
}

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  rainDrops.forEach(drop => {
    drop.update();
    drop.draw();
  });

  requestAnimationFrame(animate);
}

animate();

window.addEventListener('resize', () => {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
});

HTML结构

需要在HTML中添加canvas元素:

<!DOCTYPE html>
<html>
<head>
  <title>雨滴效果</title>
  <style>
    body { margin: 0; overflow: hidden; background: #1a1a2e; }
    canvas { display: block; }
  </style>
</head>
<body>
  <canvas id="rainCanvas"></canvas>
  <script src="raindrops.js"></script>
</body>
</html>

效果增强选项

可以通过修改参数来增强雨滴效果:

js实现雨滴

增加雨滴数量:

const rainCount = 200; // 改为更大的数字

添加重力效果:

js实现雨滴

this.speed += 0.05; // 在update方法中添加

创建溅射效果:

if (this.y > canvas.height - 10) {
  this.speed = 0;
  this.length -= 0.2;
  if (this.length < 0) {
    this.y = Math.random() * -100;
    this.x = Math.random() * canvas.width;
    this.speed = 2 + Math.random() * 5;
    this.length = 10 + Math.random() * 20;
  }
}

性能优化

对于大量雨滴,可以使用对象池技术:

// 对象池实现
const rainPool = [];
const POOL_SIZE = 300;

function createRainDrop() {
  if (rainPool.length < POOL_SIZE) {
    return new RainDrop();
  }
  const drop = rainPool.shift();
  drop.x = Math.random() * canvas.width;
  drop.y = Math.random() * -100;
  drop.speed = 2 + Math.random() * 5;
  drop.length = 10 + Math.random() * 20;
  drop.opacity = Math.random() * 0.6;
  return drop;
}

function recycleDrop(drop) {
  rainPool.push(drop);
}

标签: 雨滴js
分享给朋友:

相关文章

js实现计算器

js实现计算器

实现基础计算器功能 使用JavaScript创建一个基础计算器需要处理数字输入、运算符逻辑和结果显示。以下是一个简单的实现示例: let currentInput = '0'; let previo…

js轮播图实现原理

js轮播图实现原理

轮播图的基本原理 轮播图通过动态切换显示的内容(图片、文本等)实现视觉上的滑动效果。核心原理是利用CSS和JavaScript控制元素的显示与隐藏,或通过变换位置实现滑动动画。 实现方法一:…

js实现vr

js实现vr

使用WebXR API实现VR体验 WebXR是浏览器中实现VR和AR体验的标准API,它取代了早期的WebVR。现代浏览器如Chrome、Edge和Firefox已支持WebXR。 // 初始化W…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js实现代码雨

js实现代码雨

实现代码雨效果 使用HTML5 Canvas和JavaScript可以轻松实现经典的代码雨效果。以下是完整的实现代码和说明: HTML结构 <!DOCTYPE html> <…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…