Task:
Create a self-contained web application that runs in a browser. The application should display English characters falling from the sky under the influence of gravity. When these characters hit the bottom of the viewport, they should bounce with a damping effect to simulate energy loss. Additionally, ensure that the characters do not overlap each other as they fall and bounce.
Requirements:
-
Canvas & Rendering:
- Use an HTML
<canvas>element that covers the full viewport. - Render falling characters on the canvas.
- Ensure that the characters are positioned such that they do not overlap each other. This may require checking the positions of existing characters before placing a new one, or dynamically adjusting their positions during the animation.
- Use an HTML
-
Physics Simulation:
- Implement gravity that accelerates the letters downward.
- When a letter reaches the bottom, reverse its vertical velocity using a friction factor to simulate bounce damping.
- Optionally, add a small random horizontal velocity for variation.
-
Letter Representation:
- Each falling character should be represented as an object or class (e.g.,
FallingLetter) encapsulating its position, velocity, and rendering logic. - The character should be randomly chosen from the English alphabet (both uppercase and lowercase).
- Each falling character should be represented as an object or class (e.g.,
-
Animation Loop:
- Use
requestAnimationFrameto create a smooth animation loop. - Update the positions of all letters and render them on every frame.
- Clear the canvas before each frame to prevent ghosting.
- Use
-
Letter Generation:
- New letters should be created at regular intervals at random horizontal positions near the top of the canvas (starting off-screen).
- Before adding a new letter, check that its intended initial position does not cause it to overlap with any currently active letter.
- Alternatively, if an overlap is detected, adjust the position or delay the addition of the new letter.
-
Responsive Design:
- Ensure the canvas resizes when the browser window size changes.
-
Modern JavaScript:
- Utilize ES6+ features (classes, arrow functions, template literals, etc.).
- Ensure the code is clean, modular, and self-documenting.
-
Documentation & Readability:
- Include comments that explain why certain decisions are made (especially around physics calculations and collision avoidance).
- Optionally include a simple diagram (using Mermaid syntax) to visually describe the flow of the application.
Additional Notes:
- The solution should be completely self-contained within one HTML file.
- You may import any well-known libraries if necessary, but the core functionality (physics simulation, collision avoidance, and rendering) should be implemented directly.
- Prioritize code readability and maintainability.
Output: Provide the complete HTML and JavaScript code that implements the above functionality. The application should work when saved as an HTML file and opened in a modern browser.