# pickentcode/knn-digit-recognition architecture

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

- Interactive diagram: https://gitdiagram.com/pickentcode/knn-digit-recognition
- Repository: https://github.com/pickentcode/knn-digit-recognition

## Overview

##### Project type and purpose
- This is a client‑side, browser‑based web application that demonstrates a very basic k‑nearest‑neighbors (KNN) digit‑recognition tool written entirely in JavaScript.

##### File structure
- Top‑level assets:
- index.html – entry point for the UI
- styles.css – presentation (canvas styling, buttons, results display)
- script.js – orchestrates initialization, event wiring, and result rendering
- drawing.js – handles user input on the HTML canvas and generates raw pixel data
- Data modules:
- data.js – imports or embeds the labeled digit vectors (digits/0…digits/9) for local inference
- dataCloud.js – alternative module to fetch the same data from a remote endpoint or CDN
- digits/0…digits/9 – directories or JSON modules containing preprocessed training vectors for each digit label
- LICENSE & README.md – documentation and licensing

##### Key insights from the README
- Confirms the project is a tutorial/demo for KNN digit recognition in JS.
- Emphasizes basic functionality, no backend, purely front‑end logic and data.

a. User Interface Layer
- HTML Canvas & Controls (Start/Reset buttons, prediction display)
- Event Handler (mouse/touch events)
b. Drawing Module
- drawing.js captures strokes, normalizes into a fixed‑size grid or vector
c. Data Layer
- Local Dataset Loader (data.js + digits/ folder)
- Remote Dataset Loader (dataCloud.js)
d. KNN Engine
- script.js contains the KNN algorithm:
– Distance calculator (e.g., Euclidean)
– Neighbor sorter
– Label majority vote
e. Output Renderer
- UI update with predicted digit and optionally confidence or neighbor examples

##### Relationships and interactions
- User draws → drawing.js emits vector
- script.js receives vector → selects Data Loader (local or cloud) → fetches training set
- KNN Engine processes input vector against training set → outputs label
- Output Renderer updates index.html DOM with result

##### Architectural patterns & design principles
- Separation of concerns: UI, Drawing, Data loading, KNN logic, and Result rendering are distinct modules.
- Plugin‑style data sources: swappable local vs. cloud dataset loader.
- Single‑page app: no page reloads, all logic lives in the browser.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "User Interface Layer"
        IndexHTML["index.html"]:::frontend
        StylesCSS["styles.css"]:::frontend
    end

    IndexHTML -->|"user draws"| Drawing["Event Handler & Drawing Module (drawing.js)"]:::frontend
    Drawing -->|"pixel vector"| Script["KNN Engine & Output Renderer (script.js)"]:::core

    subgraph "Data Sources"
        DataJS["Local Dataset Loader (data.js + digits/)"]:::data
        DataCloud["Remote Dataset Loader (dataCloud.js)"]:::data
    end

    Script -->|"fetch training set"| DataJS
    Script -->|"fetch training set"| DataCloud
    DataJS -->|"training vectors"| Script
    DataCloud -->|"training vectors"| Script

    Script -->|"predicted label"| IndexHTML

    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef data fill:#90EE90,stroke:#333,stroke-width:1px
    classDef core fill:#FFA500,stroke:#333,stroke-width:1px

    click IndexHTML "https://github.com/pickentcode/knn-digit-recognition/blob/main/index.html"
    click StylesCSS "https://github.com/pickentcode/knn-digit-recognition/blob/main/styles.css"
    click Drawing "https://github.com/pickentcode/knn-digit-recognition/blob/main/drawing.js"
    click DataJS "https://github.com/pickentcode/knn-digit-recognition/blob/main/data.js"
    click DataCloud "https://github.com/pickentcode/knn-digit-recognition/blob/main/dataCloud.js"
    click Script "https://github.com/pickentcode/knn-digit-recognition/blob/main/script.js"
    click DataJS "https://github.com/pickentcode/knn-digit-recognition/tree/main/digits/"
```
