当前位置:首页 > JavaScript

js实现水柱

2026-01-31 17:23:28JavaScript

实现水柱效果的JavaScript方法

使用Canvas绘制动态水柱效果是一种常见的实现方式。以下是基于Canvas和JavaScript的详细实现步骤:

创建Canvas基础结构

<canvas id="waterCanvas" width="400" height="200"></canvas>
const canvas = document.getElementById('waterCanvas');
const ctx = canvas.getContext('2d');

定义水柱参数

const wave = {
  y: canvas.height / 2,
  length: 0.01,
  amplitude: 20,
  frequency: 0.01
};

let time = 0;

绘制水柱动画

function animate() {
  ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  ctx.beginPath();
  ctx.moveTo(0, canvas.height);

  for (let i = 0; i < canvas.width; i++) {
    const y = wave.y + 
              Math.sin(i * wave.length + time) * wave.amplitude * 
              Math.sin(time * wave.frequency);
    ctx.lineTo(i, y);
  }

  ctx.lineTo(canvas.width, canvas.height);
  ctx.fillStyle = '#00a1ff';
  ctx.fill();

  time += 0.05;
  requestAnimationFrame(animate);
}

animate();

增强水柱效果

添加水花飞溅效果可提升真实感:

js实现水柱

const drops = [];

function createDrop(x, y) {
  drops.push({
    x,
    y,
    size: Math.random() * 3 + 1,
    speedY: Math.random() * -3 - 2,
    speedX: Math.random() * 2 - 1,
    opacity: Math.random() * 0.5 + 0.5
  });
}

function updateDrops() {
  for (let i = 0; i < drops.length; i++) {
    const drop = drops[i];
    drop.y += drop.speedY;
    drop.x += drop.speedX;
    drop.opacity -= 0.01;

    if (drop.opacity <= 0) {
      drops.splice(i, 1);
      i--;
    }
  }

  if (Math.random() < 0.1) {
    createDrop(
      Math.random() * canvas.width,
      wave.y + wave.amplitude
    );
  }
}

function drawDrops() {
  drops.forEach(drop => {
    ctx.beginPath();
    ctx.arc(drop.x, drop.y, drop.size, 0, Math.PI * 2);
    ctx.fillStyle = `rgba(100, 200, 255, ${drop.opacity})`;
    ctx.fill();
  });
}

整合动画循环

修改后的动画函数:

js实现水柱

function animate() {
  ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  // 绘制水柱
  ctx.beginPath();
  ctx.moveTo(0, canvas.height);
  for (let i = 0; i < canvas.width; i++) {
    const y = wave.y + 
              Math.sin(i * wave.length + time) * wave.amplitude * 
              Math.sin(time * wave.frequency);
    ctx.lineTo(i, y);
  }
  ctx.lineTo(canvas.width, canvas.height);
  ctx.fillStyle = '#00a1ff';
  ctx.fill();

  // 处理水滴
  updateDrops();
  drawDrops();

  time += 0.05;
  requestAnimationFrame(animate);
}

交互式控制

添加鼠标交互控制水柱高度:

canvas.addEventListener('mousemove', (e) => {
  wave.y = e.clientY - canvas.offsetTop;
});

性能优化建议

对于复杂场景,可采用以下优化措施:

  • 使用离屏Canvas进行预渲染
  • 限制重绘区域
  • 减少不必要的图形操作
  • 适当降低帧率

这种实现方式可以创建流畅的水柱动画效果,通过调整参数可获得不同风格的水柱表现。

标签: 水柱js
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,用于简化 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的设计宗旨是“Write Less, Do Mor…

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

使用js实现

使用js实现

使用JavaScript实现 JavaScript可以通过Math.random()函数生成随机数,结合其他方法可以实现多种随机数生成需求。 // 生成0到1之间的随机小数 const random…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…