当前位置:首页 > JavaScript

js实现漩涡

2026-02-01 21:22:31JavaScript

js实现漩涡

实现漩涡效果的方法

使用JavaScript和Canvas可以轻松实现漩涡效果。以下是两种常见的实现方式:

使用粒子系统创建漩涡

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

const particles = [];
const particleCount = 500;

class Particle {
  constructor() {
    this.x = canvas.width / 2;
    this.y = canvas.height / 2;
    this.size = Math.random() * 5 + 1;
    this.speedX = Math.random() * 3 - 1.5;
    this.speedY = Math.random() * 3 - 1.5;
    this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
  }

  update() {
    const dx = this.x - canvas.width / 2;
    const dy = this.y - canvas.height / 2;
    const distance = Math.sqrt(dx * dx + dy * dy);

    const forceDirectionX = dx / distance;
    const forceDirectionY = dy / distance;
    const force = (canvas.width / 2 - distance) / canvas.width * 2;

    this.speedX += forceDirectionX * force;
    this.speedY += forceDirectionY * force;

    this.x += this.speedX;
    this.y += this.speedY;
    this.size -= 0.1;
  }

  draw() {
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fill();
  }
}

function init() {
  for (let i = 0; i < particleCount; i++) {
    particles.push(new Particle());
  }
}

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

  for (let i = 0; i < particles.length; i++) {
    particles[i].update();
    particles[i].draw();

    if (particles[i].size <= 0.3) {
      particles.splice(i, 1);
      particles.push(new Particle());
    }
  }

  requestAnimationFrame(animate);
}

init();
animate();

使用数学公式绘制漩涡

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

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

  const centerX = canvas.width / 2;
  const centerY = canvas.height / 2;
  const maxRadius = Math.min(canvas.width, canvas.height) * 0.4;

  for (let angle = 0; angle < Math.PI * 16; angle += 0.01) {
    const radius = angle * 10;
    if (radius > maxRadius) break;

    const x = centerX + Math.cos(angle) * radius;
    const y = centerY + Math.sin(angle) * radius;

    const hue = (angle * 180 / Math.PI) % 360;
    ctx.fillStyle = `hsl(${hue}, 100%, 50%)`;
    ctx.fillRect(x, y, 2, 2);
  }

  requestAnimationFrame(drawSwirl);
}

drawSwirl();

优化漩涡效果

  1. 添加交互性可以让漩涡对鼠标移动做出反应
  2. 调整颜色渐变可以创建更生动的视觉效果
  3. 控制粒子生命周期可以影响漩涡的密度和流动感
  4. 添加模糊效果可以增强漩涡的柔和度

性能考虑

  1. 对于大量粒子,考虑使用WebGL代替Canvas 2D
  2. 限制动画帧率可以降低CPU/GPU使用率
  3. 使用requestAnimationFrame确保平滑动画
  4. 避免在动画循环中创建新对象,尽量复用现有对象

js实现漩涡

标签: 漩涡js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterva…

js实现轮播图

js实现轮播图

基础轮播图实现 使用HTML、CSS和JavaScript实现一个简单的自动轮播图。HTML结构包含一个容器和多个图片项。 <div class="slider"> <div…

js实现图片轮播

js实现图片轮播

实现基础图片轮播 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS设置样式和动画效果,JavaScript处理轮播逻辑。 <div cl…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js轮播图实现原理

js轮播图实现原理

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