当前位置:首页 > JavaScript

js实现海浪

2026-03-02 04:12:09JavaScript

使用 Canvas 绘制海浪效果

通过 HTML5 的 Canvas API 可以实现动态的海浪效果。以下是一个简单的实现方法:

const canvas = document.getElementById('waveCanvas');
const ctx = canvas.getContext('2d');
let time = 0;

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

  ctx.beginPath();
  for (let x = 0; x < canvas.width; x++) {
    const y = Math.sin(x * 0.02 + time) * 20 + canvas.height / 2;
    if (x === 0) {
      ctx.moveTo(x, y);
    } else {
      ctx.lineTo(x, y);
    }
  }

  ctx.strokeStyle = '#1a73e8';
  ctx.lineWidth = 2;
  ctx.stroke();

  time += 0.1;
  requestAnimationFrame(drawWave);
}

drawWave();

添加渐变和填充效果

为了使海浪看起来更真实,可以添加渐变填充:

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

  ctx.beginPath();
  for (let x = 0; x < canvas.width; x++) {
    const y = Math.sin(x * 0.02 + time) * 20 + canvas.height / 2;
    if (x === 0) {
      ctx.moveTo(x, y);
    } else {
      ctx.lineTo(x, y);
    }
  }

  ctx.lineTo(canvas.width, canvas.height);
  ctx.lineTo(0, canvas.height);
  ctx.closePath();

  const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
  gradient.addColorStop(0, 'rgba(26, 115, 232, 0.7)');
  gradient.addColorStop(1, 'rgba(26, 115, 232, 0.2)');

  ctx.fillStyle = gradient;
  ctx.fill();

  time += 0.1;
  requestAnimationFrame(drawFilledWave);
}

实现多层波浪效果

创建多个波浪层可以增加视觉效果:

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

  // 第一层波浪
  ctx.beginPath();
  for (let x = 0; x < canvas.width; x++) {
    const y = Math.sin(x * 0.02 + time) * 15 + canvas.height / 2;
    if (x === 0) {
      ctx.moveTo(x, y);
    } else {
      ctx.lineTo(x, y);
    }
  }

  ctx.strokeStyle = 'rgba(26, 115, 232, 0.8)';
  ctx.lineWidth = 2;
  ctx.stroke();

  // 第二层波浪
  ctx.beginPath();
  for (let x = 0; x < canvas.width; x++) {
    const y = Math.sin(x * 0.03 + time * 1.2) * 10 + canvas.height / 2 + 20;
    if (x === 0) {
      ctx.moveTo(x, y);
    } else {
      ctx.lineTo(x, y);
    }
  }

  ctx.strokeStyle = 'rgba(66, 133, 244, 0.6)';
  ctx.lineWidth = 1.5;
  ctx.stroke();

  time += 0.1;
  requestAnimationFrame(drawMultiLayerWave);
}

使用贝塞尔曲线创建更自然的波浪

贝塞尔曲线可以产生更平滑的波浪运动:

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

  const waveHeight = 30;
  const wavelength = 100;
  const segments = Math.ceil(canvas.width / wavelength);

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

  for (let i = 0; i < segments; i++) {
    const x = i * wavelength;
    const cp1x = x + wavelength * 0.25;
    const cp1y = canvas.height / 2 + Math.sin(time + i) * waveHeight;
    const cp2x = x + wavelength * 0.75;
    const cp2y = canvas.height / 2 + Math.sin(time + i + 0.5) * waveHeight;
    const endX = x + wavelength;
    const endY = canvas.height / 2;

    ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, endX, endY);
  }

  ctx.strokeStyle = '#4285f4';
  ctx.lineWidth = 2;
  ctx.stroke();

  time += 0.05;
  requestAnimationFrame(drawBezierWave);
}

响应式调整

确保波浪效果适应不同屏幕尺寸:

js实现海浪

function resizeCanvas() {
  canvas.width = window.innerWidth;
  canvas.height = 200;
}

window.addEventListener('resize', resizeCanvas);
resizeCanvas();

这些方法提供了从基础到进阶的海浪效果实现,可以根据需要调整参数如波浪高度、速度、颜色等来创建不同的视觉效果。

标签: 海浪js
分享给朋友:

相关文章

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js实现搜索

js实现搜索

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

js 实现页面刷新

js 实现页面刷新

使用 location.reload() location.reload() 是原生 JavaScript 提供的页面刷新方法。调用该方法会重新加载当前页面,相当于点击浏览器的刷新按钮。 // 基本…

js实现左右滑动

js实现左右滑动

实现左右滑动的 JavaScript 方法 监听触摸事件 通过 touchstart、touchmove 和 touchend 事件来检测用户的手势操作。记录触摸的起始位置和移动距离,判断滑动方向。…