当前位置:首页 > JavaScript

js实现黑洞

2026-02-01 19:02:34JavaScript

实现黑洞效果的 JavaScript 方法

使用 Canvas 绘制动态黑洞效果,结合引力模拟和粒子系统。

js实现黑洞

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

const particles = [];
const blackHole = {
  x: canvas.width / 2,
  y: canvas.height / 2,
  mass: 10000
};

class Particle {
  constructor() {
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * canvas.height;
    this.size = Math.random() * 3 + 1;
    this.speedX = Math.random() * 2 - 1;
    this.speedY = Math.random() * 2 - 1;
  }

  update() {
    const dx = blackHole.x - this.x;
    const dy = blackHole.y - this.y;
    const distance = Math.sqrt(dx * dx + dy * dy);
    const force = blackHole.mass / (distance * distance);

    this.speedX += dx / distance * force;
    this.speedY += dy / distance * force;

    this.x += this.speedX;
    this.y += this.speedY;
  }

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

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

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

  particles.forEach(particle => {
    particle.update();
    particle.draw();
  });

  // 绘制黑洞
  ctx.fillStyle = 'black';
  ctx.beginPath();
  ctx.arc(blackHole.x, blackHole.y, 30, 0, Math.PI * 2);
  ctx.fill();

  // 绘制吸积盘
  const gradient = ctx.createRadialGradient(
    blackHole.x, blackHole.y, 30,
    blackHole.x, blackHole.y, 100
  );
  gradient.addColorStop(0, 'rgba(255, 255, 255, 0)');
  gradient.addColorStop(0.5, 'rgba(200, 100, 255, 0.5)');
  gradient.addColorStop(1, 'rgba(100, 0, 200, 0.2)');

  ctx.fillStyle = gradient;
  ctx.beginPath();
  ctx.arc(blackHole.x, blackHole.y, 100, 0, Math.PI * 2);
  ctx.fill();

  requestAnimationFrame(animate);
}

init();
animate();

HTML 基础结构

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript 黑洞模拟</title>
  <style>
    body { margin: 0; overflow: hidden; background: #000; }
    canvas { display: block; }
  </style>
</head>
<body>
  <canvas id="blackHoleCanvas"></canvas>
  <script src="blackhole.js"></script>
</body>
</html>

效果增强技巧

增加引力透镜效果模拟光线弯曲:

js实现黑洞

function drawGravitationalLensing() {
  const lensingRadius = 150;
  const steps = 36;

  for (let i = 0; i < steps; i++) {
    const angle = (i / steps) * Math.PI * 2;
    const x = blackHole.x + Math.cos(angle) * lensingRadius;
    const y = blackHole.y + Math.sin(angle) * lensingRadius;

    const gradient = ctx.createRadialGradient(
      x, y, 0,
      x, y, 20
    );
    gradient.addColorStop(0, 'rgba(255, 255, 255, 0.8)');
    gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');

    ctx.fillStyle = gradient;
    ctx.beginPath();
    ctx.arc(x, y, 20, 0, Math.PI * 2);
    ctx.fill();
  }
}

性能优化建议

使用对象池管理粒子对象避免频繁创建销毁:

const particlePool = [];
const activeParticles = [];

function getParticle() {
  if (particlePool.length > 0) {
    return particlePool.pop();
  }
  return new Particle();
}

function recycleParticle(particle) {
  particlePool.push(particle);
}

交互功能实现

添加鼠标交互控制黑洞位置:

canvas.addEventListener('mousemove', (e) => {
  blackHole.x = e.clientX;
  blackHole.y = e.clientY;
});

标签: 黑洞js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document.…

js实现分页

js实现分页

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

js实现轮播图

js实现轮播图

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

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callbac…