10% inside shape still tracks outline

This commit is contained in:
Mark Tolmacs 2025-03-03 17:00:09 +01:00
parent 1819661828
commit cbe6705c98
3 changed files with 34 additions and 19 deletions

View file

@ -917,6 +917,7 @@ export const bindPointToSnapToElementOutline = (
arrow: ExcalidrawElbowArrowElement,
bindableElement: ExcalidrawBindableElement,
startOrEnd: "start" | "end",
elementsMap: ElementsMap,
): GlobalPoint => {
if (isDevEnv() || isTestEnv()) {
invariant(arrow.points.length > 1, "Arrow should have at least 2 points");
@ -1023,26 +1024,16 @@ export const bindPointToSnapToElementOutline = (
pointDistance(intersection ?? edgePoint, center),
1e-5, // Avoid division by zero
);
const ratio = round(currentDistance / fullDistance);
switch (true) {
case ratio > 0.5:
return pointFromVector(
vectorScale(
vectorNormalize(
vectorFromPoint(intersection ?? center, adjacentPoint),
),
-FIXED_BINDING_DISTANCE,
),
intersection ?? edgePoint,
);
default:
if (elbowed) {
return headingToMidBindPoint(edgePoint, bindableElement, aabb);
}
return edgePoint;
if (!isInside) {
return intersection;
}
if (elbowed) {
return headingToMidBindPoint(edgePoint, bindableElement, aabb);
}
return edgePoint;
};
export const avoidRectangularCorner = (
@ -1232,6 +1223,7 @@ const updateBoundPoint = (
linearElement,
bindableElement,
startOrEnd === "startBinding" ? "start" : "end",
elementsMap,
).fixedPoint;
const globalMidPoint = pointFrom<GlobalPoint>(
bindableElement.x + bindableElement.width / 2,
@ -1318,6 +1310,24 @@ const updateBoundPoint = (
),
];
// debugClear();
// intersections.forEach((intersection) => {
// debugDrawPoint(intersection, { permanent: true, color: "red" });
// });
// debugDrawLine(
// lineSegment<GlobalPoint>(
// adjacentPoint,
// pointFromVector(
// vectorScale(
// vectorNormalize(vectorFromPoint(focusPointAbsolute, adjacentPoint)),
// interceptorLength,
// ),
// adjacentPoint,
// ),
// ),
// { permanent: true, color: "green" },
// );
if (intersections.length > 1) {
// The adjacent point is outside the shape (+ gap)
newEdgePoint = intersections[0];
@ -1341,6 +1351,7 @@ export const calculateFixedPointForElbowArrowBinding = (
linearElement: NonDeleted<ExcalidrawElbowArrowElement>,
hoveredElement: ExcalidrawBindableElement,
startOrEnd: "start" | "end",
elementsMap: ElementsMap,
): { fixedPoint: FixedPoint } => {
const bounds = [
hoveredElement.x,
@ -1352,6 +1363,7 @@ export const calculateFixedPointForElbowArrowBinding = (
linearElement,
hoveredElement,
startOrEnd,
elementsMap,
);
const globalMidPoint = pointFrom(
bounds[0] + (bounds[2] - bounds[0]) / 2,

View file

@ -251,7 +251,7 @@ export class LinearElementEditor {
app.scene.getNonDeletedElements(),
app.scene.getNonDeletedElementsMap(),
app.state.zoom,
elbowed,
true,
elbowed,
);
const p = pointFrom<GlobalPoint>(coords.x, coords.y);
@ -270,6 +270,7 @@ export class LinearElementEditor {
},
hoveredElement,
pointIndex === 0 ? "start" : "end",
app.scene.getNonDeletedElementsMap(),
);
}