Skip to content

fix(studio): a turned layer's cropped box is the bounds of its cut corners - #8502

Merged
miguel-heygen merged 14 commits into
mainfrom
fix/turned-crop-box-bounds
Nov 22, 2026
Merged

miguel-heygen merged 14 commits into
mainfrom
fix/turned-crop-box-bounds

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

What

A turned, cropped layer's axis-aligned boxes (its outline in a group, its snap target and its marquee hit box) are now the bounds of the layer after its crop is cut along its own edges. Before, the crop was cut out of the axis-aligned box itself, so a turned layer's box came out the wrong size in both kinds of preview: a 200 x 100 layer turned 45 degrees with a 50 px right crop drew 81.1 x 106.1 overlay px where its cut shape spans 88.4 x 88.4.

Why

A crop (clip-path: inset(...)) cuts the layer in its own space, before its transform. For a turned layer, its axis-aligned box is the bounds of its turned corners, so cutting insets straight off that box's sides removes the wrong amount from the wrong sides. The right box is: move each of the layer's own corners in by the insets along its own edges, then take the bounds of those four points. The selection's own box is already drawn turned with the layer and keeps cutting the turned box as before.

How

  • One cut for every crop outline, cropRectFromInsets (domEditOverlayCrop.ts): the insets, in the layer's own CSS px times its own scale, cut along its own edges, and the result is the bounds of the four cut corners. Without corners its own edges are the box's, so an upright layer is cut as before.
    • Insets that add up past the layer's width or height shrink in proportion until they meet, as CSS does for inset(), so a crop wider than its layer is a zero-width line at the same place in both previews.
  • Corners reach the cut the same way in both previews:
    • sealed: the frame now sends a layer's painted corners whenever it is turned, mirrored or skewed (boxOf, editingGeometry.ts). Before, it sent them only for a nonzero angle, and a mirror reports angle 0. A plain positive scale still sends none, since its corners say nothing its box doesn't;
    • unsealed: read off the live element only when it has a crop to cut (liveOwnScale), mapped onto the axis-aligned rect.
  • A rect already drawn turned (one with an angle) keeps cutting its own turned box.
  • One transform source for every overlay measure, composed by the browser (elementTransform.ts): each node's matrix is new DOMMatrix("<rotate> <scale> <transform>"). Its computed rotate and scale are written as the CSS functions they stand for (x 30deg becomes rotateX(30deg), 1 1 2 becomes scale3d(1, 1, 2)), followed by its computed transform, in the order CSS applies them. The browser does all the composing, 3D included. Corners in both previews and the crop frame's angle and scale (which reads a, b, c, d off the same matrix) come from it. The hand-written 2D algebra and the rotate and scale parsers are deleted.
    • A layer whose matrix leaves its plane gets an axis-aligned box, the one the browser paints: a turn about x or y, or perspective (any of m13, m23, m14, m24 nonzero, or m44 not 1). CSS flattens such a layer into its parent, which a product of matrices does not reproduce. The check runs on each node and on the chain composed so far, so a perspective parent and a depth-moved child, flat on their own, fall back too; the crop frame's own perspective check folds into it. A layer inside any 3D context, meaning its parent has a perspective property or transform-style: preserve-3d, falls back as well: those project children in ways the matrix walk does not see.
    • Known limit: a flat, turned layer inside such a 3D context gets an axis-aligned box too. Every 2D layer stays exact, including GSAP's matrix3d for a flat spin and a depth-only scale: 1 1 2, since planarity is read from the values rather than from DOMMatrix.is2D (false for any matrix3d or scale3d form).
    • translate is left out: it moves a box without changing its shape, corners are re-anchored to the measured box, and DOMMatrix throws on translate(50%) (checked in Chrome 158), which a centered layer commonly carries.

