Skip to content

Free open-source image editor for the browser

Simplify README and document the control API (#9)

Rodrigo Belfiorec4b885254e0a
diff --git a/API.md b/API.md
new file mode 100644
index 0000000..1c68eac
--- /dev/null
+++ b/API.md
@@ -0,0 +1,86 @@
+# Control API
+
+`window.openlight` exposes the editor's commands while the app is mounted. Commands act on the current document. Load an image before editing or exporting.
+
+## Example
+
+With an image `File` named `imageFile`:
+
+```js
+const editor = window.openlight;
+await editor.openFile(imageFile);
+if (!editor.getState().documentId) throw new Error("Image could not be loaded.");
+
+editor.setAdjustments({ exposure: 1, shadows: 25 });
+editor.undo();
+editor.redo();
+const image = await editor.exportImage({ format: "jpeg", quality: 90 });
+```
+
+Export returns a `File`. It does not start a download.
+
+## Loading
+
+These methods accept browser `File` objects and return `Promise<void>`. Await them before editing.
+
+| Method | Behavior |
+| --- | --- |
+| `openFile(file)` | Opens one image or imports one Camera Raw XMP file. |
+| `openFiles(files)` | Opens the first recognized image, then imports recognized XMP files in order. Unsupported files are ignored. |
+| `loadImage(file)` | Loads a file as an image, replacing the current document and its history. |
+| `importXmp(file)` | Applies supported Camera Raw adjustments as one undoable edit. |
+
+Loading calls are queued. XMP import requires a loaded document and is skipped if none is ready. Image decoding failures appear in the workspace's error state and do not reject the loading promise. Check `getState().documentId` after loading to confirm a document is available.
+
+## Editing
+
+`setAdjustments(change)` updates only the supplied adjustments. Values must be finite numbers within these inclusive ranges. Unknown names and invalid values throw.
+
+| Adjustment | Range | Default |
+| --- | --- | --- |
+| `exposure` | -5 to 5 | 0 |
+| `contrast`, `highlights`, `shadows`, `whites`, `blacks` | -100 to 100 | 0 |
+| `incrementalTemperature`, `incrementalTint`, `vibrance`, `saturation` | -100 to 100 | 0 |
+| `clarity` | -100 to 100 | 0 |
+| `sharpening` | 0 to 150 | 0 |
+| `sharpenRadius` | 0.5 to 3 | 1 |
+
+`setToneCurve(points)` replaces the tone curve. Each point is `{ x, y }` with coordinates between 0 and 1. Supply at least two points, ordered by `x` with a minimum gap of `1/1024`. The first point must have `x = 0` or `y = 0`; the last must have `x = 1` or `y = 1`. Call `setToneCurve()` to reset it.
+
+`editScene(change)` shallowly merges a partial [Scene](src/lib/editor/scene.ts) into the document as an undoable edit. Supply complete values for nested fields such as `frame`. Prefer `setAdjustments` and `setToneCurve` for their validation.
+
+## History
+
+Each edit creates an undo step unless a group is open. Preview changes are outside history.
+
+| Method | Behavior |
+| --- | --- |
+| `beginEdit()` | Starts a group. Subsequent edits appear immediately. |
+| `commitEdit()` | Records the group as one undo step. |
+| `cancelEdit()` | Restores the scene from before the group. |
+| `undo()` | Commits any open group, then undoes one step. |
+| `redo()` | Commits any open group, then redoes one step if available. |
+
+## Preview
+
+`setPreview(change)` updates only the supplied preview settings.
+
+| Field | Values | Default |
+| --- | --- | --- |
+| `comparison` | `"edited"`, `"original"`, `"split"` | `"edited"` |
+| `split` | Divider position from 0 to 1 | 0.5 |
+| `shadows` | Show shadow clipping | `false` |
+| `highlights` | Show highlight clipping | `false` |
+
+## Export
+
+`exportImage(options)` returns `Promise<File>` containing the edited image at the document's dimensions. Preview settings do not affect export.
+
+- `exportImage()` or `exportImage({ format: "png" })` exports PNG.
+- `exportImage({ format: "jpeg", quality: 90 })` exports JPEG. Quality must be between 1 and 100.
+
+## State
+
+`getState()` returns a detached snapshot containing `file`, `documentId`, `size`, `frame`, `adjustments`, `toneCurve`, `preview`, and `history`. `file` is the filename, and `history` contains `undoCount` and `redoCount`.
+
+Without a document, `documentId`, `size`, `frame`, and `preview` are undefined. Adjustments and the tone curve use their defaults, and history counts are zero. Mutating the snapshot does not edit the document.
diff --git a/README.md b/README.md
index 3c83390..79aa73d 100644
--- a/README.md
+++ b/README.md
@@ -2,15 +2,21 @@
**An open-source image editor for the browser, built with [vgpu.sh](https://vgpu.sh)**
-Non-destructive editing, rendered on your GPU. Everything stays on your machine: no account, no upload.
+Image processing runs locally with WebGPU. Built with TypeScript and React.
## Features
-- Light and color adjustments, tone curves, and Camera Raw XMP import.
-- Crop, rotate, straighten, and flip, with undo/redo.
-- Before/after comparison, RGB histograms, and clipping overlays.
-- Pan and zoom, including Space + drag while cropping.
-- PNG and JPEG export.
+- Light and color adjustments
+- Tone curves
+- Clarity and sharpening
+- Crop, rotate, straighten, and flip
+- Undo and redo
+- Before/after comparison
+- RGB histogram
+- Highlight and shadow clipping overlays
+- Pan and zoom
+- Camera Raw XMP import
+- PNG and JPEG export
Requires a WebGPU-capable browser.
@@ -21,23 +27,11 @@ bun install
bun dev
```
-```sh
-bun run check # format + lint
-bun run build # typecheck + bundle
-bun run test # browser-free integration tests
-bun run test:browser # pixels, UI, and codecs; requires Chromium + WebGPU
-```
+Run `bun run check` to format and lint, `bun run build` to type-check and build, and `bun run test` for integration tests. GPU and UI tests run with `bun run test:browser` and require Chromium with WebGPU.
## Scripting
-`window.openlight` exposes the editor commands in the browser. `createControls(gpu, workspace)` provides the same API without React.
-
-```js
-await openlight.openFiles([imageFile, xmpFile]);
-openlight.setAdjustments({ exposure: 1, shadows: 25 });
-openlight.undo();
-const image = await openlight.exportImage();
-```
+`window.openlight` exposes commands for loading, editing, undo/redo, and export. See the [API reference](API.md).
## License