Sure! Here's a simple solar system simulation in a single HTML file using HTML, CSS, and JavaScript (with Canvas). It simulates the Sun and a few planets orbiting around it.
You can copy and paste the following into a .html file and open it in your browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Solar System Simulation</title>
<style>
body {
margin: 0;
background-color: black;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="solarSystem"></canvas>
<script>
const canvas = document.getElementById('solarSystem');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Sun and Planet data
const sun = {
radius: 30,
color: 'yellow'
};
const planets = [
{ radius: 5, distance: 60, speed: 0.02, angle: 0, color: 'gray' }, // Mercury
{ radius: 7, distance: 100, speed: 0.015, angle: 0, color: 'orange' }, // Venus
{ radius: 8, distance: 140, speed: 0.01, angle: 0, color: 'blue' }, // Earth
{ radius: 6, distance: 180, speed: 0.008, angle: 0, color: 'red' }, // Mars
{ radius: 10, distance: 240, speed: 0.004, angle: 0, color: 'orange' } // Jupiter
];
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Sun
ctx.beginPath();
ctx.arc(centerX, centerY, sun.radius, 0, Math.PI * 2);
ctx.fillStyle = sun.color;
ctx.fill();
// Draw planets
planets.forEach(planet => {
planet.angle += planet.speed;
const x = centerX + Math.cos(planet.angle) * planet.distance;
const y = centerY + Math.sin(planet.angle) * planet.distance;
// Draw orbit
ctx.beginPath();
ctx.arc(centerX, centerY, planet.distance, 0, Math.PI * 2);
ctx.strokeStyle = 'rgba(255,255,255,0.1)';
ctx.stroke();
// Draw planet
ctx.beginPath();
ctx.arc(x, y, planet.radius, 0, Math.PI * 2);
ctx.fillStyle = planet.color;
ctx.fill();
});
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>
💫 Features:
- Draws Sun at the center.
- Simulates 5 planets orbiting it.
- Responsive to full screen.
- Smooth animation using
requestAnimationFrame.
🔧 To try it out:
- Open any text editor.
- Paste the code in a new file.
- Save it as
solar-system.html.
- Open it in your browser.
Would you like this to include moons or be more realistic (e.g., scale, rotation, labels, interactivity)?