Line data Source code
1 : import type {
2 : CompactSize,
3 : CompactRect as Rect
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 : export interface SnapTargets {
6 : xs: number[]
7 : ys: number[]
8 : }
9 : export interface Guide {
10 : axis: 'x' | 'y'
11 : pos: number
12 : kind?: 'edge' | 'gap'
13 : cross?: number
14 : spans?: Array<[number, number]>
15 : }
16 : export interface SnapExtras {
17 : gridX?: number
18 : gridY?: number
19 : guideXs?: number[]
20 : guideYs?: number[]
21 : }
22 : export interface SnapOpts {
23 : thrX: number
24 : thrY: number
25 : minWH: number
26 : boundsW?: number
27 : boundsH?: number
28 : clamp?: boolean
29 : eqRects?: Rect[]
30 : }
31 : export interface SnapResult {
32 : rect: Rect
33 : guides: Guide[]
34 : }
35 :
36 0 : function clampNum(v: number, lo: number, hi: number): number {
37 0 : return Math.max(lo, Math.min(hi, v))
38 : }
39 :
40 0 : export function buildSnapTargets(
41 : otherRects: Rect[],
42 : bounds?: CompactSize,
43 : extras?: SnapExtras
44 : ): SnapTargets {
45 0 : const bw = bounds?.w ?? 1
46 0 : const bh = bounds?.h ?? 1
47 0 : const xs = [0, bw / 2, bw]
48 0 : const ys = [0, bh / 2, bh]
49 0 : for (const r of otherRects) {
50 0 : xs.push(r.x, r.x + r.w / 2, r.x + r.w)
51 0 : ys.push(r.y, r.y + r.h / 2, r.y + r.h)
52 : }
53 0 : if (extras?.gridX && extras.gridX > 0) {
54 0 : for (let v = 0; v <= bw + 1e-9; v += extras.gridX) xs.push(v)
55 : }
56 0 : if (extras?.gridY && extras.gridY > 0) {
57 0 : for (let v = 0; v <= bh + 1e-9; v += extras.gridY) ys.push(v)
58 : }
59 0 : for (const g of extras?.guideXs ?? []) xs.push(g)
60 0 : for (const g of extras?.guideYs ?? []) ys.push(g)
61 0 : return { xs, ys }
62 : }
63 :
64 : interface EqCandidate {
65 : pos: number
66 : guide: Guide
67 : }
68 :
69 0 : function overlaps(a0: number, a1: number, b0: number, b1: number): boolean {
70 0 : return a0 < b1 && a1 > b0
71 : }
72 :
73 0 : function eqCandidatesAxis(
74 : rect: Rect,
75 : others: Rect[],
76 : axis: 'x' | 'y'
77 : ): EqCandidate[] {
78 0 : const p = (r: Rect) => (axis === 'x' ? r.x : r.y)
79 0 : const s = (r: Rect) => (axis === 'x' ? r.w : r.h)
80 0 : const c0 = (r: Rect) => (axis === 'x' ? r.y : r.x)
81 0 : const c1 = (r: Rect) => (axis === 'x' ? r.y + r.h : r.x + r.w)
82 0 : const cross = axis === 'x' ? rect.y + rect.h / 2 : rect.x + rect.w / 2
83 :
84 0 : const near = others.filter((o) => overlaps(c0(o), c1(o), c0(rect), c1(rect)))
85 0 : const lefts = near
86 : .filter((o) => p(o) + s(o) <= p(rect) + s(rect) / 2)
87 : .sort((a, b) => p(b) + s(b) - (p(a) + s(a)))
88 0 : const rights = near
89 : .filter((o) => p(o) >= p(rect) + s(rect) / 2)
90 : .sort((a, b) => p(a) - p(b))
91 0 : const out: EqCandidate[] = []
92 :
93 0 : const L = lefts.at(0)
94 0 : const R = rights.at(0)
95 0 : if (L && R) {
96 0 : const free = p(R) - (p(L) + s(L)) - s(rect)
97 0 : if (free >= 0) {
98 0 : const pos = p(L) + s(L) + free / 2
99 0 : out.push({
100 : pos,
101 : guide: {
102 : axis,
103 : pos,
104 : kind: 'gap',
105 : cross,
106 : spans: [
107 : [p(L) + s(L), pos],
108 : [pos + s(rect), p(R)]
109 : ]
110 : }
111 : })
112 : }
113 : }
114 0 : if (lefts.length >= 2) {
115 0 : const L1 = lefts[0]
116 0 : const L2 = lefts[1]
117 0 : const gap = p(L1) - (p(L2) + s(L2))
118 0 : if (gap >= 0) {
119 0 : const pos = p(L1) + s(L1) + gap
120 0 : out.push({
121 : pos,
122 : guide: {
123 : axis,
124 : pos,
125 : kind: 'gap',
126 : cross,
127 : spans: [
128 : [p(L2) + s(L2), p(L1)],
129 : [p(L1) + s(L1), pos]
130 : ]
131 : }
132 : })
133 : }
134 : }
135 0 : if (rights.length >= 2) {
136 0 : const R1 = rights[0]
137 0 : const R2 = rights[1]
138 0 : const gap = p(R2) - (p(R1) + s(R1))
139 0 : if (gap >= 0) {
140 0 : const pos = p(R1) - gap - s(rect)
141 0 : out.push({
142 : pos,
143 : guide: {
144 : axis,
145 : pos,
146 : kind: 'gap',
147 : cross,
148 : spans: [
149 : [pos + s(rect), p(R1)],
150 : [p(R1) + s(R1), p(R2)]
151 : ]
152 : }
153 : })
154 : }
155 : }
156 0 : return out
157 : }
158 :
159 0 : export function nearestTarget(
160 : val: number,
161 : targets: number[],
162 : thr: number
163 : ): number | null {
164 0 : let best: number | null = null
165 0 : let bd = thr
166 0 : for (const t of targets) {
167 0 : const dd = Math.abs(val - t)
168 0 : if (dd < bd) {
169 0 : bd = dd
170 0 : best = t
171 : }
172 : }
173 0 : return best
174 : }
175 :
176 0 : export function applySnap(
177 : mode: string,
178 : rect: Rect,
179 : targets: SnapTargets,
180 : opts: SnapOpts
181 : ): SnapResult {
182 0 : let { x, y, w, h } = rect
183 0 : const guides: Guide[] = []
184 0 : const { thrX, thrY, minWH } = opts
185 0 : const bw = opts.boundsW ?? 1
186 0 : const bh = opts.boundsH ?? 1
187 0 : const clamp = opts.clamp !== false
188 :
189 0 : if (mode === 'move') {
190 0 : let bestDX: number | null = null
191 0 : let guideX: Guide | null = null
192 0 : for (const v of [x, x + w / 2, x + w]) {
193 0 : const t = nearestTarget(v, targets.xs, thrX)
194 0 : if (t != null) {
195 0 : const dd = t - v
196 0 : if (bestDX === null || Math.abs(dd) < Math.abs(bestDX)) {
197 0 : bestDX = dd
198 0 : guideX = { axis: 'x', pos: t, kind: 'edge' }
199 : }
200 : }
201 : }
202 0 : let bestDY: number | null = null
203 0 : let guideY: Guide | null = null
204 0 : for (const v of [y, y + h / 2, y + h]) {
205 0 : const t = nearestTarget(v, targets.ys, thrY)
206 0 : if (t != null) {
207 0 : const dd = t - v
208 0 : if (bestDY === null || Math.abs(dd) < Math.abs(bestDY)) {
209 0 : bestDY = dd
210 0 : guideY = { axis: 'y', pos: t, kind: 'edge' }
211 : }
212 : }
213 : }
214 0 : if (opts.eqRects?.length) {
215 0 : for (const c of eqCandidatesAxis(rect, opts.eqRects, 'x')) {
216 0 : const dd = c.pos - x
217 0 : if (
218 : Math.abs(dd) < thrX &&
219 : (bestDX === null || Math.abs(dd) < Math.abs(bestDX))
220 : ) {
221 0 : bestDX = dd
222 0 : guideX = c.guide
223 : }
224 : }
225 0 : for (const c of eqCandidatesAxis(rect, opts.eqRects, 'y')) {
226 0 : const dd = c.pos - y
227 0 : if (
228 : Math.abs(dd) < thrY &&
229 : (bestDY === null || Math.abs(dd) < Math.abs(bestDY))
230 : ) {
231 0 : bestDY = dd
232 0 : guideY = c.guide
233 : }
234 : }
235 : }
236 0 : if (bestDX !== null && guideX) {
237 0 : x += bestDX
238 0 : guides.push(guideX)
239 : }
240 0 : if (bestDY !== null && guideY) {
241 0 : y += bestDY
242 0 : guides.push(guideY)
243 : }
244 : } else {
245 0 : if (mode.includes('e')) {
246 0 : const t = nearestTarget(x + w, targets.xs, thrX)
247 0 : if (t != null) {
248 0 : w = t - x
249 0 : guides.push({ axis: 'x', pos: t })
250 : }
251 : }
252 0 : if (mode.includes('w')) {
253 0 : const t = nearestTarget(x, targets.xs, thrX)
254 0 : if (t != null) {
255 0 : const rt = x + w
256 0 : x = t
257 0 : w = rt - x
258 0 : guides.push({ axis: 'x', pos: t })
259 : }
260 : }
261 0 : if (mode.includes('s')) {
262 0 : const t = nearestTarget(y + h, targets.ys, thrY)
263 0 : if (t != null) {
264 0 : h = t - y
265 0 : guides.push({ axis: 'y', pos: t })
266 : }
267 : }
268 0 : if (mode.includes('n')) {
269 0 : const t = nearestTarget(y, targets.ys, thrY)
270 0 : if (t != null) {
271 0 : const bt = y + h
272 0 : y = t
273 0 : h = bt - y
274 0 : guides.push({ axis: 'y', pos: t })
275 : }
276 : }
277 0 : w = Math.max(minWH, w)
278 0 : h = Math.max(minWH, h)
279 : }
280 :
281 0 : if (clamp) {
282 0 : x = clampNum(x, 0, Math.max(0, bw - w))
283 0 : y = clampNum(y, 0, Math.max(0, bh - h))
284 : }
285 0 : return { rect: { x, y, w, h }, guides }
286 : }
|