Line data Source code
1 : import type {
2 : SizedExtents as SpatialBounds,
3 : PositionedRect as PositionedNode
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 : import { calculatePositionExtents } from '@/utils/positionBounds'
6 :
7 : export type { PositionedRect as PositionedNode } from '@comfyorg/shared-frontend-utils/geometry'
8 :
9 : /**
10 : * Calculate the spatial bounding box of positioned nodes
11 : */
12 9183 : export function calculateNodeBounds(
13 : nodes: PositionedNode[]
14 : ): SpatialBounds | null {
15 9183 : const extents = calculatePositionExtents(nodes)
16 9183 : if (!extents) return null
17 9183 : const { minX, minY, maxX, maxY } = extents
18 :
19 9183 : return {
20 : minX,
21 : minY,
22 : maxX,
23 : maxY,
24 : width: maxX - minX,
25 : height: maxY - minY
26 : }
27 : }
28 :
29 : /**
30 : * Enforce minimum viewport dimensions for better visualization
31 : */
32 7782 : export function enforceMinimumBounds(
33 : bounds: SpatialBounds,
34 : minWidth: number = 2500,
35 : minHeight: number = 2000
36 : ): SpatialBounds {
37 7782 : let { minX, minY, maxX, maxY, width, height } = bounds
38 :
39 7782 : if (width < minWidth) {
40 7777 : const padding = (minWidth - width) / 2
41 7777 : minX -= padding
42 7777 : maxX += padding
43 7777 : width = minWidth
44 : }
45 :
46 7782 : if (height < minHeight) {
47 7781 : const padding = (minHeight - height) / 2
48 7781 : minY -= padding
49 7781 : maxY += padding
50 7781 : height = minHeight
51 : }
52 :
53 7782 : return { minX, minY, maxX, maxY, width, height }
54 : }
55 :
56 : /**
57 : * Calculate the scale factor to fit bounds within a viewport
58 : */
59 11545 : export function calculateMinimapScale(
60 : bounds: SpatialBounds,
61 : viewportWidth: number,
62 : viewportHeight: number,
63 : padding: number = 0.9
64 : ): number {
65 11545 : if (bounds.width === 0 || bounds.height === 0) {
66 0 : return 1
67 : }
68 :
69 11545 : const scaleX = viewportWidth / bounds.width
70 11545 : const scaleY = viewportHeight / bounds.height
71 :
72 11545 : return Math.min(scaleX, scaleY) * padding
73 : }
|