Line data Source code
1 : import type { LGraph } from '@/lib/litegraph/src/LGraph'
2 : import { createBounds } from '@/lib/litegraph/src/measure'
3 : import type {
4 : Bounds,
5 : Point2D as Point
6 : } from '@comfyorg/shared-frontend-utils/geometry'
7 :
8 2356 : export const RENDER_SCALE_FACTOR = 1.2
9 :
10 : export const MIN_NODE_WIDTH = 225
11 :
12 2356 : const anchorCache = new WeakMap<LGraph, Point>()
13 :
14 : /**
15 : * Returns a render anchor for a graph.
16 : *
17 : * @remarks
18 : * The anchor is computed once per graph instance and cached for that graph's
19 : * lifetime. This is intended for one-shot normalization passes.
20 : */
21 15 : export function getGraphRenderAnchor(graph: LGraph): Point {
22 15 : const cached = anchorCache.get(graph)
23 15 : if (cached) return cached
24 :
25 15 : const bounds = graph.nodes.length ? createBounds(graph.nodes) : undefined
26 15 : const anchor = bounds ? { x: bounds[0], y: bounds[1] } : { x: 0, y: 0 }
27 15 : anchorCache.set(graph, anchor)
28 15 : return anchor
29 : }
30 :
31 55 : export function unprojectPoint(
32 : point: Point,
33 : anchor: Point,
34 : scale: number
35 : ): Point {
36 55 : if (scale === 1) return point
37 55 : return {
38 : x: anchor.x + (point.x - anchor.x) / scale,
39 : y: anchor.y + (point.y - anchor.y) / scale
40 : }
41 : }
42 :
43 55 : export function unprojectBounds(
44 : bounds: Bounds,
45 : anchor: Point,
46 : scale: number
47 : ): Bounds {
48 55 : const topLeft = unprojectPoint({ x: bounds.x, y: bounds.y }, anchor, scale)
49 55 : return {
50 : x: topLeft.x,
51 : y: topLeft.y,
52 : width: bounds.width / scale,
53 : height: bounds.height / scale
54 : }
55 : }
|