HTML5 Canvas: Uses the <canvas> element for drawing the simulation. This is the standard way to do dynamic graphics in the browser.
CSS Styling: Basic CSS for a black background, white text, and centering the canvas. Includes overflow: hidden to prevent scrollbars. Added styling for the controls.
JavaScript Logic:
Particle Class: Defines a Particle class with properties for position (x, y), velocity (vx, vy), and size. Includes draw() and update() methods.
Gravity: Simulates gravity by adding a small amount to the vertical velocity (vy) of each particle in the update() method.
Collision Detection: Checks for collisions with the bottom and sides of the canvas and reverses the velocity to simulate bouncing. Uses collisionDamping to reduce energy with each bounce.
createParticles() Function: Creates an array of Particle objects with random positions and velocities. The number of particles is controlled by the input field.
animate() Function: The core animation loop. It clears the canvas, updates the position of each particle, and draws them. Uses requestAnimationFrame() for smooth animation.
startSimulation() Function: Creates the particles and starts the animation loop. Disables the start button to prevent multiple simulations running at once.
resetSimulation() Function: Stops the animation loop, clears the canvas, and resets the particles array. Enables the start button.
Event Listeners: Attaches event listeners to the "Start" and "Reset" buttons.
resizeCanvas() Function: Resizes the canvas to fit the window. This is important for responsive design.
Initial Setup: Calls resizeCanvas() to set the initial canvas size.
Controls: Added a div with buttons to start and reset the simulation, and an input field to control the number of particles.
Particle Count Control: The particleCountInput allows the user to specify the number of particles to create.
Transparency: The particles are drawn with a semi-transparent color () to create a more visually appealing effect.
How to run:
Save: Save the code as an HTML file (e.g., earth_formation.html).
Open in Browser: Open the HTML file in a web browser.
This simulation provides a basic visual representation of the early stages of Earth formation, with particles colliding and coalescing under the influence of gravity. You can adjust the gravity, collisionDamping, particleSize, and color variables to experiment with different effects. You can also increase the number of particles to create a more dense and complex simulation. Remember that this is a simplified model, and the actual formation of Earth was a much more complex process.