Below is a complete solar system simulation in a single HTML file. This simulation includes the Sun, planets, and basic orbital mechanics. The planets orbit the Sun with scaled distances and speeds for visualization purposes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Solar System Simulation</title>
<style>
body { margin: 0; overflow: hidden; background: black; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="solarSystemCanvas"></canvas>
<script>
const canvas = document.getElementById('solarSystemCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size to full window
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Solar system data
const sun = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 20,
color: 'yellow'
};
const planets = [
{ name: 'Mercury', radius: 5, distance: 50, speed: 0.08, color: '#888' },
{ name: 'Venus', radius: 8, distance: 80, speed: 0.06, color: '#E08E00' },
{ name: 'Earth', radius: 9, distance: 110, speed: 0.05, color: '#0080FF' },
{ name: 'Mars', radius: 7, distance: 140, speed: 0.04, color: '#FF5733' },
{ name: 'Jupiter', radius: 15, distance: 180, speed: 0.03, color: '#D4AF37' },
{ name: 'Saturn', radius: 14, distance: 220, speed: 0.025, color: '#FFD700' },
{ name: 'Uranus', radius: 11, distance: 260, speed: 0.02, color: '#87CEEB' },
{ name: 'Neptune', radius: 10, distance: 300, speed: 0.015, color: '#3366FF' }
];
let angle = 0;
const scale = 1; // Scaling factor for distances
function drawSun() {
ctx.beginPath();
ctx.arc(sun.x, sun.y, sun.radius, 0, Math.PI * 2);
ctx.fillStyle = sun.color;
ctx.fill();
}
function drawPlanets() {
planets.forEach((planet, index) => {
const x = sun.x + planet.distance * scale * Math.cos(angle + index);
const y = sun.y + planet.distance * scale * Math.sin(angle + index);
ctx.beginPath();
ctx.arc(x, y, planet.radius, 0, Math.PI * 2);
ctx.fillStyle = planet.color;
ctx.fill();
});
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSun();
drawPlanets();
angle += 0.01;
requestAnimationFrame(animate);
}
animate();
// Resize canvas when window is resized
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
sun.x = canvas.width / 2;
sun.y = canvas.height / 2;
});
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- A single
<canvas> element is used to draw the solar system.
- Basic styling ensures the canvas takes up the full window.
-
JavaScript:
- Sun: The Sun is positioned at the center of the canvas.
- Planets: Each planet has properties like
radius, distance, speed, and color.
- Animation Loop: The
animate() function clears the canvas, draws the Sun and planets, and updates their positions using trigonometric functions (Math.cos and Math.sin).
- Responsive Design: The canvas resizes when the window is resized, keeping the Sun centered.
-
Features:
- Planets orbit the Sun in circular paths.
- Distances and speeds are scaled for visualization.
- The simulation runs smoothly using
requestAnimationFrame.
To use this, simply copy the code into an .html file and open it in a browser. Enjoy your solar system simulation!