Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
 * Constrains a point to stay within the specified bounds
 * @param point - The point to constrain
 * @param bounds - The bounds {width, height}
 * @returns The constrained point
 */
export function constrainPointToBounds(
  point: { x: number; y: number },
  bounds: { width: number; height: number },
): { x: number; y: number } {
  const constrained = {
    x: Math.max(0, Math.min(bounds.width, point.x)),
    y: Math.max(0, Math.min(bounds.height, point.y)),
  };
 
  return constrained;
}
 
/**
 * Constrains multiple points to stay within the specified bounds
 * @param points - Array of points to constrain
 * @param bounds - The bounds {width, height}
 * @returns Array of constrained points
 */
export function constrainPointsToBounds(
  points: Array<{ x: number; y: number }>,
  bounds: { width: number; height: number },
): Array<{ x: number; y: number }> {
  return points.map((point) => constrainPointToBounds(point, bounds));
}
 
/**
 * Checks if a point is within the specified bounds
 * @param point - The point to check
 * @param bounds - The bounds {width, height}
 * @returns True if the point is within bounds
 */
export function isPointWithinBounds(
  point: { x: number; y: number },
  bounds: { width: number; height: number },
): boolean {
  return point.x >= 0 && point.x <= bounds.width && point.y >= 0 && point.y <= bounds.height;
}
 
/**
 * Calculates the bounding box of a group of points (including control points for Bezier curves)
 * @param points - Array of points (can be simple points or BezierPoint objects)
 * @returns Bounding box {left, top, right, bottom}
 */
export function calculateGroupBoundingBox(
  points: Array<{
    x: number;
    y: number;
    controlPoint1?: { x: number; y: number };
    controlPoint2?: { x: number; y: number };
  }>,
): {
  left: number;
  top: number;
  right: number;
  bottom: number;
} {
  if (points.length === 0) {
    return { left: 0, top: 0, right: 0, bottom: 0 };
  }
 
  // Start with the first point
  let left = points[0].x;
  let top = points[0].y;
  let right = points[0].x;
  let bottom = points[0].y;
 
  // Include control points if they exist
  if (points[0].controlPoint1) {
    left = Math.min(left, points[0].controlPoint1.x);
    top = Math.min(top, points[0].controlPoint1.y);
    right = Math.max(right, points[0].controlPoint1.x);
    bottom = Math.max(bottom, points[0].controlPoint1.y);
  }
  if (points[0].controlPoint2) {
    left = Math.min(left, points[0].controlPoint2.x);
    top = Math.min(top, points[0].controlPoint2.y);
    right = Math.max(right, points[0].controlPoint2.x);
    bottom = Math.max(bottom, points[0].controlPoint2.y);
  }
 
  // Process all other points
  for (let i = 1; i < points.length; i++) {
    const point = points[i];
 
    // Include main point
    left = Math.min(left, point.x);
    top = Math.min(top, point.y);
    right = Math.max(right, point.x);
    bottom = Math.max(bottom, point.y);
 
    // Include control points if they exist
    if (point.controlPoint1) {
      left = Math.min(left, point.controlPoint1.x);
      top = Math.min(top, point.controlPoint1.y);
      right = Math.max(right, point.controlPoint1.x);
      bottom = Math.max(bottom, point.controlPoint1.y);
    }
    if (point.controlPoint2) {
      left = Math.min(left, point.controlPoint2.x);
      top = Math.min(top, point.controlPoint2.y);
      right = Math.max(right, point.controlPoint2.x);
      bottom = Math.max(bottom, point.controlPoint2.y);
    }
  }
 
  return { left, top, right, bottom };
}
 
/**
 * Constrains anchor points to stay within bounds as a group
 * @param points - Array of points to constrain
 * @param bounds - The bounds {width, height}
 * @returns Array of constrained points
 */
export function constrainAnchorPointsToBounds(
  points: Array<{
    x: number;
    y: number;
    controlPoint1?: { x: number; y: number };
    controlPoint2?: { x: number; y: number };
    [key: string]: any; // Allow additional properties (id, prevPointId, isBezier, etc.)
  }>,
  bounds: { width: number; height: number },
): Array<{
  x: number;
  y: number;
  controlPoint1?: { x: number; y: number };
  controlPoint2?: { x: number; y: number };
  [key: string]: any;
}> {
  if (points.length === 0) return points;
 
  // Calculate the bounding box of only the anchor points (not control points)
  const anchorPoints = points.map((p) => ({ x: p.x, y: p.y }));
  const anchorBbox = calculateGroupBoundingBox(anchorPoints);
 
  // Calculate how much we need to move the anchor points to keep them within bounds
  let deltaX = 0;
  let deltaY = 0;
 
  // Check left edge
  if (anchorBbox.left < 0) {
    deltaX = -anchorBbox.left;
  }
  // Check right edge
  if (anchorBbox.right > bounds.width) {
    deltaX = bounds.width - anchorBbox.right;
  }
 
  // Check top edge
  if (anchorBbox.top < 0) {
    deltaY = -anchorBbox.top;
  }
  // Check bottom edge
  if (anchorBbox.bottom > bounds.height) {
    deltaY = bounds.height - anchorBbox.bottom;
  }
 
  // If no constraint needed, return original points
  if (deltaX === 0 && deltaY === 0) {
    return points;
  }
 
  // Apply the constraint to anchor points and their control points
  // Preserve all properties from the original point (id, prevPointId, isBezier, etc.)
  const constrainedPoints = points.map((point) => {
    // Spread all properties from the original point to preserve skeleton relationships
    const constrainedPoint = {
      ...point,
      x: point.x + deltaX,
      y: point.y + deltaY,
    };
 
    // Apply the same delta to control points (they move with the anchor point)
    if (point.controlPoint1) {
      constrainedPoint.controlPoint1 = {
        x: point.controlPoint1.x + deltaX,
        y: point.controlPoint1.y + deltaY,
      };
    }
    if (point.controlPoint2) {
      constrainedPoint.controlPoint2 = {
        x: point.controlPoint2.x + deltaX,
        y: point.controlPoint2.y + deltaY,
      };
    }
 
    return constrainedPoint;
  });
 
  // Debug logging
  return constrainedPoints;
}
 
