Skip to content

Selection brush and eraser

Selection brush

Fact Value
Status Implemented and tested
Group Brush
Engine Browser
Code web/src/core/mask/raster.ts, paintStroke

Selection eraser

Fact Value
Status Implemented and tested
Group Brush
Engine Browser
Code web/src/core/mask/raster.ts, paintStroke

Overview

The selection brush (B) paints the selection. The selection eraser (E) removes it. They are the most direct way to fix a selection that another tool made. A circle at the pointer shows the brush size.

How to use them

  1. Choose the brush or the eraser.
  2. Paint over the image. The selection updates while you paint.
  3. With the brush, hold Alt to remove instead of add.
  4. Turn on Quick Mask (Q) to see the result as a red tint over the unselected part.

How it works

Selection brush:

  1. Stamp the brush along the pointer path, with the brush value at each distance from the center.
  2. Keep the larger value where stamps overlap.
  3. Add the stroke to the selection, or remove it while Alt is held.
  4. Show the result while you paint, then add it to the history when you let go.

Selection eraser:

  1. Stamp the brush along the pointer path, keeping the larger value where stamps overlap.
  2. Lower the selection by the stroke: the result is the smaller of the selection and 255 minus the stroke.
  3. Show the result while you paint, then add it to the history when you let go.

Mathematical foundation

The brush value at distance \(d\) from the stroke, for a brush of radius \(r\) and hardness \(h\) from 0 to 1, is

\[ v(d) = \begin{cases} \operatorname{clamp}\big(r - d + \tfrac{1}{2},\ 0,\ 1\big) & \text{if } h = 1 \\ s^2 (3 - 2s), \quad s = \operatorname{clamp}\!\left(\dfrac{r - d}{e},\ 0,\ 1\right), \quad e = \max\!\big(\tfrac{1}{2},\ r (1 - h)\big) & \text{if } h < 1 \end{cases} \]

where \(d\) is the distance in pixels from the pixel center to the nearest point of the stroke, and \(e\) is the width of the soft edge. This is an exact definition of what the code computes. A hard brush has one pixel of anti-aliasing. A softer brush fades from full to none over the outer part of its radius, with a smooth S-shaped curve.

The stroke keeps the larger value where stamps overlap. Then, with \(m\) the selection and \(v\) the stroke:

  • the brush gives \(\max(m, v)\),
  • the eraser, or the brush with Alt, gives \(\min(m, 255 - v)\).

Parameters

Parameter Default Values Meaning
Size size 30 1 to 1000 px Diameter of the brush.
Hardness hardness 100 0 to 100 % How sharp the edge of the brush is.

The eraser has the same settings.

Implementation

While you paint, only the changed rectangle of the mask goes to the graphics card again. The stroke becomes one history step when you let go.

Limitations

  • A soft brush selects pixels only partly, so filters blend in near the edge.
  • The brush size is in image pixels, so it looks smaller when you zoom out.

Tests