diff --git a/packages/element/src/elbowArrowRouting.ts b/packages/element/src/elbowArrowRouting.ts index 00163434ef..103aa021ff 100644 --- a/packages/element/src/elbowArrowRouting.ts +++ b/packages/element/src/elbowArrowRouting.ts @@ -462,7 +462,9 @@ export const getElbowArrowRoute = ( /** * The route's corner points, without any repeated (or closer than a tiny * fraction of a px, from rounding), or between segments running on along - * the same line the same way + * the same line the same way, and without any step too short to see + * (between endpoints nearly, but not exactly, in line): only where the route + * carries on the same way after it, so it's never left doubling back */ const tidyRoute = (points: GlobalPoint[]): GlobalPoint[] => { const same = (a: number, b: number) => Math.abs(a - b) < 1e-6; @@ -487,5 +489,17 @@ const tidyRoute = (points: GlobalPoint[]): GlobalPoint[] => { } } } - return tidy; + + const isStep = (i: number) => + i >= 1 && + i + 2 < tidy.length && + Math.abs(tidy[i][0] - tidy[i + 1][0]) + + Math.abs(tidy[i][1] - tidy[i + 1][1]) < + DEDUP_THRESHOLD && + Math.sign(tidy[i][0] - tidy[i - 1][0]) === + Math.sign(tidy[i + 2][0] - tidy[i + 1][0]) && + Math.sign(tidy[i][1] - tidy[i - 1][1]) === + Math.sign(tidy[i + 2][1] - tidy[i + 1][1]); + + return tidy.filter((_, i) => !isStep(i) && !isStep(i - 1)); }; diff --git a/packages/element/src/elbowArrowTemplates.ts b/packages/element/src/elbowArrowTemplates.ts index 43602eb4ce..2568e47452 100644 --- a/packages/element/src/elbowArrowTemplates.ts +++ b/packages/element/src/elbowArrowTemplates.ts @@ -17,7 +17,9 @@ * The route keeps clear of the elements' actual outlines (see * `elbowArrowOutline.ts`), rather than their bounds: each segment of them * only as far as the outlines reach over the span the segment runs along, so - * a route runs as close by a diamond or an ellipse as by a rectangle. + * a route runs as close by a diamond or an ellipse as by a rectangle. (How + * long its legs are, and how fast it moves out of an element's way, is by + * their bounds, see `legLength`.) * * Nothing is remembered between routes: the route is a function of the * elements and the endpoints alone. @@ -50,6 +52,13 @@ const DODGE_RATE = 3; */ const SQUEEZE_OVERLAP = PADDING / 2; +/** + * How far a route can cut into an element's outline, where there's no room + * to keep clear of it, and still count as clear of it: as good as touching + * its stroke + */ +const TOUCH = 2; + /** * An endpoint's element: its outline, and its bounds (with the binding gap), * or the endpoint itself (`solid` false), where it's not bound, which the @@ -130,11 +139,17 @@ const clampClear = (v: number, lo: number, hi: number, radius: number) => { }; /** - * How long a leg is: the padding, but with the other element ahead of it, - * closer than twice that, only half the room to it, so the route turns - * halfway between them; plus how far it is to the side of the leg's line, or - * how far behind the leg it starts, so the leg changes continuously as the - * elements move. Never shorter than `minLeg`. + * How long a leg is: the padding, but with the other element (its bounds) + * ahead of it, closer than twice that, only half the room to it, so the + * route turns halfway between them; plus how far it is to the side of the + * leg's line, or how far behind the leg it starts, so the leg changes + * continuously as the elements move. Never shorter than `minLeg`. + * + * By the elements' bounds, rather than their outlines: off the leg's line, + * an outline only reaches as far as its corner nearest the line, which for a + * diamond is halfway along it, so the leg would be cut short by an element + * that's beside it, rather than ahead of it (and change as fast as a slanted + * side slides past the line, rather than as fast as the element moves). */ const legLength = ( point: Point, @@ -148,23 +163,20 @@ const legLength = ( } const axis = heading[0] !== 0 ? 0 : 1; const sign = heading[axis]; - const [back, forward]: Side[] = axis === 0 ? ["l", "r"] : ["t", "b"]; - const [acrossLo, acrossHi]: Side[] = axis === 0 ? ["t", "b"] : ["l", "r"]; - const across = point[1 - axis]; - // (where the leg leaves its element along its line, and where it would run - // into the other one, or how far the other one is to the side of it) + const ownLo = axis === 0 ? own.l : own.t; + const ownHi = axis === 0 ? own.r : own.b; + const otherLo = axis === 0 ? other.l : other.t; + const otherHi = axis === 0 ? other.r : other.b; const exit = own.solid ? sign > 0 - ? Math.max(near(own, forward, across, across), point[axis]) - : Math.min(near(own, back, across, across), point[axis]) + ? Math.max(ownHi, point[axis]) + : Math.min(ownLo, point[axis]) : point[axis]; - const ahead = - (near(other, sign > 0 ? back : forward, across, across) - exit) * sign; - const beside = Math.max( - near(other, acrossLo, -Infinity, Infinity) - across, - across - near(other, acrossHi, -Infinity, Infinity), - 0, - ); + const ahead = ((sign > 0 ? otherLo : otherHi) - exit) * sign; + const across = point[1 - axis]; + const acrossLo = axis === 0 ? other.t : other.l; + const acrossHi = axis === 0 ? other.b : other.r; + const beside = Math.max(acrossLo - across, across - acrossHi, 0); return Math.max( minLeg, @@ -255,6 +267,35 @@ const clearBetween = (points: Point[], boxes: Box[]) => ), ); +/** + * How far an outline reaches toward `side` over the span from `from` to `to` + * across it, where it's over the span at all (and not at all otherwise) + */ +const spanReach = ( + outline: readonly Point[], + side: Side, + from: number, + to: number, +) => { + const across = side === "t" || side === "b" ? 0 : 1; + const toward = side === "t" || side === "l" ? -1 : 1; + const values = outline.map((point) => point[across]); + return Math.max(from, to) < Math.min(...values) || + Math.min(from, to) > Math.max(...values) + ? -toward * Infinity + : reach(outline, side, from, to); +}; + +/** + * Whether two elements overlap each other, their bounds (without their + * binding gaps) overlapping both across and along + */ +const overlapping = (a: Box, b: Box) => + a.solid && + b.solid && + Math.max(a.l + a.gap, b.l + b.gap) < Math.min(a.r - a.gap, b.r - b.gap) && + Math.max(a.t + a.gap, b.t + b.gap) < Math.min(a.b - a.gap, b.b - b.gap); + // ----------------------------------------------------------------------------- // Routes // ----------------------------------------------------------------------------- @@ -262,7 +303,8 @@ const clearBetween = (points: Point[], boxes: Box[]) => /** * The ends facing each other: a Z, turning halfway between the elements; * past each other, an S turning back between them, or with no room between - * them (or a little, see `squeeze`), around them + * them (or a little, beside each other, see `squeeze`), around them, and + * around the far side of either where it's in the way of the route there */ const routeFacing = (input: RouteInput): Route => { const { start: a, endBox: b, end } = input; @@ -303,49 +345,87 @@ const routeFacing = (input: RouteInput): Route => { } const down = ey >= 0; - const squeeze = input.sameElement ? 0 : ms + me + 2 * input.radius; - const x1 = lo + grow(overshoot, legS, ms); - const x2 = hi - grow(overshoot, legE, me); - // (between where the upper one reaches down to and the lower one up to, - // over the span the segment between them runs) const [upper, lower] = down ? [a, b] : [b, a]; - const upperEdge = near(upper, "b", x2, x1); - const lowerEdge = near(lower, "t", x2, x1); - const y = down - ? clampClear(mid(upperEdge, lowerEdge), 0, ey, input.radius) - : clampClear(mid(upperEdge, lowerEdge), ey, 0, input.radius); - const between: Point[] = [[0, 0], [x1, 0], [x1, y], [x2, y], [x2, ey], end]; - const apart = lowerEdge - upperEdge >= -(a.gap + b.gap); - if (apart || overshoot <= squeeze || clearBetween(between, [a, b])) { + const squeeze = input.sameElement ? 0 : ms + me + 2 * input.radius; + // (only squeezing between elements beside each other, rather than through + // where they overlap) + const squeezes = overshoot <= squeeze && !overlapping(a, b); + // the S, with its legs shortened by `shorten` (from none, to as short as + // they can be) + const turnBack = (shorten: number) => { + const x1 = lo + (1 - shorten) * grow(overshoot, legS, ms); + const x2 = hi - (1 - shorten) * grow(overshoot, legE, me); + // (between where the upper one reaches down to and the lower one up to, + // over the span the segment between them runs) + const upperEdge = near(upper, "b", x2, x1); + const lowerEdge = near(lower, "t", x2, x1); + const y = down + ? clampClear(mid(upperEdge, lowerEdge), 0, ey, input.radius) + : clampClear(mid(upperEdge, lowerEdge), ey, 0, input.radius); return { - points: between, - signature: - apart || overshoot > squeeze ? "facing:between" : "facing:squeeze", + points: [[0, 0], [x1, 0], [x1, y], [x2, y], [x2, ey], end] as Point[], + apart: lowerEdge - upperEdge >= -(a.gap + b.gap), + // (how much room there is between the elements themselves over it) + room: + spanReach(lower.outline, "t", x2, x1) - + spanReach(upper.outline, "b", x2, x1), }; + }; + const between = turnBack(0); + if (between.apart || squeezes || clearBetween(between.points, [a, b])) { + return { + points: between.points, + signature: + between.apart || !squeezes ? "facing:between" : "facing:squeeze", + }; + } + // where only the corners of their bounds are in the way, as diamonds' or + // ellipses' are clear of them, still turning back between them, with the + // legs only as much shorter as it takes for it to clear them as their + // corners close in on it (or with as little of them in the way as there + // is, where that's a little) + if ([a, b].every((box) => box.solid && box.shape !== "rectangle")) { + let [from, to] = [0, 1]; + if (turnBack(1).room >= -TOUCH) { + for (let i = 0; i < 24; i++) { + const shorten = mid(from, to); + [from, to] = + turnBack(shorten).room >= -TOUCH ? [from, shorten] : [shorten, to]; + } + } + const shortened = turnBack(to); + if (shortened.room >= -TOUCH) { + return { points: shortened.points, signature: "facing:between" }; + } } // around them, over the top or under the bottom const around = (top: boolean) => { // (the start's segment up or down from its line, and the end's down or - // up to its line) - const [s1, s2] = top ? [-Infinity, 0] : [0, Infinity]; - const [e1, e2] = top ? [-Infinity, ey] : [ey, Infinity]; - // (clear of the other element on the way there, where its outline - // reaches past the segment there, ramped in with how far the segment - // runs into its bounds, which moves no faster than the element does) - let ax1 = Math.max(a.solid ? Math.max(0, near(a, "r", s1, s2)) : 0, legS); - ax1 += Math.min( - Math.max(0, far(b, "r", s1, s2) - ax1), - DODGE_RATE * Math.max(0, Math.min(ax1 - b.l, top ? -b.t : b.b)), + // up to its line, as far as they go) + const out = top ? -Infinity : Infinity; + const [s1, s2] = top ? [out, 0] : [0, out]; + const [e1, e2] = top ? [out, ey] : [ey, out]; + // (with legs of their whole length, as it's not turning between them) + let ax1 = Math.max( + a.solid ? Math.max(0, near(a, "r", s1, s2)) : 0, + Math.max(ms, PADDING), ); + // (and around the far side of the other element, where the segment + // there would run through it: going over to there gradually would run + // through it all the way) + const pastEnd = b.solid && runsThrough(b.outline, [ax1, 0], [ax1, out]); + if (pastEnd) { + ax1 = Math.max(ax1, far(b, "r", s1, s2)); + } let ax2 = Math.min( b.solid ? Math.min(ex, near(b, "l", e1, e2)) : ex, - ex - legE, - ); - ax2 -= Math.min( - Math.max(0, ax2 - far(a, "l", e1, e2)), - DODGE_RATE * Math.max(0, Math.min(a.r - ax2, top ? ey - a.t : a.b - ey)), + ex - Math.max(me, PADDING), ); + const pastStart = a.solid && runsThrough(a.outline, [ax2, ey], [ax2, out]); + if (pastStart) { + ax2 = Math.min(ax2, far(a, "l", e1, e2)); + } // (and as far out as the elements reach over the span it runs past them) const solid = [a, b].filter((box) => box.solid); const lines = solid.map((box) => @@ -355,15 +435,18 @@ const routeFacing = (input: RouteInput): Route => { ax1, ax2, ay: top ? Math.min(...lines) : Math.max(...lines), + signature: `${top ? "over" : "under"}${pastEnd ? ":past-end" : ""}${ + pastStart ? ":past-start" : "" + }`, }; }; const over = around(true); const under = around(false); const top = overTheTop(0, ey, over.ay, under.ay); - const { ax1, ax2, ay } = top ? over : under; + const { ax1, ax2, ay, signature } = top ? over : under; return { points: [[0, 0], [ax1, 0], [ax1, ay], [ax2, ay], [ax2, ey], end], - signature: top ? "facing:over" : "facing:under", + signature: `facing:${signature}`, }; }; @@ -458,17 +541,23 @@ const routeAcross = (input: RouteInput): Route => { // not where the end is below the start's leg, which coming back down to // it would cross) const ahead = b.solid ? endLeft - upExit : Infinity; + const leg = Math.min( + Math.max(upExit, legS), + ex + 2 * Math.max(0, above - DODGE_RATE * Math.max(0, ey)), + ); const past = Math.max( x1, Math.min( - Math.max(upExit, legS), - ex + 2 * Math.max(0, above - DODGE_RATE * Math.max(0, ey)), + leg, upExit + Math.max(0, ahead) / 2 + DODGE_RATE * Math.max(0, -ahead), ), ); // (but a corner's width clear of the end's line, where it would turn - // back right by it, rather than squeezing a U turn in next to the end) - x1 = past > ex - input.radius ? Math.max(past, ex + input.radius) : past; + // back right by it, rather than squeezing a U turn in next to the end: + // and then on its whole leg, as the end's element is no longer ahead of + // it) + x1 = + past > ex - input.radius ? Math.max(past, leg, ex + input.radius) : past; // (above the end's element, where it reaches over the segment's span) const climb = Math.max(above, b.solid ? -near(b, "t", ex, x1) : -Infinity); let y = -climb - grow(climb, legE, me); diff --git a/packages/element/tests/elbowArrow.test.tsx b/packages/element/tests/elbowArrow.test.tsx index 6741505ceb..ceec571828 100644 --- a/packages/element/tests/elbowArrow.test.tsx +++ b/packages/element/tests/elbowArrow.test.tsx @@ -361,14 +361,14 @@ describe("elbow arrow ui", () => { ) as HTMLInputElement; UI.updateInput(inputAngle, String("40")); - // turning halfway between the elements' bounds (the rotated one's - // bounding box) + // turning halfway between the elements (the rotated one's outline, + // rather than its bounding box) expect(arrow.points).toCloselyEqualPoints( [ [0, 0], - [40, 0], - [40, 164], - [101, 164], + [50.5, 0], + [50.5, 164], + [101.1, 164], ], 0, ); @@ -595,7 +595,8 @@ describe("elbow arrow routes with one or two corners", () => { const connectEllipse = (strokeWidth: number) => { API.setAppState({ currentItemEndArrowhead: null }); // the ellipse's top is 12px below the rectangle's side's middle, and - // the arrow enters its upper left, below its top + // the arrow enters its upper right, below its top, the first leg + // running over its top on the way there UI.createElement("rectangle", { x: 0, y: 100, width: 120, height: 55 }); UI.createElement("ellipse", { x: 220, @@ -609,7 +610,7 @@ describe("elbow arrow routes with one or two corners", () => { mouse.reset(); mouse.moveTo(120, 127.5); mouse.click(); - mouse.moveTo(260, 151.9); + mouse.moveTo(380, 151.9); mouse.click(); return h.elements.find( @@ -1123,15 +1124,16 @@ describe("elbow arrow routing between overlapping elements", () => { arrayToMap([lower, upper]) as NonDeletedSceneElementsMap, ); - // up, turning back halfway between the endpoints, rather than a U turn - // squeezed in right next to the end + // up, turning back about halfway between the endpoints (halfway between + // the elements), rather than a U turn squeezed in right next to the end const start = points![0]; const end = points![points!.length - 1]; - const middle = (start[1] + end[1]) / 2; + const turn = points![1][1]; + expect(turn).toBeCloseTo((start[1] + end[1]) / 2, -1); expect(points).toCloselyEqualPoints([ start, - [start[0], middle], - [end[0] + 40, middle], + [start[0], turn], + [end[0] + 40, turn], [end[0] + 40, end[1]], end, ]); @@ -1354,21 +1356,25 @@ describe("elbow arrow routing between overlapping elements", () => { ); it("loops back a hairpin's width where the end is right by where the start's leg ends", () => { - // as above, but with the end's line about where the start's leg ends: - // turning back onto it would fold the route back onto itself - const upper = API.createElement({ - type: "diamond", - x: 2123.59, - y: -863.11, - width: 226.95, - height: 248.6, - }); + // the start on top of the lower one, just right of the upper one, which + // overlaps the lower one's top left, and the end on the right of the + // upper one, about where the start's leg ends: turning back onto the + // end's line would fold the route back onto itself (rectangles, as a + // diamond's corner by the end would reach across the start's line, and + // the route would turn back under it instead) const lower = API.createElement({ - type: "diamond", - x: 2226.3, - y: -698.96, - width: 226.95, - height: 248.6, + type: "rectangle", + x: 0, + y: 0, + width: 224.54, + height: 112, + }); + const upper = API.createElement({ + type: "rectangle", + x: -115.25, + y: -99, + width: 224.54, + height: 112, }); const arrow = API.createElement({ type: "arrow", @@ -1378,12 +1384,12 @@ describe("elbow arrow routing between overlapping elements", () => { points: [pointFrom(0, 0), pointFrom(1, 1)], startBinding: { elementId: lower.id, - fixedPoint: [0.5023, 0.0073], + fixedPoint: [0.5001, -0.0267], mode: "orbit", }, endBinding: { elementId: upper.id, - fixedPoint: [0.995, 0.5028], + fixedPoint: [1.0267, 0.5001], mode: "orbit", }, endArrowhead: "arrow", diff --git a/packages/excalidraw/actions/actionFlip.test.tsx b/packages/excalidraw/actions/actionFlip.test.tsx index 1ac91aea88..bfd8a2cb46 100644 --- a/packages/excalidraw/actions/actionFlip.test.tsx +++ b/packages/excalidraw/actions/actionFlip.test.tsx @@ -34,8 +34,8 @@ describe("flipping re-centers selection", () => { id: "arr", x: 149, y: 95, - width: 161, - height: 245, + width: 66, + height: 244, startBinding: { elementId: "rec1", fixedPoint: [0.49, -0.05], @@ -54,9 +54,9 @@ describe("flipping re-centers selection", () => { // the route does points: [ pointFrom(0, 0), - pointFrom(0, -41), - pointFrom(-95, -41), - pointFrom(-95, 204), + pointFrom(0, -40), + pointFrom(57, -40), + pointFrom(57, 204), pointFrom(66, 204), ], elbowed: true, diff --git a/packages/excalidraw/tests/scene/__snapshots__/export.test.ts.snap b/packages/excalidraw/tests/scene/__snapshots__/export.test.ts.snap index 118e186957..ce3cb68410 100644 --- a/packages/excalidraw/tests/scene/__snapshots__/export.test.ts.snap +++ b/packages/excalidraw/tests/scene/__snapshots__/export.test.ts.snap @@ -135,141 +135,6 @@ exports[`exportToSvg > with a CJK font 1`] = ` `; -exports[`exportToSvg > with background color 1`] = ` - - - - - - - - - - - - - - - - - original text - - - - - original text - - - - - 中国你好!这是一个测试。中国你好!日本こんにちは!これはテストです。한국 안녕하세요! 이것은 테스트입니다. - - - -`; - exports[`exportToSvg > with default arguments 1`] = `