/**
 * Calculates the constrained position for a transformer within image bounds
 * @param transformer - The Konva transformer object
 * @param bounds - The image bounds {x, y, width, height}
 * @param scaleX - Component scale X factor
 * @param scaleY - Component scale Y factor
 * @param transform - Transform object with zoom and offset
 * @param fitScale - Fit scale factor
 * @returns The constrained position {x, y} or null if no constraint needed
 */
export function calculateTransformerConstraints(
  transformer: any,
  bounds: { x: number; y: number; width: number; height: number },
  scaleX = 1,
  scaleY = 1,
  transform: { zoom: number; offsetX: number; offsetY: number } = { zoom: 1, offsetX: 0, offsetY: 0 },
  fitScale = 1,
): { x: number; y: number } | null {
  if (!transformer || !bounds) return null;
 
  // Get the transformer's current position and size
  const transformerPos = { x: transformer.x(), y: transformer.y() };
  const transformerSize = { width: transformer.width(), height: transformer.height() };
 
  // Get the transformer's bounding box
  const transformerBox = transformer.getClientRect();
 
  // Convert transformer coordinates to image coordinates for comparison
  // The transformer is in stage coordinates, but bounds are in image coordinates
  const transformerInImageCoords = {
    x: (transformerBox.x - transform.offsetX) / (transform.zoom * fitScale),
    y: (transformerBox.y - transform.offsetY) / (transform.zoom * fitScale),
    width: transformerBox.width / (transform.zoom * fitScale),
    height: transformerBox.height / (transform.zoom * fitScale),
  };
 
  // Use the original bounds (no scaling needed)
  const scaledBounds = bounds;
 
  // Calculate the image bounds in the same coordinate system as the transformer
  const imageBounds = {
    left: scaledBounds.x,
    top: scaledBounds.y,
    right: scaledBounds.x + scaledBounds.width,
    bottom: scaledBounds.y + scaledBounds.height,
  };
 
  // Calculate constraints using image coordinates
  let constrainedX = transformerPos.x;
  let constrainedY = transformerPos.y;
  let needsConstraint = false;
 
  // Constrain to left edge
  if (transformerInImageCoords.x < imageBounds.left) {
    // Convert back to stage coordinates
    constrainedX = imageBounds.left * (transform.zoom * fitScale) + transform.offsetX;
    needsConstraint = true;
  }
 
  // Constrain to right edge
  if (transformerInImageCoords.x + transformerInImageCoords.width > imageBounds.right) {
    // Convert back to stage coordinates
    constrainedX =
      (imageBounds.right - transformerInImageCoords.width) * (transform.zoom * fitScale) + transform.offsetX;
    needsConstraint = true;
  }
 
  // Constrain to top edge
  if (transformerInImageCoords.y < imageBounds.top) {
    // Convert back to stage coordinates
    constrainedY = imageBounds.top * (transform.zoom * fitScale) + transform.offsetY;
    needsConstraint = true;
  }
 
  // Constrain to bottom edge
  if (transformerInImageCoords.y + transformerInImageCoords.height > imageBounds.bottom) {
    // Convert back to stage coordinates
    constrainedY =
      (imageBounds.bottom - transformerInImageCoords.height) * (transform.zoom * fitScale) + transform.offsetY;
    needsConstraint = true;
  }
 
  return needsConstraint ? { x: constrainedX, y: constrainedY } : null;
}
 
/**
 * Constrains a group of points to stay within bounds as a group (anchor points only)
 * Control points move with their anchor points and are not constrained independently
 * @param points - Array of points to constrain (can be simple points or BezierPoint objects)
 * @param bounds - The bounds {width, height}
 * @returns Array of constrained points
 */
export function constrainGroupToBounds(
  points: Array<{
    x: number;
    y: number;
    controlPoint1?: { x: number; y: number };
    controlPoint2?: { x: number; y: number };
    [key: string]: any; // Allow additional properties (id, prevPointId, isBezier, etc.)
  }>,
  bounds: { width: number; height: number },
): Array<{
  x: number;
  y: number;
  controlPoint1?: { x: number; y: number };
  controlPoint2?: { x: number; y: number };
  [key: string]: any;
}> {
  if (points.length === 0) return points;
 
  // Only constrain anchor points as a group
  // Control points will move with their anchor points automatically
  return constrainAnchorPointsToBounds(points, bounds);
}