LCOV - code coverage report
Current view: top level - src/renderer/core/canvas - pathRenderer.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 37.7 % 207 78
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              : export type Direction = 'left' | 'right' | 'up' | 'down' | 'none'
       4              : export type RenderMode = 'spline' | 'straight' | 'linear'
       5              : export type ArrowShape = 'triangle' | 'circle' | 'square'
       6              : 
       7              : export interface LinkRenderData {
       8              :   id: string
       9              :   startPoint: Point
      10              :   endPoint: Point
      11              :   startDirection: Direction
      12              :   endDirection: Direction
      13              :   color?: string
      14              :   type?: string
      15              :   controlPoints?: Point[]
      16              :   flow?: boolean
      17              :   disabled?: boolean
      18              :   // Optional multi-segment support
      19              :   segments?: Array<{
      20              :     start: Point
      21              :     end: Point
      22              :     controlPoints?: Point[]
      23              :   }>
      24              :   // Center point storage (for hit detection and menu)
      25              :   centerPos?: Point
      26              :   centerAngle?: number
      27              : }
      28              : 
      29              : interface RenderStyle {
      30              :   mode: RenderMode
      31              :   connectionWidth: number
      32              :   borderWidth?: number
      33              :   arrowShape?: ArrowShape
      34              :   showArrows?: boolean
      35              :   lowQuality?: boolean
      36              :   // Center marker properties
      37              :   showCenterMarker?: boolean
      38              :   centerMarkerShape?: 'circle' | 'arrow'
      39              :   highQuality?: boolean
      40              : }
      41              : 
      42              : interface RenderColors {
      43              :   default: string
      44              :   byType: Record<string, string>
      45              :   highlighted: string
      46              : }
      47              : 
      48              : export interface RenderContext {
      49              :   style: RenderStyle
      50              :   colors: RenderColors
      51              :   patterns?: {
      52              :     disabled?: CanvasPattern | null
      53              :   }
      54              :   animation?: {
      55              :     time: number // Seconds for flow animation
      56              :   }
      57              :   scale?: number // Canvas scale for quality adjustments
      58              :   highlightedIds?: Set<string>
      59              : }
      60              : 
      61              : interface DragLinkData {
      62              :   /** Fixed end - the slot being dragged from */
      63              :   fixedPoint: Point
      64              :   fixedDirection: Direction
      65              :   /** Moving end - follows mouse */
      66              :   dragPoint: Point
      67              :   dragDirection?: Direction
      68              :   /** Visual properties */
      69              :   color?: string
      70              :   type?: string
      71              :   disabled?: boolean
      72              :   /** Whether dragging from input (reverse direction) */
      73              :   fromInput?: boolean
      74              : }
      75              : 
      76         2356 : const LINEAR_LINK_OFFSET = 15
      77         2356 : const STRAIGHT_LINK_OFFSET = 10
      78         2356 : const CURVE_SAMPLES = 16
      79              : 
      80            0 : function directionOffset(direction: Direction, distance: number): Point {
      81            0 :   switch (direction) {
      82              :     case 'left':
      83            0 :       return { x: -distance, y: 0 }
      84              :     case 'right':
      85            0 :       return { x: distance, y: 0 }
      86              :     case 'up':
      87            0 :       return { x: 0, y: -distance }
      88              :     case 'down':
      89            0 :       return { x: 0, y: distance }
      90              :     case 'none':
      91              :     default:
      92            0 :       return { x: 0, y: 0 }
      93              :   }
      94              : }
      95              : 
      96            0 : function offsetPoint(point: Point, direction: Direction, distance: number) {
      97            0 :   const offset = directionOffset(direction, distance)
      98            0 :   return { x: point.x + offset.x, y: point.y + offset.y }
      99              : }
     100              : 
     101       117742 : function pointOnCubic(
     102              :   t: number,
     103              :   p0: Point,
     104              :   p1: Point,
     105              :   p2: Point,
     106              :   p3: Point
     107              : ): Point {
     108       117742 :   const mt = 1 - t
     109       117742 :   const mt2 = mt * mt
     110       117742 :   const mt3 = mt2 * mt
     111       117742 :   const t2 = t * t
     112       117742 :   const t3 = t2 * t
     113       117742 :   return {
     114              :     x: mt3 * p0.x + 3 * mt2 * t * p1.x + 3 * mt * t2 * p2.x + t3 * p3.x,
     115              :     y: mt3 * p0.y + 3 * mt2 * t * p1.y + 3 * mt * t2 * p2.y + t3 * p3.y
     116              :   }
     117              : }
     118              : 
     119          476 : function pointOnCurve(
     120              :   start: Point,
     121              :   controls: Point[],
     122              :   end: Point,
     123              :   t: number
     124              : ): Point {
     125          476 :   if (controls.length >= 2)
     126          476 :     return pointOnCubic(t, start, controls[0], controls[1], end)
     127            0 :   const u = 1 - t
     128            0 :   if (controls.length === 1) {
     129            0 :     const [c1] = controls
     130            0 :     return {
     131              :       x: u * u * start.x + 2 * u * t * c1.x + t * t * end.x,
     132              :       y: u * u * start.y + 2 * u * t * c1.y + t * t * end.y
     133              :     }
     134              :   }
     135            0 :   return {
     136              :     x: start.x + (end.x - start.x) * t,
     137              :     y: start.y + (end.y - start.y) * t
     138              :   }
     139              : }
     140              : 
     141           28 : function sampleCurve(start: Point, controls: Point[], end: Point): Point[] {
     142           28 :   return Array.from({ length: CURVE_SAMPLES + 1 }, (_, i) =>
     143              :     pointOnCurve(start, controls, end, i / CURVE_SAMPLES)
     144              :   )
     145              : }
     146              : 
     147         2356 : export class CanvasPathRenderer {
     148              :   /**
     149              :    * Draw a link between two points
     150              :    * Returns a Path2D object for hit detection
     151              :    */
     152              :   drawLink(
     153              :     ctx: CanvasRenderingContext2D,
     154              :     link: LinkRenderData,
     155              :     context: RenderContext
     156              :   ): Path2D {
     157       117266 :     const path = new Path2D()
     158              : 
     159       117266 :     const isHighlighted = context.highlightedIds?.has(link.id) ?? false
     160       117266 :     const color = this.determineLinkColor(link, context, isHighlighted)
     161              : 
     162       117266 :     ctx.save()
     163              : 
     164       117266 :     if (link.disabled && context.patterns?.disabled) {
     165           96 :       ctx.strokeStyle = context.patterns.disabled
     166              :     } else {
     167       117170 :       ctx.strokeStyle = color
     168              :     }
     169              : 
     170       117266 :     ctx.lineWidth = context.style.connectionWidth
     171       117266 :     ctx.lineJoin = 'round'
     172              : 
     173       117266 :     if (context.style.borderWidth && !context.style.lowQuality) {
     174       116120 :       this.drawLinkPath(
     175              :         ctx,
     176              :         path,
     177              :         link,
     178              :         context,
     179              :         context.style.connectionWidth + context.style.borderWidth,
     180              :         'rgba(0,0,0,0.5)'
     181              :       )
     182              :     }
     183              : 
     184       117266 :     this.drawLinkPath(
     185              :       ctx,
     186              :       path,
     187              :       link,
     188              :       context,
     189              :       context.style.connectionWidth,
     190              :       color
     191              :     )
     192              : 
     193       117266 :     this.calculateCenterPoint(link, context)
     194              : 
     195       117266 :     if (context.style.showArrows) {
     196            0 :       this.drawArrows(ctx, link, context, color)
     197              :     }
     198              : 
     199       117266 :     if (
     200              :       context.style.showCenterMarker &&
     201              :       context.scale &&
     202              :       context.scale >= 0.6 &&
     203              :       context.style.highQuality
     204              :     ) {
     205       111417 :       this.drawCenterMarker(ctx, link, context, color)
     206              :     }
     207              : 
     208       117266 :     if (link.flow && context.animation) {
     209            0 :       this.drawFlowAnimation(ctx, path, link, context)
     210              :     }
     211              : 
     212       117266 :     ctx.restore()
     213              : 
     214       117266 :     return path
     215              :   }
     216              : 
     217              :   private determineLinkColor(
     218              :     link: LinkRenderData,
     219              :     context: RenderContext,
     220              :     isHighlighted: boolean
     221              :   ): string {
     222       117266 :     if (isHighlighted) {
     223         6599 :       return context.colors.highlighted
     224              :     }
     225       117266 :     if (link.color) {
     226         2495 :       return link.color
     227              :     }
     228       117266 :     if (link.type && context.colors.byType[link.type]) {
     229       107000 :       return context.colors.byType[link.type]
     230              :     }
     231         1172 :     return context.colors.default
     232              :   }
     233              : 
     234              :   private drawLinkPath(
     235              :     ctx: CanvasRenderingContext2D,
     236              :     path: Path2D,
     237              :     link: LinkRenderData,
     238              :     context: RenderContext,
     239              :     lineWidth: number,
     240              :     color: string
     241              :   ): void {
     242       233386 :     ctx.strokeStyle = color
     243       233386 :     ctx.lineWidth = lineWidth
     244              : 
     245       233386 :     const start = link.startPoint
     246       233386 :     const end = link.endPoint
     247              : 
     248              :     // Build the path based on render mode
     249       233386 :     if (context.style.mode === 'linear') {
     250            0 :       this.buildPolylinePath(
     251              :         path,
     252              :         this.linearPoints(start, end, link.startDirection, link.endDirection)
     253              :       )
     254       233386 :     } else if (context.style.mode === 'straight') {
     255            0 :       this.buildPolylinePath(
     256              :         path,
     257              :         this.straightPoints(start, end, link.startDirection, link.endDirection)
     258              :       )
     259              :     } else {
     260       233386 :       this.buildSplinePath(
     261              :         path,
     262              :         start,
     263              :         end,
     264              :         link.startDirection,
     265              :         link.endDirection,
     266              :         link.controlPoints
     267              :       )
     268              :     }
     269              : 
     270       233386 :     ctx.stroke(path)
     271              :   }
     272              : 
     273              :   private linearPoints(
     274              :     start: Point,
     275              :     end: Point,
     276              :     startDir: Direction,
     277              :     endDir: Direction
     278              :   ): Point[] {
     279              :     const innerA = offsetPoint(start, startDir, LINEAR_LINK_OFFSET)
     280              :     const innerB = offsetPoint(end, endDir, LINEAR_LINK_OFFSET)
     281            0 :     return [start, innerA, innerB, end]
     282              :   }
     283              : 
     284              :   private straightPoints(
     285              :     start: Point,
     286              :     end: Point,
     287              :     startDir: Direction,
     288              :     endDir: Direction
     289              :   ): Point[] {
     290            0 :     const innerA = offsetPoint(start, startDir, STRAIGHT_LINK_OFFSET)
     291            0 :     const innerB = offsetPoint(end, endDir, STRAIGHT_LINK_OFFSET)
     292            0 :     const midX = (innerA.x + innerB.x) * 0.5
     293            0 :     return [
     294              :       start,
     295              :       innerA,
     296              :       { x: midX, y: innerA.y },
     297              :       { x: midX, y: innerB.y },
     298              :       innerB,
     299              :       end
     300              :     ]
     301              :   }
     302              : 
     303              :   private buildPolylinePath(path: Path2D, points: Point[]): void {
     304            0 :     const [first, ...rest] = points
     305            0 :     path.moveTo(first.x, first.y)
     306            0 :     for (const point of rest) path.lineTo(point.x, point.y)
     307              :   }
     308              : 
     309              :   polylineOf(link: LinkRenderData, mode: RenderMode): Point[] {
     310           28 :     const { startPoint, endPoint, startDirection, endDirection } = link
     311           28 :     if (mode === 'linear')
     312            0 :       return this.linearPoints(
     313              :         startPoint,
     314              :         endPoint,
     315              :         startDirection,
     316              :         endDirection
     317              :       )
     318           28 :     if (mode === 'straight')
     319            0 :       return this.straightPoints(
     320              :         startPoint,
     321              :         endPoint,
     322              :         startDirection,
     323              :         endDirection
     324              :       )
     325              :     const controls =
     326              :       link.controlPoints ||
     327              :       this.calculateControlPoints(
     328              :         startPoint,
     329              :         endPoint,
     330              :         startDirection,
     331              :         endDirection
     332              :       )
     333           28 :     return sampleCurve(startPoint, controls, endPoint)
     334              :   }
     335              : 
     336              :   private buildSplinePath(
     337              :     path: Path2D,
     338              :     start: Point,
     339              :     end: Point,
     340              :     startDir: Direction,
     341              :     endDir: Direction,
     342              :     controlPoints?: Point[]
     343              :   ): void {
     344       233386 :     path.moveTo(start.x, start.y)
     345              : 
     346              :     // Calculate control points if not provided
     347       233386 :     const controls =
     348              :       controlPoints || this.calculateControlPoints(start, end, startDir, endDir)
     349              : 
     350       233386 :     if (controls.length >= 2) {
     351              :       // Cubic bezier
     352       233386 :       path.bezierCurveTo(
     353              :         controls[0].x,
     354              :         controls[0].y,
     355              :         controls[1].x,
     356              :         controls[1].y,
     357              :         end.x,
     358              :         end.y
     359              :       )
     360            0 :     } else if (controls.length === 1) {
     361              :       // Quadratic bezier
     362            0 :       path.quadraticCurveTo(controls[0].x, controls[0].y, end.x, end.y)
     363              :     } else {
     364              :       // Fallback to linear
     365            0 :       path.lineTo(end.x, end.y)
     366              :     }
     367              :   }
     368              : 
     369              :   private calculateControlPoints(
     370              :     start: Point,
     371              :     end: Point,
     372              :     startDir: Direction,
     373              :     endDir: Direction
     374              :   ): Point[] {
     375            0 :     const dist = Math.sqrt(
     376              :       Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)
     377              :     )
     378            0 :     const controlDist = Math.max(30, dist * 0.25)
     379              : 
     380              :     // Calculate control point offsets based on direction
     381            0 :     const startControl = this.getDirectionOffset(startDir, controlDist)
     382            0 :     const endControl = this.getDirectionOffset(endDir, controlDist)
     383              : 
     384            0 :     return [
     385              :       { x: start.x + startControl.x, y: start.y + startControl.y },
     386              :       { x: end.x + endControl.x, y: end.y + endControl.y }
     387              :     ]
     388              :   }
     389              : 
     390              :   private getDirectionOffset(direction: Direction, distance: number): Point {
     391            0 :     return directionOffset(direction, distance)
     392              :   }
     393              : 
     394              :   private drawArrows(
     395              :     ctx: CanvasRenderingContext2D,
     396              :     link: LinkRenderData,
     397              :     context: RenderContext,
     398              :     color: string
     399              :   ): void {
     400            0 :     if (!context.style.showArrows) return
     401              : 
     402              :     // Render arrows at 0.25 and 0.75 positions along the path (matching original)
     403              :     const positions = [0.25, 0.75]
     404              : 
     405            0 :     for (const t of positions) {
     406              :       // Compute arrow position and angle
     407            0 :       const posA = this.computeConnectionPoint(link, t, context)
     408            0 :       const posB = this.computeConnectionPoint(link, t + 0.01, context) // slightly ahead for angle
     409              : 
     410            0 :       const angle = Math.atan2(posB.y - posA.y, posB.x - posA.x)
     411              : 
     412              :       // Draw arrow triangle (matching original shape)
     413            0 :       const transform = ctx.getTransform()
     414            0 :       ctx.translate(posA.x, posA.y)
     415            0 :       ctx.rotate(angle)
     416            0 :       ctx.fillStyle = color
     417            0 :       ctx.beginPath()
     418            0 :       ctx.moveTo(-5, -3)
     419            0 :       ctx.lineTo(0, 7)
     420            0 :       ctx.lineTo(5, -3)
     421            0 :       ctx.fill()
     422            0 :       ctx.setTransform(transform)
     423              :     }
     424              :   }
     425              : 
     426              :   /**
     427              :    * Compute a point along the link path at position t (0 to 1)
     428              :    * For backward compatibility with original litegraph, this always uses
     429              :    * bezier calculation with spline offsets, regardless of render mode.
     430              :    * This ensures arrow positions match the original implementation.
     431              :    */
     432              :   private computeConnectionPoint(
     433              :     link: LinkRenderData,
     434              :     t: number,
     435              :     _context: RenderContext
     436              :   ): Point {
     437            0 :     const { startPoint, endPoint, startDirection, endDirection } = link
     438              : 
     439              :     // Match original behavior: always use bezier math with spline offsets
     440              :     // regardless of render mode (for arrow position compatibility)
     441            0 :     const dist = Math.sqrt(
     442              :       Math.pow(endPoint.x - startPoint.x, 2) +
     443              :         Math.pow(endPoint.y - startPoint.y, 2)
     444              :     )
     445            0 :     const factor = 0.25
     446              : 
     447              :     // Create control points with spline offsets (matching original #addSplineOffset)
     448            0 :     const pa = { x: startPoint.x, y: startPoint.y }
     449            0 :     const pb = { x: endPoint.x, y: endPoint.y }
     450              : 
     451              :     // Apply spline offsets based on direction
     452            0 :     switch (startDirection) {
     453              :       case 'left':
     454            0 :         pa.x -= dist * factor
     455            0 :         break
     456              :       case 'right':
     457            0 :         pa.x += dist * factor
     458            0 :         break
     459              :       case 'up':
     460            0 :         pa.y -= dist * factor
     461            0 :         break
     462              :       case 'down':
     463            0 :         pa.y += dist * factor
     464              :         break
     465              :       case 'none':
     466              :         break
     467              :     }
     468              : 
     469            0 :     switch (endDirection) {
     470              :       case 'left':
     471            0 :         pb.x -= dist * factor
     472            0 :         break
     473              :       case 'right':
     474            0 :         pb.x += dist * factor
     475            0 :         break
     476              :       case 'up':
     477            0 :         pb.y -= dist * factor
     478            0 :         break
     479              :       case 'down':
     480            0 :         pb.y += dist * factor
     481              :         break
     482              :       case 'none':
     483              :         break
     484              :     }
     485              : 
     486              :     // Calculate bezier point (matching original computeConnectionPoint)
     487            0 :     const c1 = (1 - t) * (1 - t) * (1 - t)
     488            0 :     const c2 = 3 * ((1 - t) * (1 - t)) * t
     489            0 :     const c3 = 3 * (1 - t) * (t * t)
     490            0 :     const c4 = t * t * t
     491              : 
     492            0 :     return {
     493              :       x: c1 * startPoint.x + c2 * pa.x + c3 * pb.x + c4 * endPoint.x,
     494              :       y: c1 * startPoint.y + c2 * pa.y + c3 * pb.y + c4 * endPoint.y
     495              :     }
     496              :   }
     497              : 
     498              :   private drawFlowAnimation(
     499              :     ctx: CanvasRenderingContext2D,
     500              :     _path: Path2D,
     501              :     link: LinkRenderData,
     502              :     context: RenderContext
     503              :   ): void {
     504            0 :     if (!context.animation) return
     505              : 
     506              :     // Match original implementation: render 5 moving circles along the path
     507            0 :     const time = context.animation.time
     508            0 :     const linkColor = this.determineLinkColor(link, context, false)
     509              : 
     510            0 :     ctx.save()
     511            0 :     ctx.fillStyle = linkColor
     512              : 
     513              :     // Draw 5 circles at different positions along the path
     514            0 :     for (let i = 0; i < 5; ++i) {
     515              :       // Calculate position along path (0 to 1), with time-based animation
     516            0 :       const f = (time + i * 0.2) % 1
     517            0 :       const flowPos = this.computeConnectionPoint(link, f, context)
     518              : 
     519              :       // Draw circle at this position
     520            0 :       ctx.beginPath()
     521            0 :       ctx.arc(flowPos.x, flowPos.y, 5, 0, 2 * Math.PI)
     522            0 :       ctx.fill()
     523              :     }
     524              : 
     525            0 :     ctx.restore()
     526              :   }
     527              : 
     528              :   /**
     529              :    * Utility to find a point on a bezier curve (for hit detection)
     530              :    */
     531              :   findPointOnBezier(
     532              :     t: number,
     533              :     p0: Point,
     534              :     p1: Point,
     535              :     p2: Point,
     536              :     p3: Point
     537              :   ): Point {
     538       117266 :     return pointOnCubic(t, p0, p1, p2, p3)
     539              :   }
     540              : 
     541              :   /**
     542              :    * Draw a link being dragged from a slot to the mouse position
     543              :    * Returns a Path2D object for potential hit detection
     544              :    */
     545              :   drawDraggingLink(
     546              :     ctx: CanvasRenderingContext2D,
     547              :     dragData: DragLinkData,
     548              :     context: RenderContext
     549              :   ): Path2D {
     550              :     // Create LinkRenderData from drag data
     551              :     // When dragging from input, swap the points/directions
     552            0 :     const linkData: LinkRenderData = dragData.fromInput
     553              :       ? {
     554              :           id: 'dragging',
     555              :           startPoint: dragData.dragPoint,
     556              :           endPoint: dragData.fixedPoint,
     557              :           startDirection:
     558              :             dragData.dragDirection ||
     559              :             this.getOppositeDirection(dragData.fixedDirection),
     560              :           endDirection: dragData.fixedDirection,
     561              :           color: dragData.color,
     562              :           type: dragData.type,
     563              :           disabled: dragData.disabled
     564              :         }
     565              :       : {
     566              :           id: 'dragging',
     567              :           startPoint: dragData.fixedPoint,
     568              :           endPoint: dragData.dragPoint,
     569              :           startDirection: dragData.fixedDirection,
     570              :           endDirection:
     571              :             dragData.dragDirection ||
     572              :             this.getOppositeDirection(dragData.fixedDirection),
     573              :           color: dragData.color,
     574              :           type: dragData.type,
     575              :           disabled: dragData.disabled
     576              :         }
     577              : 
     578              :     // Use standard link drawing
     579            0 :     return this.drawLink(ctx, linkData, context)
     580              :   }
     581              : 
     582              :   /**
     583              :    * Get the opposite direction (for drag preview)
     584              :    */
     585              :   private getOppositeDirection(direction: Direction): Direction {
     586            0 :     switch (direction) {
     587              :       case 'left':
     588            0 :         return 'right'
     589              :       case 'right':
     590            0 :         return 'left'
     591              :       case 'up':
     592            0 :         return 'down'
     593              :       case 'down':
     594            0 :         return 'up'
     595              :       case 'none':
     596              :       default:
     597            0 :         return 'none'
     598              :     }
     599              :   }
     600              : 
     601              :   /**
     602              :    * Get the center point of a link (useful for labels, debugging)
     603              :    */
     604              :   getLinkCenter(link: LinkRenderData): Point {
     605              :     // For now, simple midpoint
     606              :     // Could be enhanced to find actual curve midpoint
     607            0 :     return {
     608              :       x: (link.startPoint.x + link.endPoint.x) / 2,
     609              :       y: (link.startPoint.y + link.endPoint.y) / 2
     610              :     }
     611              :   }
     612              : 
     613              :   private calculateCenterPoint(
     614              :     link: LinkRenderData,
     615              :     context: RenderContext
     616              :   ): void {
     617       117266 :     const { startPoint, endPoint, controlPoints } = link
     618       117266 :     const wantsAngle = context.style.centerMarkerShape === 'arrow'
     619              : 
     620       117266 :     if (
     621              :       context.style.mode === 'spline' &&
     622              :       controlPoints &&
     623              :       controlPoints.length >= 2
     624              :     ) {
     625       117266 :       const centerPos = this.findPointOnBezier(
     626              :         0.5,
     627              :         startPoint,
     628              :         controlPoints[0],
     629              :         controlPoints[1],
     630              :         endPoint
     631              :       )
     632       117266 :       link.centerPos = centerPos
     633              : 
     634       117266 :       if (wantsAngle) {
     635            0 :         const justPastCenter = this.findPointOnBezier(
     636              :           0.51,
     637              :           startPoint,
     638              :           controlPoints[0],
     639              :           controlPoints[1],
     640              :           endPoint
     641              :         )
     642            0 :         link.centerAngle = Math.atan2(
     643              :           justPastCenter.y - centerPos.y,
     644              :           justPastCenter.x - centerPos.x
     645              :         )
     646              :       }
     647            0 :     } else if (context.style.mode === 'linear') {
     648            0 :       const innerA = offsetPoint(
     649              :         startPoint,
     650              :         link.startDirection,
     651              :         LINEAR_LINK_OFFSET
     652              :       )
     653            0 :       const innerB = offsetPoint(
     654              :         endPoint,
     655              :         link.endDirection,
     656              :         LINEAR_LINK_OFFSET
     657              :       )
     658            0 :       link.centerPos = {
     659              :         x: (innerA.x + innerB.x) * 0.5,
     660              :         y: (innerA.y + innerB.y) * 0.5
     661              :       }
     662              : 
     663            0 :       if (wantsAngle) {
     664            0 :         link.centerAngle = Math.atan2(innerB.y - innerA.y, innerB.x - innerA.x)
     665              :       }
     666            0 :     } else if (context.style.mode === 'straight') {
     667            0 :       const innerA = offsetPoint(
     668              :         startPoint,
     669              :         link.startDirection,
     670              :         STRAIGHT_LINK_OFFSET
     671              :       )
     672            0 :       const innerB = offsetPoint(
     673              :         endPoint,
     674              :         link.endDirection,
     675              :         STRAIGHT_LINK_OFFSET
     676              :       )
     677            0 :       link.centerPos = {
     678              :         x: (innerA.x + innerB.x) * 0.5,
     679              :         y: (innerA.y + innerB.y) * 0.5
     680              :       }
     681              : 
     682            0 :       if (wantsAngle) {
     683            0 :         const diff = innerB.y - innerA.y
     684            0 :         if (Math.abs(diff) < 4) {
     685            0 :           link.centerAngle = 0
     686            0 :         } else if (diff > 0) {
     687            0 :           link.centerAngle = Math.PI * 0.5
     688              :         } else {
     689            0 :           link.centerAngle = -(Math.PI * 0.5)
     690              :         }
     691              :       }
     692              :     } else {
     693            0 :       link.centerPos = this.getLinkCenter(link)
     694            0 :       if (wantsAngle) {
     695            0 :         link.centerAngle = Math.atan2(
     696              :           endPoint.y - startPoint.y,
     697              :           endPoint.x - startPoint.x
     698              :         )
     699              :       }
     700              :     }
     701              :   }
     702              : 
     703              :   /**
     704              :    * Draw the center marker on a link (for menu interaction)
     705              :    * Matches the original litegraph center marker rendering
     706              :    */
     707              :   private drawCenterMarker(
     708              :     ctx: CanvasRenderingContext2D,
     709              :     link: LinkRenderData,
     710              :     context: RenderContext,
     711              :     color: string
     712              :   ): void {
     713       111417 :     if (!link.centerPos) return
     714              : 
     715       111417 :     ctx.beginPath()
     716              : 
     717       111417 :     if (
     718              :       context.style.centerMarkerShape === 'arrow' &&
     719              :       link.centerAngle !== undefined
     720              :     ) {
     721            0 :       const transform = ctx.getTransform()
     722            0 :       ctx.translate(link.centerPos.x, link.centerPos.y)
     723            0 :       ctx.rotate(link.centerAngle)
     724              :       // The math is off, but it currently looks better in chromium (from original)
     725            0 :       ctx.moveTo(-3.2, -5)
     726            0 :       ctx.lineTo(7, 0)
     727            0 :       ctx.lineTo(-3.2, 5)
     728            0 :       ctx.setTransform(transform)
     729              :     } else {
     730              :       // Default to circle
     731       111417 :       ctx.arc(link.centerPos.x, link.centerPos.y, 5, 0, Math.PI * 2)
     732              :     }
     733              : 
     734              :     // Apply disabled pattern or color
     735       111417 :     if (link.disabled && context.patterns?.disabled) {
     736           96 :       const { fillStyle, globalAlpha } = ctx
     737           96 :       ctx.fillStyle = context.patterns.disabled
     738           96 :       ctx.globalAlpha = 0.75
     739           96 :       ctx.fill()
     740           96 :       ctx.globalAlpha = globalAlpha
     741           96 :       ctx.fillStyle = fillStyle
     742              :     } else {
     743       111321 :       ctx.fillStyle = color
     744       111321 :       ctx.fill()
     745              :     }
     746              :   }
     747              : }
        

Generated by: LCOV version 2.0-1