-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arrow direction issue #146
Comments
Hello! Thank you for the issue! The lib needs many drawing enhancements indeed. How would you expect the arrows to be drawn? In that case, I'm not sure what we'd expect. |
Mmmmh in terms of API I wouldn't expect For such a result, this should be This is just a rough idea 🤔 |
Haha good point, but try putting down the opacity of your green block. I think you'll notice that the arrow goes backward, starting from the right border 😅 |
Ah I see 😄 Anyway, I think the package should solve this issue, and that it shouldn't be up to the user to try to find out exactly when the source and the target area on the same x position, and then manually have to set some sort of offset. Maybe you could add a prop to opt-out or into this though. |
I resolved the arrow direction issue in myself. simplescreenrecorder-2021-08-15_23.52.56.mp4Commit: hata6502/troopa-wasm@c305689 By using useEffect(() => {
const handle = () =>
setRelations(
getRelations({
anchorlessRelations: [
...anchorlessRelations,
...(connectionCuror ? [{ targetId: cursorID }] : []),
],
sourceId: radioID,
})
);
handle();
const intervalID = setInterval(handle, 200);
return () => clearInterval(intervalID);
}, [anchorlessRelations, connectionCuror, cursorID, radioID]); Then, detect arrow direction by getting const arrowLength = 40;
const detectAnchors = ({
sourceCenterX,
sourceCenterY,
targetCenterX,
targetCenterY,
}: {
sourceCenterX: number;
sourceCenterY: number;
targetCenterX: number;
targetCenterY: number;
}): Pick<Relation, "sourceAnchor" | "targetAnchor"> => {
if (Math.abs(sourceCenterX - targetCenterX) >= arrowLength) {
return sourceCenterX < targetCenterX
? {
sourceAnchor: "right",
targetAnchor: "left",
}
: {
sourceAnchor: "left",
targetAnchor: "right",
};
}
if (Math.abs(sourceCenterY - targetCenterY) >= arrowLength) {
return sourceCenterY < targetCenterY
? {
sourceAnchor: "bottom",
targetAnchor: "top",
}
: {
sourceAnchor: "top",
targetAnchor: "bottom",
};
}
return {
sourceAnchor: "middle",
targetAnchor: "middle",
};
};
const getRelations = ({
anchorlessRelations,
sourceId,
}: {
anchorlessRelations: AnchorlessRelation[];
sourceId: string;
}) => {
const sourceElement = document.getElementById(sourceId);
if (!sourceElement) {
throw new Error(`Could not find element with id "${sourceId}"`);
}
const soruceDOMRect = sourceElement.getBoundingClientRect();
const sourceCenterX = soruceDOMRect.left + soruceDOMRect.width / 2;
const sourceCenterY = soruceDOMRect.top + soruceDOMRect.height / 2;
return anchorlessRelations.map((anchorlessRelation): Relation => {
const targetElement = document.getElementById(anchorlessRelation.targetId);
if (!targetElement) {
throw new Error(
`Could not find element with id "${anchorlessRelation.targetId}"`
);
}
const targetDOMRect = targetElement.getBoundingClientRect();
const targetCenterX = targetDOMRect.left + targetDOMRect.width / 2;
const targetCenterY = targetDOMRect.top + targetDOMRect.height / 2;
return {
...anchorlessRelation,
...detectAnchors({
sourceCenterX,
sourceCenterY,
targetCenterX,
targetCenterY,
}),
};
});
}; |
Issue
When you have two items where the source anchor is on the same x point as the target anchor, the arrow get pointed in the wrong direction.
The text was updated successfully, but these errors were encountered: