How jakubfiala/atrament works
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 Atrament · fill/index.js Fill API · rollup.config.mjs Rollup · esm/index.js ESM bundle
Components 17
Source
Runtime code in src/
- Atrament · Main drawing runtime: canvas ownership, stroke state, mode switching, and event emission
src/index.js - Pointer input · Normalizes browser pointer lifecycle into the core drawing stream
src/pointer-events.js - Mouse input · Mouse-specific input handling and fallback behavior
src/mouse.js - Events · Custom event system for dirty/clean, stroke, fill, and pointer notifications
src/events.js - Pixels · Low-level canvas pixel helpers for read/write operations
src/pixels.js - Constants · Shared constants for sizing, modes, and drawing parameters
src/constants.js - Utilities · General helper functions used by drawing and fill logic
src/util.js
Fill subsystem
Optional flood-fill implementation
- Fill API · Separate fill entrypoint injected into the main constructor
src/fill/index.js - Flood fill · Pixel-buffer flood-fill implementation
src/fill/flood.js - Fill worker · Worker-based execution path for off-main-thread fill
src/fill/worker.js
Published bundles
Compiled CommonJS and ESM outputs
- CJS bundle · CommonJS distribution build of the main library
dist/cjs/index.js - CJS fill · CommonJS distribution build for the fill entrypoint
dist/cjs/fill.js - ESM bundle · ES module distribution build of the main library
dist/esm/index.js - ESM fill · ES module distribution build for the fill entrypoint
dist/esm/fill.js
Demo
Browser example consuming the built library
- Demo page · Example application that consumes the built library
demo/index.html
Connections 22
- 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