sg

← Writing

What an Eraser Erases

· systems

From mid-May, Fractal's default eraser deleted the strokes it touched. The canvas is split into tiles, and a long line is stored as one piece per tile. A drag across a line removed whole tile-sized pieces of it, not the part under the brush. I made delete the default because it was easier to clear things that way while testing, and then it stuck for months. The mode that could leave a clean gap was already in the code, sitting fourth in a picker and labeled "Erase along path".

What I wanted was what a pencil eraser does: cut a gap through a line, shave the edge of a broad marker stroke or make a hole in the middle of one, and touch nothing else.

That sounds like one feature, but on Fractal's canvas it's a choice about what the drawing says afterward. Every stroke is stored as a centerline, a list of points down its middle, plus a width. Strokes also live at depths, the canvas's zoom levels from 0 to 24. An eraser has to decide what happens to those strokes, at every depth. That choice was mine; agents wrote the code that carried it out (more on working that way).

The question had come up before. In March, the plan for Fractal's first eraser scored a list of approaches, and the newer plan, for this change, weighed four meanings of erase. One was rejected, one wasn't needed, one I deferred, and one shipped.

Paint the background color#

The cheapest eraser is a white pen, and Fractal's paper is white in every theme. The trouble is that a white pen is still paint, and paint follows paint rules. The client multiplies a stroke's opacity by its depth's opacity. So on a depth faded to 50%, a white stroke would cover only half of what it was meant to remove. Real erasers skip that step and always remove at full strength.

The server would notice too. It sends a cell's image only if some pixel isn't fully transparent, and an empty cell gets an empty reply. A cell scrubbed with white paint isn't empty, so instead of that empty reply it would ship a PNG of solid white pixels. The canvas would keep sending pictures of nothing. The plan rejected this option because white paint doesn't make anything transparent.

Split the centerline#

The second option works with how strokes are stored instead of painting over them. Cut the point list where the eraser crosses it and keep the shorter lines on either side. That handles a gap across a thin line. It can't shave the side of a thick mark, because an eraser running along the edge never crosses the centerline. It can't make a hole either, since a hole sits inside the width with the centerline running through it. Points plus a width have no way to say "this stroke, minus a disc in the middle". The plan marked this one as not needed for what I asked for.

Subtract the geometry#

The third option does everything I asked for, and more. Turn the stroke into its filled outline, cut the eraser's shape out of it, and store each leftover piece as its own stroke. It's the only one of the four where the halves of a cut line become things you can pick up and move.

The cost is that the rest of Fractal doesn't work with outlines. The server's rasterizer, which turns strokes into pixels, does build an outline for every tool, but only to paint it, and the stored stroke is still a centerline. Selecting a stroke measures distance to that centerline, and the client draws constant-width tools straight from the points. Storing outlines would mean changing each of those. The newer plan lists the cost as selecting, drawing, moving, history, and the math at deep zoom. The math matters because an eraser at depth 24 can cut a stroke drawn at depth 2, a scale ratio of about four million.

The March plan had no row for this option. Its closest was splitting strokes where the eraser crosses them. It rated that row best for the user but very complex, especially for undo and for several people drawing at once.

My reading of the multi-user part: the server won't let one session delete or move another session's stroke, and it skips edits at a locked depth. Subtraction rewrites the stroke it cuts, so erasing someone else's line would have needed its own rule. I deferred this option to keep the scope down.

What shipped: ordered masks#

The fourth option leaves every stroke as it is. An eraser is just one more stroke, tagged tool: "eraser", and it travels through the durable operation log the same way drawing does, so nothing new goes over the network.

The erasing comes from the order strokes are layered in. Client and server both draw strokes onto one surface in paint order. Paint goes on top of what's there (source-over), and an eraser removes what's under it at full strength (destination-out). Trimmed from the server's rasterizer:

go
func strokePaintFor(stroke Stroke) (strokePaint, bool) {
	if stroke.Tool == "eraser" {
		return strokePaint{alpha: 1, eraser: true}, true
	}
	// ...colour and opacity for paint
}

