From 916750617bb38933cd86fd81eeff043eda4ffd82 Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Fri, 28 Aug 2026 12:23:02 +0000 Subject: [PATCH] fix: make discontinuous segments touch at common point Signed-off-by: Mark Tolmacs --- packages/element/src/shape.ts | 14 ++--- packages/element/src/splitPoints.ts | 63 +++++++++++++++++++-- packages/element/tests/splitPoints.test.tsx | 45 +++++++++++++++ 3 files changed, 108 insertions(+), 14 deletions(-) diff --git a/packages/element/src/shape.ts b/packages/element/src/shape.ts index 4f7f20d143..f2634967a5 100644 --- a/packages/element/src/shape.ts +++ b/packages/element/src/shape.ts @@ -225,16 +225,12 @@ export const generateRoughOptions = ( hachureGap: element.strokeWidth * 4, roughness: adjustRoughness(element), stroke: applyDarkModeFilter(element.strokeColor, isDarkMode), - // force preserveVertices when the element has split points, because - // each split segment is an independent curve and, without preserved - // vertices, both curves would offset their shared boundary vertex - // independently, rendering a visible gap or overlap instead of a - // sharp joint (roughjs has no per-vertex option, so it is forced for - // the whole shape) + // note that rough.js only honors preserveVertices for lines/linear + // paths and svg paths. Curves always offset their vertices with + // roughness, so split arrows instead pin their shared boundary + // vertices in `generateSplitCurves()` preserveVertices: - continuousPath || - element.roughness < ROUGHNESS.cartoonist || - getSplitPoints(element).length > 0, + continuousPath || element.roughness < ROUGHNESS.cartoonist, }; switch (element.type) { diff --git a/packages/element/src/splitPoints.ts b/packages/element/src/splitPoints.ts index 05f7c58570..28c519a13b 100644 --- a/packages/element/src/splitPoints.ts +++ b/packages/element/src/splitPoints.ts @@ -1,6 +1,6 @@ import { isArrowElement, isElbowArrow } from "./typeChecks"; -import type { Drawable, Options } from "roughjs/bin/core"; +import type { Drawable, Op, Options } from "roughjs/bin/core"; import type { Point as RoughPoint } from "roughjs/bin/geometry"; import type { RoughGenerator } from "roughjs/bin/generator"; import type { ExcalidrawArrowElement, ExcalidrawElement } from "./types"; @@ -174,6 +174,40 @@ export const getSplitPointGroups =

( return groups; }; +/** + * rough.js' `curve()` ignores `preserveVertices` (only `line`/`linearPath` + * and `svgPath` honor it) and instead offsets every input point — endpoints + * included — by a random amount scaled with `roughness`. Two curves meeting + * at a split vertex therefore each wander off that vertex independently and + * visibly miss each other at any non-zero roughness. This snaps the start + * and end of every stroke (rough.js draws two per curve unless multi-stroke + * is disabled) back onto the exact boundary vertex so consecutive curves + * touch. + */ +const snapStrokeEndpoints = ( + ops: readonly Op[], + start: readonly [number, number] | null, + end: readonly [number, number] | null, +): Op[] => + ops.map((op, i) => { + if (op.op === "move" && start) { + return { ...op, data: [start[0], start[1]] }; + } + + // a stroke ends on the op right before the next `move` (or on the very + // last op); its final coordinate pair is the stroke's endpoint + if (end && (i === ops.length - 1 || ops[i + 1].op === "move")) { + const data = op.data.slice(); + + data[data.length - 2] = end[0]; + data[data.length - 1] = end[1]; + + return { ...op, data }; + } + + return op; + }); + /** * Generates one rough.js curve per split group and merges their ops into a * single drawable, so a split arrow is treated as one continuous shape for @@ -185,14 +219,33 @@ export const generateSplitCurves =

( splitPoints: readonly number[], options: Options, ): Drawable => { - const drawables = getSplitPointGroups(points, splitPoints).map((group) => - generator.curve( + const groups = getSplitPointGroups(points, splitPoints); + const drawables = groups.map((group, groupIdx) => { + const drawable = generator.curve( // SAFETY: point pairs are finite [x, y] numbers, exactly the shape // rough.js consumes; the cast only drops readonly group as unknown as RoughPoint[], options, - ), - ); + ); + + if (groups.length === 1) { + return drawable; + } + + // pin curve boundaries that fall on a split vertex onto that exact + // vertex; the arrow's own endpoints (first group start, last group end) + // keep their sketchy random offset + const start = groupIdx > 0 ? group[0] : null; + const end = groupIdx < groups.length - 1 ? group[group.length - 1] : null; + + return { + ...drawable, + sets: drawable.sets.map((set) => ({ + ...set, + ops: snapStrokeEndpoints(set.ops, start, end), + })), + }; + }); if (drawables.length === 1) { return drawables[0]; diff --git a/packages/element/tests/splitPoints.test.tsx b/packages/element/tests/splitPoints.test.tsx index 16be9b9863..c3810f95bd 100644 --- a/packages/element/tests/splitPoints.test.tsx +++ b/packages/element/tests/splitPoints.test.tsx @@ -18,8 +18,11 @@ import { import type { LocalPoint } from "@excalidraw/math"; +import { RoughGenerator } from "roughjs/bin/generator"; + import { canSplitPoints, + generateSplitCurves, getSplitPointGroups, getSplitPoints, isSplitPoint, @@ -259,6 +262,48 @@ describe("arrow split points", () => { expect(secondCurveStart[0]).toBeCloseTo(100, 5); expect(secondCurveStart[1]).toBeCloseTo(100, 5); }); + + it("makes consecutive curves touch at any roughness", () => { + // rough.js `curve()` randomly offsets endpoints proportionally to + // roughness (it ignores `preserveVertices`), so without pinning, each + // multi-stroke pass of both curves would miss the shared vertex + // independently + const arrow = createArrow({ splitPoints: [1], roughness: 1 }); + const drawable = generateSplitCurves( + new RoughGenerator(), + arrow.points, + getSplitPoints(arrow), + { seed: arrow.seed, roughness: arrow.roughness }, + ); + const ops = drawable.sets[0].ops; + + // two curves × two multi-stroke passes + const moveIndices = ops + .map((op, idx) => (op.op === "move" ? idx : -1)) + .filter((idx) => idx !== -1); + + expect(moveIndices).toHaveLength(4); + + const strokeEndsAt = (startIdx: number) => { + let idx = startIdx; + while (idx + 1 < ops.length && ops[idx + 1].op !== "move") { + idx++; + } + return ops[idx].data.slice(-2); + }; + + // both passes of the first curve end exactly on the split vertex, and + // both passes of the second curve start exactly there too + for (const [strokeIdx, moveIdx] of moveIndices.entries()) { + const firstCurve = strokeIdx < 2; + + if (firstCurve) { + expect(strokeEndsAt(moveIdx)).toEqual([100, 100]); + } else { + expect(ops[moveIdx].data).toEqual([100, 100]); + } + } + }); }); describe("duplicating points", () => {