Skip to content

Commit bb38ada

Browse files
Strata Foundercursoragent
andcommitted
fix(effects): correct blur/shadow compositing and rotation-aware resize
Background blur captures true backdrop before fills; inner shadow/glow use silhouette-difference; layerBlur uses single offscreen pass. Generalize rotated resize handles and fix ancestor rotation in world transforms. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent b1bdab7 commit bb38ada

10 files changed

Lines changed: 992 additions & 223 deletions

File tree

‎packages/editor/src/SelectionOverlay.test.tsx‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
import type { Document, SceneNode, ShapeNode } from '@strata/scene';
88
import { render } from '@testing-library/react';
99
import { describe, expect, it, vi } from 'vitest';
10-
import { computeResize, SelectionOverlay, type SelectionOverlayProps } from './SelectionOverlay';
10+
import {
11+
computeResize,
12+
computeRotatedLocalBBox,
13+
SelectionOverlay,
14+
type SelectionOverlayProps,
15+
} from './SelectionOverlay';
1116

1217
vi.mock('./context', () => ({
1318
useEditor: vi.fn(),
@@ -94,6 +99,47 @@ function renderOverlay(
9499
return container;
95100
}
96101

102+
describe('computeRotatedLocalBBox', () => {
103+
it('expands width when dragging right handle on 45-degree rotated rect', () => {
104+
const lb = { x: 0, y: 0, w: 100, h: 80 };
105+
const rot = Math.PI / 4;
106+
const cos = Math.cos(rot);
107+
const sin = Math.sin(rot);
108+
const worldMat = [cos, sin, -sin, cos, 0, 0] as const;
109+
const det = cos * cos - sin * -sin;
110+
const invMat = [cos / det, -sin / det, sin / det, cos / det, 0, 0] as const;
111+
112+
const result = computeRotatedLocalBBox(
113+
3,
114+
lb,
115+
worldMat as any,
116+
invMat as any,
117+
20,
118+
0,
119+
false,
120+
false,
121+
);
122+
expect(result.w).toBeGreaterThan(100);
123+
});
124+
125+
it('preserves aspect ratio with shift key', () => {
126+
const lb = { x: 0, y: 0, w: 100, h: 50 };
127+
const worldMat = [1, 0, 0, 1, 0, 0] as const;
128+
const invMat = [1, 0, 0, 1, 0, 0] as const;
129+
const result = computeRotatedLocalBBox(
130+
4,
131+
lb,
132+
worldMat as any,
133+
invMat as any,
134+
50,
135+
50,
136+
true,
137+
false,
138+
);
139+
expect(result.w / result.h).toBeCloseTo(2, 1);
140+
});
141+
});
142+
97143
describe('computeResize', () => {
98144
it('handle 0 (top-left) moves top-left corner', () => {
99145
const r = computeResize(0, 100, 100, 200, 100, 10, 10);

‎packages/editor/src/SelectionOverlay.tsx‎

Lines changed: 190 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
*
1010
* Research basis: Figma/Penpot handle layout conventions; MDN SVG coordinate system.
1111
*/
12+
1213
import type { SceneNode } from '@strata/scene';
14+
import { applyAffine, tryInvertAffine } from '@strata/shared';
1315
import { Fragment, useCallback, useRef } from 'react';
1416
import { useEditor } from './context';
15-
import { nodeWorldBounds } from './scene/world';
17+
import { nodeLocalBounds, nodeWorldBounds, nodeWorldTransform } from './scene/world';
1618

1719
const HANDLE_HALF = 4;
1820
const ROT_OFFSET = 20;
@@ -203,6 +205,153 @@ export function computeResize(
203205
return { x, y, w, h, flippedX, flippedY };
204206
}
205207

208+
/** Apply resized local bounding box to a shape node (per-kind geometry update). */
209+
function applyShapeLocalBBox(
210+
nodeId: string,
211+
node: SceneNode,
212+
bx: number,
213+
by: number,
214+
bw: number,
215+
bh: number,
216+
setNodePosition: (id: string, x: number, y: number) => void,
217+
setNodeSize: (id: string, w: number, h: number) => void,
218+
updateNode: (id: string, updater: (n: SceneNode) => SceneNode) => void,
219+
): void {
220+
if (node.kind === 'shape') {
221+
const s = node.shape;
222+
if (
223+
s.kind === 'rect' ||
224+
s.kind === 'polygon' ||
225+
s.kind === 'star' ||
226+
s.kind === 'path' ||
227+
s.kind === 'line' ||
228+
s.kind === 'arrow'
229+
) {
230+
setNodePosition(nodeId, bx, by);
231+
setNodeSize(nodeId, bw, bh);
232+
} else if (s.kind === 'ellipse') {
233+
setNodePosition(nodeId, bx, by);
234+
updateNode(nodeId, (n) => {
235+
if (n.kind !== 'shape') return n;
236+
const rx = bw / 2;
237+
const ry = bh / 2;
238+
return {
239+
...n,
240+
shape: { ...n.shape, kind: 'ellipse', rx, ry, cx: rx, cy: ry } as typeof n.shape,
241+
};
242+
});
243+
} else if (s.kind === 'circle') {
244+
setNodePosition(nodeId, bx, by);
245+
updateNode(nodeId, (n) => {
246+
if (n.kind !== 'shape') return n;
247+
const r = Math.max(bw, bh) / 2;
248+
return {
249+
...n,
250+
shape: { ...n.shape, kind: 'circle', r, cx: r, cy: r } as typeof n.shape,
251+
};
252+
});
253+
}
254+
} else if (node.kind === 'frame') {
255+
setNodePosition(nodeId, bx, by);
256+
setNodeSize(nodeId, bw, bh);
257+
} else if (node.kind === 'text') {
258+
setNodePosition(nodeId, bx, by);
259+
}
260+
}
261+
262+
/** Compute new local bbox after dragging a handle on a rotated/transformed node. */
263+
export function computeRotatedLocalBBox(
264+
handleIndex: number,
265+
lb: { x: number; y: number; w: number; h: number },
266+
worldMat: import('@strata/shared').Affine,
267+
invMat: import('@strata/shared').Affine,
268+
dx: number,
269+
dy: number,
270+
shiftKey: boolean,
271+
altKey: boolean,
272+
): { x: number; y: number; w: number; h: number } {
273+
const { x: bx, y: by, w: bw, h: bh } = lb;
274+
const localCorners: Record<number, [number, number]> = {
275+
0: [bx, by],
276+
1: [bx + bw / 2, by],
277+
2: [bx + bw, by],
278+
3: [bx + bw, by + bh / 2],
279+
4: [bx + bw, by + bh],
280+
5: [bx + bw / 2, by + bh],
281+
6: [bx, by + bh],
282+
7: [bx, by + bh / 2],
283+
};
284+
const localCorner = localCorners[handleIndex];
285+
if (!localCorner) return lb;
286+
287+
const curWorld = applyAffine(worldMat, localCorner);
288+
const newWorld: [number, number] = [curWorld[0] + dx, curWorld[1] + dy];
289+
const newLocal = applyAffine(invMat, newWorld);
290+
291+
let newX = bx;
292+
let newY = by;
293+
let newW = bw;
294+
let newH = bh;
295+
296+
switch (handleIndex) {
297+
case 0:
298+
newX = newLocal[0];
299+
newY = newLocal[1];
300+
newW = bw + bx - newX;
301+
newH = bh + by - newY;
302+
break;
303+
case 1:
304+
newY = newLocal[1];
305+
newH = bh + by - newY;
306+
break;
307+
case 2:
308+
newY = newLocal[1];
309+
newW = newLocal[0] - bx;
310+
newH = bh + by - newY;
311+
break;
312+
case 3:
313+
newW = newLocal[0] - bx;
314+
break;
315+
case 4:
316+
newW = newLocal[0] - bx;
317+
newH = newLocal[1] - by;
318+
break;
319+
case 5:
320+
newH = newLocal[1] - by;
321+
break;
322+
case 6:
323+
newX = newLocal[0];
324+
newW = bw + bx - newX;
325+
newH = newLocal[1] - by;
326+
break;
327+
case 7:
328+
newX = newLocal[0];
329+
newW = bw + bx - newX;
330+
break;
331+
}
332+
333+
if (shiftKey && bh > 0) {
334+
const aspect = bw / bh;
335+
if (newW / newH > aspect) {
336+
newW = newH * aspect;
337+
} else {
338+
newH = newW / aspect;
339+
}
340+
}
341+
342+
if (altKey) {
343+
const cx = bx + bw / 2;
344+
const cy = by + bh / 2;
345+
newX = cx - newW / 2;
346+
newY = cy - newH / 2;
347+
}
348+
349+
newW = Math.max(MIN_SIZE, newW);
350+
newH = Math.max(MIN_SIZE, newH);
351+
352+
return { x: newX, y: newY, w: newW, h: newH };
353+
}
354+
206355
export interface SelectionOverlayProps {
207356
canvasRef?: React.RefObject<HTMLCanvasElement | null>;
208357
}
@@ -338,6 +487,44 @@ export function SelectionOverlay({ canvasRef }: SelectionOverlayProps = {}) {
338487
const shiftKey = e.shiftKey;
339488
const altKey = e.altKey;
340489

490+
const node = state.document.nodes[g.nodeId];
491+
if (!node) return;
492+
493+
const worldMat = nodeWorldTransform(state.document, g.nodeId);
494+
const invMat = tryInvertAffine(worldMat);
495+
const hasRotation =
496+
(node.rotation ?? 0) !== 0 || Math.abs(worldMat[1]) > 1e-6 || Math.abs(worldMat[2]) > 1e-6;
497+
498+
// Rotated/transformed path: inverse world transform for all shape kinds
499+
if (hasRotation && node.kind === 'shape' && invMat) {
500+
const lb = nodeLocalBounds(node);
501+
if (lb) {
502+
const resized = computeRotatedLocalBBox(
503+
g.handleIndex,
504+
lb,
505+
worldMat,
506+
invMat,
507+
dx,
508+
dy,
509+
shiftKey,
510+
altKey,
511+
);
512+
applyShapeLocalBBox(
513+
g.nodeId,
514+
node,
515+
resized.x,
516+
resized.y,
517+
resized.w,
518+
resized.h,
519+
setNodePosition,
520+
setNodeSize,
521+
updateNode,
522+
);
523+
return;
524+
}
525+
}
526+
527+
// Non-rotated path: compute world-space AABB and apply directly
341528
const { x, y, w, h } = computeResize(
342529
g.handleIndex,
343530
g.initX,
@@ -350,50 +537,9 @@ export function SelectionOverlay({ canvasRef }: SelectionOverlayProps = {}) {
350537
altKey,
351538
);
352539

353-
const node = state.document.nodes[g.nodeId];
354-
if (!node) return;
355-
356540
// Convert world bounds back to local shape params
357-
if (node.kind === 'shape') {
358-
const s = node.shape;
359-
if (
360-
s.kind === 'rect' ||
361-
s.kind === 'polygon' ||
362-
s.kind === 'star' ||
363-
s.kind === 'path' ||
364-
s.kind === 'line' ||
365-
s.kind === 'arrow'
366-
) {
367-
setNodePosition(g.nodeId, x, y);
368-
setNodeSize(g.nodeId, w, h);
369-
} else if (s.kind === 'ellipse') {
370-
setNodePosition(g.nodeId, x, y);
371-
updateNode(g.nodeId, (n) => {
372-
if (n.kind !== 'shape') return n;
373-
const rx = w / 2;
374-
const ry = h / 2;
375-
return {
376-
...n,
377-
shape: { ...n.shape, kind: 'ellipse', rx, ry, cx: rx, cy: ry } as typeof n.shape,
378-
};
379-
});
380-
} else if (s.kind === 'circle') {
381-
setNodePosition(g.nodeId, x, y);
382-
updateNode(g.nodeId, (n) => {
383-
if (n.kind !== 'shape') return n;
384-
const r = Math.max(w, h) / 2;
385-
return {
386-
...n,
387-
shape: { ...n.shape, kind: 'circle', r, cx: r, cy: r } as typeof n.shape,
388-
};
389-
});
390-
}
391-
} else if (node.kind === 'frame') {
392-
setNodePosition(g.nodeId, x, y);
393-
setNodeSize(g.nodeId, w, h);
394-
} else if (node.kind === 'text') {
395-
// Text: adjust transform (font size stays)
396-
setNodePosition(g.nodeId, x, y);
541+
if (node.kind === 'shape' || node.kind === 'frame' || node.kind === 'text') {
542+
applyShapeLocalBBox(g.nodeId, node, x, y, w, h, setNodePosition, setNodeSize, updateNode);
397543
}
398544
},
399545
[

‎packages/editor/src/scene/transformCache.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Document, NodeId } from '@strata/scene';
22
import { getParent } from '@strata/scene';
33
import type { Affine, Rect } from '@strata/shared';
4-
import { identity, multiplyAffine, transformRect } from '@strata/shared';
4+
import { identity, multiplyAffine, rotateDeg, transformRect } from '@strata/shared';
55
import { nodeLocalBounds } from './world';
66

77
export interface TransformCache {
@@ -25,13 +25,19 @@ function computeWorldTransform(doc: Document, id: NodeId): Affine {
2525
if (!node) return identity;
2626

2727
const nodeTransform = node.transform as Affine;
28-
const chain: Affine[] = [nodeTransform];
28+
const rot = node.rotation ?? 0;
29+
const combined = rot !== 0 ? multiplyAffine(nodeTransform, rotateDeg(rot)) : nodeTransform;
30+
const chain: Affine[] = [combined];
2931

3032
let parentId = getParent(doc, id);
3133
while (parentId) {
3234
const parent = doc.nodes[parentId];
3335
if (!parent) break;
34-
chain.push(parent.transform as Affine);
36+
const parentRot = parent.rotation ?? 0;
37+
const parentTransform = parent.transform as Affine;
38+
chain.push(
39+
parentRot !== 0 ? multiplyAffine(parentTransform, rotateDeg(parentRot)) : parentTransform,
40+
);
3541
parentId = getParent(doc, parentId);
3642
}
3743

0 commit comments

Comments
 (0)