Skip to content

Free open-source image editor for the browser

Add Healing (#35)

Rodrigo Belfiorec0cd82d3147f
diff --git a/AGENTS.md b/AGENTS.md
index e261e67..c70469c 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -43,7 +43,7 @@ React composes controls and mounts engine outputs. The engine owns GPU resources
## Styling
-Keep Tailwind classes inline. Reuse presentation through components or repeat classes, whichever is simpler; do not share class-string constants across files. Use `cva` for variants and `src/index.css` for app-global tokens such as shadows and typography. Text has one size, set on `body` in `src/index.css`; express hierarchy with color and weight, not size.
+Follow [DESIGN.md](DESIGN.md) for shared interaction and surface patterns. Keep Tailwind classes inline. Reuse presentation through components or repeat classes, whichever is simpler; do not share class-string constants across files. Use `cva` for variants and `src/index.css` for app-global tokens such as shadows and typography. Text has one size, set on `body` in `src/index.css`; express hierarchy with color and weight, not size.
## GPU invariants
diff --git a/API.md b/API.md
index a974e44..d0211d8 100644
--- a/API.md
+++ b/API.md
@@ -55,6 +55,10 @@ Commands take explicit layer IDs rather than using UI selection. Without an ID,
`setFill({ color, blend }, id?)` updates a Color layer: `color` is `#rrggbb` sRGB and `blend` is `normal`, `multiply`, `screen`, `overlay`, `soft-light`, `color`, or `luminosity`. The layer paints that color over the image below it, blended as Photoshop does on encoded values, so inside a brush or gradient mask it paints the mask's coverage; the layer's opacity sets the strength. Without an ID it updates the first Color layer or creates one, which starts at `#f0763c` in Normal.
+Create an empty Heal effect with `addLayer("heal")`.
+
+`addHealPatch(id, stroke, offset)` appends a Healing patch and returns its ID. `stroke` is a painted `BrushStroke`, and `offset` is `[sourceX - targetX, sourceY - targetY]` in source pixels. `setHealSource(id, patchId, offset)` changes a donor. Patches replay in order from the accumulated composite below; the tool supplies automatic donors.
+
For RAW sources, `setWhiteBalance({ temperature, tint })` sets absolute Kelvin and DNG tint, preserving unspecified values. Temperature accepts 2000–25000 K and tint accepts -150–150, extending either range to include the file's As Shot value. `setWhiteBalance()` restores that value (the decoder's daylight fallback if camera multipliers are unavailable). Non-RAW sources and invalid values throw. Incremental temperature/tint remain separate RGB adjustments.
Edits update the scene and history synchronously. Rendering may finish later, particularly RAW development. Tests should wait for visible results; `exportImage()` renders and waits for its captured scene independently of the preview.
@@ -71,7 +75,7 @@ Direct mask children of another mask modify coverage instead of processing image
| Method | Behavior |
| --- | --- |
-| `addLayer(kind, placement?)` | Adds `"exposure"`, `"color-mixer"`, `"details"`, `"vignette"`, `"fill"`, or `"mask"`; selects and returns its ID. `{ inside: id }` appends a child to a processing layer; `{ above: id }` inserts directly above that layer among its siblings; without a placement, the layer goes on top of the root stack. Exposure starts at +1 EV, Vignette at intensity 50; Color Mixer, Details, and masks start neutral. |
+| `addLayer(kind, placement?)` | Adds `"exposure"`, `"color-mixer"`, `"details"`, `"vignette"`, `"fill"`, `"heal"`, or `"mask"`; selects and returns its ID. `{ inside: id }` appends a child to a processing layer; `{ above: id }` inserts directly above that layer among its siblings; without a placement, the layer goes on top of the root stack. Exposure starts at +1 EV, Vignette at intensity 50; Color Mixer, Details, Healing, and masks start neutral. |
| `selectLayer(id)` | Selects any layer. |
| `setLayer(id, change)` | Updates processing-layer `name`, `visible`, or `opacity` (0–1). |
| `setExposure(id, value)` | Sets an Exposure layer to -5…5 EV. |
@@ -85,7 +89,9 @@ A linear gradient has full coverage at `start`, zero at `end`; its points must b
A brush mask is a list of strokes. Each stroke has `mode` (`"paint"` or `"erase"`), `size` (diameter in source pixels), `feather` and `flow` (0–1), and `points`, each `[x, y, pressure]` in source pixels with pressure 0–1. Dabs land every quarter diameter along the points; a paint stroke adds `flow × pressure` of the remaining coverage under each dab, and an erase stroke removes that share of the existing coverage. The renderer rasterizes strokes into a cached coverage texture at source resolution and only stamps new points, so appending to the last stroke is cheap and undo replays the rest. A brush inside another mask keeps its own coverage, paint and erase strokes alike, which the mask adds or subtracts scaled by the brush layer's opacity. A mask with no painted coverage and nothing added to it is bypassed.
-The sidebar always shows the output histogram, the selected layer's controls, and the layer stack; layers and controls scroll independently. The rail holds tools: **A** Adjust, **B** Brush, **L** Linear gradient, **R** Radial gradient, and **C** Crop; **E** opens Export from the header. A selected mask is *active*: the sidebar shows its adjustments and the bar its opacity. It is *edited* only while a shape tool is on the rail, which shows its guides or brush and the overlay; Adjust leaves the canvas to pan and zoom. Selecting a mask in the stack arms the tool of its shape; selecting the image or an effect returns to Adjust. **Enter**, or **Escape** with no drag in progress, leaves one level at a time: a shape tool returns to Adjust with the mask still selected, so its sliders stay at hand without guides over the image, and in Adjust the selection climbs to the parent mask, then to the image with its global controls. The shape's key or the mask's row enters editing again. Enter on a focused button stays its click. The bar over the image edits the active tool and the selected layer: the tool's options, then the layer's opacity, Overlay, and a radial mask's Feather or a child mask's operation. It never wraps: when the canvas is narrow it drops the slider bars, then moves the layer options and finally everything into a menu at its end. Dragging with a gradient tool draws a new mask above the selection; the tool stays active, and **Alt** while starting the drag subtracts the gradient from the selected mask instead. Choosing the Brush tool creates an empty brush mask above the selection at once, so adjustments made before the first stroke belong to it; leaving the tool while that mask has no strokes and nothing else changed removes it together with its history entry. A brush stroke paints into the selected brush mask, or starts a new brush mask. Creating inside a mask happens in the layer stack: each root mask row has Add and Subtract menus that choose a shape and nest the next mask inside it, and **Delete** removes the selected layer with any tool. The brush bar sets size, feather, flow, and paint or erase; **[** and **]** resize, holding **Alt** erases and shows Erase pressed, and **Escape** cancels the stroke. The brush cursor previews the dab's feather. Creating the mask and each stroke are separate undo steps.
+The sidebar always shows the output histogram, the selected layer's controls, and the layer stack; layers and controls scroll independently. The rail holds tools: **A** Adjust, **B** Brush, **L** Linear gradient, **R** Radial gradient, **H** Healing, and **C** Crop; **E** opens Export from the header. A selected mask is *active*: the sidebar shows its adjustments and the bar its opacity. It is *edited* only while a shape tool is on the rail, which shows its guides or brush and the overlay; Adjust leaves the canvas to pan and zoom. Selecting a mask in the stack arms the tool of its shape; selecting the image or an effect returns to Adjust. **Enter**, or **Escape** with no drag in progress, leaves one level at a time: a shape tool returns to Adjust with the mask still selected, so its sliders stay at hand without guides over the image, and in Adjust the selection climbs to the parent mask, then to the image with its global controls. The shape's key or the mask's row enters editing again. Enter on a focused button stays its click. The bar over the image edits the active tool and the selected layer: the tool's options, then the layer's opacity, Overlay, and a radial mask's Feather or a child mask's operation. It never wraps: when the canvas is narrow it drops the slider bars, then moves the layer options and finally everything into a menu at its end. Dragging with a gradient tool draws a new mask above the selection; the tool stays active, and **Alt** while starting the drag subtracts the gradient from the selected mask instead. Choosing the Brush tool creates an empty brush mask above the selection at once, so adjustments made before the first stroke belong to it; leaving the tool while that mask has no strokes and nothing else changed removes it together with its history entry. A brush stroke paints into the selected brush mask, or starts a new brush mask. Creating inside a mask happens in the layer stack: each root mask row has Add and Subtract menus that choose a shape and nest the next mask inside it, and **Delete** removes the selected layer with any tool. The brush bar sets size, feather, flow, and paint or erase; **[** and **]** resize, holding **Alt** erases and shows Erase pressed, and **Escape** cancels the stroke. On a touch screen a second finger during a stroke cancels it and pinches instead, so painting and zooming never mix. The brush cursor previews the dab's feather and remains visible while Size or Feather is edited. Creating the mask and each stroke are separate undo steps.
+
+**H** enters Healing and creates an empty Healing layer unless one is selected. Each stroke becomes a patch filled from a donor elsewhere in the image. Size sets the next stroke and never resizes an existing patch. Healing keeps its own next-stroke Feather, initially 10%, without changing the Brush tool. A selected patch exposes editable Feather and Opacity; feather softens the accumulated hard-stroke shape inward from its contour rather than expanding it or feathering every dab independently. A stroke whose brush does not reach the image is declined. Healing chooses a nearby donor automatically; **Alt-click** sets it manually, and Automatic source clears the override. Its boundary correction is bounded to avoid extreme color gains and remains independent of Feather and Opacity. A stroke's donor search keeps its history group open, so Size, Feather, or a handle drag started meanwhile joins that stroke's undo step. Each patch receives the visible result of every earlier patch in its layer. The sidebar numbers patches in replay order; drawing, selection, and row hover show their destination contour and first-point anchor. Dragging the destination anchor moves the shape while its source stays fixed. A completed patch also shows a quieter source contour whose anchor changes the same donor coordinates as the panel. **Escape** cancels painting or pending processing; Enter leaves after completion. Leaving an untouched new layer removes it.
The red overlay shows a mask being edited that does not change the image yet: a gradient being drawn, or the selected mask while its adjustments and curve are default and its effects are off. Once the mask applies an effect, the image itself shows it, so painting continues without the tint. The Overlay button shows whether the overlay is visible; it and **O** show or hide the overlay for the selected mask in any tool, until another layer is selected. The tint follows the layer's opacity, which scales the mask's coverage. The curve's input histogram weighs pixels by that coverage, so it describes what the curve affects; it ignores opacity and stays available while the layer is hidden or at zero opacity. Selected masks stay editable: drag the center to move, guides to resize or rotate, and the radial inner handle or Feather slider to soften its edge. Shift constrains drawing; Escape cancels the gesture; Delete/Backspace removes the selected mask. Each gesture is one undo. A new mask shows a red overlay of its coverage until Enter leaves editing or its first adjustment; the overlay also shows while drawing or dragging a guide. Add/Subtract menus accept either gradient shape. **+** adds an effect, inside a selected top-level mask or above the selected sibling. Eye buttons toggle visibility; the inspector edits opacity. Drag a layer name to reorder (insertion line) or nest (highlighted row); the base stays locked and nesting is limited to two levels. Double-click a name to rename; the row menu also duplicates, deletes, reorders, or reparents. Image import still replaces the document; multiple image layers and additional blend modes are not implemented.
diff --git a/CONTEXT.md b/CONTEXT.md
index f309d50..bad4cbe 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -7,10 +7,11 @@ OpenLight edits documents containing image content and adjustments.
| **Document** | An independent editing session with its own canvas, history, and resources. Opening an image creates a document containing that image. |
| **Scene** | The document's serializable content: canvas geometry, image-source references, and adjustments. Files and GPU resources live outside it. |
| **Layer** | A source, effect, or mask in the composition. Image layers own basic adjustments; effects process the image below; masks scope their basic adjustments and child effects. Linear, radial, and brush child masks add or subtract coverage. Layers compose render nodes without owning textures. |
-| **Stroke** | One brush drag on a brush mask: paint or erase, size, feather, flow, and points with pressure in source pixels. Strokes are content; the renderer rasterizes them into a cached coverage texture. |
+| **Stroke** | One brush drag: paint or erase, size, feather, flow, and points with pressure in source pixels. Strokes are content; the renderer rasterizes them into a cached coverage texture. A Healing patch stores a hard stroke and feathers its accumulated shape separately. |
+| **Healing patch** | One painted, non-destructive repair in a Healing layer. It copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Patches replay in order and each sees the result of the preceding patches. Size belongs to the recorded hard stroke; feather and opacity remain editable on its accumulated shape. |
| **Image source** | An image used as input to the document's composition. It is content within a document. |
| **Edit** | One reversible content change. A continuous slider or curve drag is one edit, as is importing a set of adjustments. |
| **Workspace** | The open-document session. It currently holds at most one document, replaced when another image is opened. |
-| **Tool** | What a drag on the canvas does: Brush paints, Linear and Radial gradient draw masks and edit the selected gradient's guides, Adjust leaves the canvas to pan and zoom. Crop and Export bring their own view. Switching tools never changes the sidebar; Brush creates its mask on entry and removes it again if it leaves untouched. A selected mask is *active*; it is *edited* only while a shape tool is on. |
+| **Tool** | What a drag on the canvas does: Brush paints, Healing repairs, and Linear and Radial gradient draw masks and edit the selected gradient's guides. Adjust leaves the canvas to pan and zoom. Crop and Export bring their own view. Switching tools never changes the sidebar; Brush and Healing create their layer on entry and remove it again if it leaves untouched. A selected mask is *active*; it is *edited* only while a shape tool is on. |
Use these names consistently in code, UI text, and documentation.
diff --git a/DESIGN.md b/DESIGN.md
new file mode 100644
index 0000000..2fce849
--- /dev/null
+++ b/DESIGN.md
@@ -0,0 +1,25 @@
+# Interface design system
+
+OpenLight uses a small set of shared UI primitives so the editor reads as one application across tools and panels.
+
+## Selection and menus
+
+- Use `Select` for a choice that replaces a current value. Its trigger may use the panel `field` style or floating-toolbar `pill` style; its options always open on the shared elevated surface.
+- Use `Menu` for commands such as Duplicate, Move, or Delete. `Menu` and `Select` both use `MenuSurface`, which owns the anchored popover, border, background, shadow, item hover, dismissal, and keyboard navigation.
+- Do not render a native HTML `select` in product UI. Specialized browser pickers such as file and color inputs are allowed when the browser owns behavior OpenLight does not reproduce.
+- Put a check beside the selected option. Keep command menus unselected unless the command itself represents a persistent state.
+
+## Panel collections
+
+- Use `PanelListItem` for selectable rows in editor panels. It owns row height, divider, hover, muted, and selected states.
+- A row may compose its own thumbnail, label, compact fields, and action menu. Keep those feature-specific parts with the feature.
+- Keep lists flush with the panel edges. Put empty-state copy inside the panel padding rather than padding every populated row.
+
+## Surfaces and controls
+
+- Use shared components for repeated interaction and surface behavior. Keep feature-specific composition in the feature.
+- Use Tailwind tokens already present in the shared primitive for color, borders, shadows, focus, and pointer-coarse sizing. A caller chooses a documented variant instead of recreating the surface.
+- Anchor viewport-corner controls and messages 12 px from both edges. Tool overlays cover the full viewport; the camera preserves a 24 px margin around fitted content without shrinking that coordinate space.
+- Use `ScrubInput` for numbers that primarily read as inline text and secondarily accept typing. Inline variants hug the current digits and unit, keep their label close, and show only a subtly rounded background while text editing is active. Reserve the persistent bordered `box` variant for form fields that need explicit input chrome.
+- Give toolbar slider values an explicit minimum character width when their expected range changes digit count. Set it at the call site from the useful range: percentages reserve three tabular characters, while brush size uses the actual maximum's digit count. Do not guess a global maximum.
+- Text uses the global body size. Express hierarchy with weight and color.
diff --git a/PERFORMANCE.md b/PERFORMANCE.md
index 65f7194..da233e9 100644
--- a/PERFORMANCE.md
+++ b/PERFORMANCE.md
@@ -21,14 +21,16 @@ Keep unrelated work off the queue during isolated measurements. Label software a
Import rendering primitives from `@/core/renderer`. [`node`](src/core/renderer/node.ts) defines a pass without a GPU: a stable instance name, shader, and optional vgpu `set` values, sampler descriptors, float storage data, or output size/format. `input` imports a caller-owned texture. `pipeline` connects a list of definitions through the shader's `source` binding; an omitted definition bypasses the effect. A multi-pass feature can supply an image-to-image function instead. `split` builds branches sharing their input, without copying textures. `merge` connects named inputs to a definition; its shader performs the combination. Output geometry defaults to the first input. See [unsharp masking](src/features/details/unsharp-mask.ts) for branches and [app composition](src/app/editor/renderer.ts) for preview/export ordering.
-[`createRenderGraph(gpu, timer?)`](src/core/renderer/graph.ts) initializes one vgpu effect per node name on first use, keeps it through bypasses, updates bindings, and owns storage buffers. A name identifies one shader and sampler configuration for the graph's lifetime; distinct passes need distinct names even when they use the same shader. Disposal releases buffers and intermediate targets; vgpu owns its internal effect resources and sampler cache.
+[`createRenderGraph(gpu, timer?)`](src/core/renderer/graph.ts) initializes one vgpu effect per node name on first use, keeps it through bypasses, updates bindings, and owns storage buffers. A name identifies one executed pass. Passes with the same shader and non-input bindings may share an `instance`, while their names and input textures remain distinct for execution, inspection, and timing. Disposal releases buffers and intermediate targets; vgpu owns its internal effect resources and sampler cache.
-The graph executes each reachable node once and reuses compatible targets after their last consumer. Multiple inputs and requested outputs are supported. Requested outputs remain valid until the next render or disposal; imported targets remain owned by the caller. Unneeded targets are released after rendering. `inspect()` reports executed pass names and retained intermediate texture dimensions/formats, excluding source, display, decoder, buffers, and driver allocations. There is no cross-frame result cache or shader fusion.
+The graph executes each reachable node once and reuses compatible targets after their last consumer. Multiple inputs and requested outputs are supported. Requested outputs remain valid until the next render or disposal; imported targets remain owned by the caller. Unneeded targets are released after rendering. `inspect()` reports executed pass names, prepared effect count, and retained intermediate texture dimensions/formats, excluding source, display, decoder, buffers, and driver allocations. There is no cross-frame result cache or shader fusion.
Layer composition builds subgraphs without allocating textures for containers. `mixAdjustment` combines an effect with its input using opacity and optional analytical gradients in document coordinates; Add/Subtract child masks use a small storage buffer in that same pass, without mask textures; it preserves input alpha. Node names include stable layer IDs. The display pass tints the previewed mask from the same analytical coverage, so the overlay adds no processing pass. Removed layers release their graph-owned instance handles and storage buffers through `release(prefix)`; bypassed layers keep their instances. vgpu's internal pipeline and uniform caches remain GPU-owned until GPU disposal.
A mask that paints with a brush is rasterized instead: [`createMaskRaster`](src/core/renderer/mask/index.ts) keeps one `r8unorm` texture per brush with strokes at source resolution, outside the graph, whether the brush is a mask of its own or a child of one. Stroke dabs stamp in scissored chunks of 64, one frame each so buffer writes stay ordered; when a scene only appends strokes or points to a brush, only the new dabs stamp, and any earlier change replays that brush's strokes. A mask shaped by children combines its own gradient or brush with theirs, in stored order and scaled by their opacity, into a second texture named `<id>/group` in `inspect()`, rebuilt with one pass per part whenever any of them changes; a brush without children samples its own texture directly. The mix pass then samples the combined texture under the name `layer/<id>/raster` rather than `layer/<id>/mix`, the display overlay samples a row's own or combined texture, and thumbnails do the same. `inspect()` reports `stamped` dabs and the live `rasters`. A mask with no coverage is bypassed without a texture.
+Composition also exposes `brush(id, strokes)` for effects that own strokes. Healing uses one hard-stroke raster per patch with stable IDs owned by the feature. Healing reads that hard boundary for a cropped, coarse-to-fine harmonic color correction with bounded RGB gain. Jacobi iterations at each resolution share one prepared effect because their uniforms are identical, while their named executions and alternating texture bindings stay distinct; a typical patch retains 9–13 effects instead of about 80. Donor dragging moves only the overlay and applies the new offset once on release; a zero donor offset bypasses the correction and releases its prepared effects. Feather and opacity affect only the full-image blend, which softens coverage inside that boundary with fixed radial samples without another texture or pass. `inputImage(id)` exposes the accumulated visible result of earlier patches for donor search. The separate `createHealSearch` graph exposes `inspect()` and optional timestamps for its 512-pixel thumbnail pass; matching and readback are measured separately. Every composition pass remains visible in `inspect()` and timestamps, and image outputs remain linear Rec.2020 `rgba16float`.
+
Interactive edits render a proxy. While a history group is open, the renderer's `update(scene, inputId, interactive)` reduces the source by an integer factor chosen from `setDisplayScale`, the device pixels shown per source pixel, so the composition runs at about display resolution; the reduced source is cached until the source, factor, or RAW development changes. Every `RenderImage` carries `scale`, its source pixels per texel, which the mix pass, unsharp masking, and the transform pass use to keep gradients, radii, and crops in document units. Committing or cancelling the group renders the full image, and export always renders in full. Measure interactive workloads with the proxy factor stated, since a factor of 4 cuts processed pixels sixteenfold.
For GPU timing, enable the optional `timestamp-query` device feature, create a vgpu `timer(gpu)`, and pass it to the graph or `createEditorRenderer`. The graph instruments every node centrally. Results arrive asynchronously through `timer.onResults`; the caller disposes the timer. `gpu.settled()` covers timing readbacks in isolated runs or teardown. Do not wait after individual nodes during interactive editing. See the [vgpu guide](https://github.com/vercel-labs/vgpu/blob/main/docs/topics/performance-playbook.docs.md#13-time-passes-before-optimizing-timer).
@@ -41,7 +43,7 @@ Reuse the existing [benchmark](tests/rendering.bench.ts) and timing helpers for
## Running the benchmark
-After [browser setup](CONTRIBUTING.md#browser-setup), run `bun run test:browser --config playwright.bench.config.ts` separately from other GPU/browser work. Workloads cover neutral effects, color mixing, vignette, detail filters, and the combined pipeline. The fixture is a deterministic 2400×1600 linear Rec.2020 gradient containing neutrals, saturated colors, and HDR values; exposure is 0.25 and contrast is 10. Each workload uses 8 warmups and 40 measured samples. Settings live in [the benchmark](tests/rendering-benchmark.ts); select a workload with e.g. `--grep 'rendering vignette'`.
+After [browser setup](CONTRIBUTING.md#browser-setup), run `bun run test:browser --config playwright.bench.config.ts` separately from other GPU/browser work. Workloads cover neutral effects, color mixing, vignette, detail filters, Smart clone, and the combined pipeline. Healing workloads compare an empty layer, a 240 px patch, and the patch at proxy factor 2. AI inference latency is reported separately because it includes the external runtime and model. The fixture is a deterministic 2400×1600 linear Rec.2020 gradient containing neutrals, saturated colors, and HDR values; exposure is 0.25 and contrast is 10. Each workload uses 8 warmups and 40 measured samples. Settings live in [the benchmark](tests/rendering-benchmark.ts); select a workload with e.g. `--grep 'rendering heal'`.
Results and rendered PNGs are written under `test-results/benchmarks`; keep generated artifacts out of Git. JSON includes environment details, samples, median/p95, setup, first render, completed-render latency, intermediate texture storage, and per-node GPU timestamps when supported. Timestamp profiling runs separately from the latency comparison. Decoding, display, readback, and image encoding run outside the measured loop. Software-adapter results describe that backend only. A hardware measurement requires a browser configuration that does not force SwiftShader; record the adapter actually used.
diff --git a/README.md b/README.md
index 1d49943..f71df1d 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ Try it at [openlight.app](https://openlight.app).
Image processing runs on your own GPU with WebGPU. Photos never leave your device.
-![The OpenLight editor with a mountain trail photo open and a gradient mask layer](public/og.png)
+![The OpenLight editor repairing a mountain trail photo with the Healing tool](public/og.png)
## Features
@@ -25,6 +25,7 @@ Image processing runs on your own GPU with WebGPU. Photos never leave your devic
**Tools**
+- Healing paints patches from an automatic or manually positioned donor. Repairs remain editable and each patch builds on the previous result.
- Brush, linear, and radial gradient tools on the rail draw masks; their options sit in the bar over the image.
- Brush masks with size, feather, flow, pen pressure, and Alt to erase.
- The mask overlay shows until a mask changes the image, then the image shows the mask; the Overlay button shows or hides it.
diff --git a/index.html b/index.html
index d7b3a69..ecc4135 100644
--- a/index.html
+++ b/index.html
@@ -18,12 +18,12 @@
<meta property="og:image" content="https://openlight.app/og.png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
- <meta property="og:image:alt" content="The OpenLight editor with a mountain trail photo open and a gradient mask layer." />
+ <meta property="og:image:alt" content="The OpenLight editor repairing a mountain trail photo with the Healing tool." />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="OpenLight | A free open-source photo editor for the web" />
<meta name="twitter:description" content="Edit photos in your browser. An open-source photo editor built on WebGPU." />
<meta name="twitter:image" content="https://openlight.app/og.png" />
- <meta name="twitter:image:alt" content="The OpenLight editor with a mountain trail photo open and a gradient mask layer." />
+ <meta name="twitter:image:alt" content="The OpenLight editor repairing a mountain trail photo with the Healing tool." />
</head>
<body>
<div id="root"></div>
diff --git a/public/og.png b/public/og.png
index 2d29411..34abf0b 100644
Binary files a/public/og.png and b/public/og.png differ
diff --git a/src/app/controls.ts b/src/app/controls.ts
index 6360d92..135b59a 100644
--- a/src/app/controls.ts
+++ b/src/app/controls.ts
@@ -8,6 +8,7 @@ import { createImageLoader } from "@/app/loaders/image";
import { createLoaderRegistry } from "@/app/loaders/registry";
import type {
Adjustments,
+ BrushStroke,
Details,
Fill,
Mask,
@@ -17,7 +18,7 @@ import type {
Vignette,
} from "@/core/document";
import type { WhiteBalance } from "@/core/image";
-import type { ImageFrame } from "@/core/image/frame";
+import type { ImageFrame, Point } from "@/core/image/frame";
import { setAdjustments } from "@/features/adjustments/edits";
import { defaultAdjustments } from "@/features/adjustments/model";
import { resetColorMixer, setColorMixer } from "@/features/color-mixer/edits";
@@ -29,6 +30,7 @@ import {
import { setDetails } from "@/features/details/edits";
import { defaultDetails } from "@/features/details/model";
import { setFill } from "@/features/fill/edits";
+import { addHealPatch, setHealSource } from "@/features/heal/edits";
import {
addLayer,
deleteLayer,
@@ -105,6 +107,10 @@ export function createControls(gpu: Gpu, workspace: Workspace) {
const document = workspace.getDocument();
editEffect(document, "fill", id, (id) => setFill(document, change, id));
},
+ addHealPatch: (id: string, stroke: BrushStroke, offset: Point) =>
+ addHealPatch(workspace.getDocument(), id, stroke, offset),
+ setHealSource: (id: string, patchId: string, offset: Point) =>
+ setHealSource(workspace.getDocument(), id, patchId, offset),
addLayer(kind: ProcessingLayer["kind"], placement?: LayerPlacement) {
const document = workspace.getDocument();
if (kind !== "mask") {
diff --git a/src/app/editor/adjust/index.tsx b/src/app/editor/adjust/index.tsx
index 399ab0f..2d91e5b 100644
--- a/src/app/editor/adjust/index.tsx
+++ b/src/app/editor/adjust/index.tsx
@@ -10,6 +10,7 @@ import { AdjustmentControls } from "@/features/adjustments/controls";
import { ColorMixerControls } from "@/features/color-mixer/controls";
import { DetailsControls } from "@/features/details/controls";
import { FillControls } from "@/features/fill/controls";
+import { HealControls } from "@/features/heal/controls";
import { Histogram } from "@/features/histogram";
import { createHistogram } from "@/features/histogram/histogram";
import { setExposure } from "@/features/layers/edits";
@@ -55,6 +56,18 @@ function LayerCurve({ id, toneCurve }: { id: string; toneCurve: ToneCurve }) {
);
}
+/** The sidebar heading for each layer kind's controls; a new kind must name one. */
+const titles: Record<Layer["kind"], string> = {
+ image: "Adjustments",
+ mask: "Adjustments",
+ exposure: "Adjustments",
+ details: "Adjustments",
+ vignette: "Adjustments",
+ "color-mixer": "Adjustments",
+ fill: "Adjustments",
+ heal: "Healing",
+};
+
function SelectedControls({ layer }: { layer: Layer }) {
const document = useDocument();
switch (layer.kind) {
@@ -81,6 +94,8 @@ function SelectedControls({ layer }: { layer: Layer }) {
return <VignetteControls id={layer.id} vignette={layer.vignette} />;
case "fill":
return <FillControls id={layer.id} fill={layer.fill} />;
+ case "heal":
+ return <HealControls id={layer.id} patches={layer.patches} />;
case "exposure":
return (
<section className="p-3">
@@ -114,7 +129,7 @@ export function AdjustPanel() {
);
return (
<div {...gesture}>
- <PanelHeader title="Adjustments" />
+ <PanelHeader title={titles[target.kind]} />
<SelectedControls layer={target} />
</div>
);
diff --git a/src/app/editor/empty.tsx b/src/app/editor/empty.tsx
index 3134b10..96e7147 100644
--- a/src/app/editor/empty.tsx
+++ b/src/app/editor/empty.tsx
@@ -104,7 +104,11 @@ export function EmptyEditor({
useEffect(() => () => document.dispose(), [document]);
return (
<DocumentProvider value={document}>
- <RendererProvider createRenderer={createEditorRenderer}>
+ <RendererProvider
+ createRenderer={(rendererGpu, source) =>
+ createEditorRenderer(rendererGpu, source)
+ }
+ >
<MaskToolProvider onCreate={() => {}}>
<EditorHeader file={state.file} />
<div className="flex min-h-0 flex-1 flex-col md:flex-row">
diff --git a/src/app/editor/export/index.tsx b/src/app/editor/export/index.tsx
index 7c2ecf9..c1d4ddb 100644
--- a/src/app/editor/export/index.tsx
+++ b/src/app/editor/export/index.tsx
@@ -75,17 +75,16 @@ function FormatSelect({
<Select
aria-label="Format"
value={value.id}
+ options={formats.map((format) => ({
+ value: format.id,
+ label: format.label,
+ }))}
className="w-28"
- onChange={(event) =>
- onChange(formats[event.currentTarget.selectedIndex])
- }
- >
- {formats.map((format) => (
- <option key={format.id} value={format.id}>
- {format.label}
- </option>
- ))}
- </Select>
+ onChange={(id) => {
+ const format = formats.find((format) => format.id === id);
+ if (format) onChange(format);
+ }}
+ />
</div>
<p className="text-neutral-500">{value.description}</p>
</div>
diff --git a/src/app/editor/index.tsx b/src/app/editor/index.tsx
index dd7bdaa..d0cd7c9 100644
--- a/src/app/editor/index.tsx
+++ b/src/app/editor/index.tsx
@@ -1,11 +1,12 @@
import type { ReactNode } from "react";
import type { Workspace } from "@/app/workspace";
+import { BrushProvider } from "@/components/editor/brush-tool";
import { RendererProvider } from "@/components/editor/pipeline";
import { DocumentProvider, useDocument } from "@/components/editor/session";
import Button from "@/components/ui/button";
import type { Mask } from "@/core/document";
import { findLayer, locateLayer } from "@/core/document";
-import { BrushProvider } from "@/features/layers/brush-tool";
+import { HealingProvider } from "@/features/heal/mode";
import { addLayer } from "@/features/layers/edits";
import { MaskToolProvider, type Nesting } from "@/features/layers/mask-tool";
import { useShortcuts } from "@/hooks/use-shortcuts";
@@ -105,25 +106,42 @@ function MaskTools({ children }: { children: ReactNode }) {
);
}
+/** Connects feature-owned patch selection to the application-owned tool rail. */
+function HealingTools({ children }: { children: ReactNode }) {
+ const { setTool } = useTool();
+ return (
+ <HealingProvider
+ onEdit={() => {
+ const healing = tools.find((tool) => tool.id === "heal");
+ if (healing) setTool(healing);
+ }}
+ >
+ {children}
+ </HealingProvider>
+ );
+}
+
function DocumentEditor({ file }: { file: string }) {
return (
<ToolProvider>
<MaskTools>
- <BrushProvider>
- <EditorHeader file={file}>
- <HistoryControls />
- <hr
- aria-orientation="vertical"
- className="mx-1 h-4 w-px border-0 bg-neutral-600"
- />
- <ComparisonControl />
- <ExportButton />
- </EditorHeader>
- <div className="flex min-h-0 flex-1 flex-col md:flex-row">
- <ToolRail />
- <ToolView />
- </div>
- </BrushProvider>
+ <HealingTools>
+ <BrushProvider>
+ <EditorHeader file={file}>
+ <HistoryControls />
+ <hr
+ aria-orientation="vertical"
+ className="mx-1 h-4 w-px border-0 bg-neutral-600"
+ />
+ <ComparisonControl />
+ <ExportButton />
+ </EditorHeader>
+ <div className="flex min-h-0 flex-1 flex-col md:flex-row">
+ <ToolRail />
+ <ToolView />
+ </div>
+ </BrushProvider>
+ </HealingTools>
</MaskTools>
</ToolProvider>
);
diff --git a/src/app/editor/layers.ts b/src/app/editor/layers.ts
index 0574049..303b100 100644
--- a/src/app/editor/layers.ts
+++ b/src/app/editor/layers.ts
@@ -56,6 +56,8 @@ export function createLayer(kind: EffectLayer["kind"]): EffectLayer {
return { ...base, kind, name: "Color Mixer", colorMixer: defaultMixer };
case "fill":
return { ...base, kind, name: "Color", fill: { ...defaultFill } };
+ case "heal":
+ return { ...base, kind, name: "Healing", patches: [] };
default:
throw Error("Unknown layer kind.");
}
@@ -97,6 +99,8 @@ function effectNeutral(layer: ProcessingLayer): boolean {
return isNeutral(layer.colorMixer);
case "fill":
return false;
+ case "heal":
+ return layer.patches.length === 0;
case "mask":
return true;
}
diff --git a/src/app/editor/renderer.ts b/src/app/editor/renderer.ts
index 818e99a..0c96472 100644
--- a/src/app/editor/renderer.ts
+++ b/src/app/editor/renderer.ts
@@ -16,6 +16,7 @@ import { adjustments } from "@/features/adjustments/pass";
import { colorMixer } from "@/features/color-mixer/pass";
import { unsharpMask } from "@/features/details/unsharp-mask";
import { fill } from "@/features/fill/pass";
+import { heal } from "@/features/heal/pass";
import { toneCurves } from "@/features/tone-curves/pass";
import { vignette } from "@/features/vignette/pass";
@@ -29,12 +30,17 @@ function composeLayer(
layer: ProcessingLayer,
composition: Composition,
): Branch {
+ const name = `layer/${layer.id}`;
+ composition.retain(name);
// A hidden or transparent layer still shows what its curve receives while it is inspected.
const bypassed = !layer.visible || layer.opacity === 0;
- if (bypassed && layer.id !== composition.inputId) {
+ const inspected =
+ layer.id === composition.inputId ||
+ (layer.kind === "heal" &&
+ layer.patches.some((patch) => patch.id === composition.inputId));
+ if (bypassed && !inspected) {
return { image: below };
}
- const name = `layer/${layer.id}`;
const masks = layer.kind === "mask" ? maskModifiers(layer) : [];
const coverage =
layer.kind === "mask" ? composition.coverage(layer) : undefined;
@@ -75,6 +81,12 @@ function composeLayer(
case "fill":
edited = pipeline(below, [fill(layer.fill, `${name}/fill`)]);
break;
+ case "heal": {
+ const result = heal(below, layer.patches, name, composition);
+ edited = result.image;
+ input = result.input;
+ break;
+ }
}
// Child masks of a mask shape its coverage; every other child processes the image.
const effects =
@@ -82,18 +94,16 @@ function composeLayer(
? layer.children.filter((child) => child.kind !== "mask")
: layer.children;
const children = composeLayers(edited, effects, composition);
- return {
- image: mixAdjustment(
- name,
- below,
- children.image,
- bypassed ? 0 : layer.opacity,
- layer.kind === "mask" ? layer.mask : undefined,
- masks,
- coverage,
- ),
- input: input ?? children.input,
- };
+ const image = mixAdjustment(
+ name,
+ below,
+ children.image,
+ bypassed ? 0 : layer.opacity,
+ layer.kind === "mask" ? layer.mask : undefined,
+ masks,
+ coverage,
+ );
+ return { image, input: input ?? children.input };
}
function composeLayers(
@@ -127,6 +137,7 @@ export function createEditorRenderer(
(image, scene, composition) => {
const [sourceLayer, ...layers] = scene.layers;
const name = `layer/${sourceLayer.id}`;
+ composition.retain(name);
const children = composeLayers(image, sourceLayer.children, composition);
const composite = composeLayers(children.image, layers, composition);
const adjusted = pipeline(composite.image, [
diff --git a/src/app/editor/tools.tsx b/src/app/editor/tools.tsx
index 5bd3d9e..e2388da 100644
--- a/src/app/editor/tools.tsx
+++ b/src/app/editor/tools.tsx
@@ -6,17 +6,23 @@ import {
useContext,
useState,
} from "react";
+import { useDocument } from "@/components/editor/session";
import { AdjustIcon } from "@/components/icons/adjust";
import { BrushIcon } from "@/components/icons/brush";
import { CropIcon } from "@/components/icons/crop";
import { ExportIcon } from "@/components/icons/export";
+import { HealIcon } from "@/components/icons/heal";
import { LinearGradientIcon } from "@/components/icons/linear-gradient";
import { RadialGradientIcon } from "@/components/icons/radial-gradient";
import { CropEditor } from "@/features/crop/view";
+import { HealOptions } from "@/features/heal/options";
+import { HealOverlay } from "@/features/heal/overlay";
import { BrushOptions } from "@/features/layers/brush-options";
import { BrushOverlay } from "@/features/layers/brush-overlay";
+import { addLayer } from "@/features/layers/edits";
import { GradientOverlay } from "@/features/layers/gradient-overlay";
import { ExportMode } from "./export";
+import { createLayer } from "./layers";
const ToolContext = createContext<{
tool: Tool;
@@ -39,6 +45,17 @@ function RadialCanvas() {
return <GradientOverlay shape="radial" />;
}
+function HealCanvas() {
+ const document = useDocument();
+ const { setTool } = useTool();
+ return (
+ <HealOverlay
+ onCreate={() => addLayer(document, createLayer("heal"))}
+ onDone={() => setTool(adjust)}
+ />
+ );
+}
+
/** No mask canvas: the selected layer is active for its sliders, but not being edited on the image. */
const adjust = {
id: "adjust",
@@ -90,6 +107,15 @@ export const tools = [
group: "edit",
Canvas: RadialCanvas,
},
+ {
+ id: "heal",
+ label: "Healing",
+ key: "h",
+ Icon: HealIcon,
+ group: "edit",
+ Canvas: HealCanvas,
+ Options: HealOptions,
+ },
{
id: "crop",
label: "Crop",
diff --git a/src/components/editor/brush-canvas.tsx b/src/components/editor/brush-canvas.tsx
new file mode 100644
index 0000000..cc7a654
--- /dev/null
+++ b/src/components/editor/brush-canvas.tsx
@@ -0,0 +1,325 @@
+import {
+ type PointerEvent,
+ type ReactNode,
+ useEffect,
+ useId,
+ useRef,
+ useState,
+} from "react";
+import type { BrushStroke, StrokePoint } from "@/core/document";
+import type { Point } from "@/core/image/frame";
+import { useShortcuts } from "@/hooks/use-shortcuts";
+import { useBrushTool } from "./brush-tool";
+import { useDocumentMapping } from "./mapping";
+import { useDocument } from "./session";
+import { useViewport } from "./viewport";
+
+type Stroke = {
+ pointer: number;
+ touch: boolean;
+ /** The last screen position, handed to the viewport when a second finger turns the stroke into a pinch. */
+ client: Point;
+ /** The viewport bounds measured once; pointer capture keeps them valid for the drag. */
+ box: DOMRect | undefined;
+ pending: StrokePoint[];
+ frame?: number;
+};
+
+type PointerLike = {
+ clientX: number;
+ clientY: number;
+ pressure: number;
+ pointerType: string;
+};
+
+/** Shared pressure-aware brush input and cursor. The caller owns the scene edit. */
+export function BrushCanvas({
+ label,
+ hint,
+ erase,
+ feather,
+ onStart,
+ onExtend,
+ onComplete,
+ onFinish,
+ onPickSource,
+ onDone,
+ children,
+}: {
+ label: string;
+ hint?: string;
+ erase: boolean;
+ /** Overrides the shared brush feather for tools that own their stroke softness. */
+ feather?: number;
+ /** Records the first dab and returns whether the stroke started; a declined stroke leaves nothing behind. */
+ onStart: (stroke: BrushStroke) => boolean;
+ onExtend: (points: readonly StrokePoint[]) => void;
+ onComplete?: (signal: AbortSignal) => void | Promise<void>;
+ onFinish?: (committed: boolean) => void;
+ onPickSource?: (point: Point) => void;
+ onDone: () => void;
+ children?: ReactNode;
+}) {
+ const document = useDocument();
+ const brush = useBrushTool();
+ const strokeFeather = feather ?? brush.settings.feather;
+ const camera = useViewport();
+ const mapping = useDocumentMapping();
+ const stroke = useRef<Stroke | null>(null);
+ const completing = useRef<AbortController | null>(null);
+ const [pointer, setPointer] = useState<Point | null>(null);
+ const [pointerVisible, setPointerVisible] = useState(false);
+ const [busy, setBusy] = useState(false);
+ const [error, setError] = useState<string>();
+ const gradient = useId();
+ function point(event: PointerLike, box: DOMRect | undefined): StrokePoint {
+ const [x, y] = mapping.toDocument(event.clientX, event.clientY, box);
+ return [x, y, event.pointerType === "pen" ? event.pressure : 1];
+ }
+ /** Ends the stroke; a cancelled stroke restores the scene. */
+ function finish(commit: boolean) {
+ const current = stroke.current;
+ if (!current) {
+ if (!commit && completing.current) {
+ completing.current.abort();
+ completing.current = null;
+ document.history.cancel();
+ setBusy(false);
+ }
+ return;
+ }
+ stroke.current = null;
+ onFinish?.(commit);
+ if (current.frame !== undefined) {
+ cancelAnimationFrame(current.frame);
+ }
+ if (commit) {
+ flush(current);
+ if (!onComplete) {
+ document.history.commit();
+ return;
+ }
+ setBusy(true);
+ const controller = new AbortController();
+ completing.current = controller;
+ const unsubscribe = document.history.status.subscribe(({ editing }) => {
+ if (!editing) {
+ controller.abort();
+ }
+ });
+ void Promise.resolve()
+ .then(() => onComplete(controller.signal))
+ .then(() => {
+ if (!controller.signal.aborted) {
+ document.history.commit();
+ }
+ })
+ .catch((error: unknown) => {
+ if (!controller.signal.aborted) {
+ document.history.cancel();
+ setError(String(error));
+ }
+ })
+ .finally(() => {
+ unsubscribe();
+ if (completing.current === controller) {
+ completing.current = null;
+ setBusy(false);
+ }
+ });
+ return;
+ }
+ document.history.cancel();
+ }
+ function flush(current: Stroke) {
+ current.frame = undefined;
+ if (!current.pending.length) {
+ return;
+ }
+ const points = current.pending;
+ current.pending = [];
+ try {
+ onExtend(points);
+ } catch (error) {
+ document.history.cancel();
+ stroke.current = null;
+ setError(String(error));
+ }
+ }
+ useEffect(
+ () => () => {
+ finish(false);
+ brush.setPreview(false);
+ },
+ [],
+ );
+ useEffect(() => {
+ if (!brush.preview || pointer) return;
+ const bounds = camera.ref.current?.getBoundingClientRect();
+ if (bounds) setPointer([bounds.width / 2, bounds.height / 2]);
+ }, [brush.preview, pointer, camera.ref]);
+ useShortcuts({
+ escape: () =>
+ stroke.current || completing.current ? finish(false) : onDone(),
+ enter: () => {
+ if (!stroke.current && !completing.current) {
+ onDone();
+ }
+ },
+ "[": () => brush.update({ size: Math.round(brush.settings.size / 1.25) }),
+ "]": () => brush.update({ size: Math.round(brush.settings.size * 1.25) }),
+ });
+ function start(event: PointerEvent<HTMLDivElement>) {
+ const current = stroke.current;
+ if (current?.touch && event.pointerType === "touch" && !event.isPrimary) {
+ // A second finger means a pinch: drop the stroke and let the viewport take both fingers.
+ finish(false);
+ event.currentTarget.releasePointerCapture(current.pointer);
+ camera.adopt(current.pointer, current.client);
+ return;
+ }
+ if (event.button !== 0 || !event.isPrimary || camera.panMode) {
+ return;
+ }
+ if (busy) {
+ return;
+ }
+ const box = camera.ref.current?.getBoundingClientRect();
+ const first = point(event, box);
+ if (event.altKey && onPickSource) {
+ onPickSource([first[0], first[1]]);
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
+ setError(undefined);
+ const opened = document.history.begin();
+ const started = onStart({
+ mode: erase ? "erase" : "paint",
+ size: brush.settings.size,
+ feather: strokeFeather,
+ flow: brush.settings.flow,
+ points: [first],
+ });
+ if (!started) {
+ if (opened) document.history.cancel();
+ return;
+ }
+ stroke.current = {
+ pointer: event.pointerId,
+ touch: event.pointerType === "touch",
+ client: [event.clientX, event.clientY],
+ box,
+ pending: [],
+ };
+ // The viewport below would otherwise capture the pointer to pan.
+ event.preventDefault();
+ event.stopPropagation();
+ event.currentTarget.setPointerCapture(event.pointerId);
+ if (window.document.activeElement instanceof HTMLElement) {
+ window.document.activeElement.blur();
+ }
+ }
+ function move(event: PointerEvent<HTMLDivElement>) {
+ const bounds = event.currentTarget.getBoundingClientRect();
+ const overHandle =
+ event.target instanceof Element &&
+ event.target.closest("[data-hide-brush-cursor]");
+ setPointerVisible(!overHandle);
+ if (!overHandle) {
+ setPointer([event.clientX - bounds.left, event.clientY - bounds.top]);
+ }
+ const current = stroke.current;
+ if (!current || current.pointer !== event.pointerId) {
+ return;
+ }
+ current.client = [event.clientX, event.clientY];
+ const native = event.nativeEvent;
+ const events = native.getCoalescedEvents?.() ?? [];
+ for (const item of events.length ? events : [native]) {
+ current.pending.push(point(item, current.box));
+ }
+ // One edit per frame keeps the queue from outrunning the display.
+ current.frame ??= requestAnimationFrame(() => flush(current));
+ }
+ function end(event: PointerEvent<HTMLDivElement>) {
+ const current = stroke.current;
+ if (!current || current.pointer !== event.pointerId) {
+ return;
+ }
+ finish(true);
+ event.currentTarget.releasePointerCapture(event.pointerId);
+ }
+ const radius = brush.settings.size / 2 / mapping.pixelsPerViewportPixel;
+ const color = erase ? "black" : "white";
+ const dash = erase ? "4 3" : undefined;
+ const status = error ?? (busy ? "Finishing stroke…" : hint);
+ const cursor = pointerVisible || brush.preview ? pointer : null;
+ return (
+ <div
+ role="application"
+ aria-label={label}
+ className="absolute inset-0 cursor-none touch-none data-[pan=true]:pointer-events-none"
+ data-pan={camera.panMode}
+ onDoubleClick={(event) => event.stopPropagation()}
+ onPointerDown={start}
+ onPointerMove={move}
+ onPointerUp={end}
+ onPointerLeave={() => setPointerVisible(false)}
+ onPointerCancel={() => finish(false)}
+ onLostPointerCapture={() => {
+ if (stroke.current) {
+ finish(false);
+ }
+ }}
+ >
+ {children}
+ {cursor && !camera.panMode && (
+ <svg
+ aria-hidden="true"
+ data-brush-cursor="true"
+ data-preview={brush.preview}
+ className="pointer-events-none absolute inset-0 size-full overflow-visible"
+ >
+ <defs>
+ {/* The fill previews the dab: solid inside the feather, fading to the edge. */}
+ <radialGradient id={gradient} cx="0.5" cy="0.5" r="0.5">
+ <stop offset="0" stopColor={color} stopOpacity="0.3" />
+ <stop
+ offset={1 - strokeFeather}
+ stopColor={color}
+ stopOpacity="0.3"
+ />
+ <stop offset="1" stopColor={color} stopOpacity="0" />
+ </radialGradient>
+ </defs>
+ <circle
+ cx={cursor[0]}
+ cy={cursor[1]}
+ r={Math.max(2, radius)}
+ fill={`url(#${gradient})`}
+ stroke="white"
+ strokeOpacity="0.9"
+ strokeDasharray={dash}
+ />
+ <circle
+ cx={cursor[0]}
+ cy={cursor[1]}
+ r={Math.max(2, radius)}
+ fill="none"
+ stroke="black"
+ strokeOpacity="0.5"
+ strokeWidth="3"
+ strokeDasharray={dash}
+ style={{ paintOrder: "stroke" }}
+ />
+ </svg>
+ )}
+ {status && (
+ <p className="pointer-events-none absolute bottom-3 left-3 rounded-full bg-neutral-800/80 px-3 py-1.5 text-white backdrop-blur-sm">
+ {status}
+ </p>
+ )}
+ </div>
+ );
+}
diff --git a/src/features/layers/brush-tool.tsx b/src/components/editor/brush-tool.tsx
similarity index 73%
rename from src/features/layers/brush-tool.tsx
rename to src/components/editor/brush-tool.tsx
index 1e68f87..5791fd4 100644
--- a/src/features/layers/brush-tool.tsx
+++ b/src/components/editor/brush-tool.tsx
@@ -5,8 +5,8 @@ import {
useEffect,
useState,
} from "react";
-import { useDocument, useScene } from "@/components/editor/session";
import { clamp } from "@/lib/math";
+import { useDocument, useScene } from "./session";
export type BrushSettings = {
/** Diameter in source pixels. */
@@ -23,6 +23,9 @@ const BrushTool = createContext<{
erase: boolean;
/** The largest useful diameter: half the image's long edge. */
maxSize: number;
+ /** Keeps the brush cursor visible while a setting that shapes it is edited. */
+ preview: boolean;
+ setPreview: (preview: boolean) => void;
update: (change: Partial<BrushSettings>) => void;
} | null>(null);
@@ -39,14 +42,21 @@ export function BrushProvider({ children }: { children: ReactNode }) {
const document = useDocument();
const sourceId = useScene((scene) => scene.layers[0].source);
const size = document.resources.get(sourceId).image.size;
- const maxSize = Math.max(1, Math.round(Math.max(size[0], size[1]) / 2));
+ const longest = Math.max(size[0], size[1]);
+ const maxSize = Math.max(1, Math.round(longest / 2));
+ // 3% of the image in steps of 5, never under 10 px unless the image itself is that small.
+ const initialSize = Math.min(
+ maxSize,
+ Math.max(10, Math.round((longest * 0.03) / 5) * 5),
+ );
const [settings, setSettings] = useState<BrushSettings>({
- size: Math.max(1, Math.round(maxSize / 10)),
+ size: initialSize,
feather: 0.5,
flow: 1,
erase: false,
});
const [alt, setAlt] = useState(false);
+ const [preview, setPreview] = useState(false);
useEffect(() => {
const controller = new AbortController();
const { signal } = controller;
@@ -65,7 +75,14 @@ export function BrushProvider({ children }: { children: ReactNode }) {
}
return (
<BrushTool
- value={{ settings, erase: settings.erase !== alt, maxSize, update }}
+ value={{
+ settings,
+ erase: settings.erase !== alt,
+ maxSize,
+ preview,
+ setPreview,
+ update,
+ }}
>
{children}
</BrushTool>
diff --git a/src/components/editor/coverage-thumbnail.tsx b/src/components/editor/coverage-thumbnail.tsx
new file mode 100644
index 0000000..be2147d
--- /dev/null
+++ b/src/components/editor/coverage-thumbnail.tsx
@@ -0,0 +1,72 @@
+import { type ReactNode, useEffect, useRef, useState } from "react";
+import { useGpu } from "vgpu-react";
+import { type CoverageRegion, renderCoverage } from "@/core/renderer";
+import { useRenderer } from "./pipeline";
+import { useDocument } from "./session";
+
+/**
+ * The renderer's coverage raster for one id, drawn into a small canvas after each committed change.
+ * `version` is the scene content the raster follows; a change redraws once its gesture ends.
+ * Without a raster, such as a mask that paints nothing yet, the fallback shows instead.
+ */
+export function CoverageThumbnail({
+ id,
+ version,
+ region,
+ label,
+ fallback,
+}: {
+ id: string;
+ version: unknown;
+ region?: CoverageRegion;
+ label: string;
+ fallback: ReactNode;
+}) {
+ const gpu = useGpu();
+ const document = useDocument();
+ const renderer = useRenderer();
+ const canvas = useRef<HTMLCanvasElement>(null);
+ const [raster, setRaster] = useState(false);
+ const [ready, setReady] = useState<unknown>();
+ useEffect(() => {
+ function check() {
+ const coverage = renderer.coverage(id);
+ setRaster(coverage !== undefined);
+ if (coverage && !document.history.status.getState().editing) {
+ setReady(version);
+ }
+ }
+ // A gesture's end renders nothing new when the scene already rendered in full.
+ const unsubscribe = document.history.status.subscribe(check);
+ const detach = renderer.subscribe(check);
+ return () => {
+ unsubscribe();
+ detach();
+ };
+ }, [renderer, document, id, version]);
+ useEffect(() => {
+ const coverage = ready !== undefined && renderer.coverage(id);
+ if (!coverage) return;
+ let active = true;
+ renderCoverage(gpu, coverage, [64, 64], region)
+ .then((bitmap) => {
+ const context = canvas.current?.getContext("2d");
+ if (active && context) context.drawImage(bitmap, 0, 0);
+ bitmap.close();
+ })
+ .catch(() => {});
+ return () => {
+ active = false;
+ };
+ }, [gpu, renderer, id, ready, region]);
+ if (!raster) return fallback;
+ return (
+ <canvas
+ ref={canvas}
+ width={64}
+ height={64}
+ aria-label={label}
+ className="size-8 shrink-0 rounded-sm border border-neutral-600 bg-neutral-950"
+ />
+ );
+}
diff --git a/src/components/editor/toolbar-density.tsx b/src/components/editor/toolbar-density.tsx
new file mode 100644
index 0000000..c789242
--- /dev/null
+++ b/src/components/editor/toolbar-density.tsx
@@ -0,0 +1,14 @@
+import { createContext, useContext } from "react";
+
+/** How much room the bar has: sliders with bars, fields only, or a column inside the overflow menu. */
+export type BarDensity = "full" | "compact" | "menu";
+export const Density = createContext<BarDensity>("full");
+export function useBarDensity() {
+ return useContext(Density);
+}
+export function barSlider(density: BarDensity) {
+ if (density === "menu") {
+ return "panel";
+ }
+ return density === "full" ? "toolbar" : "compact";
+}
diff --git a/src/components/editor/viewport.tsx b/src/components/editor/viewport.tsx
index 9f34644..5d4c4ca 100644
--- a/src/components/editor/viewport.tsx
+++ b/src/components/editor/viewport.tsx
@@ -69,10 +69,10 @@ export function EditorViewport({
tools?: ReactNode;
}) {
const { camera } = useEditorSession();
- const viewport = usePanZoom(camera, size, { constrain });
+ const viewport = usePanZoom(camera, size, { constrain, padding: 24 });
return (
<section
- className="@container relative min-h-0 min-w-0 flex-1 overflow-hidden p-6"
+ className="@container relative min-h-0 min-w-0 flex-1 overflow-hidden"
aria-label="Image canvas"
>
<Viewport value={viewport}>
@@ -82,9 +82,7 @@ export function EditorViewport({
data-pan-mode={viewport.panMode}
className="relative size-full cursor-grab touch-none active:cursor-grabbing data-[pan-mode=true]:[&_*]:cursor-grab! data-[pan-mode=true]:active:[&_*]:cursor-grabbing!"
>
- <Canvas className="absolute -inset-6 size-[calc(100%+3rem)]">
- {children}
- </Canvas>
+ <Canvas className="absolute inset-0 size-full">{children}</Canvas>
{tools}
</div>
{overlay}
diff --git a/src/components/icons/heal.tsx b/src/components/icons/heal.tsx
new file mode 100644
index 0000000..418114a
--- /dev/null
+++ b/src/components/icons/heal.tsx
@@ -0,0 +1,10 @@
+import { Icon, type IconProps } from "./icon";
+
+export function HealIcon(props: IconProps) {
+ return (
+ <Icon {...props}>
+ <path d="m7 21-4-4a2.8 2.8 0 0 1 0-4L12.5 3.5a2.8 2.8 0 0 1 4 0l4 4a2.8 2.8 0 0 1 0 4L11 21H7Z" />
+ <path d="m5 11 9 9M7 21h14" />
+ </Icon>
+ );
+}
diff --git a/src/components/ui/field.tsx b/src/components/ui/field.tsx
index 73514b6..dc2ba33 100644
--- a/src/components/ui/field.tsx
+++ b/src/components/ui/field.tsx
@@ -5,17 +5,17 @@ import type { ComponentProps } from "react";
type FieldProps = ComponentProps<"span"> & {
/**
* "box" always shows the chrome; "text" reads as plain text until focused, then sinks a little;
- * "pill" sinks the same way at the height and roundness of the bar over the canvas.
+ * "pill" keeps the bar's control height without adding permanent chrome.
*/
variant?: "box" | "text" | "pill";
};
-const chrome = cva("focus-within:ring-1 focus-within:ring-white/10", {
+const chrome = cva("", {
variants: {
variant: {
- box: "rounded border border-black bg-neutral-900 px-1 py-0.5 shadow-groove",
- text: "rounded border border-transparent px-1 py-0.5 focus-within:bg-black/25 focus-within:shadow-groove",
- pill: "h-7 rounded-full px-2.5 focus-within:bg-black/25 focus-within:shadow-groove pointer-coarse:h-9",
+ box: "rounded border border-black bg-neutral-900 px-1 py-0.5 shadow-groove focus-within:ring-1 focus-within:ring-white/10",
+ text: "rounded-[2px] px-0.5 py-0.5 focus-within:bg-black/25",
+ pill: "h-7 rounded-[2px] px-0.5 focus-within:bg-black/25 pointer-coarse:h-9",
},
},
});
diff --git a/src/components/ui/menu-surface.tsx b/src/components/ui/menu-surface.tsx
new file mode 100644
index 0000000..1d44cb5
--- /dev/null
+++ b/src/components/ui/menu-surface.tsx
@@ -0,0 +1,74 @@
+import { type AriaRole, type ReactNode, useId, useRef, useState } from "react";
+
+function focusChoice(surface: HTMLElement) {
+ const selected = surface.querySelector<HTMLElement>('[aria-selected="true"]');
+ const first = surface.querySelector<HTMLElement>("button:not(:disabled)");
+ (selected ?? first)?.focus();
+}
+
+function moveFocus(menu: HTMLElement, direction: number) {
+ const items = [
+ ...menu.querySelectorAll<HTMLButtonElement>("button:not(:disabled)"),
+ ];
+ const current = items.indexOf(document.activeElement as HTMLButtonElement);
+ const next =
+ current < 0 ? 0 : (current + direction + items.length) % items.length;
+ items[next]?.focus();
+}
+
+/** Anchored elevated surface shared by command menus and value selectors. */
+export function MenuSurface({
+ trigger,
+ children,
+ role = "menu",
+}: {
+ trigger: (id: string, open: boolean) => ReactNode;
+ children: ReactNode;
+ role?: AriaRole;
+}) {
+ const id = useId();
+ const popover = useRef<HTMLDivElement>(null);
+ const [open, setOpen] = useState(false);
+ return (
+ <>
+ {trigger(id, open)}
+ <div
+ id={id}
+ ref={popover}
+ popover="auto"
+ onBeforeToggle={(event) => setOpen(event.newState === "open")}
+ onToggle={(event) => {
+ if (event.newState === "open") {
+ requestAnimationFrame(() => {
+ const surface = popover.current;
+ if (surface?.matches(":popover-open")) focusChoice(surface);
+ });
+ }
+ }}
+ className="fixed inset-auto z-50 m-0 mt-1 max-h-[calc(100dvh-1rem)] min-w-40 overflow-y-auto rounded-lg border border-black/60 bg-neutral-800 p-1 text-neutral-200 shadow-float [position-area:bottom_span-left] [position-try-fallbacks:flip-block] [&_button]:w-full [&_button]:rounded-md [&_button]:px-2.5 [&_button]:py-1.5 [&_button]:text-left [&_button:disabled]:text-neutral-600 [&_button:disabled:hover]:bg-transparent [&_button:hover]:bg-white/8"
+ >
+ {/* biome-ignore lint/a11y/noStaticElementInteractions: delegated interaction serves the dynamic menu or listbox role. */}
+ <div
+ role={role}
+ onClick={(event) => {
+ const target = event.target as Element;
+ if (target.closest('button[type="submit"]')) {
+ popover.current?.hidePopover();
+ }
+ }}
+ onKeyDown={(event) => {
+ if (event.key === "ArrowDown" || event.key === "ArrowUp") {
+ event.preventDefault();
+ moveFocus(
+ event.currentTarget,
+ event.key === "ArrowDown" ? 1 : -1,
+ );
+ }
+ }}
+ >
+ {open && children}
+ </div>
+ </div>
+ </>
+ );
+}
diff --git a/src/components/ui/menu.tsx b/src/components/ui/menu.tsx
index dab6c4b..d3cdc04 100644
--- a/src/components/ui/menu.tsx
+++ b/src/components/ui/menu.tsx
@@ -1,5 +1,6 @@
import { cva } from "class-variance-authority";
-import { type ReactNode, useId, useRef, useState } from "react";
+import type { ReactNode } from "react";
+import { MenuSurface } from "./menu-surface";
const trigger = cva(
"grid shrink-0 place-items-center text-neutral-400 hover:text-neutral-100",
@@ -28,36 +29,24 @@ export function Menu({
icon: ReactNode;
variant?: "row" | "pill";
}) {
- const id = useId();
- const popover = useRef<HTMLDivElement>(null);
- const [open, setOpen] = useState(false);
return (
- <>
- <button
- type="button"
- aria-label={label}
- title={label}
- popoverTarget={id}
- className={trigger({ variant })}
- >
- {icon}
- </button>
- <div
- id={id}
- ref={popover}
- popover="auto"
- onBeforeToggle={(event) => setOpen(event.newState === "open")}
- className="fixed inset-auto z-50 m-0 mt-1 max-h-[calc(100dvh-1rem)] min-w-40 overflow-y-auto rounded-lg border border-black/60 bg-neutral-800 p-1 text-neutral-200 shadow-float [position-area:bottom_span-left] [position-try-fallbacks:flip-block] [&_button]:block [&_button]:w-full [&_button]:rounded-md [&_button]:px-2.5 [&_button]:py-1.5 [&_button]:text-left [&_button:disabled]:text-neutral-600 [&_button:disabled:hover]:bg-transparent [&_button:hover]:bg-white/8"
- >
- <form
- onSubmit={(event) => {
- event.preventDefault();
- popover.current?.hidePopover();
- }}
+ <MenuSurface
+ trigger={(id, open) => (
+ <button
+ type="button"
+ aria-label={label}
+ aria-expanded={open}
+ aria-haspopup="menu"
+ aria-controls={id}
+ title={label}
+ popoverTarget={id}
+ className={trigger({ variant })}
>
- {open && children}
- </form>
- </div>
- </>
+ {icon}
+ </button>
+ )}
+ >
+ {children}
+ </MenuSurface>
);
}
diff --git a/src/components/ui/panel-list.tsx b/src/components/ui/panel-list.tsx
new file mode 100644
index 0000000..d773539
--- /dev/null
+++ b/src/components/ui/panel-list.tsx
@@ -0,0 +1,24 @@
+import { cn } from "cn";
+import { type ComponentProps, forwardRef } from "react";
+
+/** Shared row chrome for selectable collections in editor panels. */
+export const PanelListItem = forwardRef<
+ HTMLDivElement,
+ ComponentProps<"div"> & { selected?: boolean; muted?: boolean }
+>(function PanelListItem(
+ { selected = false, muted = false, className, ...props },
+ ref,
+) {
+ return (
+ <div
+ {...props}
+ ref={ref}
+ data-selected={selected}
+ data-muted={muted}
+ className={cn(
+ "group relative flex h-10 items-center border-b border-black/50 text-neutral-300 pointer-coarse:h-12 data-[selected=false]:hover:bg-white/5 data-[selected=true]:bg-neutral-700 data-[muted=true]:text-neutral-500",
+ className,
+ )}
+ />
+ );
+});
diff --git a/src/components/ui/scrub-input.tsx b/src/components/ui/scrub-input.tsx
index df98867..38b57d8 100644
--- a/src/components/ui/scrub-input.tsx
+++ b/src/components/ui/scrub-input.tsx
@@ -16,9 +16,12 @@ type ScrubInputProps = Omit<
label?: string;
value: number;
onChange: (value: number) => void;
+ onEditingChange?: (editing: boolean) => void;
min: number;
max: number;
step?: number;
+ /** Minimum width of the numeric part in tabular characters. */
+ minChars?: number;
/** Follows the number after a hair of space, in a quieter color. */
unit?: string;
/**
@@ -48,9 +51,11 @@ export function ScrubInput({
label,
value,
onChange,
+ onEditingChange,
min,
max,
step = 1,
+ minChars = 1,
unit,
variant = "box",
className,
@@ -59,11 +64,8 @@ export function ScrubInput({
const decimals = `${step}`.split(".")[1]?.length ?? 0;
const [draft, setDraft] = useState<string>();
const drag = useRef<Drag | undefined>(undefined);
- // Room for the longest value in range, so the field neither clips nor floats in empty space.
- const chars = Math.max(
- 2,
- ...[min, max].map((bound) => bound.toFixed(decimals).length),
- );
+ const shown = draft ?? value.toFixed(decimals);
+ const chars = Math.max(1, shown.length);
const clamp = (next: number) =>
Math.min(max, Math.max(min, Number(next.toFixed(decimals))));
@@ -71,6 +73,7 @@ export function ScrubInput({
const commit = () => {
if (draft && !Number.isNaN(Number(draft))) onChange(clamp(Number(draft)));
setDraft(undefined);
+ onEditingChange?.(false);
};
const startScrub = (event: PointerEvent<HTMLInputElement>) => {
@@ -78,6 +81,7 @@ export function ScrubInput({
event.preventDefault();
event.currentTarget.setPointerCapture(event.pointerId);
drag.current = { dx: 0, lastX: event.clientX, value, moved: false };
+ onEditingChange?.(true);
};
const scrub = (event: PointerEvent<HTMLInputElement>) => {
@@ -103,6 +107,7 @@ export function ScrubInput({
if (!state) return;
if (state.moved) {
document.exitPointerLock();
+ onEditingChange?.(false);
return;
}
// A press that never moved is a click: enter text editing.
@@ -112,7 +117,7 @@ export function ScrubInput({
return (
<label
- className={cn("flex items-center gap-1.5 text-neutral-400", className)}
+ className={cn("flex items-center gap-1 text-neutral-400", className)}
>
{label}
<Field
@@ -125,18 +130,27 @@ export function ScrubInput({
<input
{...props}
className={input({ variant })}
- style={variant === "box" ? undefined : { width: `${chars}ch` }}
+ style={
+ variant === "box"
+ ? undefined
+ : { width: `${Math.max(chars, minChars)}ch` }
+ }
inputMode="decimal"
onBlur={commit}
onChange={(event) => setDraft(event.currentTarget.value)}
+ onFocus={() => onEditingChange?.(true)}
onKeyDown={(event) =>
event.key === "Enter" && event.currentTarget.blur()
}
onPointerDown={startScrub}
onPointerMove={scrub}
+ onPointerCancel={() => {
+ drag.current = undefined;
+ onEditingChange?.(false);
+ }}
onPointerUp={endScrub}
type="text"
- value={draft ?? value.toFixed(decimals)}
+ value={shown}
/>
{unit && <span className="text-neutral-500">{unit}</span>}
</Field>
diff --git a/src/components/ui/select.tsx b/src/components/ui/select.tsx
index 424292f..79ad1ae 100644
--- a/src/components/ui/select.tsx
+++ b/src/components/ui/select.tsx
@@ -1,49 +1,95 @@
import { cva } from "class-variance-authority";
import { cn } from "cn";
-import type { ComponentProps } from "react";
import { Icon } from "@/components/icons/icon";
+import { MenuSurface } from "./menu-surface";
-type SelectProps = ComponentProps<"select"> & {
- /** "field" sinks into panel chrome; "pill" sits in the bar over the canvas. */
+export type SelectOption<T extends string = string> = {
+ value: T;
+ label: string;
+ disabled?: boolean;
+};
+
+type SelectProps<T extends string> = {
+ "aria-label": string;
+ value?: T;
+ options: readonly SelectOption<T>[];
+ onChange: (value: T) => void;
+ placeholder?: string;
+ title?: string;
+ className?: string;
+ /** "field" sits in a panel; "pill" sits in the bar over the canvas. */
variant?: "field" | "pill";
};
-const frame = cva("relative inline-flex items-center", {
- variants: {
- variant: {
- field:
- "rounded border border-black bg-neutral-900 shadow-groove focus-within:ring-1 focus-within:ring-white/10",
- pill: "h-7 rounded-full bg-white/10 hover:bg-white/15 pointer-coarse:h-9",
- },
- },
-});
const control = cva(
- "w-full cursor-pointer appearance-none bg-transparent outline-none [color-scheme:dark]",
+ "inline-flex min-w-0 shrink-0 cursor-pointer items-center justify-between gap-2 text-neutral-200 outline-none focus-visible:ring-1 focus-visible:ring-white/20",
{
variants: {
variant: {
- field: "py-0.5 pr-6 pl-1 text-neutral-100",
- pill: "h-full pr-7 pl-3 text-neutral-200",
+ field:
+ "rounded border border-black bg-neutral-900 py-0.5 pr-2 pl-1 shadow-groove",
+ pill: "h-7 rounded-full bg-white/10 px-3 hover:bg-white/15 pointer-coarse:h-9",
},
},
},
);
-/** A native select with the app's chrome and chevron; options come as children. */
-export function Select({
- variant = "field",
+/** A value selector using the same elevated option surface as row and toolbar menus. */
+export function Select<T extends string>({
+ "aria-label": label,
+ value,
+ options,
+ onChange,
+ placeholder,
+ title,
className,
- children,
- ...props
-}: SelectProps) {
+ variant = "field",
+}: SelectProps<T>) {
+ const selectedIndex = options.findIndex((option) => option.value === value);
+ const selected = options[selectedIndex];
return (
- <span className={cn(frame({ variant }), className)}>
- <select {...props} className={control({ variant })}>
- {children}
- </select>
- <Icon className="pointer-events-none absolute top-1/2 right-2 size-3 -translate-y-1/2 text-neutral-400">
- <path d="m6 9 6 6 6-6" />
- </Icon>
- </span>
+ <MenuSurface
+ role="listbox"
+ trigger={(id, open) => (
+ <button
+ type="button"
+ role="combobox"
+ aria-label={label}
+ aria-controls={id}
+ aria-expanded={open}
+ aria-haspopup="listbox"
+ title={title}
+ popoverTarget={id}
+ className={cn(control({ variant }), className)}
+ >
+ <span className="truncate">{selected?.label ?? placeholder}</span>
+ <Icon className="size-3 shrink-0 text-neutral-400">
+ <path d="m6 9 6 6 6-6" />
+ </Icon>
+ </button>
+ )}
+ >
+ {options.map((option, index) => {
+ const active = index === selectedIndex;
+ return (
+ <button
+ key={`${option.value}-${index}`}
+ type="submit"
+ role="option"
+ aria-selected={active}
+ disabled={option.disabled}
+ onClick={() => onChange(option.value)}
+ className="flex items-center justify-between gap-4 aria-selected:bg-neutral-700"
+ >
+ {option.label}
+ {active && (
+ <Icon className="size-3.5 shrink-0 text-neutral-300">
+ <path d="m5 12 4 4L19 6" />
+ </Icon>
+ )}
+ </button>
+ );
+ })}
+ </MenuSurface>
);
}
diff --git a/src/components/ui/slider.tsx b/src/components/ui/slider.tsx
index 3c22f8c..8137652 100644
--- a/src/components/ui/slider.tsx
+++ b/src/components/ui/slider.tsx
@@ -5,6 +5,8 @@ type SliderProps = {
label: string;
value: number;
onChange: (value: number) => void;
+ /** Reports interaction with either the range or its numeric field. */
+ onEditingChange?: (editing: boolean) => void;
min: number;
max: number;
step?: number;
@@ -13,12 +15,14 @@ type SliderProps = {
/** CSS color stops painting the bar left to right, e.g. ["#46f", "#fc3"]. */
stops?: string[];
unit?: string;
+ /** Minimum width of the numeric value in tabular characters. */
+ valueWidth?: number;
/** "panel" stacks the bar under its label; "toolbar" keeps one row; "compact" is that row without the bar. */
variant?: "panel" | "toolbar" | "compact";
};
// In the bar, the label is inset like the text of the pills around it.
-const root = cva("items-center text-neutral-400", {
+const root = cva("items-center whitespace-nowrap text-neutral-400", {
variants: {
variant: {
panel: "grid grid-cols-[1fr_auto] gap-y-0.5",
@@ -50,12 +54,14 @@ export function Slider({
label,
value,
onChange,
+ onEditingChange,
min,
max,
step = 1,
defaultValue,
stops,
unit,
+ valueWidth,
variant = "panel",
}: SliderProps) {
const gradient = stops && {
@@ -72,7 +78,9 @@ export function Slider({
max={max}
min={min}
onChange={onChange}
+ onEditingChange={onEditingChange}
step={step}
+ minChars={valueWidth}
unit={unit}
value={value}
variant={variant === "panel" ? "text" : "pill"}
@@ -86,10 +94,15 @@ export function Slider({
min={min}
onChange={(event) => onChange(event.currentTarget.valueAsNumber)}
onDoubleClick={reset}
+ onBlur={() => onEditingChange?.(false)}
+ onFocus={() => onEditingChange?.(true)}
onKeyDown={(event) =>
event.key === "Enter" && event.currentTarget.blur()
}
step={step}
+ onPointerCancel={() => onEditingChange?.(false)}
+ onPointerDown={() => onEditingChange?.(true)}
+ onPointerUp={() => onEditingChange?.(false)}
type="range"
value={value}
/>
diff --git a/src/core/document/brush.ts b/src/core/document/brush.ts
new file mode 100644
index 0000000..1fa1d21
--- /dev/null
+++ b/src/core/document/brush.ts
@@ -0,0 +1,35 @@
+import type { BrushStroke, StrokePoint } from "./scene";
+
+export function validPoints(points: readonly StrokePoint[]) {
+ return (
+ Array.isArray(points) &&
+ points.length > 0 &&
+ points.every(
+ (point) =>
+ Array.isArray(point) &&
+ point.length === 3 &&
+ point.every(Number.isFinite) &&
+ point[2] >= 0 &&
+ point[2] <= 1,
+ )
+ );
+}
+
+export function validateStroke(stroke: BrushStroke) {
+ if (
+ (stroke.mode !== "paint" && stroke.mode !== "erase") ||
+ !Number.isFinite(stroke.size) ||
+ stroke.size <= 0 ||
+ !Number.isFinite(stroke.feather) ||
+ stroke.feather < 0 ||
+ stroke.feather > 1 ||
+ !Number.isFinite(stroke.flow) ||
+ stroke.flow < 0 ||
+ stroke.flow > 1 ||
+ !validPoints(stroke.points)
+ ) {
+ throw Error(
+ "A stroke needs a paint or erase mode, a positive size, feather and flow from 0 to 1, and finite points with pressure from 0 to 1.",
+ );
+ }
+}
diff --git a/src/core/document/index.ts b/src/core/document/index.ts
index 7a2f16a..2b76474 100644
--- a/src/core/document/index.ts
+++ b/src/core/document/index.ts
@@ -16,6 +16,7 @@ export type {
EffectLayer,
Fill,
Gradient,
+ HealPatch,
ImageLayer,
Layer,
LinearGradient,
diff --git a/src/core/document/scene.ts b/src/core/document/scene.ts
index 3d05151..a3fbd09 100644
--- a/src/core/document/scene.ts
+++ b/src/core/document/scene.ts
@@ -92,6 +92,15 @@ export type BrushMask = {
readonly kind: "brush";
readonly strokes: readonly BrushStroke[];
};
+/** One non-destructive repair: a painted shape filled from a donor at `offset` source pixels away. */
+export type HealPatch = {
+ readonly id: string;
+ /** Feather applied after the stroke's dabs have accumulated into one patch shape. */
+ readonly feather: number;
+ readonly stroke: BrushStroke;
+ readonly opacity: number;
+ readonly offset: Point;
+};
export type Mask = Gradient | BrushMask;
export type ProcessingLayer = {
readonly id: string;
@@ -105,6 +114,7 @@ export type ProcessingLayer = {
| { readonly kind: "vignette"; readonly vignette: Vignette }
| { readonly kind: "color-mixer"; readonly colorMixer: ColorMixer }
| { readonly kind: "fill"; readonly fill: Fill }
+ | { readonly kind: "heal"; readonly patches: readonly HealPatch[] }
| {
readonly kind: "mask";
readonly operation: "add" | "subtract";
diff --git a/src/core/renderer/display/coverage.wgsl b/src/core/renderer/display/coverage.wgsl
index f71e5b2..55c1d5f 100644
--- a/src/core/renderer/display/coverage.wgsl
+++ b/src/core/renderer/display/coverage.wgsl
@@ -1,16 +1,24 @@
-// Box-averages a coverage raster into a small gray preview: near black where nothing is covered, white where all is.
+// Box-averages a region of a coverage raster into a small gray preview: near black where nothing is covered, white where all is.
+struct Params {
+ size: vec2f,
+ origin: vec2f,
+ extent: vec2f,
+}
@group(0) @binding(0) var coverage: texture_2d<f32>;
-@group(0) @binding(1) var<uniform> size: vec2f;
+@group(0) @binding(1) var<uniform> params: Params;
@fragment fn fs_main(@location(0) uv: vec2f) -> @location(0) vec4f {
- let source = vec2f(textureDimensions(coverage));
- let block = max(vec2i(ceil(source / size)), vec2i(1));
- let origin = vec2i(floor(uv * source - vec2f(block) * 0.5));
- let limit = vec2i(source) - 1;
+ let limit = vec2i(textureDimensions(coverage)) - 1;
+ let block = max(vec2i(ceil(params.extent / params.size)), vec2i(1));
+ let origin = vec2i(floor(params.origin + uv * params.extent - vec2f(block) * 0.5));
var total = 0.0;
for (var y = 0; y < block.y; y++) {
for (var x = 0; x < block.x; x++) {
- total += textureLoad(coverage, clamp(origin + vec2i(x, y), vec2i(0), limit), 0).r;
+ let texel = origin + vec2i(x, y);
+ // A region reaching past the raster, such as a square around a patch at the edge, shows nothing there.
+ if (all(texel >= vec2i(0)) && all(texel <= limit)) {
+ total += textureLoad(coverage, texel, 0).r;
+ }
}
}
let value = total / f32(block.x * block.y);
diff --git a/src/core/renderer/display/index.ts b/src/core/renderer/display/index.ts
index 77cef69..0e60741 100644
--- a/src/core/renderer/display/index.ts
+++ b/src/core/renderer/display/index.ts
@@ -128,8 +128,15 @@ export function createDisplay(gpu: Gpu) {
const displays = new WeakMap<Gpu, ReturnType<typeof createDisplay>>();
const previews = new WeakMap<Gpu, Effect>();
-/** Draws a coverage raster as a gray preview of `size` and takes its pixels; no texture outlives the call. */
-export async function renderCoverage(gpu: Gpu, coverage: Target, size: Point) {
+export type CoverageRegion = { origin: Point; extent: Point };
+
+/** Draws a coverage raster, or a region of it in raster pixels, as a gray preview of `size` and takes its pixels; no texture outlives the call. */
+export async function renderCoverage(
+ gpu: Gpu,
+ coverage: Target,
+ size: Point,
+ region: CoverageRegion = { origin: [0, 0], extent: coverage.size },
+) {
let preview = previews.get(gpu);
if (!preview) {
preview = effect(gpu, coverageShader);
@@ -139,7 +146,13 @@ export async function renderCoverage(gpu: Gpu, coverage: Target, size: Point) {
const output = surface(gpu, canvas, { size, dpr: 1 });
try {
frame(gpu, (frame) =>
- frame.pass(output, preview.set({ coverage: coverage.color, size })),
+ frame.pass(
+ output,
+ preview.set({
+ coverage: coverage.color,
+ params: { size, origin: region.origin, extent: region.extent },
+ }),
+ ),
);
await gpu.gpu.queue.onSubmittedWorkDone();
return canvas.transferToImageBitmap();
diff --git a/src/core/renderer/graph.ts b/src/core/renderer/graph.ts
index 88f4e05..66aa0fc 100644
--- a/src/core/renderer/graph.ts
+++ b/src/core/renderer/graph.ts
@@ -51,7 +51,8 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {
let passes: string[] = [];
let disposed = false;
function prepare(node: RenderNode) {
- let pass = effects.get(node.name);
+ const instance = node.instance ?? node.name;
+ let pass = effects.get(instance);
if (!pass) {
const bindings = Object.fromEntries(
Object.entries(node.samplers ?? {}).map(([name, descriptor]) => [
@@ -67,7 +68,7 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {
}),
buffers: new Map(),
};
- effects.set(node.name, pass);
+ effects.set(instance, pass);
}
if (pass.shader !== node.shader) {
throw Error(`Render node ${node.name} changed shader; use a new name.`);
@@ -183,6 +184,7 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {
inspect() {
return {
passes: [...passes],
+ effects: effects.size,
textures: pool.map(({ size, format }) => ({
size: [...size],
format,
diff --git a/src/core/renderer/index.ts b/src/core/renderer/index.ts
index 465315a..12741f5 100644
--- a/src/core/renderer/index.ts
+++ b/src/core/renderer/index.ts
@@ -1,10 +1,5 @@
import type { Gpu, Target, Timer } from "vgpu";
-import {
- type Layer,
- type MaskLayer,
- type Scene,
- walkLayers,
-} from "@/core/document";
+import type { BrushStroke, Layer, MaskLayer, Scene } from "@/core/document";
import type { ImageSource, WhiteBalance } from "@/core/image";
import { createRenderGraph } from "./graph";
import { createMaskRaster } from "./mask";
@@ -14,6 +9,7 @@ import { createProxy } from "./proxy";
export { maskInput, mixAdjustment } from "./blend";
export {
type Clipping,
+ type CoverageRegion,
createDisplay,
type MaskOverlay,
renderBitmap,
@@ -39,8 +35,11 @@ export { createRenderGraph };
export type Composition = {
inputId?: string;
+ /** Keep one stable composition instance and its render-graph resources reusable. */
+ retain: (id: string) => void;
/** Rasterized coverage of a mask that paints with brushes, prepared before composition. */
coverage: (layer: MaskLayer) => RenderInput | undefined;
+ brush: (id: string, strokes: readonly BrushStroke[]) => RenderInput;
};
/** App composition describes requested outputs; the engine owns their storage. */
@@ -104,13 +103,7 @@ export function createRenderer(
) {
return;
}
- const active = new Set(walkLayers(scene.layers).map((layer) => layer.id));
- for (const id of instances) {
- if (!active.has(id)) {
- graph.release(`layer/${id}/`);
- }
- }
- instances = active;
+ const active = new Set<string>();
const developed = raw?.render() ?? source;
// A mask updates its rasters with those of the masks inside it, which only shape its coverage.
function prepare(layer: Layer, parent?: Layer) {
@@ -124,13 +117,19 @@ export function createRenderer(
for (const layer of scene.layers) {
prepare(layer);
}
- raster.sweep();
const image =
factor > 1 ? proxy.render(developed, factor, version) : input(developed);
const images = compose(image, scene, {
inputId,
- coverage: (layer) => raster.get(layer.id),
+ retain: (id) => active.add(id),
+ coverage: (layer) => raster.update(layer, developed.size),
+ brush: (id, strokes) => raster.brush(id, strokes, developed.size),
});
+ for (const id of instances) {
+ if (!active.has(id)) graph.release(`${id}/`);
+ }
+ instances = active;
+ raster.sweep();
const targets = graph.render([
images.original,
images.full,
diff --git a/src/core/renderer/mask/index.ts b/src/core/renderer/mask/index.ts
index ab4f3f9..64be664 100644
--- a/src/core/renderer/mask/index.ts
+++ b/src/core/renderer/mask/index.ts
@@ -228,7 +228,7 @@ export function createMaskRaster(gpu: Gpu) {
}));
const applied = brush.strokes;
if (applied === strokes) {
- return;
+ return brush;
}
if (applied.length && extendsStrokes(applied, strokes)) {
brush.dabs = stampStrokes(
@@ -242,6 +242,7 @@ export function createMaskRaster(gpu: Gpu) {
brush.dabs = stampStrokes(brush.target, strokes);
}
brush.strokes = strokes;
+ return brush;
}
/** The pass that adds or subtracts one op's coverage, or nothing for a brush without strokes. */
@@ -293,6 +294,10 @@ export function createMaskRaster(gpu: Gpu) {
}
return {
+ /** Shares the incremental brush cache with effects that own individual strokes. */
+ brush(id: string, strokes: readonly BrushStroke[], size: Size) {
+ return input(updateBrush(id, strokes, size).target);
+ },
/** Brings the layer's rasters up to date and returns the coverage its composition samples, if any. */
update(layer: MaskLayer, size: Size): RenderInput | undefined {
const [own, ...children] = maskOps(layer);
diff --git a/src/core/renderer/node.ts b/src/core/renderer/node.ts
index c6e5b03..7e9114d 100644
--- a/src/core/renderer/node.ts
+++ b/src/core/renderer/node.ts
@@ -1,6 +1,8 @@
import type { EffectOptions, ShaderSource, Target } from "vgpu";
type NodeOptions = {
+ /** Shares state across passes whose shader and non-input bindings are identical. */
+ readonly instance?: string;
readonly set?: EffectOptions["set"];
readonly samplers?: Readonly<Record<string, GPUSamplerDescriptor>>;
readonly storage?: Readonly<Record<string, Float32Array<ArrayBuffer>>>;
diff --git a/src/features/crop/overlay.tsx b/src/features/crop/overlay.tsx
index 3b504ec..be59cf0 100644
--- a/src/features/crop/overlay.tsx
+++ b/src/features/crop/overlay.tsx
@@ -99,7 +99,7 @@ export function CropOverlay({
}
return (
<div
- className="absolute -inset-6 touch-none"
+ className="absolute inset-0 touch-none"
onPointerDown={start}
onPointerMove={(event) => {
const target = hit(event);
diff --git a/src/features/crop/view.tsx b/src/features/crop/view.tsx
index 35eae99..5c7d116 100644
--- a/src/features/crop/view.tsx
+++ b/src/features/crop/view.tsx
@@ -62,6 +62,16 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
"5:4": 5 / 4,
};
const custom = ratio !== null && !Object.values(ratios).includes(ratio);
+ const ratioOptions = [
+ { value: "free", label: "Free" },
+ ...(custom && ratio !== null
+ ? [{ value: `${ratio}`, label: "Current" }]
+ : []),
+ ...Object.entries(ratios).map(([label, value]) => ({
+ value: `${value}`,
+ label,
+ })),
+ ];
function changeRatio(value: string) {
const ratio = Number(value) || null;
setRatio(ratio);
@@ -115,18 +125,11 @@ export function CropEditor({ onClose }: { onClose: () => void }) {
Aspect ratio
<Select
aria-label="Aspect ratio"
- value={ratio ?? "free"}
+ value={ratio === null ? "free" : `${ratio}`}
+ options={ratioOptions}
className="w-24"
- onChange={(event) => changeRatio(event.currentTarget.value)}
- >
- <option value="free">Free</option>
- {custom && <option value={ratio}>Current</option>}
- {Object.entries(ratios).map(([label, value]) => (
- <option key={label} value={value}>
- {label}
- </option>
- ))}
- </Select>
+ onChange={changeRatio}
+ />
</div>
<section
aria-label="Rotate and flip image"
diff --git a/src/features/fill/controls.tsx b/src/features/fill/controls.tsx
index 8b08eae..cddb063 100644
--- a/src/features/fill/controls.tsx
+++ b/src/features/fill/controls.tsx
@@ -41,22 +41,10 @@ export function FillControls({ id, fill }: { id: string; fill: Fill }) {
<Select
aria-label="Blend"
value={fill.blend}
+ options={blends.map(([value, label]) => ({ value, label }))}
className="w-28"
- onChange={(event) => {
- const blend = blends.find(
- ([blend]) => blend === event.currentTarget.value,
- );
- if (blend) {
- setFill(document, { blend: blend[0] }, id);
- }
- }}
- >
- {blends.map(([blend, label]) => (
- <option key={blend} value={blend}>
- {label}
- </option>
- ))}
- </Select>
+ onChange={(blend) => setFill(document, { blend }, id)}
+ />
</div>
</section>
);
diff --git a/src/features/heal/anchor.tsx b/src/features/heal/anchor.tsx
new file mode 100644
index 0000000..18d728d
--- /dev/null
+++ b/src/features/heal/anchor.tsx
@@ -0,0 +1,139 @@
+import { type PointerEvent, useEffect, useRef } from "react";
+import { useDocumentMapping } from "@/components/editor/mapping";
+import { useDocument } from "@/components/editor/session";
+import { useViewport } from "@/components/editor/viewport";
+import type { Point } from "@/core/image/frame";
+
+type Drag = {
+ pointer: number;
+ box?: DOMRect;
+ start: Point;
+ /** The value when the drag began; a live edit changes the rendered value under the pointer. */
+ from: Point;
+ next: Point;
+};
+
+/** A dragged value: the destination's first point or the donor offset, changed as one history edit. */
+export type AnchorDrag = {
+ from: Point;
+ /** Follows the pointer; no value ends the drag. */
+ onDrag: (next?: Point) => void;
+ onDrop: (next: Point) => void;
+};
+
+const marker = { r: 7, fill: "#3b82f6", stroke: "white", strokeWidth: 2 };
+
+/** The first-point anchor of a destination or source contour, draggable while its patch is selected. */
+export function HealAnchor({
+ kind,
+ center,
+ drag,
+}: {
+ kind: "destination" | "source";
+ center: Point;
+ drag?: AnchorDrag;
+}) {
+ if (drag) return <DraggedAnchor kind={kind} center={center} drag={drag} />;
+ return (
+ <circle
+ {...{ [`data-heal-${kind}-anchor`]: "true" }}
+ cx={center[0]}
+ cy={center[1]}
+ {...marker}
+ />
+ );
+}
+
+function DraggedAnchor({
+ kind,
+ center,
+ drag,
+}: {
+ kind: "destination" | "source";
+ center: Point;
+ drag: AnchorDrag;
+}) {
+ const document = useDocument();
+ const mapping = useDocumentMapping();
+ const camera = useViewport();
+ const current = useRef<Drag | undefined>(undefined);
+ const opened = useRef(false);
+ /** A drag inside an open group, such as a finishing stroke, joins it and leaves the group to its opener. */
+ function end(commit: boolean) {
+ if (!opened.current) return;
+ opened.current = false;
+ if (commit) document.history.commit();
+ else document.history.cancel();
+ }
+ function point(event: PointerEvent<SVGCircleElement>, box?: DOMRect) {
+ return mapping.toDocument(event.clientX, event.clientY, box);
+ }
+ function cancel() {
+ if (!current.current) return;
+ current.current = undefined;
+ drag.onDrag();
+ end(false);
+ }
+ useEffect(() => () => end(false), []);
+ function start(event: PointerEvent<SVGCircleElement>) {
+ if (event.button !== 0 || !event.isPrimary || camera.panMode) {
+ return;
+ }
+ const box = camera.ref.current?.getBoundingClientRect();
+ opened.current = document.history.begin();
+ current.current = {
+ pointer: event.pointerId,
+ box,
+ start: point(event, box),
+ from: drag.from,
+ next: drag.from,
+ };
+ event.preventDefault();
+ event.stopPropagation();
+ event.currentTarget.setPointerCapture(event.pointerId);
+ }
+ function move(event: PointerEvent<SVGCircleElement>) {
+ const active = current.current;
+ if (!active || active.pointer !== event.pointerId) return;
+ const at = point(event, active.box);
+ active.next = [
+ active.from[0] + at[0] - active.start[0],
+ active.from[1] + at[1] - active.start[1],
+ ];
+ drag.onDrag(active.next);
+ event.preventDefault();
+ event.stopPropagation();
+ }
+ function finish(event: PointerEvent<SVGCircleElement>) {
+ const active = current.current;
+ if (!active || active.pointer !== event.pointerId) return;
+ current.current = undefined;
+ event.stopPropagation();
+ event.currentTarget.releasePointerCapture(event.pointerId);
+ const moved =
+ active.next[0] !== active.from[0] || active.next[1] !== active.from[1];
+ if (!moved) {
+ drag.onDrag();
+ end(false);
+ return;
+ }
+ drag.onDrop(active.next);
+ drag.onDrag();
+ end(true);
+ }
+ return (
+ <circle
+ {...{ [`data-heal-${kind}-handle`]: "true" }}
+ data-hide-brush-cursor="true"
+ cx={center[0]}
+ cy={center[1]}
+ {...marker}
+ className="pointer-events-auto cursor-grab active:cursor-grabbing"
+ onPointerDown={start}
+ onPointerMove={move}
+ onPointerUp={finish}
+ onPointerCancel={cancel}
+ onLostPointerCapture={cancel}
+ />
+ );
+}
diff --git a/src/features/heal/controls.tsx b/src/features/heal/controls.tsx
new file mode 100644
index 0000000..794acaf
--- /dev/null
+++ b/src/features/heal/controls.tsx
@@ -0,0 +1,165 @@
+import { CoverageThumbnail } from "@/components/editor/coverage-thumbnail";
+import { useDocument } from "@/components/editor/session";
+import { HealIcon } from "@/components/icons/heal";
+import { Icon } from "@/components/icons/icon";
+import { Menu } from "@/components/ui/menu";
+import { PanelListItem } from "@/components/ui/panel-list";
+import { ScrubInput } from "@/components/ui/scrub-input";
+import type { HealPatch } from "@/core/document";
+import { deleteHealPatch, duplicateHealPatch, setHealSource } from "./edits";
+import { useHealing } from "./mode";
+import { patchThumbnailRegion } from "./model";
+
+/** A patch still finding its donor has no raster yet. */
+function PendingThumbnail() {
+ return (
+ <span
+ role="img"
+ aria-label="Patch pending"
+ className="grid size-8 shrink-0 place-items-center rounded-sm border border-neutral-600 bg-neutral-950 text-neutral-500"
+ >
+ <HealIcon className="size-4" />
+ </span>
+ );
+}
+
+function PatchActions({
+ layer,
+ patch,
+ next,
+}: {
+ layer: string;
+ patch: HealPatch;
+ next?: string;
+}) {
+ const document = useDocument();
+ const { selectPatch } = useHealing();
+ return (
+ <Menu
+ label="Patch actions"
+ icon={
+ <Icon className="size-4">
+ <path
+ d="M5 12h.01M12 12h.01M19 12h.01"
+ strokeWidth="3"
+ strokeLinecap="round"
+ />
+ </Icon>
+ }
+ >
+ <button
+ type="submit"
+ onClick={() =>
+ selectPatch(duplicateHealPatch(document, layer, patch.id))
+ }
+ >
+ Duplicate
+ </button>
+ <button
+ type="submit"
+ onClick={() => {
+ deleteHealPatch(document, layer, patch.id);
+ selectPatch(next);
+ }}
+ >
+ Delete
+ </button>
+ </Menu>
+ );
+}
+
+function SourceFields({
+ layer,
+ patch,
+ limit,
+}: {
+ layer: string;
+ patch: HealPatch;
+ limit: readonly [number, number];
+}) {
+ const document = useDocument();
+ const { selectPatch } = useHealing();
+ const destination = patch.stroke.points[0];
+ return (
+ <div
+ className="flex shrink-0 items-center gap-1"
+ onPointerDownCapture={() => selectPatch(patch.id)}
+ >
+ {(["X", "Y"] as const).map((axis, index) => (
+ <ScrubInput
+ key={axis}
+ aria-label={`Source ${axis}`}
+ label={axis}
+ value={Math.round(destination[index] + patch.offset[index])}
+ min={0}
+ max={limit[index]}
+ variant="text"
+ onChange={(value) => {
+ const offset: [number, number] = [patch.offset[0], patch.offset[1]];
+ offset[index] = Math.round(value - destination[index]);
+ setHealSource(document, layer, patch.id, offset);
+ }}
+ />
+ ))}
+ </div>
+ );
+}
+
+/** Shows the ordered repair stack; selection is shared with the canvas and floating bar. */
+export function HealControls({
+ id,
+ patches,
+}: {
+ id: string;
+ patches: readonly HealPatch[];
+}) {
+ const document = useDocument();
+ const { selectedPatch, selectPatch, hoverPatch } = useHealing();
+ const source = document.resources.get(
+ document.scene.getState().layers[0].source,
+ );
+ if (!patches.length) {
+ return (
+ <p className="p-4 text-center text-neutral-500">
+ Paint over a spot or object to create the first patch.
+ </p>
+ );
+ }
+ return (
+ <ol aria-label="Healing patches">
+ {patches.map((patch, index) => (
+ <li key={patch.id}>
+ <PanelListItem
+ selected={selectedPatch === patch.id}
+ className="pr-1"
+ onPointerEnter={() => hoverPatch(patch.id)}
+ onPointerLeave={() => hoverPatch()}
+ >
+ <button
+ type="button"
+ aria-label={`Select patch ${index + 1}`}
+ aria-pressed={selectedPatch === patch.id}
+ onClick={() => selectPatch(patch.id)}
+ className="flex min-w-0 flex-1 items-center gap-2 self-stretch px-2 text-left"
+ >
+ <CoverageThumbnail
+ id={`layer/${id}/${patch.id}`}
+ version={patch}
+ region={patchThumbnailRegion(patch.stroke, source.image.size)}
+ label="Patch shape"
+ fallback={<PendingThumbnail />}
+ />
+ <span className="min-w-0 flex-1 truncate">Patch {index + 1}</span>
+ </button>
+ <SourceFields layer={id} patch={patch} limit={source.image.size} />
+ <PatchActions
+ layer={id}
+ patch={patch}
+ next={patches[index - 1]?.id ?? patches[index + 1]?.id}
+ />
+ </PanelListItem>
+ </li>
+ ))}
+ </ol>
+ );
+}
diff --git a/src/features/heal/edits.ts b/src/features/heal/edits.ts
new file mode 100644
index 0000000..332b539
--- /dev/null
+++ b/src/features/heal/edits.ts
@@ -0,0 +1,173 @@
+import {
+ type BrushStroke,
+ type EditorDocument,
+ editLayer,
+ type HealPatch,
+ type Layer,
+ type StrokePoint,
+} from "@/core/document";
+import { validateStroke, validPoints } from "@/core/document/brush";
+import type { Point } from "@/core/image/frame";
+import { validateOffset } from "./model";
+
+export function addHealPatch(
+ document: EditorDocument,
+ id: string,
+ stroke: BrushStroke,
+ offset: Point,
+) {
+ validateStroke(stroke);
+ if (stroke.mode !== "paint") {
+ throw Error("Heal patches use painted strokes.");
+ }
+ validateOffset(offset);
+ const patch: HealPatch = {
+ id: crypto.randomUUID(),
+ feather: stroke.feather,
+ stroke: { ...structuredClone(stroke), feather: 0 },
+ opacity: 1,
+ offset: [offset[0], offset[1]],
+ };
+ editLayer(document, id, (layer) => ({
+ ...layer,
+ patches: [...healPatches(layer), patch],
+ }));
+ return patch.id;
+}
+
+function healPatches(layer: Layer) {
+ if (layer.kind !== "heal") throw Error("Select a Healing layer.");
+ return layer.patches;
+}
+
+/** Rewrites the patch list around one patch, found by id. */
+function editPatches(
+ document: EditorDocument,
+ id: string,
+ patchId: string,
+ edit: (patches: readonly HealPatch[], index: number) => readonly HealPatch[],
+) {
+ editLayer(document, id, (layer) => {
+ const patches = healPatches(layer);
+ const index = patches.findIndex((patch) => patch.id === patchId);
+ if (index < 0) throw Error("Heal patch is unavailable.");
+ return { ...layer, patches: edit(patches, index) };
+ });
+}
+
+function unit(name: string, value: number | undefined) {
+ if (value !== undefined && !(value >= 0 && value <= 1)) {
+ throw Error(`Heal patch ${name} must be between 0 and 1.`);
+ }
+}
+
+/** Edits one patch's blend; its shape and donor stay as painted. */
+export function setHealPatch(
+ document: EditorDocument,
+ id: string,
+ patchId: string,
+ change: { feather?: number; opacity?: number },
+) {
+ unit("feather", change.feather);
+ unit("opacity", change.opacity);
+ editPatches(document, id, patchId, (patches, index) =>
+ patches.with(index, {
+ ...patches[index],
+ feather: change.feather ?? patches[index].feather,
+ opacity: change.opacity ?? patches[index].opacity,
+ }),
+ );
+}
+
+export function duplicateHealPatch(
+ document: EditorDocument,
+ id: string,
+ patchId: string,
+) {
+ const nextId = crypto.randomUUID();
+ editPatches(document, id, patchId, (patches, index) =>
+ patches.toSpliced(index + 1, 0, {
+ ...structuredClone(patches[index]),
+ id: nextId,
+ }),
+ );
+ return nextId;
+}
+
+export function deleteHealPatch(
+ document: EditorDocument,
+ id: string,
+ patchId: string,
+) {
+ editPatches(document, id, patchId, (patches, index) =>
+ patches.toSpliced(index, 1),
+ );
+}
+
+export function extendHealPatch(
+ document: EditorDocument,
+ id: string,
+ points: readonly StrokePoint[],
+) {
+ if (!validPoints(points)) {
+ throw Error(
+ "Stroke points need finite coordinates and pressure from 0 to 1.",
+ );
+ }
+ editLayer(document, id, (layer) => {
+ if (layer.kind !== "heal" || !layer.patches.length) {
+ throw Error("Start a heal patch before extending it.");
+ }
+ return {
+ ...layer,
+ patches: layer.patches.map((patch, index) =>
+ index === layer.patches.length - 1
+ ? {
+ ...patch,
+ stroke: {
+ ...patch.stroke,
+ points: [...patch.stroke.points, ...points],
+ },
+ }
+ : patch,
+ ),
+ };
+ });
+}
+
+export function setHealSource(
+ document: EditorDocument,
+ id: string,
+ patchId: string,
+ offset: Point,
+) {
+ validateOffset(offset);
+ editPatches(document, id, patchId, (patches, index) =>
+ patches.with(index, { ...patches[index], offset: [offset[0], offset[1]] }),
+ );
+}
+
+/** Moves a patch while its donor stays fixed. */
+export function setHealDestination(
+ document: EditorDocument,
+ id: string,
+ patchId: string,
+ destination: Point,
+) {
+ validateOffset(destination);
+ editPatches(document, id, patchId, (patches, index) => {
+ const patch = patches[index];
+ const [x, y] = patch.stroke.points[0];
+ const delta: Point = [destination[0] - x, destination[1] - y];
+ return patches.with(index, {
+ ...patch,
+ stroke: {
+ ...patch.stroke,
+ points: patch.stroke.points.map(
+ ([x, y, pressure]) => [x + delta[0], y + delta[1], pressure] as const,
+ ),
+ },
+ offset: [patch.offset[0] - delta[0], patch.offset[1] - delta[1]],
+ });
+ });
+}
diff --git a/src/features/heal/heal.wgsl b/src/features/heal/heal.wgsl
new file mode 100644
index 0000000..6837b32
--- /dev/null
+++ b/src/features/heal/heal.wgsl
@@ -0,0 +1,101 @@
+struct Params {
+ origin: vec2f,
+ extent: vec2f,
+ dimensions: vec2f,
+ offset: vec2f,
+ grid: vec2f,
+ mode: u32,
+ feather: f32,
+ opacity: f32,
+}
+@group(0) @binding(0) var source: texture_2d<f32>;
+@group(0) @binding(1) var coverage: texture_2d<f32>;
+@group(0) @binding(2) var previous: texture_2d<f32>;
+@group(0) @binding(3) var linearSampler: sampler;
+@group(0) @binding(4) var<uniform> params: Params;
+
+fn color(p: vec2f) -> vec4f {
+ return textureSampleLevel(source, linearSampler, p / params.dimensions, 0.0);
+}
+fn mask(p: vec2f) -> f32 {
+ return textureSampleLevel(coverage, linearSampler, p / params.dimensions, 0.0).r;
+}
+fn difference(p: vec2f) -> vec3f {
+ // A smooth log gain matches color without subtracting bright-boundary differences
+ // from dark donor texture, which would clip it into black islands.
+ return log(max(color(p).rgb, vec3f(0.0)) + 0.0001) - log(max(color(p + params.offset).rgb, vec3f(0.0)) + 0.0001);
+}
+fn correction(uv: vec2f) -> vec3f {
+ let p = params.origin + uv * params.extent;
+ if (any(uv < vec2f(0.0)) || any(uv > vec2f(1.0)) || mask(p) < 0.001) {
+ return difference(p);
+ }
+ return textureSampleLevel(previous, linearSampler, uv, 0.0).rgb;
+}
+// Softens the hard patch coverage inward by `radius`, never outward.
+fn patchCoverage(
+ texture: texture_2d<f32>,
+ textureSampler: sampler,
+ p: vec2f,
+ dimensions: vec2f,
+ radius: f32,
+) -> f32 {
+ let uv = p / dimensions;
+ let center = textureSampleLevel(texture, textureSampler, uv, 0.0).r;
+ if (radius < 0.5) {
+ return center;
+ }
+ let directions = array(
+ vec2f(1.0, 0.0), vec2f(-1.0, 0.0), vec2f(0.0, 1.0), vec2f(0.0, -1.0),
+ vec2f(0.707, 0.707), vec2f(-0.707, 0.707), vec2f(0.707, -0.707), vec2f(-0.707, -0.707),
+ );
+ var total = center * 4.0;
+ for (var i = 0u; i < 8u; i++) {
+ let direction = directions[i] * radius / dimensions;
+ total += textureSampleLevel(texture, textureSampler, uv + direction * 0.35, 0.0).r * 2.0;
+ total += textureSampleLevel(texture, textureSampler, uv + direction * 0.85, 0.0).r;
+ }
+ // Feather only the covered side of the hard patch boundary.
+ return min(center, total / 28.0);
+}
+
+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ let uv = position.xy / params.grid;
+ if (params.mode == 3u) {
+ let p = uv * params.dimensions;
+ let original = color(p);
+ if (any(p < params.origin) || any(p > params.origin + params.extent)) {
+ return original;
+ }
+ let amount = patchCoverage(coverage, linearSampler, p, params.dimensions, params.feather) * params.opacity;
+ let donor = p + params.offset;
+ if (amount == 0.0 || any(donor < vec2f(0.5)) || any(donor > params.dimensions - 0.5)) {
+ return original;
+ }
+ let donorColor = max(color(donor).rgb, vec3f(0.0));
+ let delta = textureSampleLevel(previous, linearSampler, (p - params.origin) / params.extent, 0.0).rgb;
+ // Bound the gain and damp chroma correction so a small dark channel cannot
+ // produce the extreme saturation shifts of an unrestricted RGB ratio.
+ let luminanceDelta = dot(delta, vec3f(0.2627, 0.6780, 0.0593));
+ let safeDelta = clamp(vec3f(luminanceDelta) + (delta - vec3f(luminanceDelta)) * 0.95, vec3f(-0.35), vec3f(0.35));
+ let healed = (donorColor + 0.0001) * exp(safeDelta) - 0.0001;
+ return vec4f(mix(original.rgb, max(vec3f(0.0), healed), amount), original.a);
+ }
+ let p = params.origin + uv * params.extent;
+ if (mask(p) < 0.001) {
+ return vec4f(difference(p), 1.0);
+ }
+ if (params.mode == 0u) {
+ return vec4f(0.0, 0.0, 0.0, 1.0);
+ }
+ if (params.mode == 1u) {
+ return vec4f(correction(uv), 1.0);
+ }
+ let step = 1.0 / params.grid;
+ let x = correction(uv + vec2f(step.x, 0.0)) + correction(uv - vec2f(step.x, 0.0));
+ let y = correction(uv + vec2f(0.0, step.y)) + correction(uv - vec2f(0.0, step.y));
+ // Non-square crops have unequal grid spacing in document pixels.
+ let spacing = params.extent / params.grid;
+ let weights = 1.0 / (spacing * spacing);
+ return vec4f((x * weights.x + y * weights.y) / (2.0 * (weights.x + weights.y)), 1.0);
+}
diff --git a/src/features/heal/mode.tsx b/src/features/heal/mode.tsx
new file mode 100644
index 0000000..c49e341
--- /dev/null
+++ b/src/features/heal/mode.tsx
@@ -0,0 +1,56 @@
+import {
+ createContext,
+ type ReactNode,
+ useCallback,
+ useContext,
+ useState,
+} from "react";
+
+const HealingContext = createContext<{
+ feather: number;
+ setFeather: (feather: number) => void;
+ selectedPatch?: string;
+ selectPatch: (id?: string) => void;
+ hoveredPatch?: string;
+ hoverPatch: (id?: string) => void;
+} | null>(null);
+
+/** Owns the tool's next-stroke feather and patch selection, shared by the canvas, bar, and sidebar. */
+export function HealingProvider({
+ children,
+ onEdit,
+}: {
+ children: ReactNode;
+ onEdit?: () => void;
+}) {
+ const [feather, setFeather] = useState(0.1);
+ const [selectedPatch, setSelectedPatch] = useState<string>();
+ const [hoveredPatch, setHoveredPatch] = useState<string>();
+ const selectPatch = useCallback(
+ (id?: string) => {
+ setSelectedPatch(id);
+ if (id) onEdit?.();
+ },
+ [onEdit],
+ );
+ return (
+ <HealingContext
+ value={{
+ feather,
+ setFeather,
+ selectedPatch,
+ selectPatch,
+ hoveredPatch,
+ hoverPatch: setHoveredPatch,
+ }}
+ >
+ {children}
+ </HealingContext>
+ );
+}
+
+export function useHealing() {
+ const context = useContext(HealingContext);
+ if (!context) throw Error("A healing provider is required.");
+ return context;
+}
diff --git a/src/features/heal/model.ts b/src/features/heal/model.ts
new file mode 100644
index 0000000..bfaed69
--- /dev/null
+++ b/src/features/heal/model.ts
@@ -0,0 +1,72 @@
+import type { BrushStroke, Scene } from "@/core/document";
+import type { Point } from "@/core/image/frame";
+
+export function findHealPatch(scene: Scene, layerId: string, patchId: string) {
+ const layer = scene.layers.find((layer) => layer.id === layerId);
+ if (layer?.kind !== "heal") return;
+ return layer.patches.find((patch) => patch.id === patchId);
+}
+
+/** Whether a dab reaches the image at all; a stroke starting further away has nothing to repair. */
+export function dabTouchesImage(
+ [x, y]: readonly number[],
+ radius: number,
+ size: readonly number[],
+) {
+ return (
+ x + radius > 0 &&
+ y + radius > 0 &&
+ x - radius < size[0] &&
+ y - radius < size[1]
+ );
+}
+
+/** Bounds include the whole soft brush edge, with room for a known boundary. */
+export function patchBounds(
+ stroke: BrushStroke,
+ size: readonly number[],
+ margin = 0,
+) {
+ const radius = stroke.size / 2 + margin + 4;
+ let left = size[0];
+ let top = size[1];
+ let right = 0;
+ let bottom = 0;
+ for (const [x, y] of stroke.points) {
+ left = Math.min(left, x - radius);
+ top = Math.min(top, y - radius);
+ right = Math.max(right, x + radius);
+ bottom = Math.max(bottom, y + radius);
+ }
+ const origin: Point = [
+ Math.max(0, Math.floor(left)),
+ Math.max(0, Math.floor(top)),
+ ];
+ const extent: Point = [
+ Math.max(1, Math.min(size[0], Math.ceil(right)) - origin[0]),
+ Math.max(1, Math.min(size[1], Math.ceil(bottom)) - origin[1]),
+ ];
+ return { origin, extent };
+}
+
+/** A square around the patch with a little margin, so a thumbnail keeps the stroke's proportions. */
+export function patchThumbnailRegion(
+ stroke: BrushStroke,
+ size: readonly number[],
+) {
+ const { origin, extent } = patchBounds(stroke, size);
+ const side = Math.max(extent[0], extent[1]) * 1.1;
+ return {
+ origin: [
+ origin[0] + (extent[0] - side) / 2,
+ origin[1] + (extent[1] - side) / 2,
+ ] as Point,
+ extent: [side, side] as Point,
+ };
+}
+
+export function validateOffset(offset: Point) {
+ if (offset.length !== 2 || !offset.every(Number.isFinite)) {
+ throw Error("A heal source needs two finite source-pixel offsets.");
+ }
+}
diff --git a/src/features/heal/options.tsx b/src/features/heal/options.tsx
new file mode 100644
index 0000000..e7b1ccb
--- /dev/null
+++ b/src/features/heal/options.tsx
@@ -0,0 +1,70 @@
+import { useBrushTool } from "@/components/editor/brush-tool";
+import { useDocument, useScene } from "@/components/editor/session";
+import { barSlider, useBarDensity } from "@/components/editor/toolbar-density";
+import { Slider } from "@/components/ui/slider";
+import { findLayer } from "@/core/document";
+import { setHealPatch } from "./edits";
+import { useHealing } from "./mode";
+
+export function HealOptions() {
+ const document = useDocument();
+ const { settings, maxSize, setPreview, update } = useBrushTool();
+ const { feather: nextFeather, setFeather, selectedPatch } = useHealing();
+ const layer = useScene((scene) =>
+ findLayer(scene.layers, document.selection.getState().layerId),
+ );
+ const selected =
+ layer?.kind === "heal"
+ ? layer.patches.find((patch) => patch.id === selectedPatch)
+ : undefined;
+ const variant = barSlider(useBarDensity());
+ const size = settings.size;
+ const feather = selected?.feather ?? nextFeather;
+ function changePatch(change: Parameters<typeof setHealPatch>[3]) {
+ if (selected && layer?.kind === "heal") {
+ setHealPatch(document, layer.id, selected.id, change);
+ }
+ }
+ return (
+ <>
+ <Slider
+ label="Size"
+ min={1}
+ max={maxSize}
+ unit="px"
+ valueWidth={`${maxSize}`.length}
+ variant={variant}
+ value={size}
+ onEditingChange={setPreview}
+ onChange={(value) => update({ size: Math.round(value) })}
+ />
+ <Slider
+ label="Feather"
+ value={feather * 100}
+ min={0}
+ max={100}
+ unit="%"
+ valueWidth={3}
+ variant={variant}
+ onEditingChange={setPreview}
+ onChange={(value) => {
+ const feather = value / 100;
+ setFeather(feather);
+ changePatch({ feather });
+ }}
+ />
+ {selected && (
+ <Slider
+ label="Opacity"
+ value={selected.opacity * 100}
+ min={0}
+ max={100}
+ unit="%"
+ valueWidth={3}
+ variant={variant}
+ onChange={(value) => changePatch({ opacity: value / 100 })}
+ />
+ )}
+ </>
+ );
+}
diff --git a/src/features/heal/outline.tsx b/src/features/heal/outline.tsx
new file mode 100644
index 0000000..6197777
--- /dev/null
+++ b/src/features/heal/outline.tsx
@@ -0,0 +1,183 @@
+import { memo, type PointerEvent, useId, useState } from "react";
+import { useDocumentMapping } from "@/components/editor/mapping";
+import { useDocument } from "@/components/editor/session";
+import type { BrushStroke, HealPatch } from "@/core/document";
+import type { Point } from "@/core/image/frame";
+import { type AnchorDrag, HealAnchor } from "./anchor";
+import { setHealDestination, setHealSource } from "./edits";
+
+type Geometry = {
+ center: Point;
+ d: string;
+ first: Point;
+ last: Point;
+ width: number;
+};
+type Mapping = ReturnType<typeof useDocumentMapping>;
+
+function geometry(
+ stroke: BrushStroke,
+ offset: Point,
+ mapping: Mapping,
+): Geometry {
+ const points = stroke.points.map(([x, y]) =>
+ mapping.toScreen([x + offset[0], y + offset[1]]),
+ );
+ const width = stroke.size / mapping.pixelsPerViewportPixel;
+ return {
+ center: points[0],
+ d: points.map(([x, y], index) => `${index ? "L" : "M"}${x} ${y}`).join(""),
+ first: points[0],
+ last: points.at(-1) ?? points[0],
+ width,
+ };
+}
+
+function StrokeShape({
+ shape,
+ width,
+ color,
+}: {
+ shape: Geometry;
+ width: number;
+ color: string;
+}) {
+ return (
+ <>
+ <path
+ d={shape.d}
+ fill="none"
+ stroke={color}
+ strokeWidth={width}
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ <circle
+ cx={shape.first[0]}
+ cy={shape.first[1]}
+ r={width / 2}
+ fill={color}
+ />
+ <circle
+ cx={shape.last[0]}
+ cy={shape.last[1]}
+ r={width / 2}
+ fill={color}
+ />
+ </>
+ );
+}
+
+function Outline({
+ shape,
+ kind,
+}: {
+ shape: Geometry;
+ kind: "destination" | "source";
+}) {
+ const mask = useId();
+ const outer = shape.width + 2;
+ const inner = Math.max(0, shape.width - 2);
+ return (
+ <g data-heal-outline={kind} opacity={kind === "source" ? 0.55 : 1}>
+ <defs>
+ <mask id={mask} maskUnits="userSpaceOnUse">
+ <rect width="100%" height="100%" fill="black" />
+ <StrokeShape shape={shape} width={outer} color="white" />
+ {inner > 0 && (
+ <StrokeShape shape={shape} width={inner} color="black" />
+ )}
+ </mask>
+ </defs>
+ <rect
+ width="100%"
+ height="100%"
+ fill="white"
+ mask={`url(#${mask})`}
+ className="drop-shadow-[0_0_1px_black]"
+ />
+ </g>
+ );
+}
+
+/** Draws a solid destination and a quieter source contour with first-point anchors. */
+export function HealPatchOutline({
+ layer,
+ patch,
+ showSource,
+ interactive,
+}: {
+ layer: string;
+ patch: HealPatch;
+ showSource: boolean;
+ interactive: boolean;
+}) {
+ const document = useDocument();
+ const mapping = useDocumentMapping();
+ // The donor only previews its contour while dragged, so the correction renders once on release.
+ const [sourcePreview, setSourcePreview] = useState<Point>();
+ const first = patch.stroke.points[0];
+ const destination = geometry(patch.stroke, [0, 0], mapping);
+ const source =
+ showSource &&
+ geometry(patch.stroke, sourcePreview ?? patch.offset, mapping);
+ const moveDestination = (next?: Point) => {
+ if (next) setHealDestination(document, layer, patch.id, next);
+ };
+ const destinationDrag: AnchorDrag | undefined = interactive
+ ? {
+ from: [first[0], first[1]],
+ onDrag: moveDestination,
+ onDrop: moveDestination,
+ }
+ : undefined;
+ const sourceDrag: AnchorDrag | undefined = interactive
+ ? {
+ from: patch.offset,
+ onDrag: setSourcePreview,
+ onDrop: (next) => setHealSource(document, layer, patch.id, next),
+ }
+ : undefined;
+ return (
+ <>
+ <Outline shape={destination} kind="destination" />
+ <HealAnchor
+ kind="destination"
+ center={destination.center}
+ drag={destinationDrag}
+ />
+ {source && (
+ <>
+ <Outline shape={source} kind="source" />
+ <HealAnchor kind="source" center={source.center} drag={sourceDrag} />
+ </>
+ )}
+ </>
+ );
+}
+
+/** Keeps canvas selection on the painted geometry without adding a visible marker over the result. */
+export const HealPatchHitTarget = memo(function HealPatchHitTarget({
+ patch,
+ onSelect,
+}: {
+ patch: HealPatch;
+ onSelect: (id: string) => void;
+}) {
+ const shape = geometry(patch.stroke, [0, 0], useDocumentMapping());
+ const select = (event: PointerEvent<SVGElement>) => {
+ event.preventDefault();
+ event.stopPropagation();
+ onSelect(patch.id);
+ };
+ const width = Math.max(16, shape.width);
+ return (
+ <g
+ data-heal-hit-target={patch.id}
+ onPointerDown={select}
+ pointerEvents="all"
+ >
+ <StrokeShape shape={shape} width={width} color="transparent" />
+ </g>
+ );
+});
diff --git a/src/features/heal/overlay.tsx b/src/features/heal/overlay.tsx
new file mode 100644
index 0000000..cce6a5a
--- /dev/null
+++ b/src/features/heal/overlay.tsx
@@ -0,0 +1,203 @@
+import { useEffect, useMemo, useRef, useState } from "react";
+import { useGpu } from "vgpu-react";
+import { BrushCanvas } from "@/components/editor/brush-canvas";
+import { useDocumentMapping } from "@/components/editor/mapping";
+import { useRenderer } from "@/components/editor/pipeline";
+import { useDocument, useScene } from "@/components/editor/session";
+import { findLayer } from "@/core/document";
+import type { Point } from "@/core/image/frame";
+import { addHealPatch, extendHealPatch, setHealSource } from "./edits";
+import { useHealing } from "./mode";
+import { dabTouchesImage, findHealPatch } from "./model";
+import { HealPatchHitTarget, HealPatchOutline } from "./outline";
+import { createHealSearch } from "./source";
+
+/** Paints ordinary brush strokes; only the donor search and patch edits belong to Heal. */
+export function HealOverlay({
+ onCreate,
+ onDone,
+}: {
+ onCreate: () => string;
+ onDone: () => void;
+}) {
+ const document = useDocument();
+ const renderer = useRenderer();
+ const mapping = useDocumentMapping();
+ const gpu = useGpu();
+ const search = useMemo(() => createHealSearch(gpu), [gpu]);
+ const { feather, selectedPatch, selectPatch, hoveredPatch } = useHealing();
+ const [source, setSource] = useState<Point>();
+ const [drawingPatch, setDrawingPatch] = useState<string>();
+ const [resolvingSource, setResolvingSource] = useState<string>();
+ const layer = useScene((scene) =>
+ findLayer(scene.layers, document.selection.getState().layerId),
+ );
+ const healLayer = layer?.kind === "heal" ? layer : undefined;
+ const patches = healLayer?.patches ?? [];
+ const hovered = patches.some((patch) => patch.id === hoveredPatch)
+ ? hoveredPatch
+ : undefined;
+ const visiblePatch = drawingPatch ?? hovered ?? selectedPatch;
+ const pending = useRef<
+ { layer: string; patch: string; automatic: boolean } | undefined
+ >(undefined);
+ useEffect(() => {
+ const layer = findLayer(
+ document.scene.getState().layers,
+ document.selection.getState().layerId,
+ );
+ const before =
+ layer?.kind === "heal" ? undefined : document.scene.getState();
+ if (before) {
+ onCreate();
+ }
+ const unsubscribe = document.selection.subscribe(() => {
+ const layer = findLayer(
+ document.scene.getState().layers,
+ document.selection.getState().layerId,
+ );
+ if (layer?.kind !== "heal") {
+ onDone();
+ }
+ });
+ return () => {
+ unsubscribe();
+ if (before) {
+ document.history.drop(before);
+ }
+ };
+ }, []);
+ useEffect(() => () => search.dispose(), [search]);
+ async function complete(signal: AbortSignal) {
+ const current = pending.current;
+ try {
+ if (!current?.automatic || signal.aborted) return;
+ const scene = document.scene.getState();
+ const patch = findHealPatch(scene, current.layer, current.patch);
+ if (!patch) return;
+ await renderer.update(scene, current.patch, true);
+ if (signal.aborted) return;
+ const image = renderer.inputImage(current.patch);
+ if (!image) throw Error("Heal input is unavailable.");
+ const dimensions = document.resources.get(scene.layers[0].source).image
+ .size;
+ const offset = await search.find(image, dimensions, patch.stroke);
+ // The stroke's group stays open through the search; undo or cancel aborts it and removes the patch.
+ if (
+ signal.aborted ||
+ !findHealPatch(document.scene.getState(), current.layer, current.patch)
+ ) {
+ return;
+ }
+ setHealSource(document, current.layer, current.patch, offset);
+ } finally {
+ setResolvingSource((id) => (id === current?.patch ? undefined : id));
+ }
+ }
+ const marker = source && mapping.toScreen(source);
+ return (
+ <BrushCanvas
+ label="Healing canvas"
+ erase={false}
+ feather={feather}
+ onStart={(stroke) => {
+ const layer = healLayer;
+ if (!layer) throw Error("Select a Healing layer.");
+ const [x, y] = stroke.points[0];
+ const size = document.resources.get(
+ document.scene.getState().layers[0].source,
+ ).image.size;
+ if (!dabTouchesImage([x, y], stroke.size / 2, size)) return false;
+ const offset: Point = source ? [source[0] - x, source[1] - y] : [0, 0];
+ const patch = addHealPatch(
+ document,
+ layer.id,
+ { ...stroke, flow: 1 },
+ offset,
+ );
+ setDrawingPatch(patch);
+ setResolvingSource(source ? undefined : patch);
+ selectPatch(patch);
+ pending.current = { layer: layer.id, patch, automatic: !source };
+ return true;
+ }}
+ onExtend={(points) => {
+ const id = pending.current?.layer;
+ if (id) {
+ extendHealPatch(document, id, points);
+ }
+ }}
+ onComplete={complete}
+ onFinish={(committed) => {
+ setDrawingPatch(undefined);
+ if (!committed) setResolvingSource(undefined);
+ }}
+ onPickSource={setSource}
+ onDone={onDone}
+ >
+ {healLayer && patches.length > 0 && (
+ <svg
+ aria-hidden="true"
+ className="absolute inset-0 size-full overflow-visible"
+ style={{ pointerEvents: "none" }}
+ >
+ {patches.map(
+ (patch) =>
+ visiblePatch === patch.id && (
+ <g key={`outline-${patch.id}`} data-heal-patch={patch.id}>
+ <HealPatchOutline
+ layer={healLayer.id}
+ patch={patch}
+ showSource={
+ patch.id !== drawingPatch && patch.id !== resolvingSource
+ }
+ interactive={
+ patch.id === selectedPatch && patch.id !== drawingPatch
+ }
+ />
+ </g>
+ ),
+ )}
+ {patches.map(
+ (patch) =>
+ patch.id !== selectedPatch && (
+ <HealPatchHitTarget
+ key={`hit-${patch.id}`}
+ patch={patch}
+ onSelect={selectPatch}
+ />
+ ),
+ )}
+ </svg>
+ )}
+ {marker && (
+ <svg
+ aria-hidden="true"
+ className="pointer-events-none absolute inset-0 size-full overflow-visible"
+ >
+ <circle
+ cx={marker[0]}
+ cy={marker[1]}
+ r="8"
+ fill="none"
+ stroke="white"
+ />
+ <path
+ d={`M${marker[0] - 12} ${marker[1]}h24M${marker[0]} ${marker[1] - 12}v24`}
+ stroke="white"
+ />
+ </svg>
+ )}
+ {source && (
+ <button
+ type="button"
+ onPointerDown={(event) => event.stopPropagation()}
+ onClick={() => setSource(undefined)}
+ className="absolute right-3 bottom-3 rounded-full bg-neutral-800/80 px-3 py-1.5 text-white"
+ >
+ Automatic source
+ </button>
+ )}
+ </BrushCanvas>
+ );
+}
diff --git a/src/features/heal/pass.ts b/src/features/heal/pass.ts
new file mode 100644
index 0000000..ed2f628
--- /dev/null
+++ b/src/features/heal/pass.ts
@@ -0,0 +1,100 @@
+import type { HealPatch } from "@/core/document";
+import {
+ type Composition,
+ merge,
+ node,
+ type RenderImage,
+ type RenderInput,
+ sourceSize,
+} from "@/core/renderer";
+import shader from "./heal.wgsl";
+import { patchBounds } from "./model";
+
+type HealComposition = Pick<Composition, "brush" | "inputId" | "retain">;
+
+function healPatch(
+ source: RenderImage,
+ coverage: RenderInput,
+ patch: HealPatch,
+ name: string,
+) {
+ const dimensions = sourceSize(source);
+ const { origin, extent } = patchBounds(patch.stroke, dimensions, 0);
+ const samplers = {
+ linearSampler: { minFilter: "linear", magFilter: "linear" },
+ } as const;
+ const common = {
+ origin,
+ extent,
+ dimensions,
+ offset: patch.offset,
+ };
+ let correction = source;
+ // Coarse-to-fine harmonic extension of the destination/donor log color ratio.
+ // The correction is smooth; the donor retains its full-resolution texture.
+ for (const resolution of [4, 8, 16, 32, 64, 128]) {
+ const ratio = Math.min(1, resolution / Math.max(...extent));
+ const size: [number, number] = [
+ Math.max(2, Math.ceil(extent[0] * ratio)),
+ Math.max(2, Math.ceil(extent[1] * ratio)),
+ ];
+ const params = { ...common, grid: size, feather: 0, opacity: 1 };
+ correction = merge(
+ { source, coverage, previous: correction },
+ node(`${name}/${resolution}/seed`, shader, {
+ size,
+ samplers,
+ set: { params: { ...params, mode: resolution === 4 ? 0 : 1 } },
+ }),
+ );
+ const iterations = resolution === 4 ? 32 : 8;
+ for (let i = 0; i < iterations; i++) {
+ correction = merge(
+ { source, coverage, previous: correction },
+ node(`${name}/${resolution}/relax-${i}`, shader, {
+ instance: `${name}/${resolution}/relax`,
+ size,
+ samplers,
+ set: { params: { ...params, mode: 2 } },
+ }),
+ );
+ }
+ if (resolution >= Math.max(...extent)) {
+ break;
+ }
+ }
+ return merge(
+ { source, coverage, previous: correction },
+ node(`${name}/blend`, shader, {
+ samplers,
+ set: {
+ params: {
+ ...common,
+ grid: source.size,
+ mode: 3,
+ feather: (patch.stroke.size * patch.feather) / 2,
+ opacity: patch.opacity,
+ },
+ },
+ }),
+ );
+}
+
+export function heal(
+ source: RenderImage,
+ patches: readonly HealPatch[],
+ name: string,
+ composition: HealComposition,
+) {
+ let image = source;
+ let inspected: RenderImage | undefined;
+ for (const patch of patches) {
+ if (patch.id === composition.inputId) inspected = image;
+ const id = `${name}/${patch.id}`;
+ if (patch.offset[0] === 0 && patch.offset[1] === 0) continue;
+ const coverage = composition.brush(id, [patch.stroke]);
+ composition.retain(id);
+ image = healPatch(image, coverage, patch, id);
+ }
+ return { image, input: inspected };
+}
diff --git a/src/features/heal/source.ts b/src/features/heal/source.ts
new file mode 100644
index 0000000..737702e
--- /dev/null
+++ b/src/features/heal/source.ts
@@ -0,0 +1,131 @@
+import type { Gpu, Target, Timer } from "vgpu";
+import type { BrushStroke } from "@/core/document";
+import type { Point } from "@/core/image/frame";
+import { createRenderGraph, input, merge, node } from "@/core/renderer";
+import { patchBounds } from "./model";
+import shader from "./source.wgsl";
+
+/** Boundary matching on a thumbnail. The painted interior never contributes to the score. */
+export function matchSource(
+ pixels: Float32Array,
+ size: readonly number[],
+ dimensions: readonly number[],
+ stroke: BrushStroke,
+): Point {
+ const { origin, extent } = patchBounds(stroke, dimensions);
+ const [left, top] = origin;
+ const [width, height] = extent;
+ function color(x: number, y: number, channel: number) {
+ const px = Math.max(
+ 0,
+ Math.min(size[0] - 1, Math.floor((x * size[0]) / dimensions[0])),
+ );
+ const py = Math.max(
+ 0,
+ Math.min(size[1] - 1, Math.floor((y * size[1]) / dimensions[1])),
+ );
+ return Math.log1p(Math.max(0, pixels[(py * size[0] + px) * 4 + channel]));
+ }
+ const border: Point[] = [];
+ for (let i = 0; i < 16; i++) {
+ const t = (i + 0.5) / 16;
+ border.push(
+ [left + width * t, top],
+ [left + width * t, top + height],
+ [left, top + height * t],
+ [left + width, top + height * t],
+ );
+ }
+ const step = Math.max(4, Math.min(width, height) / 3);
+ const reach = Math.max(width, height) * 4;
+ let best: Point | undefined;
+ let score = Infinity;
+ const count = Math.ceil(reach / step);
+ for (let row = -count; row <= count; row++) {
+ for (let column = -count; column <= count; column++) {
+ const dx = Math.round(column * step);
+ const dy = Math.round(row * step);
+ const x = left + dx;
+ const y = top + dy;
+ if (
+ x < 0 ||
+ y < 0 ||
+ x + width > dimensions[0] ||
+ y + height > dimensions[1]
+ ) {
+ continue;
+ }
+ if (
+ x < left + width &&
+ x + width > left &&
+ y < top + height &&
+ y + height > top
+ ) {
+ continue;
+ }
+ let error = 0;
+ for (let c = 0; c < 3; c++) {
+ let sum = 0;
+ let squared = 0;
+ for (const [px, py] of border) {
+ const difference = color(px, py, c) - color(px + dx, py + dy, c);
+ sum += difference;
+ squared += difference * difference;
+ }
+ // Removing most of the mean allows the heal solver to match illumination.
+ error += squared - (0.8 * sum * sum) / border.length;
+ }
+ // Similar nearby texture is less conspicuous and avoids surprising jumps
+ // to a distant region when several candidates have comparable borders.
+ error += (0.02 * (dx * dx + dy * dy)) / (reach * reach);
+ if (error < score) {
+ score = error;
+ best = [dx, dy];
+ }
+ }
+ }
+ if (!best) {
+ throw Error(
+ "No clean source fits nearby. Use a smaller brush or Alt-click a source.",
+ );
+ }
+ return best;
+}
+
+/** Owns one thumbnail pass, reused for each automatic source search. */
+export function createHealSearch(gpu: Gpu, timer?: Timer) {
+ const graph = createRenderGraph(gpu, timer);
+ return {
+ async find(
+ image: Target,
+ dimensions: readonly number[],
+ stroke: BrushStroke,
+ ) {
+ const ratio = Math.min(1, 512 / Math.max(...image.size));
+ const size: [number, number] = [
+ Math.max(1, Math.round(image.size[0] * ratio)),
+ Math.max(1, Math.round(image.size[1] * ratio)),
+ ];
+ const [thumbnail] = graph.render([
+ merge(
+ { source: input(image) },
+ node("heal/search-thumbnail", shader, {
+ size,
+ set: { params: { size } },
+ samplers: {
+ linearSampler: { minFilter: "linear", magFilter: "linear" },
+ },
+ }),
+ ),
+ ]);
+ return matchSource(
+ await thumbnail.readFloats(),
+ size,
+ dimensions,
+ stroke,
+ );
+ },
+ inspect: graph.inspect,
+ dispose: graph.dispose,
+ };
+}
diff --git a/src/features/heal/source.wgsl b/src/features/heal/source.wgsl
new file mode 100644
index 0000000..0234dc2
--- /dev/null
+++ b/src/features/heal/source.wgsl
@@ -0,0 +1,7 @@
+struct Params { size: vec2f }
+@group(0) @binding(0) var source: texture_2d<f32>;
+@group(0) @binding(1) var linearSampler: sampler;
+@group(0) @binding(2) var<uniform> params: Params;
+@fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {
+ return textureSampleLevel(source, linearSampler, p.xy / params.size, 0.0);
+}
diff --git a/src/features/layers/brush-options.tsx b/src/features/layers/brush-options.tsx
index 1fa6324..5439084 100644
--- a/src/features/layers/brush-options.tsx
+++ b/src/features/layers/brush-options.tsx
@@ -1,6 +1,6 @@
+import { useBrushTool } from "@/components/editor/brush-tool";
+import { barSlider, useBarDensity } from "@/components/editor/toolbar-density";
import { Slider } from "@/components/ui/slider";
-import { useBrushTool } from "./brush-tool";
-import { barSlider, useBarDensity } from "./toolbar";
const modes = [
["paint", "Paint", "Add coverage"],
@@ -9,7 +9,7 @@ const modes = [
/** The next stroke's mode, size, edge, and flow, in the bar over the canvas. Alt shows on the Erase chip. */
export function BrushOptions() {
- const { settings, erase, maxSize, update } = useBrushTool();
+ const { settings, erase, maxSize, setPreview, update } = useBrushTool();
const variant = barSlider(useBarDensity());
return (
<>
@@ -36,7 +36,9 @@ export function BrushOptions() {
min={1}
max={maxSize}
unit="px"
+ valueWidth={`${maxSize}`.length}
variant={variant}
+ onEditingChange={setPreview}
onChange={(size) => update({ size: Math.round(size) })}
/>
<Slider
@@ -46,7 +48,9 @@ export function BrushOptions() {
max={100}
defaultValue={50}
unit="%"
+ valueWidth={3}
variant={variant}
+ onEditingChange={setPreview}
onChange={(value) => update({ feather: value / 100 })}
/>
<Slider
@@ -56,6 +60,7 @@ export function BrushOptions() {
max={100}
defaultValue={100}
unit="%"
+ valueWidth={3}
variant={variant}
onChange={(value) => update({ flow: value / 100 })}
/>
diff --git a/src/features/layers/brush-overlay.tsx b/src/features/layers/brush-overlay.tsx
index dddcfdd..6be1666 100644
--- a/src/features/layers/brush-overlay.tsx
+++ b/src/features/layers/brush-overlay.tsx
@@ -1,45 +1,17 @@
-import { type PointerEvent, useEffect, useId, useRef, useState } from "react";
-import { useDocumentMapping } from "@/components/editor/mapping";
+import { useEffect, useRef } from "react";
+import { BrushCanvas } from "@/components/editor/brush-canvas";
+import { useBrushTool } from "@/components/editor/brush-tool";
import { useDocument } from "@/components/editor/session";
-import { useViewport } from "@/components/editor/viewport";
-import { findLayer, type StrokePoint } from "@/core/document";
-import type { Point } from "@/core/image/frame";
-import { useShortcuts } from "@/hooks/use-shortcuts";
-import { useBrushTool } from "./brush-tool";
+import { findLayer } from "@/core/document";
import { extendStroke, paintStroke } from "./edits";
import { useMaskTool } from "./mask-tool";
-type Stroke = {
- pointer: number;
- /** The viewport bounds measured once; pointer capture keeps them valid for the drag. */
- box: DOMRect | undefined;
- id: string;
- pending: StrokePoint[];
- frame?: number;
-};
-
-type PointerLike = {
- clientX: number;
- clientY: number;
- pressure: number;
- pointerType: string;
-};
-
-/**
- * Paints into the selected brush mask, which the tool keeps selected: choosing the tool creates that
- * mask at once, so adjustments before the first stroke belong to it, and leaving with it untouched
- * removes it and its history entry. Once the selection is no longer a brush mask, the tool leaves.
- */
+/** Keeps a brush mask selected; an untouched new mask disappears on leaving. */
export function BrushOverlay() {
const document = useDocument();
const tool = useMaskTool();
const brush = useBrushTool();
- const camera = useViewport();
- const mapping = useDocumentMapping();
- const stroke = useRef<Stroke | null>(null);
- const [pointer, setPointer] = useState<Point | null>(null);
- const { erase } = brush;
- const gradient = useId();
+ const painting = useRef<string | undefined>(undefined);
function selectedBrush() {
const layer = findLayer(
document.scene.getState().layers,
@@ -49,41 +21,6 @@ export function BrushOverlay() {
? layer.id
: undefined;
}
- function point(event: PointerLike, box: DOMRect | undefined): StrokePoint {
- const [x, y] = mapping.toDocument(event.clientX, event.clientY, box);
- return [x, y, event.pointerType === "pen" ? event.pressure : 1];
- }
- /** Ends the stroke; a cancelled stroke restores the scene. */
- function finish(commit: boolean) {
- const current = stroke.current;
- if (!current) {
- return;
- }
- stroke.current = null;
- if (current.frame !== undefined) {
- cancelAnimationFrame(current.frame);
- }
- if (commit) {
- flush(current);
- document.history.commit();
- return;
- }
- document.history.cancel();
- }
- function flush(current: Stroke) {
- current.frame = undefined;
- if (!current.pending.length) {
- return;
- }
- const points = current.pending;
- current.pending = [];
- try {
- extendStroke(document, current.id, points);
- } catch {
- finish(false);
- }
- }
- useEffect(() => () => finish(false), []);
useEffect(() => {
// A chosen nesting starts a new brush even over a selected one.
const before =
@@ -106,130 +43,25 @@ export function BrushOverlay() {
}
};
}, []);
- useShortcuts({
- escape: () => (stroke.current ? finish(false) : tool.edit()),
- enter: () => {
- if (!stroke.current) {
- tool.edit();
- }
- },
- "[": () => brush.update({ size: Math.round(brush.settings.size / 1.25) }),
- "]": () => brush.update({ size: Math.round(brush.settings.size * 1.25) }),
- });
- function start(event: PointerEvent<HTMLDivElement>) {
- if (event.button !== 0 || !event.isPrimary || camera.panMode) {
- return;
- }
- const id = selectedBrush();
- if (!id) {
- return;
- }
- const box = camera.ref.current?.getBoundingClientRect();
- document.history.begin();
- paintStroke(document, id, {
- mode: erase ? "erase" : "paint",
- size: brush.settings.size,
- feather: brush.settings.feather,
- flow: brush.settings.flow,
- points: [point(event, box)],
- });
- stroke.current = { pointer: event.pointerId, box, id, pending: [] };
- // The viewport below would otherwise capture the pointer to pan.
- event.preventDefault();
- event.stopPropagation();
- event.currentTarget.setPointerCapture(event.pointerId);
- if (window.document.activeElement instanceof HTMLElement) {
- window.document.activeElement.blur();
- }
- }
- function move(event: PointerEvent<HTMLDivElement>) {
- const bounds = event.currentTarget.getBoundingClientRect();
- setPointer([event.clientX - bounds.left, event.clientY - bounds.top]);
- const current = stroke.current;
- if (!current || current.pointer !== event.pointerId) {
- return;
- }
- const native = event.nativeEvent;
- const events = native.getCoalescedEvents?.() ?? [];
- for (const item of events.length ? events : [native]) {
- current.pending.push(point(item, current.box));
- }
- // One edit per frame keeps the queue from outrunning the display.
- current.frame ??= requestAnimationFrame(() => flush(current));
- }
- function end(event: PointerEvent<HTMLDivElement>) {
- const current = stroke.current;
- if (!current || current.pointer !== event.pointerId) {
- return;
- }
- finish(true);
- event.currentTarget.releasePointerCapture(event.pointerId);
- }
- const radius = brush.settings.size / 2 / mapping.pixelsPerViewportPixel;
return (
- <div
- role="application"
- aria-label="Brush canvas"
- className="absolute inset-0 cursor-none touch-none data-[pan=true]:pointer-events-none"
- data-pan={camera.panMode}
- onDoubleClick={(event) => event.stopPropagation()}
- onPointerDown={start}
- onPointerMove={move}
- onPointerUp={end}
- onPointerLeave={() => setPointer(null)}
- onPointerCancel={() => finish(false)}
- onLostPointerCapture={() => finish(false)}
- >
- {pointer && !camera.panMode && (
- <svg
- aria-hidden="true"
- className="pointer-events-none absolute inset-0 size-full overflow-visible"
- >
- <defs>
- {/* The fill previews the dab: solid inside the feather, fading to the edge. */}
- <radialGradient id={gradient} cx="0.5" cy="0.5" r="0.5">
- <stop
- offset="0"
- stopColor={erase ? "black" : "white"}
- stopOpacity="0.3"
- />
- <stop
- offset={1 - brush.settings.feather}
- stopColor={erase ? "black" : "white"}
- stopOpacity="0.3"
- />
- <stop
- offset="1"
- stopColor={erase ? "black" : "white"}
- stopOpacity="0"
- />
- </radialGradient>
- </defs>
- <circle
- cx={pointer[0]}
- cy={pointer[1]}
- r={Math.max(2, radius)}
- fill={`url(#${gradient})`}
- stroke="white"
- strokeOpacity="0.9"
- strokeDasharray={erase ? "4 3" : undefined}
- />
- <circle
- cx={pointer[0]}
- cy={pointer[1]}
- r={Math.max(2, radius)}
- fill="none"
- stroke="black"
- strokeOpacity="0.5"
- strokeWidth="3"
- strokeDasharray={erase ? "4 3" : undefined}
- style={{ paintOrder: "stroke" }}
- />
- </svg>
- )}
- <p className="pointer-events-none absolute bottom-3 left-3 rounded-full bg-neutral-800/80 px-3 py-1.5 text-white backdrop-blur-sm">
- Drag to paint · Alt erases · [ ] resize · Enter when done
- </p>
- </div>
+ <BrushCanvas
+ label="Brush canvas"
+ hint="Drag to paint · Alt erases · [ ] resize · Enter when done"
+ erase={brush.erase}
+ onStart={(stroke) => {
+ const id = selectedBrush();
+ if (!id) return false;
+ painting.current = id;
+ paintStroke(document, id, stroke);
+ return true;
+ }}
+ onExtend={(points) => {
+ const id = painting.current;
+ if (id) {
+ extendStroke(document, id, points);
+ }
+ }}
+ onDone={() => tool.edit()}
+ />
);
}
diff --git a/src/features/layers/controls.tsx b/src/features/layers/controls.tsx
index 0803ea9..2b35378 100644
--- a/src/features/layers/controls.tsx
+++ b/src/features/layers/controls.tsx
@@ -8,8 +8,10 @@ import {
import { useStore } from "zustand";
import { PanelHeader } from "@/components/editor/panel";
import { useDocument, useScene } from "@/components/editor/session";
+import { HealIcon } from "@/components/icons/heal";
import { Icon } from "@/components/icons/icon";
import { Menu } from "@/components/ui/menu";
+import { PanelListItem } from "@/components/ui/panel-list";
import { ScrollArea } from "@/components/ui/scroll-area";
import {
TreeDrag,
@@ -29,31 +31,40 @@ import { useMaskTool } from "./mask-tool";
import { LayerActions, MaskNesting } from "./menu";
import { ImageThumbnail, MaskThumbnail } from "./thumbnails";
-function EffectSymbol({ kind }: { kind: ProcessingLayer["kind"] }) {
+type EffectKind = Exclude<ProcessingLayer["kind"], "mask" | "fill">;
+
+/** Each effect's mark in the layer stack; a new effect kind must name one. */
+function EffectIcon({ kind }: { kind: EffectKind }) {
switch (kind) {
+ case "heal":
+ return <HealIcon className="size-4" />;
case "color-mixer":
return (
- <>
+ <Icon className="size-4">
<circle cx="9" cy="9" r="5" />
<circle cx="15" cy="9" r="5" />
<circle cx="12" cy="15" r="5" />
- </>
+ </Icon>
);
case "details":
- return <path d="m4 18 8-14 8 14H4Zm8-8v6" />;
+ return (
+ <Icon className="size-4">
+ <path d="m4 18 8-14 8 14H4Zm8-8v6" />
+ </Icon>
+ );
case "vignette":
return (
- <>
+ <Icon className="size-4">
<circle cx="12" cy="12" r="8" />
<circle cx="12" cy="12" r="3" />
- </>
+ </Icon>
);
- default:
+ case "exposure":
return (
- <>
+ <Icon className="size-4">
<circle cx="12" cy="12" r="8" />
<path d="M12 4a8 8 0 0 1 0 16Z" fill="currentColor" stroke="none" />
- </>
+ </Icon>
);
}
}
@@ -77,9 +88,7 @@ function LayerThumbnail({ layer }: { layer: Layer }) {
}
return (
<span className="grid size-8 shrink-0 place-items-center rounded border border-black/50 bg-neutral-950/40 text-neutral-400">
- <Icon className="size-4">
- <EffectSymbol kind={layer.kind} />
- </Icon>
+ <EffectIcon kind={layer.kind} />
</span>
);
}
@@ -176,14 +185,15 @@ const LayerRow = memo(function LayerRow({
const expandLabel = `${expanded ? "Collapse" : "Expand"} ${layer.name}`;
return (
<>
- <div
+ <PanelListItem
ref={drag.ref}
data-drop={drag.drop}
data-dragging={drag.dragging}
- data-selected={selected === layer.id}
data-hidden={!visible}
+ selected={selected === layer.id}
+ muted={!visible}
style={{ paddingLeft: depth * 12 }}
- className="group relative flex h-10 items-center border-b border-black/50 pr-1 text-neutral-300 pointer-coarse:h-12 data-[selected=false]:hover:bg-white/5 data-[selected=true]:bg-neutral-700 data-[hidden=true]:text-neutral-500 data-[dragging=true]:opacity-40 data-[drop=inside]:ring-1 data-[drop=inside]:ring-blue-400 data-[drop=inside]:ring-inset data-[drop=before]:before:absolute data-[drop=before]:before:inset-x-0 data-[drop=before]:before:-top-px data-[drop=before]:before:border-t-2 data-[drop=before]:before:border-blue-400 data-[drop=after]:after:absolute data-[drop=after]:after:inset-x-0 data-[drop=after]:after:-bottom-px data-[drop=after]:after:border-b-2 data-[drop=after]:after:border-blue-400"
+ className="pr-1 data-[dragging=true]:opacity-40 data-[drop=inside]:ring-1 data-[drop=inside]:ring-blue-400 data-[drop=inside]:ring-inset data-[drop=before]:before:absolute data-[drop=before]:before:inset-x-0 data-[drop=before]:before:-top-px data-[drop=before]:before:border-t-2 data-[drop=before]:before:border-blue-400 data-[drop=after]:after:absolute data-[drop=after]:after:inset-x-0 data-[drop=after]:after:-bottom-px data-[drop=after]:after:border-b-2 data-[drop=after]:after:border-blue-400"
>
<button
type="button"
@@ -251,7 +261,7 @@ const LayerRow = memo(function LayerRow({
{layer.kind !== "image" && (
<LayerActions layer={layer} onSelect={onSelect} />
)}
- </div>
+ </PanelListItem>
{expanded &&
layer.children
.toReversed()
diff --git a/src/features/layers/edits.ts b/src/features/layers/edits.ts
index 33e4a56..f432ffe 100644
--- a/src/features/layers/edits.ts
+++ b/src/features/layers/edits.ts
@@ -12,40 +12,7 @@ import {
updateLayer,
walkLayers,
} from "@/core/document";
-
-function validPoints(points: readonly StrokePoint[]) {
- return (
- Array.isArray(points) &&
- points.length > 0 &&
- points.every(
- (point) =>
- Array.isArray(point) &&
- point.length === 3 &&
- point.every(Number.isFinite) &&
- point[2] >= 0 &&
- point[2] <= 1,
- )
- );
-}
-
-function validateStroke(stroke: BrushStroke) {
- if (
- (stroke.mode !== "paint" && stroke.mode !== "erase") ||
- !Number.isFinite(stroke.size) ||
- stroke.size <= 0 ||
- !Number.isFinite(stroke.feather) ||
- stroke.feather < 0 ||
- stroke.feather > 1 ||
- !Number.isFinite(stroke.flow) ||
- stroke.flow < 0 ||
- stroke.flow > 1 ||
- !validPoints(stroke.points)
- ) {
- throw Error(
- "A stroke needs a paint or erase mode, a positive size, feather and flow from 0 to 1, and finite points with pressure from 0 to 1.",
- );
- }
-}
+import { validateStroke, validPoints } from "@/core/document/brush";
function validateMask(mask: Mask) {
if (mask.kind === "brush") {
diff --git a/src/features/layers/menu.tsx b/src/features/layers/menu.tsx
index 0bcd496..6fb7093 100644
--- a/src/features/layers/menu.tsx
+++ b/src/features/layers/menu.tsx
@@ -64,10 +64,14 @@ function LayerActionItems({
Move into
<Select
aria-label={`Move ${layer.name} into`}
- value=""
+ placeholder="Choose layer"
+ options={containers.map((item) => ({
+ value: item.id,
+ label: item.name,
+ }))}
className="max-w-32"
- onChange={(event) => {
- const target = findLayer(layers, event.target.value);
+ onChange={(id) => {
+ const target = findLayer(layers, id);
if (target) {
moveLayer(
document,
@@ -77,16 +81,7 @@ function LayerActionItems({
);
}
}}
- >
- <option value="" disabled>
- Choose layer
- </option>
- {containers.map((item) => (
- <option key={item.id} value={item.id}>
- {item.name}
- </option>
- ))}
- </Select>
+ />
</div>
)}
<div className="my-1 border-t border-white/10" />
diff --git a/src/features/layers/thumbnails.tsx b/src/features/layers/thumbnails.tsx
index e980db5..51b0c36 100644
--- a/src/features/layers/thumbnails.tsx
+++ b/src/features/layers/thumbnails.tsx
@@ -1,10 +1,10 @@
import { useEffect, useId, useRef, useState } from "react";
import { useGpu } from "vgpu-react";
-import { useRenderer } from "@/components/editor/pipeline";
+import { CoverageThumbnail } from "@/components/editor/coverage-thumbnail";
import { useDocument, useScene } from "@/components/editor/session";
import { Icon } from "@/components/icons/icon";
import type { MaskLayer } from "@/core/document";
-import { renderBitmap, renderCoverage } from "@/core/renderer";
+import { renderBitmap } from "@/core/renderer";
import { MaskFill } from "./mask-fill";
const frame =
@@ -55,69 +55,14 @@ export function ImageThumbnail() {
);
}
-/**
- * A mask's coverage: the renderer's raster when the mask has one, drawn again after each committed
- * change, else the gradient itself. An unpainted brush shows its tool.
- */
+/** A mask's coverage from the renderer's raster; an unpainted brush shows its tool and a gradient draws itself. */
export function MaskThumbnail({ layer }: { layer: MaskLayer }) {
const id = useId();
- const gpu = useGpu();
const document = useDocument();
- const renderer = useRenderer();
const sourceId = useScene((scene) => scene.layers[0].source);
const size = document.resources.get(sourceId).image.size;
- const canvas = useRef<HTMLCanvasElement>(null);
- const [raster, setRaster] = useState(false);
- // The layer version whose raster is ready: strokes in progress wait for their gesture to end.
- const [ready, setReady] = useState<MaskLayer>();
- useEffect(() => {
- function check() {
- const coverage = renderer.coverage(layer.id);
- setRaster(coverage !== undefined);
- if (coverage && !document.history.status.getState().editing) {
- setReady(layer);
- }
- }
- // A gesture's end renders nothing new when the scene already rendered in full.
- const unsubscribe = document.history.status.subscribe(check);
- const detach = renderer.subscribe(check);
- return () => {
- unsubscribe();
- detach();
- };
- }, [renderer, document, layer]);
- useEffect(() => {
- const coverage = ready && renderer.coverage(ready.id);
- if (!coverage) {
- return;
- }
- let active = true;
- renderCoverage(gpu, coverage, [64, 64])
- .then((bitmap) => {
- const context = canvas.current?.getContext("2d");
- if (active && context) {
- context.drawImage(bitmap, 0, 0);
- }
- bitmap.close();
- })
- .catch(() => {});
- return () => {
- active = false;
- };
- }, [gpu, renderer, ready]);
- if (raster) {
- return (
- <canvas
- ref={canvas}
- width={64}
- height={64}
- aria-label="Mask thumbnail"
- className={frame}
- />
- );
- }
- if (layer.mask.kind === "brush") {
- return (
+ const fallback =
+ layer.mask.kind === "brush" ? (
<span
role="img"
aria-label="Empty brush thumbnail"
@@ -127,19 +72,25 @@ export function MaskThumbnail({ layer }: { layer: MaskLayer }) {
<path d="M10 14a3 3 0 0 1-3 3c-1.5 0-3-1-3-1s2-1 2-2.5c0-1.5 1-2.5 2.5-2.5M11 13l7-7a1.4 1.4 0 0 0-2-2l-7 7" />
</Icon>
</span>
+ ) : (
+ <svg
+ aria-label="Gradient mask thumbnail"
+ role="img"
+ viewBox={`0 0 ${size[0]} ${size[1]}`}
+ className={frame}
+ >
+ <defs>
+ <MaskFill id={id} mask={layer.mask} />
+ </defs>
+ <rect width={size[0]} height={size[1]} fill={`url(#${id})`} />
+ </svg>
);
- }
return (
- <svg
- aria-label="Gradient mask thumbnail"
- role="img"
- viewBox={`0 0 ${size[0]} ${size[1]}`}
- className={frame}
- >
- <defs>
- <MaskFill id={id} mask={layer.mask} />
- </defs>
- <rect width={size[0]} height={size[1]} fill={`url(#${id})`} />
- </svg>
+ <CoverageThumbnail
+ id={layer.id}
+ version={layer}
+ label="Mask thumbnail"
+ fallback={fallback}
+ />
);
}
diff --git a/src/features/layers/toolbar.tsx b/src/features/layers/toolbar.tsx
index 1313fb7..22e4e21 100644
--- a/src/features/layers/toolbar.tsx
+++ b/src/features/layers/toolbar.tsx
@@ -1,13 +1,11 @@
-import {
- createContext,
- type ReactNode,
- useContext,
- useLayoutEffect,
- useRef,
- useState,
-} from "react";
+import { type ReactNode, useLayoutEffect, useRef, useState } from "react";
import { useStore } from "zustand";
import { useDocument, useScene } from "@/components/editor/session";
+import {
+ barSlider,
+ Density,
+ useBarDensity,
+} from "@/components/editor/toolbar-density";
import { Icon } from "@/components/icons/icon";
import { Menu } from "@/components/ui/menu";
import { Select } from "@/components/ui/select";
@@ -23,19 +21,6 @@ import { useShortcuts } from "@/hooks/use-shortcuts";
import { setLayer, setLayerMask, setMaskOperation } from "./edits";
import { useMaskTool } from "./mask-tool";
-/** How much room the bar has: sliders with bars, fields only, or a column inside the overflow menu. */
-export type BarDensity = "full" | "compact" | "menu";
-const Density = createContext<BarDensity>("full");
-export function useBarDensity() {
- return useContext(Density);
-}
-export function barSlider(density: BarDensity) {
- if (density === "menu") {
- return "panel";
- }
- return density === "full" ? "toolbar" : "compact";
-}
-
function MaskOptions({ layer }: { layer: MaskLayer }) {
const document = useDocument();
const tool = useMaskTool();
@@ -75,6 +60,7 @@ function MaskOptions({ layer }: { layer: MaskLayer }) {
max={100}
defaultValue={50}
unit="%"
+ valueWidth={3}
variant={barSlider(density)}
onChange={(value) => {
if (layer.mask.kind === "radial") {
@@ -92,16 +78,14 @@ function MaskOptions({ layer }: { layer: MaskLayer }) {
aria-label="Mask operation"
title="Combine with the parent mask"
value={layer.operation}
- onChange={(event) => {
- const operation = event.target.value;
- if (operation === "add" || operation === "subtract") {
- setMaskOperation(document, layer.id, operation);
- }
- }}
- >
- <option value="add">Add</option>
- <option value="subtract">Subtract</option>
- </Select>
+ options={[
+ { value: "add", label: "Add" },
+ { value: "subtract", label: "Subtract" },
+ ]}
+ onChange={(operation) =>
+ setMaskOperation(document, layer.id, operation)
+ }
+ />
)}
</>
);
@@ -119,6 +103,7 @@ function LayerOptions({ layer }: { layer: ProcessingLayer }) {
max={100}
defaultValue={100}
unit="%"
+ valueWidth={3}
variant={barSlider(density)}
onChange={(value) =>
setLayer(document, layer.id, { opacity: value / 100 })
@@ -145,8 +130,8 @@ export function CanvasToolbar({ children }: { children?: ReactNode }) {
);
const bar = useRef<HTMLFieldSetElement>(null);
const [step, setStep] = useState(0);
- const hasLayer = layer.kind !== "image";
- const shown = Boolean(children) || hasLayer;
+ const hasLayerOptions = layer.kind !== "image" && layer.kind !== "heal";
+ const shown = Boolean(children) || hasLayerOptions;
const content = `${Boolean(children)}/${layer.kind}/${layer.kind === "mask" ? layer.mask.kind : ""}`;
// New content or a resized canvas starts again from the roomiest layout.
useLayoutEffect(() => setStep(0), [content]);
@@ -173,15 +158,15 @@ export function CanvasToolbar({ children }: { children?: ReactNode }) {
return null;
}
const inlineTool = step < 3 ? children : null;
- const inlineLayer = step < 2 && hasLayer;
+ const inlineLayer = step < 2 && hasLayerOptions;
const menuTool = step >= 3 ? children : null;
- const menuLayer = step >= 2 && hasLayer;
+ const menuLayer = step >= 2 && hasLayerOptions;
return (
<fieldset
ref={bar}
aria-label="Layer options"
{...gesture}
- className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-1.5rem)] items-center gap-x-2.5 overflow-hidden rounded-full bg-neutral-800/80 p-1.5 backdrop-blur-sm"
+ className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-1.5rem)] items-center gap-x-2.5 overflow-hidden rounded-full bg-neutral-800/80 p-1 pr-2 backdrop-blur-sm"
>
<Density value={step === 0 ? "full" : "compact"}>
{inlineTool}
diff --git a/src/hooks/use-edit-gesture.ts b/src/hooks/use-edit-gesture.ts
index f6b9204..d9f9c9b 100644
--- a/src/hooks/use-edit-gesture.ts
+++ b/src/hooks/use-edit-gesture.ts
@@ -5,15 +5,16 @@ import {
useRef,
} from "react";
-type Edit = { begin: () => void; commit: () => void; cancel: () => void };
+type Edit = { begin: () => boolean; commit: () => void; cancel: () => void };
type Gesture = "pointerup" | "keyup";
-/** Groups pointer and repeated keyboard changes; text fields commit their own draft. */
+/** Groups pointer and repeated keyboard changes; text fields commit their own draft. A gesture inside an open group joins it and leaves the group to its opener. */
export function useEditGesture(edit: Edit) {
const active = useRef<Gesture | null>(null);
+ const opened = useRef(false);
function begin(end: Gesture) {
active.current = end;
- edit.begin();
+ opened.current = edit.begin() || opened.current;
}
useEffect(() => {
function finish(event: Event) {
@@ -21,14 +22,20 @@ export function useEditGesture(edit: Edit) {
return;
}
active.current = null;
- edit.commit();
+ if (opened.current) {
+ opened.current = false;
+ edit.commit();
+ }
}
function cancel() {
if (!active.current) {
return;
}
active.current = null;
- edit.cancel();
+ if (opened.current) {
+ opened.current = false;
+ edit.cancel();
+ }
}
const controller = new AbortController();
const { signal } = controller;
@@ -57,7 +64,7 @@ export function useEditGesture(edit: Edit) {
},
onPointerMoveCapture() {
if (active.current === "pointerup") {
- edit.begin();
+ opened.current = edit.begin() || opened.current;
}
},
onKeyDownCapture(event: KeyboardEvent) {
diff --git a/src/hooks/use-pan-zoom.ts b/src/hooks/use-pan-zoom.ts
index e373ad6..99e063c 100644
--- a/src/hooks/use-pan-zoom.ts
+++ b/src/hooks/use-pan-zoom.ts
@@ -65,6 +65,13 @@ function clamp(view: View, content: Size, viewport: Size): View {
};
}
+function inset(size: Size, padding: number): Point {
+ return [
+ Math.max(0, size[0] - padding * 2),
+ Math.max(0, size[1] - padding * 2),
+ ];
+}
+
/**
* Pan and zoom over `content` inside the element given `ref`.
* Zoom 1 is the initial fit: contain, but capped at 200%. `pan` is in CSS px from the viewport center.
@@ -73,7 +80,7 @@ function clamp(view: View, content: Size, viewport: Size): View {
export function usePanZoom(
state: Camera,
content: Size,
- { constrain = true } = {},
+ { constrain = true, padding = 0 } = {},
) {
const ref = useRef<HTMLDivElement>(null);
const pointers = useRef(new Map<number, Point>());
@@ -130,11 +137,13 @@ export function usePanZoom(
const viewport: Size = [element.clientWidth, element.clientHeight];
state.setState((view) => {
const result = next(view);
- return bounded ? clamp(result, content, viewport) : result;
+ return bounded
+ ? clamp(result, content, inset(viewport, padding))
+ : result;
}, true);
}
},
- [state, content, constrain],
+ [state, content, constrain, padding],
);
useLayoutEffect(() => update((view) => view), [update, viewport]);
@@ -191,15 +200,24 @@ export function usePanZoom(
constrain ? 1 : fitZoom.current * 0.1,
),
);
+ function track(pointerId: number, position: Point, element: Element) {
+ if (pointers.current.size === 0) {
+ boundedDrag.current = constrain && !panMode;
+ }
+ pointers.current.set(pointerId, position);
+ element.setPointerCapture(pointerId);
+ }
function startDrag(event: PointerEvent<HTMLElement>) {
if (event.button !== 0 || pointers.current.size === 2) {
return;
}
- if (pointers.current.size === 0) {
- boundedDrag.current = constrain && !panMode;
+ track(event.pointerId, [event.clientX, event.clientY], event.currentTarget);
+ }
+ /** Takes over a pointer a tool was tracking, so a second finger turns its stroke into a pinch. */
+ function adopt(pointerId: number, position: Point) {
+ if (ref.current && pointers.current.size < 2) {
+ track(pointerId, position, ref.current);
}
- pointers.current.set(event.pointerId, [event.clientX, event.clientY]);
- event.currentTarget.setPointerCapture(event.pointerId);
}
const handlers = {
onPointerDownCapture: (event: PointerEvent<HTMLElement>) => {
@@ -248,12 +266,13 @@ export function usePanZoom(
return {
ref,
+ adopt,
view,
viewport,
handlers,
resetView,
zoomBy,
panMode,
- scale: fitScale(content, viewport) * view.zoom,
+ scale: fitScale(content, inset(viewport, padding)) * view.zoom,
};
}
diff --git a/tests/brush.e2e.ts b/tests/brush.e2e.ts
index 4d8b46b..19112f4 100644
--- a/tests/brush.e2e.ts
+++ b/tests/brush.e2e.ts
@@ -57,7 +57,24 @@ test("paint a brush mask, adjust it in the sidebar, erase, and undo", async ({
const center = [bounds.x + bounds.width / 2, bounds.y + bounds.height / 2];
const from = [center[0] - 150 * scale, center[1]];
const to = [center[0] + 150 * scale, center[1]];
- await setField("Size", "200");
+ const brushCursor = canvas.locator('[data-brush-cursor="true"]');
+ const sizeField = options.getByRole("textbox", {
+ name: "Size",
+ exact: true,
+ });
+ await sizeField.focus();
+ await expect(brushCursor).toHaveAttribute("data-preview", "true");
+ await sizeField.fill("200");
+ await sizeField.press("Enter");
+ await expect(brushCursor).toHaveCount(0);
+ const featherField = options.getByRole("textbox", {
+ name: "Feather",
+ exact: true,
+ });
+ await featherField.focus();
+ await expect(brushCursor).toHaveAttribute("data-preview", "true");
+ await featherField.press("Enter");
+ await expect(brushCursor).toHaveCount(0);
await test.step("a drag paints the new brush mask, which shows its overlay", async () => {
await drag(page, from, to, 16);
const layers = (await state()).scene?.layers;
@@ -263,3 +280,65 @@ test("paint a brush mask, adjust it in the sidebar, erase, and undo", async ({
expect((await state()).selectedLayerId).toBe(initial.scene?.layers[0].id);
});
});
+
+test("a second finger during a touch stroke cancels it and pinches instead", async ({
+ browser,
+}) => {
+ const context = await browser.newContext({
+ viewport: { width: 390, height: 844 },
+ isMobile: true,
+ hasTouch: true,
+ deviceScaleFactor: 2,
+ });
+ try {
+ const page = await context.newPage();
+ await page.goto("/");
+ await page
+ .locator('input[type="file"]')
+ .setInputFiles("tests/fixtures/photo.svg");
+ await expect(
+ page.getByRole("textbox", { name: "Exposure", exact: true }),
+ ).toHaveValue("0.00");
+ await page.getByRole("tab", { name: "Brush", exact: true }).click();
+ const canvas = page.getByLabel("Brush canvas", { exact: true });
+ await expect(canvas).toBeVisible();
+ const zoom = page.locator('button[title="Fit to view"]');
+ const before = await zoom.textContent();
+ const bounds = await box(canvas);
+ const [cx, cy] = [
+ bounds.x + bounds.width / 2,
+ bounds.y + bounds.height / 2,
+ ];
+ const touch = await context.newCDPSession(page);
+ const send = (
+ type: "touchStart" | "touchMove" | "touchEnd",
+ points: number[][],
+ ) =>
+ touch.send("Input.dispatchTouchEvent", {
+ type,
+ touchPoints: points.map(([x, y]) => ({ x, y })),
+ });
+ await send("touchStart", [[cx - 40, cy]]);
+ await send("touchMove", [[cx - 20, cy + 10]]);
+ await send("touchStart", [
+ [cx - 20, cy + 10],
+ [cx + 40, cy],
+ ]);
+ for (let step = 1; step <= 6; step++) {
+ await send("touchMove", [
+ [cx - 20 - step * 8, cy + 10 + step * 4],
+ [cx + 40 + step * 8, cy - step * 4],
+ ]);
+ }
+ await send("touchEnd", []);
+ await expect(zoom).not.toHaveText(before ?? "");
+ const state = await page.evaluate(() => window.openlight.getState());
+ const mask = state.scene?.layers[1];
+ expect(
+ mask?.kind === "mask" && mask.mask.kind === "brush" && mask.mask.strokes,
+ ).toEqual([]);
+ expect("editing" in state.history && state.history.editing).toBe(false);
+ } finally {
+ await context.close();
+ }
+});
diff --git a/tests/editing.e2e.ts b/tests/editing.e2e.ts
index ca12615..dbcaf18 100644
--- a/tests/editing.e2e.ts
+++ b/tests/editing.e2e.ts
@@ -5,7 +5,7 @@ import { interpolatePchip } from "@/lib/math";
import { colorMixerEditing } from "./color-mixer-editing";
import { expect, test } from "./fixtures";
import { readImage, readPixel, readPreview } from "./images";
-import { box, drag } from "./pointer";
+import { box, choose, drag } from "./pointer";
function expectCentered(
actual: { x: number; y: number; width: number; height: number },
@@ -502,6 +502,8 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
await page.keyboard.press("ArrowLeft");
await page.keyboard.press("ArrowLeft");
await page.keyboard.press("ArrowLeft");
+ await expect(selected).toHaveAccessibleName("Healing");
+ await page.keyboard.press("ArrowLeft");
await expect(selected).toHaveAccessibleName("Adjust");
await expect(selected).toBeFocused();
await page.keyboard.press("e");
@@ -535,7 +537,7 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
await test.step("Enter activates focused crop panel buttons", async () => {
const before = await state();
await open.click();
- await aspect.selectOption({ label: "Square" });
+ await choose(page, aspect, "Square");
await page
.getByRole("button", { name: "Close", exact: true })
.press("Enter");
@@ -589,7 +591,7 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
] as const) {
await setFrame({ size: [300, 200] });
await open.click();
- if (!locked) await aspect.selectOption({ label: "Free" });
+ if (!locked) await choose(page, aspect, "Free");
const bounds = await box(selection);
const edge = page.getByRole("button", {
name: `Resize crop ${label}`,
@@ -629,8 +631,8 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
const before = await state();
await expect(output).toHaveAttribute("points", histogram ?? "");
await open.click();
- await expect(aspect).toHaveValue("1.5");
- await aspect.selectOption({ label: "Square" });
+ await expect(aspect).toContainText("Original");
+ await choose(page, aspect, "Square");
const bounds = await box(selection);
await drag(
page,
@@ -697,7 +699,7 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
expect((await state()).history).toEqual(before.history);
await open.focus();
await page.keyboard.press("c");
- await aspect.selectOption({ label: "Square" });
+ await choose(page, aspect, "Square");
await corner.press("Enter");
await expect(panel).toBeHidden();
await expectImage([800, 800], 128);
@@ -924,8 +926,8 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
await expect(
page.getByRole("slider", { name: "Rotation", exact: true }),
).toHaveValue("0");
- await expect(aspect).toHaveValue("1.5");
- await aspect.selectOption({ label: "Square" });
+ await expect(aspect).toContainText("Original");
+ await choose(page, aspect, "Square");
const fixed = await box(selection);
for (const direction of [
"ArrowLeft",
@@ -984,14 +986,14 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
return readFile(path);
}
const format = panel.getByRole("combobox", { name: "Format" });
- await format.selectOption("png");
+ await choose(page, format, "PNG");
await expect(panel.getByRole("textbox", { name: "Width" })).toHaveValue(
"1200",
);
await expect(panel.getByText(/kB|MB/)).toBeVisible();
expect(await readImage(page, await save("photo.png"))).toEqual(expected);
- await format.selectOption("jpeg");
- await expect(format).toHaveValue("jpeg");
+ await choose(page, format, "JPEG");
+ await expect(format).toContainText("JPEG");
await quality.fill("20");
await quality.press("Enter");
const small = await save("photo.jpg");
@@ -1006,7 +1008,7 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
).toBeLessThanOrEqual(3);
}
expect(large.length).toBeGreaterThan(small.length);
- await format.selectOption("webp");
+ await choose(page, format, "WebP");
const width = panel.getByRole("textbox", { name: "Width" });
await width.fill("600");
await width.press("Enter");
diff --git a/tests/fill.e2e.ts b/tests/fill.e2e.ts
index 7ad0fd1..93e7e83 100644
--- a/tests/fill.e2e.ts
+++ b/tests/fill.e2e.ts
@@ -1,6 +1,6 @@
import { expect, test } from "./fixtures";
import { readImage } from "./images";
-import { box, drag } from "./pointer";
+import { box, choose, drag } from "./pointer";
test("a color layer paints the image or a brush stroke with Photoshop blends", async ({
page,
@@ -24,16 +24,13 @@ test("a color layer paints the image or a brush stroke with Photoshop blends", a
expect((await readImage(page)).center).toEqual([128, 128, 128, 255]);
await test.step("Normal paints the color; Multiply darkens by it", async () => {
await addColor();
- await expect(page.getByRole("combobox", { name: "Blend" })).toHaveValue(
- "normal",
- );
+ const blend = page.getByRole("combobox", { name: "Blend" });
+ await expect(blend).toContainText("Normal");
const painted = (await readImage(page)).center;
for (const [channel, expected] of [240, 118, 60].entries()) {
expect(Math.abs(painted[channel] - expected)).toBeLessThanOrEqual(2);
}
- await page
- .getByRole("combobox", { name: "Blend" })
- .selectOption("multiply");
+ await choose(page, blend, "Multiply");
const multiplied = (await readImage(page)).center;
expect(multiplied[0]).toBeLessThan(128);
expect(multiplied[0]).toBeGreaterThan(multiplied[2] + 40);
diff --git a/tests/heal-gpu.ts b/tests/heal-gpu.ts
new file mode 100644
index 0000000..b8cd042
--- /dev/null
+++ b/tests/heal-gpu.ts
@@ -0,0 +1,106 @@
+import { effect, frame, init, target } from "vgpu";
+import { createImageLayer, createLayer } from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import type { Scene } from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+
+/** A dark spot crosses a luminance edge on an HDR gradient. Its donor crosses the same edge. */
+export async function renderHealReference() {
+ const gpu = await init();
+ const errors: string[] = [];
+ gpu.onError((error) => errors.push(error.message));
+ const image = target(gpu, { size: [256, 192], format: "rgba16float" });
+ const source = createImageSource(image);
+ const renderer = createEditorRenderer(gpu, source);
+ const shader = effect(
+ gpu,
+ `
+ @fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {
+ let background = vec3f(0.2 + p.x * 0.002 + select(0.0, 0.5, p.x >= 128.0), 0.3 + p.y * 0.001, 1.4);
+ let defect = distance(p.xy, vec2f(128.0, 96.0)) < 10.0 || distance(p.xy, vec2f(158.0, 156.0)) < 2.0;
+ return vec4f(select(background, vec3f(0.02), defect), 0.75);
+ }
+ `,
+ );
+ const scene: Scene = {
+ frame: imageFrame(image.size),
+ layers: [
+ createImageLayer("source", "Reference"),
+ {
+ ...createLayer("heal"),
+ patches: [
+ {
+ id: "spot",
+ feather: 0.25,
+ opacity: 1,
+ stroke: {
+ mode: "paint",
+ size: 56,
+ feather: 0,
+ flow: 1,
+ points: [[128, 96, 1]],
+ },
+ offset: [0, 60],
+ },
+ ],
+ },
+ ],
+ };
+ try {
+ frame(gpu, (frame) => frame.pass(image, shader));
+ const results = [];
+ const healing = scene.layers[1];
+ if (healing.kind !== "heal") throw Error("Healing layer missing.");
+ for (const feather of [0, 0.75]) {
+ const renderScene: Scene = {
+ ...scene,
+ layers: [
+ scene.layers[0],
+ {
+ ...healing,
+ patches: healing.patches.map((patch) => ({
+ ...patch,
+ feather,
+ })),
+ },
+ ],
+ };
+ for (const proxy of [false, true]) {
+ renderer.setDisplayScale(0.25);
+ await renderer.update(renderScene, undefined, proxy);
+ const output = renderer.fullImage();
+ const pixels = await output.readFloats();
+ const scale = 256 / output.size[0];
+ const samples = [
+ [128, 96],
+ [120, 96],
+ [136, 96],
+ [100, 96],
+ [158, 96],
+ [64, 64],
+ ].map(([x, y]) => {
+ const px = Math.floor(x / scale);
+ const py = Math.floor(y / scale);
+ const p = [(px + 0.5) * scale, (py + 0.5) * scale];
+ const i = (py * output.size[0] + px) * 4;
+ return {
+ actual: [...pixels.slice(i, i + 4)],
+ expected: [
+ 0.2 + p[0] * 0.002 + (p[0] >= 128 ? 0.5 : 0),
+ 0.3 + p[1] * 0.001,
+ 1.4,
+ 0.75,
+ ],
+ };
+ });
+ results.push({ feather, proxy, samples });
+ }
+ }
+ return { results, errors };
+ } finally {
+ renderer.dispose();
+ source.dispose();
+ gpu.dispose();
+ }
+}
diff --git a/tests/heal.e2e.ts b/tests/heal.e2e.ts
new file mode 100644
index 0000000..dbce56e
--- /dev/null
+++ b/tests/heal.e2e.ts
@@ -0,0 +1,352 @@
+import { expect, test } from "./fixtures";
+import { readImage } from "./images";
+import { box } from "./pointer";
+
+test("healing preserves an edge, alpha, and HDR texture at full and proxy resolution", async ({
+ page,
+}) => {
+ await page.goto("/tests/gpu.html");
+ const result = await page.evaluate(async () => {
+ const path = "/tests/heal-gpu.ts";
+ const { renderHealReference } = (await import(
+ path
+ )) as typeof import("./heal-gpu");
+ return renderHealReference();
+ });
+ expect(result.errors).toEqual([]);
+ for (const { feather, samples } of result.results) {
+ for (const [index, { actual, expected }] of samples.entries()) {
+ if (feather > 0 && index < 4) continue;
+ for (let channel = 0; channel < 4; channel++) {
+ expect(Math.abs(actual[channel] - expected[channel])).toBeLessThan(
+ 0.015,
+ );
+ }
+ }
+ }
+ const interactive = result.results.filter(({ proxy }) => proxy);
+ expect(interactive).toHaveLength(2);
+ for (let channel = 0; channel < 4; channel++) {
+ expect(
+ Math.abs(
+ interactive[0].samples[0].actual[channel] -
+ interactive[1].samples[0].actual[channel],
+ ),
+ ).toBeLessThan(0.002);
+ }
+});
+
+test("Healing paints patches, edits them, and undoes", async ({ page }) => {
+ await page.setViewportSize({ width: 1440, height: 1000 });
+ await page.goto("/");
+ await page
+ .locator('input[type="file"]')
+ .setInputFiles("tests/fixtures/photo.svg");
+ await expect(
+ page.getByRole("textbox", { name: "Exposure", exact: true }),
+ ).toHaveValue("0.00");
+ const before = await readImage(page, undefined, [
+ [350, 200],
+ [600, 400],
+ ]);
+ await page.keyboard.press("h");
+ const canvas = page.getByLabel("Healing canvas", { exact: true });
+ await expect(canvas).toBeVisible();
+ expect(await box(canvas)).toEqual(
+ await box(page.getByRole("region", { name: "Image canvas" })),
+ );
+ await expect(page.getByText("Paint to repair", { exact: false })).toHaveCount(
+ 0,
+ );
+ await expect(
+ page.getByRole("heading", { name: "Healing", exact: true }),
+ ).toBeVisible();
+ const toolbar = page.getByRole("group", { name: "Layer options" });
+ const initialToolbarWidth = await toolbar.evaluate(
+ (element) => element.getBoundingClientRect().width,
+ );
+ const size = page.getByRole("textbox", { name: "Size", exact: true });
+ const brushCursor = canvas.locator('[data-brush-cursor="true"]');
+ await size.focus();
+ await expect(brushCursor).toHaveAttribute("data-preview", "true");
+ await size.fill("300");
+ await size.press("Enter");
+ await expect(brushCursor).toHaveCount(0);
+ const feather = page.getByRole("textbox", { name: "Feather", exact: true });
+ await expect(feather).toHaveValue("10");
+ await feather.focus();
+ await expect(brushCursor).toHaveAttribute("data-preview", "true");
+ await feather.fill("0");
+ await feather.press("Enter");
+ await expect(brushCursor).toHaveCount(0);
+ expect(
+ await toolbar.evaluate((element) => element.getBoundingClientRect().width),
+ ).toBe(initialToolbarWidth);
+ const bounds = await box(canvas);
+ const scale = Math.min(bounds.width / 1200, bounds.height / 800, 2);
+ await page.mouse.click(
+ bounds.x + bounds.width / 2 - 250 * scale,
+ bounds.y + bounds.height / 2 - 200 * scale,
+ );
+ await expect
+ .poll(async () =>
+ page.evaluate(() => {
+ const { history } = window.openlight.getState();
+ return "editing" in history && history.editing;
+ }),
+ )
+ .toBe(false);
+ const state = await page.evaluate(() => window.openlight.getState());
+ const layer = state.scene?.layers.find((layer) => layer.kind === "heal");
+ expect(layer?.kind).toBe("heal");
+ if (layer?.kind !== "heal") {
+ throw Error("Heal layer missing");
+ }
+ expect(layer.patches).toHaveLength(1);
+ // A click in the canvas margin, where the brush cannot reach the image, creates nothing.
+ await page.mouse.click(bounds.x + 4, bounds.y + 4);
+ expect(
+ await page.evaluate(() => {
+ const { scene, history } = window.openlight.getState();
+ const healing = scene?.layers.find((layer) => layer.kind === "heal");
+ return {
+ patches: healing?.kind === "heal" ? healing.patches.length : 0,
+ editing: "editing" in history && history.editing,
+ };
+ }),
+ ).toEqual({ patches: 1, editing: false });
+ await expect(page.getByText("Patch 1", { exact: true })).toBeVisible();
+ const patchList = page.getByRole("list", { name: "Healing patches" });
+ await expect(
+ patchList.getByRole("button", { name: "Select patch 1" }),
+ ).toHaveAttribute("aria-pressed", "true");
+ await expect(canvas.locator('[data-heal-outline="destination"]')).toHaveCount(
+ 1,
+ );
+ await expect(canvas.locator('[data-heal-outline="source"]')).toHaveCount(1);
+ await expect(canvas.locator('[data-heal-source-handle="true"]')).toHaveCount(
+ 1,
+ );
+ await expect(
+ canvas.locator('[data-heal-destination-handle="true"]'),
+ ).toHaveCount(1);
+ await expect(canvas.locator("[data-heal-connector]")).toHaveCount(0);
+ await expect(
+ canvas.getByRole("button", { name: "Select patch 1" }),
+ ).toHaveCount(0);
+ await expect(
+ page.getByRole("textbox", { name: "Opacity", exact: true }),
+ ).toHaveValue("100");
+ await expect(
+ page.getByRole("textbox", { name: "Opacity", exact: true }),
+ ).toHaveCount(1);
+ await size.fill("120");
+ await size.press("Enter");
+ expect(
+ await page.evaluate(() => {
+ const healing = window.openlight
+ .getState()
+ .scene?.layers.find((item) => item.kind === "heal");
+ return healing?.kind === "heal" ? healing.patches[0].stroke.size : 0;
+ }),
+ ).toBe(300);
+ await size.fill("300");
+ await size.press("Enter");
+ expect(layer.patches[0].offset).not.toEqual([0, 0]);
+ const after = await readImage(page, undefined, [
+ [350, 200],
+ [600, 400],
+ [510, 200],
+ ]);
+ for (const channel of after.samples?.[0].slice(0, 3) ?? []) {
+ expect(Math.abs(channel - 128)).toBeLessThanOrEqual(5);
+ }
+ expect(after.samples?.[1]).toEqual(before.samples?.[1]);
+ expect(after.samples?.[2]).toEqual([128, 128, 128, 255]);
+ await page.evaluate(
+ ({ id, patch }) => window.openlight.setHealSource(id, patch, [0, 360]),
+ { id: layer.id, patch: layer.patches[0].id },
+ );
+ expect(
+ (await readImage(page, undefined, [[350, 200]])).samples?.[0][0],
+ ).toBeGreaterThan(120);
+ await page.keyboard.press("ControlOrMeta+z");
+ await page.keyboard.press("ControlOrMeta+z");
+ expect((await readImage(page, undefined, [[350, 200]])).samples?.[0]).toEqual(
+ before.samples?.[0],
+ );
+ // Alt-click explicitly chooses the next donor using the same document mapping.
+ await page.keyboard.down("Alt");
+ await page.mouse.click(
+ bounds.x + bounds.width / 2 - 250 * scale,
+ bounds.y + bounds.height / 2 + 160 * scale,
+ );
+ await page.keyboard.up("Alt");
+ const targetX = bounds.x + bounds.width / 2 - 250 * scale;
+ const targetY = bounds.y + bounds.height / 2 - 200 * scale;
+ await page.mouse.move(targetX, targetY);
+ await page.mouse.down();
+ await page.mouse.move(targetX, targetY + 60);
+ const drawingAnchor = canvas.locator('[data-heal-destination-anchor="true"]');
+ await expect(drawingAnchor).toHaveCount(1);
+ const drawingAnchorBounds = await drawingAnchor.boundingBox();
+ if (!drawingAnchorBounds) throw Error("Healing anchor is unavailable");
+ expect(
+ Math.abs(drawingAnchorBounds.x + drawingAnchorBounds.width / 2 - targetX),
+ ).toBeLessThan(2);
+ expect(
+ Math.abs(drawingAnchorBounds.y + drawingAnchorBounds.height / 2 - targetY),
+ ).toBeLessThan(2);
+ await page.mouse.up();
+ const manual = await page.evaluate(() => {
+ const layer = window.openlight
+ .getState()
+ .scene?.layers.find((layer) => layer.kind === "heal");
+ if (layer?.kind !== "heal") throw Error("Healing layer missing");
+ const patch = layer.patches.at(-1);
+ if (!patch) throw Error("Healing patch missing");
+ return {
+ id: patch.id,
+ y: Math.round(patch.stroke.points[0][1] + patch.offset[1]),
+ };
+ });
+ await expect(
+ page.getByRole("textbox", { name: "Source Y", exact: true }),
+ ).toHaveValue(`${manual.y}`);
+ expect((await readImage(page, undefined, [[350, 200]])).samples?.[0]).toEqual(
+ [128, 128, 128, 255],
+ );
+ await page.mouse.click(
+ bounds.x + bounds.width / 2 + 250 * scale,
+ bounds.y + bounds.height / 2 + 200 * scale,
+ );
+ await expect
+ .poll(async () => {
+ const state = await page.evaluate(() => window.openlight.getState());
+ const healing = state.scene?.layers.find((item) => item.kind === "heal");
+ const editing = "editing" in state.history && state.history.editing;
+ return healing?.kind === "heal" && !editing ? healing.patches.length : 0;
+ })
+ .toBe(2);
+ const firstPatch = patchList.getByRole("button", { name: "Select patch 1" });
+ await firstPatch.hover();
+ await expect(canvas.locator(`[data-heal-patch="${manual.id}"]`)).toHaveCount(
+ 1,
+ );
+ await page.mouse.click(
+ bounds.x + bounds.width / 2 - 250 * scale,
+ bounds.y + bounds.height / 2 - 200 * scale,
+ );
+ await expect(firstPatch).toHaveAttribute("aria-pressed", "true");
+ expect(
+ await page.evaluate(() => {
+ const healing = window.openlight
+ .getState()
+ .scene?.layers.find((item) => item.kind === "heal");
+ return healing?.kind === "heal" ? healing.patches.length : 0;
+ }),
+ ).toBe(2);
+ const sourceX = firstPatch
+ .locator("..")
+ .getByRole("textbox", { name: "Source X", exact: true });
+ const beforeDrag = Number(await sourceX.inputValue());
+ const handle = canvas.locator('[data-heal-source-handle="true"]');
+ const handleBounds = await handle.boundingBox();
+ if (!handleBounds) throw Error("Healing source handle is unavailable");
+ await handle.hover();
+ await expect(canvas.locator("svg:has(radialGradient)")).toHaveCount(0);
+ await page.mouse.move(bounds.x + 10, bounds.y + 10);
+ await expect(canvas.locator("svg:has(radialGradient)")).toHaveCount(1);
+ await page.mouse.move(
+ handleBounds.x + handleBounds.width / 2,
+ handleBounds.y + handleBounds.height / 2,
+ );
+ await page.mouse.down();
+ await page.mouse.move(
+ handleBounds.x + handleBounds.width / 2 + 30,
+ handleBounds.y + handleBounds.height / 2,
+ );
+ await page.mouse.up();
+ await expect
+ .poll(async () => Number(await sourceX.inputValue()))
+ .not.toBe(beforeDrag);
+ const fixedSource = Number(await sourceX.inputValue());
+ const beforeDestination = await page.evaluate((patchId) => {
+ const healing = window.openlight
+ .getState()
+ .scene?.layers.find((item) => item.kind === "heal");
+ const patch =
+ healing?.kind === "heal"
+ ? healing.patches.find((item) => item.id === patchId)
+ : undefined;
+ return patch?.stroke.points[0][0];
+ }, manual.id);
+ const destination = canvas.locator('[data-heal-destination-handle="true"]');
+ const destinationBounds = await destination.boundingBox();
+ if (!destinationBounds) throw Error("Healing destination is unavailable");
+ await page.mouse.move(
+ destinationBounds.x + destinationBounds.width / 2,
+ destinationBounds.y + destinationBounds.height / 2,
+ );
+ await page.mouse.down();
+ await page.mouse.move(
+ destinationBounds.x + destinationBounds.width / 2 + 30,
+ destinationBounds.y + destinationBounds.height / 2 + 20,
+ );
+ await page.mouse.up();
+ await expect
+ .poll(async () =>
+ page.evaluate((patchId) => {
+ const healing = window.openlight
+ .getState()
+ .scene?.layers.find((item) => item.kind === "heal");
+ const patch =
+ healing?.kind === "heal"
+ ? healing.patches.find((item) => item.id === patchId)
+ : undefined;
+ return patch?.stroke.points[0][0];
+ }, manual.id),
+ )
+ .not.toBe(beforeDestination);
+ await expect(sourceX).toHaveValue(`${fixedSource}`);
+ await feather.fill("60");
+ await feather.press("Enter");
+ const opacity = page.getByRole("textbox", {
+ name: "Opacity",
+ exact: true,
+ });
+ await opacity.fill("50");
+ await opacity.press("Enter");
+ const thumbnail = patchList.getByLabel("Patch shape").first();
+ const thumbnailBytes = await thumbnail.screenshot();
+ const thumbnailMask = await page.evaluate(
+ async (bytes) => {
+ const image = await createImageBitmap(new Blob([new Uint8Array(bytes)]));
+ const canvas = new OffscreenCanvas(image.width, image.height);
+ const context = canvas.getContext("2d");
+ if (!context) throw Error("Cannot inspect patch thumbnail.");
+ context.drawImage(image, 0, 0);
+ image.close();
+ const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
+ const sample = (x: number, y: number) =>
+ pixels.data[(y * canvas.width + x) * 4];
+ return {
+ center: sample(
+ Math.floor(canvas.width / 2),
+ Math.floor(canvas.height / 2),
+ ),
+ edge: sample(Math.floor(canvas.width / 2), 2),
+ };
+ },
+ [...thumbnailBytes],
+ );
+ // The thumbnail is the patch's own raster: hard coverage, with feather and opacity left to the blend.
+ expect(thumbnailMask.edge).toBeLessThan(20);
+ expect(thumbnailMask.center).toBeGreaterThan(200);
+ await page.keyboard.press("Enter");
+ await expect(canvas).not.toBeVisible();
+ await page.getByRole("tab", { name: "Brush", exact: true }).click();
+ await expect(
+ page.getByRole("textbox", { name: "Feather", exact: true }),
+ ).toHaveValue("50");
+});
diff --git a/tests/heal.test.ts b/tests/heal.test.ts
new file mode 100644
index 0000000..3ac7a9f
--- /dev/null
+++ b/tests/heal.test.ts
@@ -0,0 +1,231 @@
+import { expect, test } from "bun:test";
+import { init, target } from "vgpu/mock";
+import { createImageLayer, createLayer } from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import {
+ type BrushStroke,
+ createDocument,
+ createResources,
+ findLayer,
+} from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import {
+ addHealPatch,
+ deleteHealPatch,
+ duplicateHealPatch,
+ extendHealPatch,
+ setHealDestination,
+ setHealPatch,
+ setHealSource,
+} from "@/features/heal/edits";
+import { addLayer, deleteLayer } from "@/features/layers/edits";
+
+function healFixture(size: readonly [number, number] = [64, 64]) {
+ const resources = createResources();
+ const document = createDocument(
+ {
+ frame: imageFrame(size),
+ layers: [createImageLayer("source", "Photo")],
+ },
+ resources,
+ );
+ const layer = addLayer(document, createLayer("heal"));
+ return { document, layer };
+}
+
+const dab: BrushStroke = {
+ mode: "paint",
+ size: 8,
+ feather: 0,
+ flow: 1,
+ points: [[10, 10, 1]],
+};
+
+function patchesOf(document: ReturnType<typeof createDocument>, layer: string) {
+ const healing = findLayer(document.scene.getState().layers, layer);
+ if (healing?.kind !== "heal") throw Error("Healing layer missing.");
+ return healing.patches;
+}
+
+test("heal patches reuse brush rasters, scale with the proxy, undo, and release with the layer", async () => {
+ const gpu = await init();
+ const source = createImageSource(
+ target(gpu, { size: [256, 192], format: "rgba16float" }),
+ );
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [createImageLayer(sourceId, "Photo")],
+ },
+ resources,
+ );
+ const renderer = createEditorRenderer(gpu, source);
+ try {
+ const id = addLayer(document, createLayer("heal"));
+ const stroke: BrushStroke = {
+ mode: "paint",
+ size: 30,
+ feather: 0.4,
+ flow: 1,
+ points: [[100, 80, 1]],
+ };
+ document.history.begin();
+ const patch = addHealPatch(document, id, stroke, [60, 0]);
+ const created = document.scene
+ .getState()
+ .layers.find((item) => item.id === id);
+ expect(
+ created?.kind === "heal" &&
+ created.patches.find((item) => item.id === patch),
+ ).toMatchObject({ feather: 0.4, stroke: { size: 30, feather: 0 } });
+ renderer.setDisplayScale(0.25);
+ await renderer.update(document.scene.getState(), id, true);
+ expect(renderer.fullImage().size).toEqual([64, 48]);
+ expect(renderer.inspect().rasters).toEqual([
+ { id: `layer/${id}/${patch}`, size: [256, 192] },
+ ]);
+ const stamped = renderer.inspect().stamped;
+ extendHealPatch(document, id, [[120, 80, 1]]);
+ await renderer.update(document.scene.getState(), id, true);
+ expect(renderer.inspect().stamped).toBeGreaterThan(stamped);
+ const extended = renderer.inspect().stamped;
+ document.history.commit();
+ await renderer.update(document.scene.getState());
+ expect(renderer.fullImage().size).toEqual([256, 192]);
+ expect(renderer.inspect().stamped).toBe(extended);
+ expect(renderer.inspect().passes.at(-1)).toBe(`layer/${id}/${patch}/blend`);
+ expect(renderer.inspect().passes.length).toBeGreaterThan(1);
+ expect(renderer.inspect().effects).toBeLessThan(15);
+ const textures = renderer.inspect().textures;
+ setHealSource(document, id, patch, [70, 0]);
+ await renderer.update(document.scene.getState());
+ expect(renderer.inspect().textures).toEqual(textures);
+ expect(renderer.inspect().stamped).toBe(extended);
+ document.history.undo();
+ document.history.undo();
+ await renderer.update(document.scene.getState());
+ expect(renderer.inspect().rasters).toEqual([]);
+ expect(renderer.inspect().passes).toEqual([]);
+ document.history.redo();
+ await renderer.update(document.scene.getState());
+ expect(renderer.inspect().rasters).toHaveLength(1);
+ expect(() =>
+ addHealPatch(document, id, { ...stroke, mode: "erase" }, [1, 2]),
+ ).toThrow("painted");
+ expect(() => setHealSource(document, id, patch, [NaN, 0])).toThrow(
+ "finite",
+ );
+ expect(() => setHealPatch(document, id, patch, { feather: NaN })).toThrow(
+ "feather",
+ );
+ setHealSource(document, id, patch, [0, 0]);
+ await renderer.update(document.scene.getState());
+ expect(renderer.inspect().effects).toBe(0);
+ expect(renderer.inspect().rasters).toEqual([]);
+ deleteLayer(document, id);
+ await renderer.update(document.scene.getState());
+ expect(renderer.inspect().rasters).toEqual([]);
+ } finally {
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
+test("one Healing layer composes its patches in order through render nodes", async () => {
+ const gpu = await init();
+ const resources = createResources();
+ const source = createImageSource(
+ target(gpu, { size: [128, 96], format: "rgba16float" }),
+ );
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [createImageLayer(sourceId, "Photo")],
+ },
+ resources,
+ );
+ const renderer = createEditorRenderer(gpu, source);
+ try {
+ const layer = addLayer(document, createLayer("heal"));
+ const stroke: BrushStroke = {
+ mode: "paint",
+ size: 24,
+ feather: 0.4,
+ flow: 1,
+ points: [[64, 48, 1]],
+ };
+ const first = addHealPatch(document, layer, stroke, [30, 0]);
+ const second = addHealPatch(document, layer, stroke, [-30, 0]);
+ await renderer.update(document.scene.getState(), layer);
+ expect(renderer.inspect().passes).toContain(
+ `layer/${layer}/${first}/blend`,
+ );
+ expect(renderer.inspect().passes).toContain(
+ `layer/${layer}/${second}/blend`,
+ );
+ await renderer.update(document.scene.getState(), first);
+ const firstInput = renderer.inputImage(first);
+ expect(firstInput).toBeDefined();
+ // A later patch sees the result of the earlier ones.
+ await renderer.update(document.scene.getState(), second);
+ expect(renderer.inputImage(second)).toBeDefined();
+ expect(renderer.inputImage(second)).not.toBe(firstInput);
+ const correctionGraph = renderer.inspect();
+ setHealDestination(document, layer, first, [80, 60]);
+ setHealPatch(document, layer, first, { feather: 0.2, opacity: 0.6 });
+ await renderer.update(document.scene.getState(), second);
+ expect(renderer.inspect().passes).toEqual(correctionGraph.passes);
+ expect(renderer.inspect().textures).toEqual(correctionGraph.textures);
+ const copy = duplicateHealPatch(document, layer, first);
+ expect(patchesOf(document, layer)).toMatchObject([
+ {
+ id: first,
+ stroke: { points: [[80, 60, 1]], size: 24, feather: 0 },
+ offset: [14, -12],
+ feather: 0.2,
+ opacity: 0.6,
+ },
+ { id: copy, offset: [14, -12] },
+ { id: second },
+ ]);
+ deleteHealPatch(document, layer, copy);
+ expect(patchesOf(document, layer).map((patch) => patch.id)).toEqual([
+ first,
+ second,
+ ]);
+ } finally {
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
+test("a gesture during a finishing stroke joins its undo step", () => {
+ const { document, layer } = healFixture();
+ try {
+ expect(document.history.begin()).toBe(true);
+ const patch = addHealPatch(document, layer, dab, [0, 0]);
+ // A slider or handle finds the group open and leaves it to the stroke.
+ expect(document.history.begin()).toBe(false);
+ setHealPatch(document, layer, patch, { feather: 0.5 });
+ setHealSource(document, layer, patch, [6, -2]);
+ document.history.commit();
+ expect(document.history.status.getState()).toMatchObject({
+ undoCount: 2,
+ editing: false,
+ });
+ expect(patchesOf(document, layer)[0]).toMatchObject({
+ feather: 0.5,
+ offset: [6, -2],
+ });
+ document.history.undo();
+ expect(patchesOf(document, layer)).toHaveLength(0);
+ } finally {
+ document.dispose();
+ }
+});
diff --git a/tests/layers.e2e.ts b/tests/layers.e2e.ts
index 3f66362..e8a0de2 100644
--- a/tests/layers.e2e.ts
+++ b/tests/layers.e2e.ts
@@ -2,7 +2,7 @@ import { writeFile } from "node:fs/promises";
import type { Page } from "@playwright/test";
import { expect, test } from "./fixtures";
import { readImage, readPreview } from "./images";
-import { box, drag } from "./pointer";
+import { box, choose, drag } from "./pointer";
async function samples(page: Page) {
const { samples } = await readImage(page, undefined, [
@@ -252,7 +252,7 @@ test("draw a mask, edit its child effects, reorder layers and undo", async ({
name: "Mask operation",
exact: true,
});
- await operation.selectOption("add");
+ await choose(page, operation, "Add");
expect((await samples(page))[0]).toEqual(masked[0]);
await page.getByRole("button", { name: "Undo", exact: true }).click();
expect((await samples(page))[0]).toEqual(original[0]);
diff --git a/tests/loaders.e2e.ts b/tests/loaders.e2e.ts
index 19aa204..908e60a 100644
--- a/tests/loaders.e2e.ts
+++ b/tests/loaders.e2e.ts
@@ -1,6 +1,7 @@
import { readFile } from "node:fs/promises";
import { expect, test } from "./fixtures";
import { readImage } from "./images";
+import { choose } from "./pointer";
test("decode an image, apply XMP, recover from failure, and replace a document during export", async ({
page,
@@ -52,9 +53,11 @@ test("decode an image, apply XMP, recover from failure, and replace a document d
await page.evaluate(() => window.openlight.redo());
expect(await readImage(page)).toEqual(expected);
await page.getByRole("tab", { name: "Crop" }).click();
- await page
- .getByRole("combobox", { name: "Aspect ratio" })
- .selectOption({ label: "Square" });
+ await choose(
+ page,
+ page.getByRole("combobox", { name: "Aspect ratio" }),
+ "Square",
+ );
const exported = await page.evaluate(async () => {
const api = window.openlight;
const convert = OffscreenCanvas.prototype.convertToBlob;
diff --git a/tests/pointer.ts b/tests/pointer.ts
index bf947f8..db281c5 100644
--- a/tests/pointer.ts
+++ b/tests/pointer.ts
@@ -19,3 +19,9 @@ export async function drag(
await page.mouse.move(to[0], to[1], { steps });
await page.mouse.up();
}
+
+/** Chooses from OpenLight's custom select surface through the visible UI. */
+export async function choose(page: Page, select: Locator, option: string) {
+ await select.click();
+ await page.getByRole("option", { name: option, exact: true }).click();
+}
diff --git a/tests/render-graph.test.ts b/tests/render-graph.test.ts
index fb99b8e..43f607a 100644
--- a/tests/render-graph.test.ts
+++ b/tests/render-graph.test.ts
@@ -127,3 +127,27 @@ test("a shared branch renders once and reuses effects, buffers, and temporary st
gpu.dispose();
}
});
+
+test("passes with identical uniforms can share prepared binding state", async () => {
+ const gpu = await init();
+ const image = target(gpu, { size: [8, 8], format: "rgba16float" });
+ const source = input(image);
+ const graph = createRenderGraph(gpu);
+ const first = merge(
+ { source, base: source },
+ node("first", shader, { ...options, instance: "shared" }),
+ );
+ const second = merge(
+ { source: first, base: source },
+ node("second", shader, { ...options, instance: "shared" }),
+ );
+ try {
+ graph.render([second]);
+ expect(graph.inspect().passes).toEqual(["first", "second"]);
+ expect(graph.inspect().effects).toBe(1);
+ } finally {
+ graph.dispose();
+ image.color.dispose();
+ gpu.dispose();
+ }
+});
diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.ts
index 7669a83..12059d0 100644
--- a/tests/rendering-benchmark.ts
+++ b/tests/rendering-benchmark.ts
@@ -27,6 +27,9 @@ export type Workload =
| "brush-exposure"
| "layer-stack"
| "fill"
+ | "heal"
+ | "heal-empty"
+ | "heal-proxy"
| "pipeline-proxy";
function summarize(values: number[]) {
@@ -107,7 +110,7 @@ export async function benchmarkRendering(
workload === "pipeline" ||
workload === "pipeline-input" ||
workload === "pipeline-proxy";
- const proxy = workload === "pipeline-proxy";
+ const proxy = workload === "pipeline-proxy" || workload === "heal-proxy";
const detail = combined || workload === "detail";
const effects: ProcessingLayer[] = [];
const common = { visible: true, opacity: 1, children: [] };
@@ -180,6 +183,36 @@ export async function benchmarkRendering(
fill: { color: "#f0763c", blend: "soft-light" },
});
}
+ if (
+ workload === "heal" ||
+ workload === "heal-empty" ||
+ workload === "heal-proxy"
+ ) {
+ effects.push({
+ ...common,
+ id: "benchmark-heal",
+ name: "Heal",
+ kind: "heal",
+ patches:
+ workload === "heal-empty"
+ ? []
+ : [
+ {
+ id: "spot",
+ feather: 0.4,
+ opacity: 1,
+ stroke: {
+ mode: "paint",
+ size: 240,
+ feather: 0,
+ flow: 1,
+ points: [[size[0] / 2, size[1] / 2, 1]],
+ },
+ offset: [320, 0],
+ },
+ ],
+ });
+ }
const scene: Scene = {
frame: imageFrame(size),
layers: [
@@ -312,9 +345,10 @@ export async function benchmarkRendering(
pixelHash: [...pixelHash]
.map((value) => value.toString(16).padStart(2, "0"))
.join(""),
- // All graph targets in these workloads are rgba16float. Source and driver memory excluded.
+ // Image targets use rgba16float. Source and driver memory are excluded.
intermediateBytes: storage.textures.reduce(
- (sum, { size }) => sum + size[0] * size[1] * 8,
+ (sum, { size, format }) =>
+ sum + size[0] * size[1] * (format === "rgba32float" ? 16 : 8),
0,
),
// Brush rasters are r8unorm at source resolution, outside the graph.
diff --git a/tests/rendering.bench.ts b/tests/rendering.bench.ts
index f9ad577..74ce8d1 100644
--- a/tests/rendering.bench.ts
+++ b/tests/rendering.bench.ts
@@ -16,6 +16,9 @@ for (const workload of [
"brush-exposure",
"layer-stack",
"fill",
+ "heal",
+ "heal-empty",
+ "heal-proxy",
"pipeline-proxy",
] as const) {
test(`rendering ${workload}`, async ({ page, browser }, info) => {