# jakubfiala/atrament architecture

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

- Interactive diagram: https://gitdiagram.com/jakubfiala/atrament
- Repository: https://github.com/jakubfiala/atrament

## Overview

1. **What this repo is**
   - Atrament is a browser-only JavaScript drawing library for `<canvas>`.
   - It is centered around a single runtime object, the `Atrament` class, which owns the canvas, input handling, drawing state, and event emission.
   - It draws directly into the canvas bitmap instead of maintaining a retained vector scene graph.

2. **Primary runtime boundary**
   - The core library lives in `src/` and is published as bundled output in `dist/` for both CommonJS and ESM consumers.
   - The demo in `demo/` is a thin browser client that exercises the compiled files from `dist/`; it is not the core architecture.
   - The package exposes two entrypoints conceptually: the main Atrament API and a separate fill module.

3. **Core drawing subsystem**
   - The main drawing path handles brush strokes, erasing, smoothing, adaptive stroke width, pressure sensitivity, and mode switching.
   - Drawing is stateful and incremental: pointer input is transformed into processed points, then immediately rendered to the canvas.
   - Because it renders directly to pixels, the library is lightweight and fast, but it does not keep full vector paths unless stroke recording is enabled.

4. **Input/event normalization**
   - `src/pointer-events.js` and `src/mouse.js` indicate an input abstraction layer that normalizes browser pointer/mouse interaction into a consistent stream for the core engine.
   - The library tracks pointer down/move/up lifecycle, supports secondary mouse button behavior, and can ignore modifier-key strokes.
   - This layer is the boundary between browser interaction and drawing logic.

5. **Stroke data model**
   - The library models output as independent strokes, each containing ordered segments.
   - Each segment stores a point `{x, y}`, elapsed time since stroke start, and pressure.
   - Stroke recording is optional; when enabled, the library emits stroke data suitable for replay, undo/redo, or export.

6. **Event system**
   - `src/events.js` underpins custom library events such as `dirty`, `clean`, `strokestart`, `strokeend`, `fillstart`, `fillend`, `pointerdown`, `pointerup`, `segmentdrawn`, and `strokerecorded`.
   - These events are part of the public integration boundary: the host app listens to them to persist state, synchronize UI, or reconstruct drawings.
   - The `dirty/clean` state is a simple lifecycle flag indicating whether the canvas has been modified.

7. **Fill mode as a separate subsystem**
   - Fill mode is split out under `src/fill/` and also shipped as a separate bundle in `dist/*/fill.js`.
   - The README makes this explicit: fill support is injected into the constructor, which keeps the default bundle smaller for apps that do not need flood fill.
   - The fill implementation includes a flood-fill algorithm and a worker-based execution path, so fill can run off the main thread.

8. **Canvas pixel utilities**
   - `src/pixels.js`, `src/constants.js`, and `src/util.js` likely support low-level canvas coordinate, bitmap, and configuration operations.
   - These helpers matter because Atrament draws at the pixel level and must handle canvas size, device pixel ratio / high-DPI behavior, and color/weight calculations consistently.
   - This layer sits beneath the stroke and fill engines.

9. **Build and packaging architecture**
   - The repo is configured to build distributable artifacts with Rollup into both `dist/cjs` and `dist/esm`.
   - The presence of both source and compiled outputs indicates the project is intended for direct npm consumption, not just local development.
   - Linting/formatting/tooling (`eslint`, `prettier`, Travis CI) support library maintenance rather than runtime behavior.

10. **Data flow to visualize**
   - User input enters through browser pointer/mouse events.
   - The input layer normalizes it and hands it to the `Atrament` core.
   - The core updates stroke state, applies smoothing/pressure/adaptive width rules, and writes directly to the canvas bitmap.
   - Optional recording captures the stroke for replay, and events notify the host app.
   - In fill mode, the click location is forwarded to the fill subsystem, which performs flood fill on the pixel buffer, often via a worker.

11. **What is not in the architecture**
   - There is no backend, database, synchronization layer, or retained scene graph.
   - Undo/redo is intentionally not built into the library; the repo instead exposes enough stroke data for the application to implement it.
   - The demo is illustrative only and should be drawn as a consumer of the built library, not as part of the core engine.

