LCOV - code coverage report
Current view: top level - packages/layer-editor/src/tools - transformMath.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 10.7 % 121 13
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Transform } from '#/node'
       2              : import type { Point2D as Vec2 } from '@comfyorg/shared-frontend-utils/geometry'
       3              : 
       4              : export type HandleId =
       5              :   | 'nw'
       6              :   | 'n'
       7              :   | 'ne'
       8              :   | 'e'
       9              :   | 'se'
      10              :   | 's'
      11              :   | 'sw'
      12              :   | 'w'
      13              :   | 'rotate'
      14              : 
      15            2 : const ROTATE_OFFSET = 24
      16              : 
      17            2 : const SIGN: Record<HandleId, Vec2> = {
      18              :   nw: { x: -1, y: -1 },
      19              :   n: { x: 0, y: -1 },
      20              :   ne: { x: 1, y: -1 },
      21              :   e: { x: 1, y: 0 },
      22              :   se: { x: 1, y: 1 },
      23              :   s: { x: 0, y: 1 },
      24              :   sw: { x: -1, y: 1 },
      25              :   w: { x: -1, y: 0 },
      26              :   rotate: { x: 0, y: -1 }
      27              : }
      28              : 
      29            2 : const OPP: Record<HandleId, HandleId> = {
      30              :   nw: 'se',
      31              :   n: 's',
      32              :   ne: 'sw',
      33              :   e: 'w',
      34              :   se: 'nw',
      35              :   s: 'n',
      36              :   sw: 'ne',
      37              :   w: 'e',
      38              :   rotate: 'rotate'
      39              : }
      40              : 
      41          210 : function rot(p: Vec2, a: number): Vec2 {
      42          210 :   const c = Math.cos(a)
      43          210 :   const s = Math.sin(a)
      44          210 :   return { x: p.x * c - p.y * s, y: p.x * s + p.y * c }
      45              : }
      46              : 
      47          210 : export function center(t: Transform): Vec2 {
      48          210 :   return { x: t.x + t.w / 2, y: t.y + t.h / 2 }
      49              : }
      50              : 
      51            0 : function axes(t: Transform): { ex: Vec2; ey: Vec2 } {
      52            0 :   const c = Math.cos(t.rotation)
      53            0 :   const s = Math.sin(t.rotation)
      54            0 :   return { ex: { x: c, y: s }, ey: { x: -s, y: c } }
      55              : }
      56              : 
      57          210 : export function handlePos(t: Transform, h: HandleId): Vec2 {
      58          210 :   const cen = center(t)
      59              :   const local =
      60              :     h === 'rotate'
      61              :       ? { x: 0, y: -t.h / 2 - ROTATE_OFFSET }
      62              :       : { x: (SIGN[h].x * t.w) / 2, y: (SIGN[h].y * t.h) / 2 }
      63          210 :   const p = rot(local, t.rotation)
      64          210 :   return { x: cen.x + p.x, y: cen.y + p.y }
      65              : }
      66              : 
      67            0 : export function toLocalFrame(t: Transform, pt: Vec2): Vec2 {
      68            0 :   const cen = center(t)
      69            0 :   return rot({ x: pt.x - cen.x, y: pt.y - cen.y }, -t.rotation)
      70              : }
      71              : 
      72            0 : export function hitHandle(
      73              :   t: Transform,
      74              :   pt: Vec2,
      75              :   tol: number
      76              : ): HandleId | null {
      77              :   const order: HandleId[] = [
      78              :     'rotate',
      79              :     'nw',
      80              :     'ne',
      81              :     'se',
      82              :     'sw',
      83              :     'n',
      84              :     'e',
      85              :     's',
      86              :     'w'
      87              :   ]
      88            0 :   for (const h of order) {
      89            0 :     const hp = handlePos(t, h)
      90            0 :     if (Math.hypot(pt.x - hp.x, pt.y - hp.y) <= tol) return h
      91              :   }
      92            0 :   return null
      93              : }
      94              : 
      95            0 : export function insideBox(t: Transform, pt: Vec2): boolean {
      96            0 :   const l = toLocalFrame(t, pt)
      97            0 :   return Math.abs(l.x) <= t.w / 2 && Math.abs(l.y) <= t.h / 2
      98              : }
      99              : 
     100            0 : export function applyMove(t: Transform, dx: number, dy: number): Transform {
     101            0 :   return { ...t, x: t.x + dx, y: t.y + dy }
     102              : }
     103              : 
     104            0 : export function applyResize(
     105              :   t: Transform,
     106              :   h: HandleId,
     107              :   pt: Vec2,
     108              :   minSize = 1,
     109              :   keepAspect = false
     110              : ): Transform {
     111            0 :   if (h === 'rotate') return t
     112            0 :   const anchor = handlePos(t, OPP[h])
     113            0 :   const { ex, ey } = axes(t)
     114            0 :   const dir = SIGN[h]
     115            0 :   const controlsX = dir.x !== 0
     116            0 :   const controlsY = dir.y !== 0
     117              : 
     118            0 :   const d = { x: pt.x - anchor.x, y: pt.y - anchor.y }
     119            0 :   const projX = d.x * ex.x + d.y * ex.y
     120            0 :   const projY = d.x * ey.x + d.y * ey.y
     121              : 
     122            0 :   const oc = center(t)
     123            0 :   const ocRel = {
     124              :     x: (oc.x - anchor.x) * ex.x + (oc.y - anchor.y) * ex.y,
     125              :     y: (oc.x - anchor.x) * ey.x + (oc.y - anchor.y) * ey.y
     126              :   }
     127              : 
     128            0 :   if (keepAspect && t.w > 0 && t.h > 0) {
     129            0 :     let s: number
     130            0 :     if (controlsX && controlsY) {
     131            0 :       const vx = dir.x * t.w
     132            0 :       const vy = dir.y * t.h
     133            0 :       s = (projX * vx + projY * vy) / (vx * vx + vy * vy)
     134            0 :     } else if (controlsX) {
     135            0 :       s = (dir.x * projX) / t.w
     136              :     } else {
     137            0 :       s = (dir.y * projY) / t.h
     138              :     }
     139            0 :     s = Math.max(s, minSize / t.w, minSize / t.h)
     140            0 :     const relX = ocRel.x * s
     141            0 :     const relY = ocRel.y * s
     142            0 :     const newW = t.w * s
     143            0 :     const newH = t.h * s
     144            0 :     const nc = {
     145              :       x: anchor.x + ex.x * relX + ey.x * relY,
     146              :       y: anchor.y + ex.y * relX + ey.y * relY
     147              :     }
     148            0 :     return {
     149              :       x: nc.x - newW / 2,
     150              :       y: nc.y - newH / 2,
     151              :       w: newW,
     152              :       h: newH,
     153              :       rotation: t.rotation
     154              :     }
     155              :   }
     156              : 
     157            0 :   const newW = controlsX ? Math.max(minSize, Math.abs(projX)) : t.w
     158            0 :   const newH = controlsY ? Math.max(minSize, Math.abs(projY)) : t.h
     159              : 
     160            0 :   const relX = controlsX ? (dir.x * newW) / 2 : ocRel.x
     161            0 :   const relY = controlsY ? (dir.y * newH) / 2 : ocRel.y
     162              : 
     163            0 :   const nc = {
     164              :     x: anchor.x + ex.x * relX + ey.x * relY,
     165              :     y: anchor.y + ex.y * relX + ey.y * relY
     166              :   }
     167            0 :   return {
     168              :     x: nc.x - newW / 2,
     169              :     y: nc.y - newH / 2,
     170              :     w: newW,
     171              :     h: newH,
     172              :     rotation: t.rotation
     173              :   }
     174              : }
     175              : 
     176            0 : export function angleTo(t: Transform, pt: Vec2): number {
     177            0 :   const c = center(t)
     178            0 :   return Math.atan2(pt.y - c.y, pt.x - c.x)
     179              : }
     180              : 
     181            0 : export function applyRotate(
     182              :   t: Transform,
     183              :   baseRotation: number,
     184              :   grabAngle: number,
     185              :   pt: Vec2,
     186              :   snap = 0
     187              : ): Transform {
     188            0 :   let rotation = baseRotation + (angleTo(t, pt) - grabAngle)
     189            0 :   if (snap > 0) rotation = Math.round(rotation / snap) * snap
     190            0 :   return { ...t, rotation }
     191              : }
     192              : 
     193            0 : export function unionBounds(boxes: Transform[]): Transform {
     194            0 :   let minX = Infinity
     195            0 :   let minY = Infinity
     196            0 :   let maxX = -Infinity
     197            0 :   let maxY = -Infinity
     198            0 :   const corners: HandleId[] = ['nw', 'ne', 'se', 'sw']
     199            0 :   for (const b of boxes) {
     200            0 :     for (const h of corners) {
     201            0 :       const p = handlePos(b, h)
     202            0 :       minX = Math.min(minX, p.x)
     203            0 :       minY = Math.min(minY, p.y)
     204            0 :       maxX = Math.max(maxX, p.x)
     205            0 :       maxY = Math.max(maxY, p.y)
     206              :     }
     207              :   }
     208            0 :   return { x: minX, y: minY, w: maxX - minX, h: maxY - minY, rotation: 0 }
     209              : }
     210              : 
     211            0 : export function scaleAround(
     212              :   t: Transform,
     213              :   anchor: Vec2,
     214              :   scale: number
     215              : ): Transform {
     216            0 :   const c = center(t)
     217            0 :   const nc = {
     218              :     x: anchor.x + (c.x - anchor.x) * scale,
     219              :     y: anchor.y + (c.y - anchor.y) * scale
     220              :   }
     221            0 :   const w = t.w * scale
     222            0 :   const h = t.h * scale
     223            0 :   return { x: nc.x - w / 2, y: nc.y - h / 2, w, h, rotation: t.rotation }
     224              : }
     225              : 
     226            0 : export function rotateAround(
     227              :   t: Transform,
     228              :   pivot: Vec2,
     229              :   theta: number
     230              : ): Transform {
     231            0 :   const c = center(t)
     232            0 :   const cos = Math.cos(theta)
     233            0 :   const sin = Math.sin(theta)
     234            0 :   const dx = c.x - pivot.x
     235            0 :   const dy = c.y - pivot.y
     236            0 :   const nc = {
     237              :     x: pivot.x + dx * cos - dy * sin,
     238              :     y: pivot.y + dx * sin + dy * cos
     239              :   }
     240            0 :   return {
     241              :     x: nc.x - t.w / 2,
     242              :     y: nc.y - t.h / 2,
     243              :     w: t.w,
     244              :     h: t.h,
     245              :     rotation: t.rotation + theta
     246              :   }
     247              : }
     248              : 
     249            0 : export function groupResize(
     250              :   gizmo: Transform,
     251              :   handle: HandleId,
     252              :   pt: Vec2,
     253              :   minSize = 1
     254              : ): { gizmo: Transform; anchor: Vec2; scale: number } {
     255            0 :   const next = applyResize(gizmo, handle, pt, minSize, true)
     256              :   const anchor = handlePos(gizmo, OPP[handle])
     257              :   const scale = gizmo.w > 0 ? next.w / gizmo.w : 1
     258            0 :   return { gizmo: next, anchor, scale }
     259              : }
     260              : 
     261            0 : export function alignedTo(
     262              :   rotation: number,
     263              :   frameRotation: number,
     264              :   eps = 1e-6
     265              : ): boolean {
     266            0 :   const k = (rotation - frameRotation) / (Math.PI / 2)
     267            0 :   return Math.abs(k - Math.round(k)) < eps
     268              : }
     269              : 
     270            0 : export function groupScale(
     271              :   gizmo: Transform,
     272              :   handle: HandleId,
     273              :   pt: Vec2,
     274              :   minSize = 1
     275              : ): { gizmo: Transform; anchor: Vec2; sx: number; sy: number } {
     276            0 :   const next = applyResize(gizmo, handle, pt, minSize, false)
     277              :   const anchor = handlePos(gizmo, OPP[handle])
     278              :   const sx = gizmo.w > 0 ? next.w / gizmo.w : 1
     279              :   const sy = gizmo.h > 0 ? next.h / gizmo.h : 1
     280            0 :   return { gizmo: next, anchor, sx, sy }
     281              : }
     282              : 
     283            0 : export function scaleAroundFrame(
     284              :   t: Transform,
     285              :   anchor: Vec2,
     286              :   frameRotation: number,
     287              :   sx: number,
     288              :   sy: number
     289              : ): Transform {
     290            0 :   const c = center(t)
     291            0 :   const cos = Math.cos(frameRotation)
     292            0 :   const sin = Math.sin(frameRotation)
     293            0 :   const dx = c.x - anchor.x
     294            0 :   const dy = c.y - anchor.y
     295            0 :   const fx = (dx * cos + dy * sin) * sx
     296            0 :   const fy = (-dx * sin + dy * cos) * sy
     297            0 :   const nc = {
     298              :     x: anchor.x + fx * cos - fy * sin,
     299              :     y: anchor.y + fx * sin + fy * cos
     300              :   }
     301            0 :   const swap =
     302              :     Math.round((t.rotation - frameRotation) / (Math.PI / 2)) % 2 !== 0
     303            0 :   const w = t.w * (swap ? sy : sx)
     304            0 :   const h = t.h * (swap ? sx : sy)
     305            0 :   return { x: nc.x - w / 2, y: nc.y - h / 2, w, h, rotation: t.rotation }
     306              : }
        

Generated by: LCOV version 2.0-1