# Atelier: browser image editor Vanilla ES modules, no build step, **no npm dependencies** (the user wants it that way). The app is entirely in `build/`, which is deployed as is (a Caddy SPA root), so keep it self-contained: no references outside it, and no dev or test files inside it. Paths below are relative to `build/` except `dev/` and `serve.js`. Serve with `node serve.js` (port 8080, serves `build/`) and test with `dev/test.sh`, which runs headless Chromium over CDP. ## Layout - `js/app.js`: the App shell. It holds documents and tabs, tools, colors and prefs, and routes pointer, keyboard, wheel, drag-and-drop input. `window.atelier` exposes it for tests. - `js/actions.js`: the command registry (id → {label, shortcut, run, enabled, checked}). Menus (`ui/menudef.js`) and shortcuts refer to these ids. Dialog-driven commands (export, filters with live preview, image size, …) are also implemented here. - `js/core/` - `document.js` (ImageDocument): layers ordered bottom→top, selection, history, view. - `layer.js`: Layer, blend modes, and text layout/rendering. - `selection.js`: immutable alpha-mask selections. - `history.js`: PixelEntry, StateEntry and CompoundEntry, plus the PixelEdit helper. - `ops.js`: document and layer operations. - `js/render/` - `compositor.js`: layer blending, masks, clipping groups, live strokes and floating pixels. - `effects.js`: layer styles (shadows, glow, overlay, stroke via a distance transform). - `renderer.js`: the view canvas, zoom/pan, overlays and marching ants. - `rulers.js`: drawing rulers and guide lines (screen space, over the view canvas). - `js/tools/`: one class per tool. Each extends `Tool` and declares a static `schema` that builds its options bar. - `js/imaging/`: filters (pure ImageData → ImageData), blur, and flood fill. `dither.js` holds the dithering pipeline and algorithms, with no DOM, because `dither-worker.js` imports it to run user scripts. A filter's `apply` may return a Promise (runFilter awaits it), and its `params` may be a getter (the dither algorithm list includes the user's scripts). - `js/io/`: project format, export and import, encoders (BMP, GIF, TIFF, ICO), clipboard, and autosave. - `js/ui/`: menus (with keyboard navigation), dialogs, widgets, color picker, chrome (toolbox, options bar, tabs, status bar), and the panels. `ditherscripts.js` is the user dither-script editor; scripts are stored in the `ditherScripts` pref. Also `guides.js` (ruler/guide input and the snapping service tools call), `palette.js` (Ctrl+K command palette) and `layerstyle.js`. ## Invariants (easy to break) - **Undo model.** `doc.change(label, fn)` and `doc.commitChange(label, before)` store *references* to canvases. Code inside them must **replace** layer canvases, never mutate them in place. In-place pixel edits must go through `PixelEdit` (snapshot, then `commit(label, rect)`). - **Canvas context state persists.** Reset `globalCompositeOperation` and `globalAlpha` after using them on any canvas that outlives the function, or use save/restore. `layer.ctx` resets the state for you; `layer.canvas.getContext('2d')` does not. Getting this wrong once caused moved pixels to vanish. - **Painting** goes through `LiveStroke` (`tools/paint.js`). It is a doc-sized buffer that `compositor.applyStroke` previews live and commits with selection clipping. - **Masks** are opaque grayscale canvases (white = visible). `layer.maskAlpha()` converts a mask to alpha. When `doc.editMask` is set, tools paint grayscale versions of the colors. - **Text layers** re-render their canvas from `layer.text`. Pixel tools refuse to work on them until the layer is rasterized. - **Layer styles** (`layer.effects`) and **guides** (`doc.guideLines`) are immutable values held by reference in undo state: replace them, never mutate them. Rendered effects are cached per layer and keyed on `layer.thumbVersion`, so any in-place pixel change must call `layer.touch()` (it already must for thumbnails). - **Tool options** marked `more: true` in a schema go into the options bar's "More" popover. Keep each tool's bar within one row at 1440 px wide (`dev/features.js` checks the brush). ## Testing notes - Test scripts compute screen coordinates from `renderer.canvas.getBoundingClientRect()` and `doc.view`. Re-measure after anything that could change layout. The options bar is a fixed single row so that switching tools does not move the canvas. - Dialogs can be dragged by the header and remember their position per title for the session (`positions` in `ui/dialogs.js`). Measure a dialog's rect instead of assuming it is centered. - Fire actions that open modal dialogs with `setTimeout(() => atelier.run(...), 0)`. Otherwise `Runtime.evaluate` with awaitPromise blocks until the dialog closes. - Snapping is on by default: drags that end within 8 screen px of the canvas edges or center (or a guide) snap there. Pick coordinates away from those when a test expects exact values. - `dev/features.js` covers layer styles, guides, the palette, preferences, menu keys, symmetry, align, dodge/burn and touch pinch (via `Input.dispatchTouchEvent`), and ends with regression checks for fixed bugs.