AtomCut has no flipX property anywhere in its document schema. If you mirror a layer, what changes is a sign: transform.scale or transform.scaleY goes negative. That's it. There used to be a flag for this, and deleting it deleted a family of bugs at once, because the flag and the sign were two stored facts describing one idea, and two facts describing one idea will eventually disagree.
This is the smallest of the three data-model rules I keep coming back to when something in AtomCut's transform system misbehaves, and it's the easiest one to explain, so it's a good place to see the pattern clearly.
One fact, one field
Here's the shape of the bug, independent of any codebase: you have a boolean flipX and a numeric scale. Rendering multiplies them together — a flipped layer's effective scale is flipX ? -scale : scale. This works fine for a static layer. It breaks the moment you keyframe scale across a flip, because now you have two keyframe tracks (or one track and one non-animatable flag) that both have to agree about which side of zero the layer is on, and interpolation doesn't know about the flag at all. It interpolates the numbers it's holding. If the flag flips instantaneously at some frame while the scale value keeps sliding smoothly through its own range, you get a visible snap instead of a mirror that grows naturally out of a shrink.
The fix, stated as a rule, is boring on purpose: represent the fact once. A mirror isn't "flipped, and also scaled" — it's scaled by a negative number. There's exactly one animatable quantity, and it can cross zero like any other number, because that's what negative numbers do.
// packages/core/src/resize.ts
/**
* ## Mirroring is NEGATIVE SCALE — one fact, one representation
*
* Dragging a handle past the corner it is anchored to does not stop at zero: it
* flips the layer, exactly like Figma/Illustrator. That flip is stored as a
* negative `transform.scale` / `transform.scaleY` and NOTHING else. There is no
* `flipX` boolean anywhere in the document — a boolean plus a scale would be two
* stored fields describing one fact, and they would disagree the moment a
* keyframe interpolates one of them.
*/Geometry doesn't get to go negative
The corollary is where this gets interesting, and where a naive version of the fix would still be wrong. If mirroring is "just" a negative scale, what happens to a shape's width, or a text box's wrap width? Those can't go negative — a corner radius, a stroke join, a line wrapper, all assume a positive magnitude. So the rule splits into two halves: the transform can go negative, but geometry never does.
// packages/core/src/resize.ts
* The corollary is the reason this module hands back MAGNITUDE and SIGN
* separately: geometry never goes negative. A shape's `width`, a text box's
* `maxWidth`, a crop inset — those stay positive magnitudes, because a negative
* width is meaningless to a corner radius, a stroke join or a line wrapper. So a
* native-geometry resize that crosses its anchor grows the geometry back up from
* zero AND multiplies the layer's scale by -1. Both modes end up mirrored; only
* one of them changed a dimension.That's the actual cost of the rule, and I'll come back to it: because two different quantities (the transform's sign and the geometry's magnitude) can each be doing the work of "you dragged past the anchor," the solver has to know, for every resize mode, which one it's supposed to touch. That's more branches in resize.ts. It's fewer facts in the document, which is the trade I want, but it's not free.
resize.ts is the one solver
Before this was centralized, the stage carried four independent copies of resize arithmetic — a single layer's handles, the multi-select group box, and the two native-geometry modes each had their own hand-rolled version, and they drifted. resize.ts is now the one function every resize gesture in the app calls, working in the box's own local axes so a rotated or sheared layer resizes and mirrors along its own edges instead of the world's:
// packages/core/src/resize.ts
export interface ResizeBox {
cx: number;
cy: number;
rotation: number;
halfW: number;
halfH: number;
sx: HandleSign;
sy: HandleSign;
pivot?: { x: number; y: number };
}
export interface ResizeConstraints {
centerAnchor: boolean;
keepAspect: boolean;
/** Allow the drag to cross its anchor and flip the layer. */
allowMirror: boolean;
}allowMirror exists because not everything should flip. An artboard or a crop window has no meaningful mirror, so those clamp at zero instead of crossing into negative territory — the same solver, a different constraint, rather than a second code path.
Where null earns its keep, and where it stops
Most layers scale uniformly, so storing a separate scaleY for every layer would be schema noise for the overwhelmingly common case. AtomCut represents that with scaleY: null, meaning "mirror scale":
// packages/core/src/resize.ts
export interface ScalePair {
scale: number;
/** `null` = uniform: Y mirrors X. Kept null whenever the two agree, to avoid schema noise. */
scaleY: number | null;
}
export function resolveScalePair(t: ScalePair): { x: number; y: number } {
return { x: t.scale, y: t.scaleY ?? t.scale };
}null works right up until the two axes need to disagree — the moment you flip only one of them. Then a single stored number genuinely can't express two independent signs, and the pair has to materialize into two concrete values:
// packages/core/src/resize.ts
/**
* Flip a transform across one axis. The trap this exists to prevent: a uniform
* layer stores `scaleY: null`, so negating `scale` alone would flip BOTH axes —
* a 180° rotation, not a mirror. Flipping X therefore MATERIALIZES the Y scale
* first.
*/
export function flipScalePair(t: ScalePair, axis: "x" | "y"): ScalePair {
const { x, y } = resolveScalePair(t);
if (axis === "x") {
const nx = -x;
return { scale: nx, scaleY: nx === y ? null : y };
}
const ny = -y;
return { scale: x, scaleY: ny === x ? null : ny };
}The trap in that comment is worth sitting with, because it's the kind of bug that only shows up once you've already fixed the bigger one. If you naively negate scale on a layer where scaleY is null, you haven't mirrored the layer — you've rotated it 180 degrees, because both axes flipped together. flipScalePair has to materialize the resolved Y value first, then decide whether the result still counts as uniform (collapsing back to null only when the two axes happen to agree again).
The pivot is the same rule, applied to a different fact
The transform's x/y position a layer's pivot, the point rotation, scale and shear act about, and that pivot is stored as a normalized fraction of the layer's own box, not a pixel offset. {0.5, 0.5} is the box center. This is the same "one fact, one representation" instinct pointed at a different problem: a pixel offset would quietly stop meaning what it meant the moment the layer resizes, rescales, re-wraps, or has its media swapped, because a pixel coordinate depends on a box that just changed size:
// packages/core/src/transform-map.ts
/**
* The pivot is stored NORMALIZED (a fraction of the layer's own box: 0 = left/
* top edge, 0.5 = center, 1 = right/bottom edge, and values outside 0..1 are
* legal and put the pivot outside the layer). Normalized, not pixels, because
* the pivot is a statement about the SHAPE ("spin about my bottom-right
* corner"), and a pixel offset would quietly stop meaning that the moment the
* layer is resized, rescaled, re-wrapped or its media swap. This is the same
* reason CSS `transform-origin` takes percentages.
*/There's one more twist specific to paths, since a path's box isn't declared, it's derived from the outline. Dragging a node changes the box, which would silently move the pivot's point if the fraction were left alone — so pivot.ts rebases the stored fraction on every content edit, holding the pivot's world point still while the number that describes it changes underneath:
// packages/core/src/pivot.ts
/**
* A content edit edits content. It must not move the layer, and it must
* not move the pivot the user placed on it.
*/resize.ts, transform-map.ts and pivot.ts are all in packages/core/src if you want to see the whole solver. The repo's here.
