# brodbar/3d-renderer-canvas-api architecture

> Architecture diagram of the GitHub repository brodbar/3d-renderer-canvas-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/brodbar/3d-renderer-canvas-api
- Repository: https://github.com/brodbar/3d-renderer-canvas-api

## Overview

This project is a browser‐based 3D rendering engine built using the Canvas API. The overall purpose is to render 3D models (provided as OBJ and MTL files) onto a canvas without relying on any external frameworks. The project takes in 3D model data and provides various components for setting up a virtual camera, handling user input, managing the rendering canvas, and performing 3D math operations.

##### Main Components
- HTML & CSS – The entry point (index.html) and styling (css/style.css) for the overall UI.
- JavaScript Core – The backbone of the engine, residing in js/main.js.
- Rendering Components – Under js/components, include:
- Camera: Responsible for setting up and managing the viewpoint.
- Canvas: Governs the drawing area where 3D objects are rendered.
- Controls & Input: Manage user interaction and camera control.
- Geometric Primitives: Files like Line.js, Plane.js, Triangle (in Mesh), Vertice.js, and Mesh (folder) that define the building blocks of 3D objects.
- Timer: Likely used for animation/frame timing.
- Additional Components: E.g., any utility for camera movement or managing animations.
- Math Utilities – Under js/math (functions.js) and possibly js/components/Vector.js which handle mathematical operations crucial for 3D transformations.
- Model Loading – The js/utils/LoadModel.js which is responsible for loading external 3D model files.
- External Assets – The models directory containing OBJ and MTL files for different 3D objects.

##### Relationships and Interactions
- The HTML (index.html) loads the CSS and the main JS file.
- js/main.js initializes the rendering application that orchestrates the instantiation and interconnection of the different components.
- The Camera, Canvas, and Controls components interact to update views and respond to user inputs.
- Geometric components (Line, Plane, Triangle etc.) are used by the rendering pipeline to construct the 3D scene.
- Math utilities are used across components for calculating transformations, rotations, and vector operations.
- The Model Loader interacts with the file system (models folder) to fetch model data which is then rendered.

##### Architectural Patterns and Design Principles
- Component-Based Architecture: The separation within js/components indicates a modular design, where each file/class encapsulates a specific functionality (e.g., Camera, Controls).
- Separation of Concerns: The architecture cleanly separates rendering logic from mathematics computations and model loading.
- Lightweight Design: The avoidance of any frameworks underlines a focus on core JavaScript and manual control over rendering processes.
- Data Flow: Typically, data flows from the models and math utilities into the rendering components, and user input is captured by Controls and Input modules.

##### Additional Details Specific to the Project
- Minimalistic nature of the design since no frameworks are used.
- Temporal component by including the Timer component to illustrate the animation loop.
- How modularity in the js/components directory enables easy extension of functionality, e.g., additional geometric primitives or controls.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        Html["index.html"]
        CSS["CSS (style.css)"]
    end

    %% Application Layer
    subgraph "Application Layer"
        A1["Main Engine (main.js)"]
        A2["Camera Component"]
        A3["Canvas Component"]
        A4["Controls Component"]
        A5["Input Component"]
        A6["Timer Component"]
        subgraph "Geometric Primitives"
            GP1["Line Primitive"]
            GP2["Plane Primitive"]
            GP3["Triangle Primitive"]
            GP4["Vertices"]
        end
    end

    %% Utility Layer
    subgraph "Utility Layer"
        U1["Math Functions"]
        U2["Vector Operations"]
        U3["Model Loader"]
    end

    %% External & Actor
    E1["External Assets"]
    Uactor["User"]

    %% Connections between Presentation and Application Layers
    Html -->|"loads"| CSS
    Html -->|"loads"| A1

    %% Connections within Application Layer
    A1 -->|"initializes"| A2
    A1 -->|"initializes"| A3
    A1 -->|"initializes"| A4
    A1 -->|"initializes"| A5
    A1 -->|"initializes"| A6

    A1 -->|"buildsScene"| GP1
    A1 -->|"buildsScene"| GP2
    A1 -->|"buildsScene"| GP3
    A1 -->|"buildsScene"| GP4

    %% User interactions
    Uactor -->|"input"| A4
    Uactor -->|"input"| A5

    %% Utility dependencies (Math functions)
    U1 -->|"mathOps"| A2
    U1 -->|"mathOps"| A3
    U1 -->|"mathOps"| A4
    U1 -->|"mathOps"| A5
    U1 -->|"mathOps"| GP1

    U2 -->|"vectorMath"| A2

    %% Model Loading flow
    E1 -->|"suppliesModels"| U3
    U3 -->|"loadsModels"| GP1

    %% Click Events for Presentation Layer
    click Html "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/index.html"
    click CSS "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/css/style.css"

    %% Click Events for Application Layer
    click A1 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/main.js"
    click A2 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Camera.js"
    click A3 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Canvas.js"
    click A4 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Controls.js"
    click A5 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Input.js"
    click A6 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Timer.js"
    click GP1 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Line.js"
    click GP2 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Plane.js"
    click GP3 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Mesh/Triangle.js"
    click GP4 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Vertice.js"

    %% Click Events for Utility Layer
    click U1 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/math/functions.js"
    click U2 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/components/Vector.js"
    click U3 "https://github.com/brodbar/3d-renderer-canvas-api/blob/main/js/utils/LoadModel.js"

    %% Click Event for External Assets
    click E1 "https://github.com/brodbar/3d-renderer-canvas-api/tree/main/models"

    %% Styles
    classDef ui fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef core fill:#A3E4D7,stroke:#148F77,stroke-width:2px;
    classDef util fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef actor fill:#D2B4DE,stroke:#884EA0,stroke-width:2px;

    class Html,CSS ui;
    class A1,A2,A3,A4,A5,A6,GP1,GP2,GP3,GP4 core;
    class U1,U2,U3 util;
    class E1 external;
    class Uactor actor;
```
