Certainly! Below is a complete, self-contained HTML file that simulates a simple solar system with the Sun and a few planets orbiting around it. The simulation uses the HTML5 <canvas> element and JavaScript for animation.
You can save this as solar_system.html and open it in any modern web browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Solar System Simulation</title>
<style>
body {
margin: 0;
background: radial-gradient(circle at center, #000010, #000000);
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: white;
font-family: Arial, sans-serif;
}
canvas {
background: transparent;
display: block;
}
#info {
position: absolute;
top: 10px;
left: 10px;
font-size: 14px;
opacity: 0.7;
}
</style>
</head>
<body>
<canvas id="solarSystem"></canvas>
<div id="info">Solar System Simulation - Sun & Planets</div>
<script>
(() => {
const canvas = document.getElementById('solarSystem');
const ctx = canvas.getContext('2d');
// Resize canvas to fill window
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
// Define the Sun and planets
const sun = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 30,
color: 'yellow'
};
// Planets data: name, color, radius, distance from sun, orbital period (seconds)
const planets = [
{ name: 'Mercury', color: '#a9a9a9', radius: 5, distance: 60, period: 10 },
{ name: 'Venus', color: '#f5deb3', radius: 8, distance: 90, period: 18 },
{ name: 'Earth', color: '#3b82f6', radius: 9, distance: 130, period: 30 },
{ name: 'Mars', color: '#b22222', radius: 7, distance: 170, period: 56 },
{ name: 'Jupiter', color: '#d2b48c', radius: 15, distance: 230, period: 120 },
{ name: 'Saturn', color: '#deb887', radius: 13, distance: 290, period: 180 },
{ name: 'Uranus', color: '#7fffd4', radius: 11, distance: 340, period: 240 },
{ name: 'Neptune', color: '#4169e1', radius: 11, distance: 390, period: 300 }
];
// Draw the sun
function drawSun() {
const gradient = ctx.createRadialGradient(sun.x, sun.y, sun.radius * 0.1, sun.x, sun.y, sun.radius);
gradient.addColorStop(0, '#fffacd');
gradient.addColorStop(1, 'yellow');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(sun.x, sun.y, sun.radius, 0, Math.PI * 2);
ctx.fill();
}
// Draw orbit paths
function drawOrbits() {
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 1;
planets.forEach(planet => {
ctx.beginPath();
ctx.arc(sun.x, sun.y, planet.distance, 0, Math.PI * 2);
ctx.stroke();
});
}
// Draw a planet at given position
function drawPlanet(x, y, radius, color) {
const gradient = ctx.createRadialGradient(x, y, radius * 0.3, x, y, radius);
gradient.addColorStop(0, 'white');
gradient.addColorStop(1, color);
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
}
// Animation loop
let startTime = null;
function animate(timestamp) {
if (!startTime) startTime = timestamp;
const elapsed = (timestamp - startTime) / 1000; // seconds
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawOrbits();
drawSun();
planets.forEach(planet => {
// Calculate angle based on orbital period
const angle = (elapsed / planet.period) * 2 * Math.PI;
const x = sun.x + planet.distance * Math.cos(angle);
const y = sun.y + planet.distance * Math.sin(angle);
drawPlanet(x, y, planet.radius, planet.color);
});
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
})();
</script>
</body>
</html>
How it works:
- The Sun is fixed at the center.
- Each planet orbits the Sun in a circular path.
- The orbital period controls the speed of each planet.
- The orbits are drawn as faint circles.
- Planets are drawn with a simple radial gradient for a bit of 3D effect.
- The canvas resizes with the window.
Open this file in a browser and watch the planets orbit the Sun smoothly! Let me know if you want moons, labels, or more features.