Test plan

  • fails without the fix: packages/studio/src/components/editor/previewOverlayRects.test.ts ("bounds a turned layer's cropped box by its cut corners, the same in both"): the 45 degree case through a scriptable frame and through a sealed one running the real editing agent. On the base it is 81.1 x 106.1 instead of 88.4 x 88.4.
  • fails without the fix: previewOverlayRects.test.ts ("cuts a turned, scaled layer's crop at its own scale, along its own edges, the same in both"): 30 degrees, scaled 2x. On the base the width is 173.2 where the cut's bounds are 179.9 (and 167.4 before fix(studio): a cropped layer's outline is cut at the layer's own scale, sealed or not #8496).
  • fails without the fix: previewOverlayRects.test.ts ("shrinks crossed insets until they meet, as CSS does, the same in both"): a half-scaled 200 px layer with inset(0 300px 0 100px). On the base the scriptable preview drew a 50 px box.
  • fails without the fix: previewOverlayRects.test.ts ("cuts a mirrored layer's right inset off its visual left, the same in both"): scaleX(-1) with a 50 px right inset, measured against the same layer unmirrored. On the base one preview cut the visual right edge (offset 0 where 25 is right).
  • fails without the fix: packages/studio/src/components/editor/overlayTransform.browser.test.ts ("reads every layer's painted corners as Chrome paints them"), in real Chrome. Each probe is a real CSS layer: transform: rotate(45deg); scale: 1 1 2, scale: 2, rotate: 45deg; scale: 2, a matrix, a mirror plus rotate, translate: 50% plus rotate, rotate: x 60deg, a nested pair turned about x, a flat spin written as matrix3d, and a scaled, turned child of a turned parent. The corners must span what Chrome's getBoundingClientRect paints, and the layer's own edges must have the expected scaled size. Layers turned out of their plane, rotate: x 60deg, a child turned x -60deg inside a parent turned x 60deg, a child moved translateZ(100px) under a perspective(500px) parent that preserves 3D, the same child under a parent's perspective: 500px property, and a flat turned child of a preserve-3d parent, must be declined so callers use the painted box. Without the planar checks the nested pair cancels to 200 x 100 where Chrome paints 200 x 25, and the projected pair measures 223 x 187 where Chrome paints 279 x 233; the test fails at each. An earlier walk failed it at the depth-scaled layer, which it could not read at all.
  • packages/core/src/runtime/elementTransform.test.ts: each computed rotate and scale form Chrome serializes, as its CSS function.
  • The crop frame's existing matrix/matrix3d cases (domEditOverlayCrop.test.ts) now run through the same matrix; their stand-in parses only those two forms, the only ones a computed transform takes.
  • With the change, on a Linux machine: the named tests (the Chrome test, studio overlay and crop, core geometry, gesture start and transform) pass 3 runs in a row; 68 covering test files in core and studio pass; tsc --noEmit in core and studio, Fallow and pre-push are clean. The unturned scaled case ("cuts a scaled layer's crop at its own scale") is unchanged.
  • Real Studio, from each commit's CLI build on a Linux machine (sealed confirmed by the frame's origin null): the layer's box in the group measures 29 x 61 px on fix(studio): a cropped layer's outline is cut at the layer's own scale, sealed or not #8496 in both kinds of preview, and 38 x 38 with this change in both (walked at 0cae8fa; the later commit changes only crossed insets and mirrored layers, neither in this walk).

Before

A cropped layer (clip-path: inset(0 150px 0 0)) scaled to 1.25 by GSAP and turned 45 degrees (rotate: 45deg), grouped with a plain square, in a sealed preview on #8496. The layer's box is a tall, narrow pill: the visible yellow shape sticks out past its right side, and the group box runs far below it.

before, sealed

After

The same, with this change: the layer's box wraps the visible turned shape, and the group box with it, sealed and unsealed alike.

after, sealed after, unsealed

Related work

Stacked on #8496 (a scaled layer's crop at its own scale), which this extends to turned layers.

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two parity regressions in the cropped bounds:

  • [P2] Opposing insets can cross in cutCornersBounds. The min/max step then turns the crossed corners into a positive box. In a focused parity probe, a 200 px layer with inset(0px 120px 0px 120px) produced a 20 px-wide box in the scriptable preview at 0.5x and zero width in the sealed preview. CSS reduces opposing insets proportionally when their sum exceeds the reference dimension. Normalize them in the layer’s own space before projecting the corners. CSS Shapes inset rules
  • [P2] Reflections can have a reported angle of zero. A layer with scaleX(-1) and a 50 px right inset now cuts the visual left edge in the scriptable preview. The sealed preview still cuts the visual right edge because boxOf sends corners only when the angle is nonzero. The same probe measured a 25 px difference in the overlay’s left edge. Provide equivalent corner data for zero-angle reflections, or preserve parity for them until both paths can use those corners.

The two existing focused test files passed, 40 tests total. Both additional parity probes failed on the reviewed head.

— tai (automated review)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Preserve the painted size of upright layers with the CSS scale property. In domEditOverlayCrop.ts:298, the scriptable path uses reconstructed corners whenever a matrix snapshot exists. The transform walk only includes transform and rotate, so it reconstructs the unscaled corners of a layer with scale: 2. The sealed path uses the measured box for this case.

