How keshav-madhav/space-simulation-html-css-js works
Purpose and Entry Points
A browser-only 2D gravitational sandbox. It simulates celestial bodies, renders layered canvas visuals, and exposes live controls for spawning, camera movement, physics options, and display layers. The application starts from index.html, with orchestration in index.js; styling and UI layout are in styles.css.
Application Orchestration and Shared State
index.js wires DOM controls, keyboard/mouse interactions, spawn actions, reset behavior, camera options, and the frame loop. Shared simulation state, constants, feature flags, scaling values, and likely the central body collection are concentrated in variables/constAndVars.js. Cached UI/canvas references are provided by variables/domElements.js. This is a mutable-state architecture rather than a service/API-based application.
Celestial-Body Model
classes/CelestialBodyClass.js is the primary entity model: body position, velocity, radius, density, mass, color, and render/update-related properties. Bodies are created both from direct UI interactions and reusable scenario factories in functions/spawnTemplates.js. The body list is the main data boundary consumed by physics, camera tracking, trails, and rendering.
Physics and Collision Stage
classes/PhysicsSystem.js advances the simulation using pairwise Newtonian gravity and numerical integration, making the current core scaling behavior O(N²). Collision detection/merge and mass-transfer behavior is separated into functions/collisionAndMassTransfer.js; collision execution is controlled by UI/shared flags. Physics mutates body state in place and can remove or consolidate bodies, so downstream render, follow-target, and trail code must tolerate changing collection membership.
Frame Timing and Control Flow
The loop infrastructure is isolated in functions/createConstantFPSGameLoop.js and functions/deltaTime.js. Per frame, the expected control flow is: derive effective delta time/time scale, update camera targeting, step physics and collisions, update optional trail/star state, then draw visual layers and overlays. functions/fpsDisplay.js is an optional frame-time consumer. No server, persistence layer, worker runtime, or external API is present.
Rendering and Visual Layers
Rendering uses browser Canvas 2D with separable visual passes. classes/GravityFieldRenderer.js and classes/WebGLRenderer.js indicate experimental/specialized renderer boundaries, while core body drawing is coordinated from index.js. classes/BackgroundStarsClass.js produces the optional background-star layer; classes/TrailManagerClass.js retains and renders motion history separately from body redraw. Layer toggles are architecturally important because they gate expensive per-frame work.
Interaction and Camera Boundary
functions/cameraHelper.js owns world/screen transforms, panning, zoom, and follow behavior for an individual body or center of mass. functions/dragListeners.js translates pointer actions into pan/spawn interactions; it should operate through camera transforms rather than physics coordinates directly. Camera state is a presentation concern but depends on live body positions and must handle deleted/merged targets.
Runtime and Evolution Constraints
The runtime is a modern browser executing JavaScript and HTML canvas; it has no build system or backend dependency in the supplied tree. Current physics is object-oriented, mutable, and main-thread based. Planned scalability paths—Barnes–Hut, typed-array storage, workers, and GPU computation—would require replacing the direct object-list assumptions centered around classes/PhysicsSystem.js and classes/CelestialBodyClass.js, while preserving the UI/orchestration boundary in index.js.
Components 18
Browser Runtime
- HTML entry · Browser page entry point.
index.html - App orchestration · Wires controls, interactions, spawning, reset, and frame passes.
index.js - UI styles
styles.css
State & Model
- Shared state · Central constants, flags, scales, and body collection.
variables/constAndVars.js - DOM references
variables/domElements.js - Celestial body · Mutable position, velocity, mass, and visual properties.
classes/CelestialBodyClass.js - Scenario factories
functions/spawnTemplates.js
Simulation
- Fixed-FPS loop
functions/createConstantFPSGameLoop.js - Delta time
functions/deltaTime.js - Physics system · Mutates the live body collection using pairwise gravity.
classes/PhysicsSystem.js - Collision & mass transfer · Merges or removes bodies when enabled.
functions/collisionAndMassTransfer.js - FPS display
functions/fpsDisplay.js
Camera & Rendering
- Camera helper · Panning, zoom, and follow targets over mutable bodies.
functions/cameraHelper.js - Pointer interactions · Converts pointer actions to pan and spawn behavior.
functions/dragListeners.js - Trail manager · Retains and renders optional body trails.
classes/TrailManagerClass.js - Background stars · Optional animated star field.
classes/BackgroundStarsClass.js - Gravity field renderer · Experimental gravity-field visualization boundary.
classes/GravityFieldRenderer.js - WebGL renderer · Experimental rendering boundary.
classes/WebGLRenderer.js
Connections 25
- HTML entry → App orchestration: loads
- HTML entry → UI styles: applies
- App orchestration → DOM references: uses controls
- App orchestration → Shared state: reads and mutates
- App orchestration → Fixed-FPS loop: starts
- Fixed-FPS loop → Delta time: derives frame step
- Fixed-FPS loop → App orchestration: invokes frame
- App orchestration → Scenario factories: spawns scenarios
- Scenario factories → Celestial body: creates
- App orchestration → Celestial body: creates from UI
- Shared state → Celestial body: holds collection
- App orchestration → Camera helper: updates target
- Camera helper → Shared state: tracks bodies
- Pointer interactions → Camera helper: pans via transforms
- App orchestration → Pointer interactions: registers
- App orchestration → Physics system: steps each frame
- Physics system → Shared state: mutates bodies
- Physics system → Collision & mass transfer: runs when enabled
- Collision & mass transfer → Shared state: merges or removes
- App orchestration → Trail manager: updates and draws
- Trail manager → Shared state: reads body motion
- App orchestration → Background stars: draws when enabled
- App orchestration → Gravity field renderer: draws when enabled
- App orchestration → WebGL renderer: delegates experimental pass
- App orchestration → FPS display: updates optionally