Skip to content

Free open-source image editor for the browser

Update healing live while its source moves (#46)

Rodrigo Belfiore8d75a89d2494
diff --git a/DESIGN.md b/DESIGN.md
index 031604d..96215d8 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -54,6 +54,6 @@ How the rail, canvas bar, sidebar, and layer stack behave. Commands they call ar
The sidebar always shows the output histogram, the layer stack, and the selected layer's controls under a title naming what they edit (Adjustments for the image, Mask adjustments for a mask, or the effect's kind); layers and controls scroll independently. The zoom percentage counts image pixels per device pixel: clicking it at fit zooms to 100%, and anywhere else returns to fit. 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 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 on top of the stack; 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 on top of the stack 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's actions menu has Add to mask and Subtract from mask submenus 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 with each one's brush size and any lowered opacity; 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 moves its donor. **Escape** cancels painting or pending processing; Enter leaves after completion. Leaving an untouched new layer removes it.
+**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 with each one's brush size and any lowered opacity; 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 moves its donor; both anchors update the repair while they are dragged, as one undo step. **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 beside a mask's adjustments title shows whether the overlay is visible; it and **O** show or hide the overlay for the selected mask in any tool, and the choice holds for the next mask 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. The Add and Subtract submenus offer either gradient or a brush. **+** 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/src/features/heal/outline.tsx b/src/features/heal/outline.tsx
index 6197777..ad9552c 100644
--- a/src/features/heal/outline.tsx
+++ b/src/features/heal/outline.tsx
@@ -1,4 +1,4 @@
-import { memo, type PointerEvent, useId, useState } from "react";
+import { memo, type PointerEvent, useId } from "react";
import { useDocumentMapping } from "@/components/editor/mapping";
import { useDocument } from "@/components/editor/session";
import type { BrushStroke, HealPatch } from "@/core/document";
@@ -114,16 +114,16 @@ export function HealPatchOutline({
}) {
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 source = showSource && geometry(patch.stroke, patch.offset, mapping);
+ // Both anchors edit live inside the drag's history group, so the repair follows the pointer.
const moveDestination = (next?: Point) => {
if (next) setHealDestination(document, layer, patch.id, next);
};
+ const moveSource = (next?: Point) => {
+ if (next) setHealSource(document, layer, patch.id, next);
+ };
const destinationDrag: AnchorDrag | undefined = interactive
? {
from: [first[0], first[1]],
@@ -134,8 +134,8 @@ export function HealPatchOutline({
const sourceDrag: AnchorDrag | undefined = interactive
? {
from: patch.offset,
- onDrag: setSourcePreview,
- onDrop: (next) => setHealSource(document, layer, patch.id, next),
+ onDrag: moveSource,
+ onDrop: moveSource,
}
: undefined;
return (
diff --git a/src/features/layers/toolbar.tsx b/src/features/layers/toolbar.tsx
index de25af5..295c693 100644
--- a/src/features/layers/toolbar.tsx
+++ b/src/features/layers/toolbar.tsx
@@ -90,8 +90,9 @@ export function CanvasToolbar({ children }: { children?: ReactNode }) {
ref={bar}
aria-label="Layer options"
{...gesture}
- // No wrapping, so running out of room overflows, which is what the steps above measure.
- className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-1.5rem)] items-center gap-x-2.5 overflow-hidden whitespace-nowrap rounded-full bg-neutral-800/80 p-1 pr-2 backdrop-blur-sm"
+ // No wrapping, so running out of room overflows, which is what the steps above measure. A button or
+ // group first nests its corners 4px in; a text label first, such as a slider's, needs 8px more.
+ className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-1.5rem)] items-center gap-x-2.5 overflow-hidden whitespace-nowrap rounded-full bg-neutral-800/80 p-1 pr-2 backdrop-blur-sm [&>:first-child:not(button,fieldset)]:ml-2"
>
<Density value={step === 0 ? "full" : "compact"}>
{inlineTool}
diff --git a/tests/heal.e2e.ts b/tests/heal.e2e.ts
index 8a5aba8..c8e14b3 100644
--- a/tests/heal.e2e.ts
+++ b/tests/heal.e2e.ts
@@ -286,8 +286,9 @@ test("Healing paints patches, edits them, and undoes", async ({ page }) => {
handleBounds.x + handleBounds.width / 2 + 30,
handleBounds.y + handleBounds.height / 2,
);
- await page.mouse.up();
+ // The repair follows the donor while it is dragged, not only once it drops.
await expect.poll(sourceX).not.toBe(beforeDrag);
+ await page.mouse.up();
const fixedSource = await sourceX();
const beforeDestination = await page.evaluate((patchId) => {
const healing = window.openlight