99 *
1010 * Research basis: Figma/Penpot handle layout conventions; MDN SVG coordinate system.
1111 */
12+
1213import type { SceneNode } from '@strata/scene' ;
14+ import { applyAffine , tryInvertAffine } from '@strata/shared' ;
1315import { Fragment , useCallback , useRef } from 'react' ;
1416import { useEditor } from './context' ;
15- import { nodeWorldBounds } from './scene/world' ;
17+ import { nodeLocalBounds , nodeWorldBounds , nodeWorldTransform } from './scene/world' ;
1618
1719const HANDLE_HALF = 4 ;
1820const 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+
206355export 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 [
0 commit comments