// compositeCoverage, per pixel and channel:
alpha := paint.alpha * coverage
source := channels[channel] * alpha
if paint.eraser {
	source = 0
}
target.Pix[at+channel] = uint8(math.Round(source + float64(target.Pix[at+channel])*(1-alpha)))

Alpha is how solid the eraser is at a pixel. An eraser keeps 1 - alpha of whatever is under it and adds nothing. Paint drawn after it is untouched, because it hasn't been applied yet.

The idea wasn't new, but it only became a good answer recently. The first eraser, in March, was already a destination-out stroke, but it only worked inside its own tile's offscreen image. An eraser at depth 10 couldn't reach a broad marker stored in a depth-2 tile.

What fixed that was the zoom rework, a later change to how strokes at different depths are layered. It gave every stroke an Order field, so strokes at every depth share one paint order, the order they were made in. The rework came with a reference scene that tests exactly this: a broad marker at depth 2, a small pen at depth 12, an eraser at depth 10 across a tile seam, then a marker at depth 4 from a second session. The eraser sits between the two earlier strokes in depth, so it has to reach both up and down. The last marker was drawn after the eraser, from a second session, so it has to stay whole. That has to hold at every zoom level.

The same shared order gives undo. One erase drag is one undo step, like a drawing, and undoing it brings the paint back. Redo puts the eraser back at its original place in the order, so it doesn't land on top of paint drawn in between.

Once every depth shared one paint order, making masks the default was a seven-line change to the toolbar. The next day the picker shrank to two choices, "Erase" and "Delete strokes", plus a "This depth only" checkbox. The old mode that deleted visible strokes went away.

"Delete strokes" still removes whole stored pieces, like the delete modes before it. It takes the part of each line stored in the tile the eraser touched, not just the part under the brush.

Two parallel diagonal lines on a two-by-two grid of tiles. Both have a short gap inside the lower-right tile, one directly above the other, and continue past it to their original ends.
Erase: a gap where the eraser crossed, nothing else touched.
The same two lines after the same pass. The upper line ends at the top edge of the lower-right tile, the lower line ends at its left edge, and nothing is drawn inside that tile.
Delete strokes: both lines lose their whole lower-right piece.
Same two lines, each crossing three tiles, same camera, one eraser pass down the lower-right tile. Dashed lines mark the tile edges.

What masks can't do#

Set side by side against what I asked for, masks give every outcome but one:

OutcomePaint the background colorSplit the centerlineSubtract the geometryOrdered masks
Removes paintNo, paints whiteYesYesYes, at full strength
Gap in a linePartly, white coverYesYesYes
Shaved edgePartly, white coverNo, misses the centerlineYesYes
HolePartly, white coverNoYesYes
Pieces you can moveNoTwo shorter linesYesNo, still one line
Plan's verdictRejectedNot neededDeferredShipped

Cut a line in half with the eraser and you still have one line. You can't move the left half or delete the right half on its own. Selection still checks the original shape, so you can select a stroke you've erased completely. Hide the depth the eraser was drawn at and the hole disappears with it. The hole belongs to the eraser, not the stroke.

Erasing also reaches further than deleting. Delete only removes your own strokes, and it skips locked depths. An eraser drawn at an unlocked depth cuts anyone's earlier paint at any depth, locked ones included. The zoom rework defined a lock as a limit on what you can draw or edit at that depth. It isn't a promise that paint there never changes. Instead of asking for confirmation, the help text says the eraser reaches every depth and anyone's paint.

Erasers also pile up, each one a stored stroke. The March plan accepted that, betting people don't erase the same spot hundreds of times. The newer plan chose not to measure it unless testing turned up a problem.

Editable pieces are still waiting, and the March verdict on splitting strokes still stands. Fill regions have moved on, though: they are now stored as areas outlined by closed loops, drawn on both client and server, and can be selected like strokes. Subtraction would turn strokes into filled outlines too, so editable pieces might build on that someday.