In this experiment, we'll build an interactive black hole animation with JavaScript and HTML5 Canvas, including an accretion disk, spiraling particles, adjustable gravity, and a visual approximation of gravitational lensing. You can run the demo directly in your browser and change gravity, mass, and density in real time.
Interactive Black Hole Demo
We are not solving general relativity in the browser. We combine a simple inverse-distance force with real visual cues — shadow, accretion disk, gravitational lensing and asymmetric brightness — so the result looks and feels much more convincing.
How the JavaScript Gravity Effect Works
const dx = blackHole.x - particle.x;
const dy = blackHole.y - particle.y;
const distanceSq = dx * dx + dy * dy;
const force = (gravity * mass) / distanceSq;
const distance = Math.sqrt(distanceSq);
const nx = dx / distance;
const ny = dy / distance;
// Pull inward + add a tangential component so matter spirals.
particle.vx += nx * force - ny * orbit;
particle.vy += ny * force + nx * orbit;
The force stays intentionally simple; most of the visual realism comes from how the disk is projected, lit and bent around the shadow.
How the Black Hole Effect Is Built
Try maximum mass with low gravity. Then reverse the radial force: the disk and lensing remain, but the outer particles begin to escape.
No hay comentarios:
Publicar un comentario