LCOV - code coverage report
Current view: top level - src/renderer/core/layout/transform - useTransformState.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 90.9 % 11 10
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
       2              : /**
       3              :  * Composable for managing transform state synchronized with LiteGraph canvas
       4              :  *
       5              :  * This composable is a critical part of the hybrid rendering architecture that
       6              :  * allows Vue components to render in perfect alignment with LiteGraph's canvas.
       7              :  *
       8              :  * ## Core Concept
       9              :  *
      10              :  * LiteGraph uses a canvas for rendering connections, grid, and handling interactions.
      11              :  * Vue components need to render nodes on top of this canvas. The challenge is
      12              :  * synchronizing the coordinate systems:
      13              :  *
      14              :  * - LiteGraph: Uses canvas coordinates with its own transform matrix
      15              :  * - Vue/DOM: Uses screen coordinates with CSS transforms
      16              :  *
      17              :  * ## Solution: Transform Container Pattern
      18              :  *
      19              :  * Instead of transforming individual nodes (O(n) complexity), we:
      20              :  * 1. Mirror LiteGraph's transform matrix to a single CSS container
      21              :  * 2. Place all Vue nodes as children with simple absolute positioning
      22              :  * 3. Achieve O(1) transform updates regardless of node count
      23              :  *
      24              :  * ## Coordinate Systems
      25              :  *
      26              :  * - **Canvas coordinates**: LiteGraph's internal coordinate system
      27              :  * - **Screen coordinates**: Browser's viewport coordinate system
      28              :  * - **Transform sync**: camera.x/y/z mirrors canvas.ds.offset/scale
      29              :  *
      30              :  * ## Performance Benefits
      31              :  *
      32              :  * - GPU acceleration via CSS transforms
      33              :  * - No layout thrashing (only transform changes)
      34              :  * - Efficient viewport culling calculations
      35              :  * - Scales to 1000+ nodes while maintaining 60 FPS
      36              :  *
      37              :  * @example
      38              :  * ```typescript
      39              :  * const { camera, transformStyle, screenToCanvas } = useTransformState()
      40              :  *
      41              :  * // In template
      42              :  * <div :style="transformStyle">
      43              :  *   <NodeComponent
      44              :  *     v-for="node in nodes"
      45              :  *     :style="{ left: node.x + 'px', top: node.y + 'px' }"
      46              :  *   />
      47              :  * </div>
      48              :  *
      49              :  * // Convert coordinates
      50              :  * const canvasPos = screenToCanvas({ x: clientX, y: clientY })
      51              :  * ```
      52              :  */
      53              : import { computed, reactive, readonly } from 'vue'
      54              : 
      55              : import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph'
      56              : import { createSharedComposable } from '@vueuse/core'
      57              : 
      58              : interface Camera extends Point {
      59              :   z: number
      60              : }
      61              : 
      62          835 : function useTransformStateIndividual() {
      63              :   // Reactive state mirroring LiteGraph's canvas transform
      64          835 :   const camera = reactive<Camera>({
      65              :     x: 0,
      66              :     y: 0,
      67              :     z: 1
      68              :   })
      69              : 
      70              :   // Computed transform string for CSS
      71          835 :   const transformStyle = computed(() => ({
      72              :     // Match LiteGraph DragAndScale.toCanvasContext():
      73              :     // ctx.scale(scale); ctx.translate(offset)
      74              :     // CSS applies right-to-left, so "scale() translate()" -> translate first, then scale
      75              :     // Effective mapping: screen = (canvas + offset) * scale
      76              :     // Using the 3D versions of scale and translate can provide a smoother experience
      77              :     // when dealing with a large number of nodes.
      78              :     transform: `scale3d(${camera.z}, ${camera.z}, ${camera.z}) translate3d(${camera.x}px, ${camera.y}px, 0)`,
      79              :     transformOrigin: '0 0'
      80              :   }))
      81              : 
      82              :   /**
      83              :    * Synchronizes Vue's reactive camera state with LiteGraph's canvas transform
      84              :    *
      85              :    * Called every frame via RAF to ensure Vue components stay aligned with canvas.
      86              :    * This is the heart of the hybrid rendering system - it bridges the gap between
      87              :    * LiteGraph's canvas transforms and Vue's reactive system.
      88              :    *
      89              :    * @param canvas - LiteGraph canvas instance with DragAndScale (ds) transform state
      90              :    */
      91        47707 :   function syncWithCanvas(canvas: LGraphCanvas) {
      92              :     // Mirror LiteGraph's transform state to Vue's reactive state
      93              :     // ds.offset = pan offset, ds.scale = zoom level
      94        47707 :     camera.x = canvas.ds.offset[0]
      95        47707 :     camera.y = canvas.ds.offset[1]
      96        47707 :     camera.z = canvas.ds.scale || 1
      97              :   }
      98              : 
      99              :   /**
     100              :    * Converts screen coordinates to canvas coordinates
     101              :    *
     102              :    * Inverse of the pane's own `transformStyle`. Useful for hit testing and
     103              :    * converting mouse events back to canvas space.
     104              :    *
     105              :    * Formula: canvas = screen / scale - offset
     106              :    *
     107              :    * @param point - Point in screen coordinate system
     108              :    * @returns Point in canvas coordinate system
     109              :    */
     110          835 :   const screenToCanvas = (point: Point): Point => {
     111            0 :     return {
     112              :       x: point.x / camera.z - camera.x,
     113              :       y: point.y / camera.z - camera.y
     114              :     }
     115              :   }
     116              : 
     117          835 :   return {
     118              :     camera: readonly(camera),
     119              :     transformStyle,
     120              :     syncWithCanvas,
     121              :     screenToCanvas
     122              :   }
     123              : }
     124              : 
     125         2355 : export const useTransformState = createSharedComposable(
     126              :   useTransformStateIndividual
     127              : )
        

Generated by: LCOV version 2.0-1