I ran the existing two-preview fixture with a 200 × 100 CSS box, scale: 2, and inset(0px 50px 0px 0px). At the fixture’s half-size preview, the width is 75 px scriptable and 150 px sealed. The base gives 175 px scriptable, so using the reconstructed corners introduces a further 100 px shrink. Use the measured box for a positive upright scale, or account for the individual scale before passing corners to the crop helper. Please cover the individual property as well as the existing transform: matrix(...) case.

All 46 existing targeted tests passed, including crossed insets and the mirror case.

— tai (automated review)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] A depth-only scale drops previously correct planar crop bounds. packages/core/src/runtime/elementTransform.ts:69 returns null for scale: 1 1 2, so the whole transform walk loses its rotation. For a flat 200×100 layer with transform: rotate(45deg) and a 50 px right inset, Chromium measures the same painted box with or without that depth scale. The overlay nevertheless falls back to cutting the axis-aligned box. Adding that single declaration to the existing rotated fixture produces 79.55×106.07 px at this head instead of 88.39×88.39 px. The same fixture passes in both preview modes with the previous transform walk. Preserve the planar transform when the Z scale has no effect on the geometry the overlay measures, and cover the case in the parity test.

The 47 existing focused overlay tests passed.

— tai (automated review)

Base automatically changed from fix/sealed-crop-outline-scale to main November 22, 2026 15:53

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Nested 3D rotations ignore the browser’s default flattening. packages/core/src/runtime/elementTransform.ts multiplies the ancestor and child matrices as if they shared one 3D context. A parent with rotate: x 60deg and its child with rotate: x -60deg cancel in that product. With default transform-style: flat, Chrome instead paints the 200×100 child at 200×25. Adding this fixture to overlayTransform.browser.test.ts returned 200×100 for the measured corners and 200×25 for Chrome’s painted bounds. Carry flattening through the transform walk or fall back to measured bounds when the projection cannot be represented. The existing depth-only scale case passes in real Chrome, and all 43 focused tests passed before the added probe.

This head also conflicts with main in four editor overlay files. The required regression check reports cancelled shards.

— tai (automated review)

…ounds

# Conflicts:
#	packages/studio/src/components/editor/domEditOverlayCrop.ts
#	packages/studio/src/components/editor/domEditOverlayGeometry.ts
#	packages/studio/src/components/editor/previewOverlayRects.test.ts
#	packages/studio/src/components/editor/previewOverlayRects.ts
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 271 (base branch 271), smooth 70 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (0)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Check planarity after composing the transform chain. In packages/core/src/runtime/elementTransform.ts, each matrix can pass the local planar check even when their product projects the child. In Chrome, a parent with transform: perspective(500px); transform-style: preserve-3d and a 200 × 100 child with transform: translateZ(100px) rotate(30deg) passes the walk. The returned corners span 223.2 × 186.6, while the browser paints 279.0 × 233.3. The crop code also dropped its former perspective check in this revision. Keep the per-node rejection for flattening and reject a projected composed result before returning corners, so it gets the documented painted-box fallback.

The 57 existing focused tests passed, including the nested-rotation fixture. The projected case fails in the same Chrome fixture.

— tai (automated review)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — The projected-box fallback still misses CSS's perspective property. In packages/core/src/runtime/elementTransform.ts, the transform walk reads the ancestor's rotate, scale, and transform, so neither planar check sees the projection from perspective: 500px. I kept the committed Chrome fixture's rotated, depth-translated child and used the perspective property on its parent. The overlay returned 223.2 × 186.6 while Chrome painted 279.0 × 233.3. Treat perspective established by an ancestor property as a reason to use the documented browser-box fallback.

All 57 committed focused tests passed, including the nested x-turn and transform: perspective(...) cases. The required regression gate is red after change detection was cancelled. CI

— tai (automated review)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 3D-context fallback resolves the perspective-property case. The real Chrome fixture verifies that both perspective spellings and preserved 3D fall back to the painted box, while the planar cases still use their transformed corners. All 79 focused Studio and core tests passed, and required CI is green.

— tai (automated review)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 22, 2026
@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 22, 2026
Merged via the queue into main with commit e5e3601 Nov 22, 2026
143 of 144 checks passed
@miguel-heygen
miguel-heygen deleted the fix/turned-crop-box-bounds branch November 22, 2026 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants