当前位置:首页 > JavaScript

js实现大海

2026-02-01 18:29:08JavaScript

实现海洋动画效果

使用Canvas绘制动态波浪效果是模拟大海的常见方法。以下代码创建了一个带有波浪动画的海洋场景:

const canvas = document.getElementById('oceanCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = 200;

let time = 0;
function animateWave() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = '#1a73e8';
    ctx.beginPath();

    const waveHeight = 20;
    const waveLength = 0.01;
    for(let x = 0; x < canvas.width; x++) {
        const y = canvas.height/2 + Math.sin(x * waveLength + time) * waveHeight;
        ctx.lineTo(x, y);
    }

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

    time += 0.05;
    requestAnimationFrame(animateWave);
}
animateWave();

添加3D海洋效果

使用Three.js可以创建更逼真的3D海洋场景。需要先引入Three.js库:

import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const waterGeometry = new THREE.PlaneGeometry(100, 100, 128, 128);
const water = new THREE.Water(waterGeometry, {
    textureWidth: 512,
    textureHeight: 512,
    waterNormals: new THREE.TextureLoader().load('waternormals.jpg'),
    sunDirection: new THREE.Vector3(),
    sunColor: 0xffffff,
    waterColor: 0x0077be,
    distortionScale: 3.7
});
scene.add(water);

function animate() {
    requestAnimationFrame(animate);
    water.material.uniforms.time.value += 0.01;
    renderer.render(scene, camera);
}
animate();

使用CSS实现简单海浪

纯CSS也可以创建基础的波浪效果,适合性能要求不高的场景:

.ocean {
    position: relative;
    height: 100px;
    background: linear-gradient(to bottom, #0077be, #00a1ff);
    overflow: hidden;
}

.wave {
    position: absolute;
    bottom: 0;
    width: 200%;
    height: 100px;
    background-repeat: repeat no-repeat;
    background-position: 0 bottom;
    background-size: 50% 100px;
    animation: wave 7s linear infinite;
}

@keyframes wave {
    0% { transform: translateX(0); }
    100% { transform: translateX(-50%); }
}

添加海洋生物元素

增强真实感可以添加游动的鱼类或其他海洋生物:

function createFish() {
    const fish = document.createElement('div');
    fish.className = 'fish';
    fish.style.left = `${Math.random() * 100}%`;
    fish.style.top = `${60 + Math.random() * 30}%`;
    fish.style.animationDuration = `${5 + Math.random() * 10}s`;
    document.querySelector('.ocean').appendChild(fish);
}

for(let i=0; i<10; i++) {
    createFish();
}

优化性能的技巧

大规模海洋动画可能消耗较多资源,可以采用以下优化方法:

  • 限制动画帧率到30fps
  • 使用离屏Canvas预渲染静态元素
  • 对3D场景使用适当的LOD(细节层次)控制
  • 在移动设备上降低波浪的顶点数量

这些方法可以根据具体需求组合使用,从简单CSS动画到复杂3D渲染都能实现不同细节程度的海洋效果。

js实现大海

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

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

js实现复制

js实现复制

使用document.execCommand方法 在较旧的浏览器中,可以使用document.execCommand('copy')实现复制功能。创建一个临时的textarea或input元素,将需要…

js实现图片上传

js实现图片上传

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

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('imag…

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…