Main entry points: [`src/index.js`](https://github.com/jakubfiala/atrament/blob/main/src/index.js) (Atrament), [`src/fill/index.js`](https://github.com/jakubfiala/atrament/blob/main/src/fill/index.js) (Fill API), [`rollup.config.mjs`](https://github.com/jakubfiala/atrament/blob/main/rollup.config.mjs) (Rollup), [`dist/esm/index.js`](https://github.com/jakubfiala/atrament/blob/main/dist/esm/index.js) (ESM bundle)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_source["Source"]
  node_src_index["Atrament<br/>core class<br/>[index.js]"]
  node_src_pointer_events["Pointer input<br/>input adapter<br/>[pointer-events.js]"]
  node_src_mouse["Mouse input<br/>legacy input<br/>[mouse.js]"]
  node_src_events["Events<br/>pubsub<br/>[events.js]"]
  node_src_pixels["Pixels<br/>bitmap utils<br/>[pixels.js]"]
  node_src_constants["Constants<br/>config values<br/>[constants.js]"]
  node_src_util["Utilities<br/>helpers<br/>[util.js]"]
end

subgraph group_fill["Fill subsystem"]
  node_src_fill_index["Fill API<br/>optional module<br/>[index.js]"]
  node_src_fill_flood["Flood fill<br/>algorithm<br/>[flood.js]"]
  node_src_fill_worker["Fill worker<br/>worker path<br/>[worker.js]"]
end

subgraph group_bundles["Published bundles"]
  node_dist_cjs_index["CJS bundle<br/>published build<br/>[index.js]"]
  node_dist_cjs_fill["CJS fill<br/>published build<br/>[fill.js]"]
  node_dist_esm_index["ESM bundle<br/>published build<br/>[index.js]"]
  node_dist_esm_fill["ESM fill<br/>published build<br/>[fill.js]"]
end

subgraph group_demo["Demo"]
  node_demo_index["Demo page<br/>browser client<br/>[index.html]"]
end

subgraph group_tooling["Tooling"]
  node_rollup_config["Rollup<br/>build config<br/>[rollup.config.mjs]"]
  node_package_json["Package<br/>[package.json]"]
end

node_src_pointer_events -->|"input stream"| node_src_index
node_src_mouse -->|"fallback"| node_src_pointer_events
node_src_index -->|"emit"| node_src_events
node_src_index -->|"draws via"| node_src_pixels
node_src_index -->|"uses"| node_src_constants
node_src_index -->|"uses"| node_src_util
node_src_index -->|"injects"| node_src_fill_index
node_src_fill_index -->|"implements"| node_src_fill_flood
node_src_fill_index -->|"offloads"| node_src_fill_worker
node_src_fill_flood -->|"reads/writes"| node_src_pixels
node_dist_cjs_index -.->|"built from"| node_src_index
node_dist_esm_index -.->|"built from"| node_src_index
node_dist_cjs_fill -.->|"built from"| node_src_fill_index
node_dist_esm_fill -.->|"built from"| node_src_fill_index
node_demo_index -->|"consumes"| node_dist_esm_index
node_demo_index -->|"consumes"| node_dist_esm_fill
node_rollup_config -->|"builds"| node_dist_cjs_index
node_rollup_config -->|"builds"| node_dist_esm_index
node_rollup_config -->|"builds"| node_dist_cjs_fill
node_rollup_config -->|"builds"| node_dist_esm_fill
node_package_json -->|"exposes"| node_src_index
node_package_json -->|"exposes"| node_src_fill_index

click node_src_index "https://github.com/jakubfiala/atrament/blob/main/src/index.js"
click node_src_pointer_events "https://github.com/jakubfiala/atrament/blob/main/src/pointer-events.js"
click node_src_mouse "https://github.com/jakubfiala/atrament/blob/main/src/mouse.js"
click node_src_events "https://github.com/jakubfiala/atrament/blob/main/src/events.js"
click node_src_pixels "https://github.com/jakubfiala/atrament/blob/main/src/pixels.js"
click node_src_constants "https://github.com/jakubfiala/atrament/blob/main/src/constants.js"
click node_src_util "https://github.com/jakubfiala/atrament/blob/main/src/util.js"
click node_src_fill_index "https://github.com/jakubfiala/atrament/blob/main/src/fill/index.js"
click node_src_fill_flood "https://github.com/jakubfiala/atrament/blob/main/src/fill/flood.js"
click node_src_fill_worker "https://github.com/jakubfiala/atrament/blob/main/src/fill/worker.js"
click node_dist_cjs_index "https://github.com/jakubfiala/atrament/blob/main/dist/cjs/index.js"
click node_dist_cjs_fill "https://github.com/jakubfiala/atrament/blob/main/dist/cjs/fill.js"
click node_dist_esm_index "https://github.com/jakubfiala/atrament/blob/main/dist/esm/index.js"
click node_dist_esm_fill "https://github.com/jakubfiala/atrament/blob/main/dist/esm/fill.js"
click node_demo_index "https://github.com/jakubfiala/atrament/blob/main/demo/index.html"
click node_rollup_config "https://github.com/jakubfiala/atrament/blob/main/rollup.config.mjs"
click node_package_json "https://github.com/jakubfiala/atrament/blob/main/package.json"

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_src_index,node_src_pointer_events,node_src_mouse,node_src_events,node_src_pixels,node_src_constants,node_src_util toneBlue
class node_src_fill_index,node_src_fill_flood,node_src_fill_worker toneAmber
class node_dist_cjs_index,node_dist_cjs_fill,node_dist_esm_index,node_dist_esm_fill toneMint
class node_demo_index toneRose
class node_rollup_config,node_package_json toneIndigo
```

## Components

### Source

Runtime code in src/

- [Atrament](https://github.com/jakubfiala/atrament/blob/main/src/index.js): core class · Main drawing runtime: canvas ownership, stroke state, mode switching, and event emission · `src/index.js`
- [Pointer input](https://github.com/jakubfiala/atrament/blob/main/src/pointer-events.js): input adapter · Normalizes browser pointer lifecycle into the core drawing stream · `src/pointer-events.js`
- [Mouse input](https://github.com/jakubfiala/atrament/blob/main/src/mouse.js): legacy input · Mouse-specific input handling and fallback behavior · `src/mouse.js`
- [Events](https://github.com/jakubfiala/atrament/blob/main/src/events.js): pubsub · Custom event system for dirty/clean, stroke, fill, and pointer notifications · `src/events.js`
- [Pixels](https://github.com/jakubfiala/atrament/blob/main/src/pixels.js): bitmap utils · Low-level canvas pixel helpers for read/write operations · `src/pixels.js`
- [Constants](https://github.com/jakubfiala/atrament/blob/main/src/constants.js): config values · Shared constants for sizing, modes, and drawing parameters · `src/constants.js`
- [Utilities](https://github.com/jakubfiala/atrament/blob/main/src/util.js): helpers · General helper functions used by drawing and fill logic · `src/util.js`

### Fill subsystem

Optional flood-fill implementation

- [Fill API](https://github.com/jakubfiala/atrament/blob/main/src/fill/index.js): optional module · Separate fill entrypoint injected into the main constructor · `src/fill/index.js`
- [Flood fill](https://github.com/jakubfiala/atrament/blob/main/src/fill/flood.js): algorithm · Pixel-buffer flood-fill implementation · `src/fill/flood.js`
- [Fill worker](https://github.com/jakubfiala/atrament/blob/main/src/fill/worker.js): worker path · Worker-based execution path for off-main-thread fill · `src/fill/worker.js`

### Published bundles

Compiled CommonJS and ESM outputs

- [CJS bundle](https://github.com/jakubfiala/atrament/blob/main/dist/cjs/index.js): published build · CommonJS distribution build of the main library · `dist/cjs/index.js`
- [CJS fill](https://github.com/jakubfiala/atrament/blob/main/dist/cjs/fill.js): published build · CommonJS distribution build for the fill entrypoint · `dist/cjs/fill.js`
- [ESM bundle](https://github.com/jakubfiala/atrament/blob/main/dist/esm/index.js): published build · ES module distribution build of the main library · `dist/esm/index.js`
- [ESM fill](https://github.com/jakubfiala/atrament/blob/main/dist/esm/fill.js): published build · ES module distribution build for the fill entrypoint · `dist/esm/fill.js`

### Demo

Browser example consuming the built library

- [Demo page](https://github.com/jakubfiala/atrament/blob/main/demo/index.html): browser client · Example application that consumes the built library · `demo/index.html`

### Tooling

Build and maintenance configuration

- [Rollup](https://github.com/jakubfiala/atrament/blob/main/rollup.config.mjs): build config · Build pipeline for generating CJS and ESM artifacts · `rollup.config.mjs`
- [Package](https://github.com/jakubfiala/atrament/blob/main/package.json): package manifest · Entry points and publish metadata for the library · `package.json`

## Connections

- Pointer input → Atrament: input stream · Pointer events are normalized before reaching the drawing core
- Mouse input → Pointer input: fallback · Mouse handling complements pointer normalization
- Atrament → Events: emit · Core runtime publishes lifecycle and stroke events
- Atrament → Pixels: draws via · Stroke rendering operates directly on canvas pixels
- Atrament → Constants: uses · Shared constants guide modes and drawing parameters
- Atrament → Utilities: uses · General helper functions support drawing behavior
- Atrament → Fill API: injects · Fill support is attached as a separate optional subsystem
- Fill API → Flood fill: implements · Fill API delegates to the flood-fill algorithm
- Fill API → Fill worker: offloads · Optional worker path can execute fill processing off the main thread
- Flood fill → Pixels: reads/writes · Flood fill operates on bitmap pixels
- CJS bundle → Atrament: built from · Compiled CommonJS main bundle is produced from source
- ESM bundle → Atrament: built from · Compiled ESM main bundle is produced from source
- CJS fill → Fill API: built from · Compiled CommonJS fill bundle is produced from source
- ESM fill → Fill API: built from · Compiled ESM fill bundle is produced from source
- Demo page → ESM bundle: consumes · Demo loads the built library rather than source
- Demo page → ESM fill: consumes · Demo can exercise the optional fill bundle
- Rollup → CJS bundle: builds · Rollup produces CommonJS artifacts
- Rollup → ESM bundle: builds · Rollup produces ESM artifacts
- Rollup → CJS fill: builds · Rollup produces the separate CJS fill bundle
- Rollup → ESM fill: builds · Rollup produces the separate ESM fill bundle
- Package → Atrament: exposes · Package metadata points consumers at the main API
- Package → Fill API: exposes · Package metadata also exposes the optional fill entrypoint
