# keshav-madhav/space-simulation-html-css-js architecture

> Architecture diagram of the GitHub repository keshav-madhav/space-simulation-html-css-js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/keshav-madhav/space-simulation-html-css-js
- Repository: https://github.com/keshav-madhav/space-simulation-html-css-js

## Overview

#### 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`.

Main entry points: [`index.js`](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/index.js) (App orchestration), [`variables/constAndVars.js`](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/variables/constAndVars.js) (Shared state), [`functions/createConstantFPSGameLoop.js`](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/createConstantFPSGameLoop.js) (Fixed-FPS loop), [`classes/PhysicsSystem.js`](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/PhysicsSystem.js) (Physics system)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Browser Runtime"]
  node_html["HTML entry<br/>document<br/>[index.html]"]
  node_app["App orchestration<br/>browser controller<br/>[index.js]"]
  node_styles["UI styles<br/>stylesheet<br/>[styles.css]"]
end

subgraph group_state["State &amp; Model"]
  node_shared_state[("Shared state<br/>mutable state<br/>[constAndVars.js]")]
  node_dom["DOM references<br/>UI cache<br/>[domElements.js]"]
  node_body["Celestial body<br/>entity model"]
  node_spawn_templates["Scenario factories<br/>body factory<br/>[spawnTemplates.js]"]
end

subgraph group_simulation["Simulation"]
  node_loop["Fixed-FPS loop<br/>frame scheduler"]
  node_delta_time["Delta time<br/>timing utility<br/>[deltaTime.js]"]
  node_physics["Physics system<br/>N² gravity integrator<br/>[PhysicsSystem.js]"]
  node_collisions["Collision &amp; mass transfer<br/>collision stage"]
  node_fps["FPS display<br/>frame metrics<br/>[fpsDisplay.js]"]
end

subgraph group_presentation["Camera &amp; Rendering"]
  node_camera["Camera helper<br/>world/screen transform<br/>[cameraHelper.js]"]
  node_drag["Pointer interactions<br/>input adapter<br/>[dragListeners.js]"]
  node_trails["Trail manager<br/>motion-history layer"]
  node_stars["Background stars<br/>background layer"]
  node_gravity_renderer["Gravity field renderer<br/>specialized canvas layer"]
  node_webgl_renderer["WebGL renderer<br/>specialized renderer<br/>[WebGLRenderer.js]"]
end

node_html -->|"loads"| node_app
node_html -->|"applies"| node_styles
node_app -->|"uses controls"| node_dom
node_app -->|"reads and mutates"| node_shared_state
node_app -->|"starts"| node_loop
node_loop -->|"derives frame step"| node_delta_time
node_loop -->|"invokes frame"| node_app
node_app -->|"spawns scenarios"| node_spawn_templates
node_spawn_templates -->|"creates"| node_body
node_app -->|"creates from UI"| node_body
node_shared_state -->|"holds collection"| node_body
node_app -->|"updates target"| node_camera
node_camera -->|"tracks bodies"| node_shared_state
node_drag -->|"pans via transforms"| node_camera
node_app -->|"registers"| node_drag
node_app -->|"steps each frame"| node_physics
node_physics -->|"mutates bodies"| node_shared_state
node_physics -->|"runs when enabled"| node_collisions
node_collisions -->|"merges or removes"| node_shared_state
node_app -->|"updates and draws"| node_trails
node_trails -->|"reads body motion"| node_shared_state
node_app -->|"draws when enabled"| node_stars
node_app -->|"draws when enabled"| node_gravity_renderer
node_app -.->|"delegates experimental pass"| node_webgl_renderer
node_app -->|"updates optionally"| node_fps

click node_html "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/index.html"
click node_app "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/index.js"
click node_shared_state "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/variables/constAndVars.js"
click node_dom "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/variables/domElements.js"
click node_body "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/CelestialBodyClass.js"
click node_spawn_templates "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/spawnTemplates.js"
click node_loop "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/createConstantFPSGameLoop.js"
click node_delta_time "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/deltaTime.js"
click node_physics "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/PhysicsSystem.js"
click node_collisions "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/collisionAndMassTransfer.js"
click node_fps "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/fpsDisplay.js"
click node_camera "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/cameraHelper.js"
click node_drag "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/dragListeners.js"
click node_trails "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/TrailManagerClass.js"
click node_stars "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/BackgroundStarsClass.js"
click node_gravity_renderer "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/GravityFieldRenderer.js"
click node_webgl_renderer "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/WebGLRenderer.js"
click node_styles "https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/styles.css"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_html,node_app,node_styles toneBlue
class node_shared_state,node_dom,node_body,node_spawn_templates toneAmber
class node_loop,node_delta_time,node_physics,node_collisions,node_fps toneMint
class node_camera,node_drag,node_trails,node_stars,node_gravity_renderer,node_webgl_renderer toneRose
```

## Components

### Browser Runtime

- [HTML entry](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/index.html): document · Browser page entry point. · `index.html`
- [App orchestration](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/index.js): browser controller · Wires controls, interactions, spawning, reset, and frame passes. · `index.js`
- [UI styles](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/styles.css): stylesheet · `styles.css`

### State & Model

- [Shared state](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/variables/constAndVars.js): mutable state · Central constants, flags, scales, and body collection. · `variables/constAndVars.js`
- [DOM references](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/variables/domElements.js): UI cache · `variables/domElements.js`
- [Celestial body](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/CelestialBodyClass.js): entity model · Mutable position, velocity, mass, and visual properties. · `classes/CelestialBodyClass.js`
- [Scenario factories](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/spawnTemplates.js): body factory · `functions/spawnTemplates.js`

### Simulation

- [Fixed-FPS loop](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/createConstantFPSGameLoop.js): frame scheduler · `functions/createConstantFPSGameLoop.js`
- [Delta time](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/deltaTime.js): timing utility · `functions/deltaTime.js`
- [Physics system](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/PhysicsSystem.js): N² gravity integrator · Mutates the live body collection using pairwise gravity. · `classes/PhysicsSystem.js`
- [Collision & mass transfer](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/collisionAndMassTransfer.js): collision stage · Merges or removes bodies when enabled. · `functions/collisionAndMassTransfer.js`
- [FPS display](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/fpsDisplay.js): frame metrics · `functions/fpsDisplay.js`

### Camera & Rendering

- [Camera helper](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/cameraHelper.js): world/screen transform · Panning, zoom, and follow targets over mutable bodies. · `functions/cameraHelper.js`
- [Pointer interactions](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/functions/dragListeners.js): input adapter · Converts pointer actions to pan and spawn behavior. · `functions/dragListeners.js`
- [Trail manager](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/TrailManagerClass.js): motion-history layer · Retains and renders optional body trails. · `classes/TrailManagerClass.js`
- [Background stars](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/BackgroundStarsClass.js): background layer · Optional animated star field. · `classes/BackgroundStarsClass.js`
- [Gravity field renderer](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/GravityFieldRenderer.js): specialized canvas layer · Experimental gravity-field visualization boundary. · `classes/GravityFieldRenderer.js`
- [WebGL renderer](https://github.com/keshav-madhav/space-simulation-html-css-js/blob/main/classes/WebGLRenderer.js): specialized renderer · Experimental rendering boundary. · `classes/WebGLRenderer.js`

## Connections

- 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
