fix: make discontinuous segments touch at common point

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs committed 2026-09-16 11:08:57 +02:00
1 parent ed02ded750
commit 916750617b
3 files changed
+108 -14

No files matched your search

+5 -9
View File
@@ -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) {
+58 -5
View File
@@ -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 = <P>(
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 = <P extends readonly [number, number]>(
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];
@@ -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", () => {