Визуализация черной дыры на JavaScript
Вот пример визуализации черной дыры с использованием HTML5 Canvas и JavaScript. Этот код создает эффект гравитационного линзирования (искажения света вокруг черной дыры).
<!DOCTYPE html>
<html>
<head>
<title>Черная дыра - визуализация</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="blackHoleCanvas"></canvas>
<script>
const canvas = document.getElementById('blackHoleCanvas');
const ctx = canvas.getContext('2d');
// Установка размеров canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Параметры черной дыры
const blackHole = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 50,
schwarzschildRadius: 30, // Радиус Шварцшильда (горизонт событий)
accretionDisk: {
innerRadius: 35,
outerRadius: 200,
angle: 0
}
};
// Создаем текстуру для аккреционного диска
function createAccretionDiskTexture(width, height) {
const textureCanvas = document.createElement('canvas');
textureCanvas.width = width;
textureCanvas.height = height;
const textureCtx = textureCanvas.getContext('2d');
// Градиент для диска
const gradient = textureCtx.createLinearGradient(0, 0, width, 0);
gradient.addColorStop(0, 'rgba(255, 100, 0, 0.8)');
gradient.addColorStop(0.3, 'rgba(255, 255, 0, 0.9)');
gradient.addColorStop(0.6, 'rgba(0, 200, 255, 0.7)');
gradient.addColorStop(1, 'rgba(100, 0, 255, 0.6)');
textureCtx.fillStyle = gradient;
textureCtx.fillRect(0, 0, width, height);
// Добавляем полосы для эффекта турбулентности
textureCtx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
textureCtx.lineWidth = 2;
for (let i = 0; i < 20; i++) {
const y = Math.random() * height;
textureCtx.beginPath();
textureCtx.moveTo(0, y);
textureCtx.bezierCurveTo(
width * 0.3, y + Math.random() * 20 - 10,
width * 0.7, y + Math.random() * 20 - 10,
width, y
);
textureCtx.stroke();
}
return textureCanvas;
}
const diskTexture = createAccretionDiskTexture(1024, 100);
// Фон - звезды
const stars = [];
for (let i = 0; i < 500; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * 2,
brightness: Math.random()
});
}
// Функция для расчета гравитационного линзирования
function calculateLensing(x, y, blackHole) {
const dx = x - blackHole.x;
const dy = y - blackHole.y;
const distanceSq = dx * dx + dy * dy;
const distance = Math.sqrt(distanceSq);
if (distance < blackHole.schwarzschildRadius) {
return null; // За горизонтом событий - ничего не видно
}
// Угол отклонения света (упрощенная модель)
const deflection = blackHole.schwarzschildRadius / distance;
const angle = Math.atan2(dy, dx);
// Новые координаты после линзирования
const newDistance = distance + deflection * 100;
const newX = blackHole.x + Math.cos(angle) * newDistance;
const newY = blackHole.y + Math.sin(angle) * newDistance;
return {
x: newX,
y: newY,
brightness: 1 - deflection * 0.8
};
}
// Анимация
function animate() {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Рисуем звезды с эффектом линзирования
for (const star of stars) {
const lensed = calculateLensing(star.x, star.y, blackHole);
if (lensed) {
ctx.globalAlpha = lensed.brightness * star.brightness;
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(lensed.x, lensed.y, star.size, 0, Math.PI * 2);
ctx.fill();
}
}
ctx.globalAlpha = 1;
// Рисуем аккреционный диск
ctx.save();
ctx.translate(blackHole.x, blackHole.y);
ctx.rotate(blackHole.accretionDisk.angle);
// Внешняя часть диска
const gradient = ctx.createRadialGradient(
0, 0, blackHole.accretionDisk.innerRadius,
0, 0, blackHole.accretionDisk.outerRadius
);
gradient.addColorStop(0, 'rgba(255, 100, 0, 0)');
gradient.addColorStop(0.3, 'rgba(255, 200, 0, 0.7)');
gradient.addColorStop(0.6, 'rgba(100, 200, 255, 0.5)');
gradient.addColorStop(1, 'rgba(50, 50, 200, 0)');
ctx.beginPath();
ctx.arc(0, 0, blackHole.accretionDisk.outerRadius, 0, Math.PI * 2);
ctx.fillStyle = gradient;
ctx.fill();
// Текстурированная внутренняя часть диска
ctx.rotate(-blackHole.accretionDisk.angle * 2);
ctx.drawImage(
diskTexture,
-blackHole.accretionDisk.outerRadius,
-blackHole.accretionDisk.outerRadius / 4,
blackHole.accretionDisk.outerRadius * 2,
blackHole.accretionDisk.outerRadius / 2
);
// Тень черной дыры (горизонт событий)
ctx.beginPath();
ctx.arc(0, 0, blackHole.schwarzschildRadius, 0, Math.PI * 2);
ctx.fillStyle = 'black';
ctx.fill();
// Эффект фотонного кольца
for (let i = 0; i < 3; i++) {
const ringRadius = blackHole.schwarzschildRadius * (1.5 + i * 0.5);
ctx.beginPath();
ctx.arc(0, 0, ringRadius, 0, Math.PI * 2);
ctx.strokeStyle = `rgba(255, 255, 255, ${0.3 - i * 0.1})`;
ctx.lineWidth = 2;
ctx.stroke();
}
ctx.restore();
// Обновляем угол вращения диска
blackHole.accretionDisk.angle += 0.005;
requestAnimationFrame(animate);
}
animate();
// Обработка изменения размера окна
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
blackHole.x = canvas.width / 2;
blackHole.y = canvas.height / 2;
});
</script>
</body>
</html>
Как это работает:
-
Гравитационное линзирование: Свет от звезд искажается при прохождении рядом с черной дырой, создавая характерные дуги и кольца.
-
Аккреционный диск: Газ и пыль, вращающиеся вокруг черной дыры, нагреваются и светятся. Визуализируется с помощью текстуры и градиентов.
-
Фотонное кольцо: Свет, который обогнул черную дыру один или несколько раз, образует характерные кольца.
-
**Тень черной дыры