# lusionltd/webgl-scroll-sync architecture

> Architecture diagram of the GitHub repository lusionltd/webgl-scroll-sync, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lusionltd/webgl-scroll-sync
- Repository: https://github.com/lusionltd/webgl-scroll-sync

## Overview

##### Main Components
- Entry/HTML Component:
- src/index.html acts as the entry point, loading CSS, JavaScript, and the WebGL canvas.
- Styling:
- The src/css folder contains CSS files that reset default styling (reset.css) and define visual styles (style.css) for layout, including the canvas adjustments.
- JavaScript Core:
- src/js/main.js contains the logic for initializing and running the WebGL canvas, managing the animation loop via requestAnimationFrame, and syncing the canvas with DOM elements and scroll events.
- WebGL Shaders:
- The src/shaders folder holds shader programs (img.frag and img.vert) that are used to render the visuals.
- Public Assets:
- The public folder (with fonts, images, site.webmanifest) supplies static assets that contribute to the feel and possibly serve as resources for the WebGL effects.
- Build and Configuration:
- Files such as package.json, vite.config.js, .npmrc, and others handle development dependencies, build setups, and coding standards.

##### Relationships and Data Flows
- User Interaction:
- A user interacts with the webpage (scrolling). These scroll events are detected by the JavaScript code.
- Animation Loop:
- main.js uses requestAnimationFrame (rAF) to continuously update the view. In this loop, the script reads the latest scroll position and aligns the canvas accordingly.
- WebGL Rendering:
- When the main loop runs, it passes the current scroll data to the WebGL context. The shaders loaded from the shaders folder are then applied to render updated visuals within the canvas.
- DOM & CSS:
- The ever-changing viewport (via scrolling) works in tandem with CSS rules, with the canvas being repositioned with ‘absolute’ positioning rather than fixed; this lets the canvas scroll with the page to avoid drift.
- Development/Build Process:
- Vite (configured in vite.config.js) processes the code, bundles assets, and serves them during development. The package.json scripts (dev/build) are responsible for kicking off these processes.

##### Architectural Patterns and Design Principles
- Separation of Concerns:
- HTML for structure, CSS for presentation, and JavaScript for logic are clearly separated.
- Single Page with Layered Updates:
- The app features a single full-screen WebGL canvas overlaid on a static HTML layout. The animation (via rAF) synchronizes scroll states to avoid visual lag.
- Modular Shader Management:
- By segregating shader code into dedicated files, the project ensures easier maintenance and potential shader swapping.

##### Including Technologies and Libraries
- Vite for bundling and development server.
- npm for dependency management (as indicated by package.json and .npmrc).
- WebGL for rendering 3D visuals using shaders.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Components
    A["Browser (User scroll events)"]:::external

    %% Frontend Components
    B["src/index.html (Entry Point)"]:::static

    subgraph "Styling"
        C["src/css/reset.css"]:::static
        D["src/css/style.css"]:::static
    end

    subgraph "Core Logic"
        E["src/js/main.js (Animation Loop & Scroll Sync)"]:::dynamic
    end

    subgraph "WebGL Rendering Engine"
        F["src/shaders/img.frag"]:::shader
        G["src/shaders/img.vert"]:::shader
        H["WebGL Canvas"]:::dynamic
    end

    subgraph "Static Assets"
        I["public/fonts & public/images"]:::asset
        J["public/site.webmanifest"]:::asset
    end

    subgraph "Build & Config"
        K["package.json"]:::config
        L["vite.config.js"]:::config
        M[".npmrc"]:::config
        N[".editorconfig / .prettierrc.json"]:::config
    end

    %% Data Flow Connections
    A -->|"triggers"| B
    B -->|"loads"| C
    B -->|"loads"| D
    B -->|"loads"| E
    A -->|"scrolls"| E
    E -->|"updates"| H
    E -->|"passes data to"| F
    E -->|"passes data to"| G
    H -->|"renders using"| F
    H -->|"renders using"| G
    B -->|"displays static assets"| I
    I -->|"provides assets to"| J
    K -->|"configures"| L
    K -->|"manages deps"| M
    L -->|"bundles & serves"| E

    %% Click Events
    click B "https://github.com/lusionltd/webgl-scroll-sync/blob/main/src/index.html"
    click C "https://github.com/lusionltd/webgl-scroll-sync/blob/main/src/css/reset.css"
    click D "https://github.com/lusionltd/webgl-scroll-sync/blob/main/src/css/style.css"
    click E "https://github.com/lusionltd/webgl-scroll-sync/blob/main/src/js/main.js"
    click F "https://github.com/lusionltd/webgl-scroll-sync/blob/main/src/shaders/img.frag"
    click G "https://github.com/lusionltd/webgl-scroll-sync/blob/main/src/shaders/img.vert"
    click I "https://github.com/lusionltd/webgl-scroll-sync/tree/main/public"
    click J "https://github.com/lusionltd/webgl-scroll-sync/blob/main/public/site.webmanifest"
    click K "https://github.com/lusionltd/webgl-scroll-sync/blob/main/package.json"
    click L "https://github.com/lusionltd/webgl-scroll-sync/blob/main/vite.config.js"
    click M "https://github.com/lusionltd/webgl-scroll-sync/blob/main/.npmrc"
    click N "https://github.com/lusionltd/webgl-scroll-sync/blob/main/.editorconfig"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:2px;
    classDef static fill:#bbf,stroke:#333,stroke-width:2px;
    classDef dynamic fill:#bfb,stroke:#333,stroke-width:2px;
    classDef shader fill:#fdb,stroke:#333,stroke-width:2px;
    classDef asset fill:#ffd,stroke:#333,stroke-width:2px;
    classDef config fill:#fdd,stroke:#333,stroke-width:2px;
```
