10% inside shape still tracks outline

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

View file

@ -916,6 +916,7 @@ export const bindPointToSnapToElementOutline = (
arrow: ExcalidrawElbowArrowElement, arrow: ExcalidrawElbowArrowElement,
bindableElement: ExcalidrawBindableElement, bindableElement: ExcalidrawBindableElement,
startOrEnd: "start" | "end", startOrEnd: "start" | "end",
elementsMap: ElementsMap,
): GlobalPoint => { ): GlobalPoint => {
if (isDevEnv() || isTestEnv()) { if (isDevEnv() || isTestEnv()) {
invariant(arrow.points.length > 1, "Arrow should have at least 2 points"); invariant(arrow.points.length > 1, "Arrow should have at least 2 points");
@ -1022,26 +1023,16 @@ export const bindPointToSnapToElementOutline = (
pointDistance(intersection ?? edgePoint, center), pointDistance(intersection ?? edgePoint, center),
1e-5, // Avoid division by zero 1e-5, // Avoid division by zero
); );
const ratio = round(currentDistance / fullDistance);
switch (true) { if (!isInside) {
case ratio > 0.5: return intersection;
return pointFromVector(
vectorScale(
vectorNormalize(
vectorFromPoint(intersection ?? center, adjacentPoint),
),
-FIXED_BINDING_DISTANCE,
),
intersection ?? edgePoint,
);
default:
if (elbowed) {
return headingToMidBindPoint(edgePoint, bindableElement, aabb);
}
return edgePoint;
} }
if (elbowed) {
return headingToMidBindPoint(edgePoint, bindableElement, aabb);
}
return edgePoint;
}; };
export const avoidRectangularCorner = ( export const avoidRectangularCorner = (
@ -1231,6 +1222,7 @@ const updateBoundPoint = (
linearElement, linearElement,
bindableElement, bindableElement,
startOrEnd === "startBinding" ? "start" : "end", startOrEnd === "startBinding" ? "start" : "end",
elementsMap,
).fixedPoint; ).fixedPoint;
const globalMidPoint = pointFrom<GlobalPoint>( const globalMidPoint = pointFrom<GlobalPoint>(
bindableElement.x + bindableElement.width / 2, bindableElement.x + bindableElement.width / 2,
@ -1317,6 +1309,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) { if (intersections.length > 1) {
// The adjacent point is outside the shape (+ gap) // The adjacent point is outside the shape (+ gap)
newEdgePoint = intersections[0]; newEdgePoint = intersections[0];
@ -1340,6 +1350,7 @@ export const calculateFixedPointForElbowArrowBinding = (
linearElement: NonDeleted<ExcalidrawElbowArrowElement>, linearElement: NonDeleted<ExcalidrawElbowArrowElement>,
hoveredElement: ExcalidrawBindableElement, hoveredElement: ExcalidrawBindableElement,
startOrEnd: "start" | "end", startOrEnd: "start" | "end",
elementsMap: ElementsMap,
): { fixedPoint: FixedPoint } => { ): { fixedPoint: FixedPoint } => {
const bounds = [ const bounds = [
hoveredElement.x, hoveredElement.x,
@ -1351,6 +1362,7 @@ export const calculateFixedPointForElbowArrowBinding = (
linearElement, linearElement,
hoveredElement, hoveredElement,
startOrEnd, startOrEnd,
elementsMap,
); );
const globalMidPoint = pointFrom( const globalMidPoint = pointFrom(
bounds[0] + (bounds[2] - bounds[0]) / 2, bounds[0] + (bounds[2] - bounds[0]) / 2,

View file

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

View file

@ -1655,6 +1655,7 @@ export const actionChangeArrowType = register({
newElement, newElement,
startHoveredElement, startHoveredElement,
"start", "start",
elementsMap,
) )
: startGlobalPoint; : startGlobalPoint;
const finalEndPoint = endHoveredElement const finalEndPoint = endHoveredElement
@ -1662,6 +1663,7 @@ export const actionChangeArrowType = register({
newElement, newElement,
endHoveredElement, endHoveredElement,
"end", "end",
elementsMap,
) )
: endGlobalPoint; : endGlobalPoint;