mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-29 00:29:42 +03:00
fix: Refactor stride extraction for polygon features
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
3 files changed
+346
-55
No files matched your search
@@ -1,3 +1,6 @@
|
||||
import { pointsOnBezierCurves } from "points-on-curve";
|
||||
import { curveToBezier } from "points-on-curve/lib/curve-to-bezier.js";
|
||||
|
||||
import { isElbowArrow, isLinearElement } from "./typeChecks";
|
||||
|
||||
import type { Drawable, Op, OpSet, Options } from "roughjs/bin/core";
|
||||
@@ -208,10 +211,80 @@ const snapStrokeEndpoints = (
|
||||
return op;
|
||||
});
|
||||
|
||||
const FILL_SHAPE_ROUGHNESS_GAIN = 0.8;
|
||||
|
||||
/**
|
||||
* Generates one rough.js curve per split group and merges their stroke ops
|
||||
* into a single drawable, so a split element is treated as one continuous
|
||||
* shape for both rendering and bounds computation.
|
||||
* rough.js fills a `curve()` along the curve through all of its points, so a
|
||||
* split element would keep being filled along the smooth contour it no longer
|
||||
* draws.
|
||||
*/
|
||||
const generateSplitSolidFillOps = <P extends readonly [number, number]>(
|
||||
generator: RoughGenerator,
|
||||
groups: readonly (readonly P[])[],
|
||||
options: Options,
|
||||
): Op[] => {
|
||||
const fillOptions: Options = {
|
||||
...options,
|
||||
// only the fill shape's outline is needed, and rough.js roughens it a
|
||||
// little more than the stroke
|
||||
fill: undefined,
|
||||
disableMultiStroke: true,
|
||||
roughness: options.roughness
|
||||
? options.roughness +
|
||||
(options.fillShapeRoughnessGain ?? FILL_SHAPE_ROUGHNESS_GAIN)
|
||||
: 0,
|
||||
};
|
||||
|
||||
return (
|
||||
groups
|
||||
.flatMap((group, groupIdx) =>
|
||||
snapStrokeEndpoints(
|
||||
generator
|
||||
// SAFETY: point pairs are finite [x, y] numbers, exactly the shape
|
||||
// rough.js consumes; the cast only drops readonly
|
||||
.curve(group as unknown as RoughPoint[], fillOptions)
|
||||
.sets.filter((set) => set.type === "path")
|
||||
.flatMap((set) => set.ops),
|
||||
groupIdx > 0 ? group[0] : null,
|
||||
groupIdx < groups.length - 1 ? group[group.length - 1] : null,
|
||||
),
|
||||
)
|
||||
// consecutive groups touch, so they describe one continuous region —
|
||||
// dropping the `move` that starts each of them keeps it that way
|
||||
.filter((op, idx) => idx === 0 || op.op !== "move")
|
||||
);
|
||||
};
|
||||
|
||||
const generateSplitPatternFillSet = <P extends readonly [number, number]>(
|
||||
generator: RoughGenerator,
|
||||
groups: readonly (readonly P[])[],
|
||||
options: Options,
|
||||
): OpSet | undefined => {
|
||||
const roughness = options.roughness ?? 1;
|
||||
// rough.js hands its fillers a polygon approximation of the contour, sampled
|
||||
// exactly like this
|
||||
const contour = groups.flatMap((group) =>
|
||||
group.length < 3
|
||||
? (group as unknown as RoughPoint[])
|
||||
: (pointsOnBezierCurves(
|
||||
curveToBezier(group as unknown as RoughPoint[]),
|
||||
10,
|
||||
(1 + roughness) / 2,
|
||||
) as RoughPoint[]),
|
||||
);
|
||||
|
||||
return generator
|
||||
.polygon(contour, options)
|
||||
.sets.find((set) => set.type === "fillSketch");
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates one rough.js curve per split group and collects them into a single
|
||||
* drawable, so a split element is treated as one shape for both rendering and
|
||||
* bounds computation. Each curve keeps its own stroke `OpSet`, so consumers can
|
||||
* still tell the curves apart. The ops of one curve are a self contained chain
|
||||
* of control points, and concatenating several of them yields a chain that no
|
||||
* longer parses.
|
||||
*/
|
||||
export const generateSplitCurves = <P extends readonly [number, number]>(
|
||||
generator: RoughGenerator,
|
||||
@@ -234,22 +307,52 @@ export const generateSplitCurves = <P extends readonly [number, number]>(
|
||||
}
|
||||
|
||||
const isStroke = (set: OpSet) => set.type === "path";
|
||||
const strokeOps = groups.flatMap((group, groupIdx) => {
|
||||
const strokeSets: OpSet[] = groups.map((group, groupIdx) => {
|
||||
// pin curve boundaries that fall on a split vertex onto that exact
|
||||
// vertex; the element'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 curve(group)
|
||||
.sets.filter(isStroke)
|
||||
.flatMap((set) => snapStrokeEndpoints(set.ops, start, end));
|
||||
return {
|
||||
type: "path",
|
||||
ops: curve(group)
|
||||
.sets.filter(isStroke)
|
||||
.flatMap((set) => snapStrokeEndpoints(set.ops, start, end)),
|
||||
};
|
||||
});
|
||||
|
||||
// the per curve stroke sets take the place of the whole element's single
|
||||
// one, so they stay in the same position relative to the fill sets
|
||||
let replaced = false;
|
||||
|
||||
return {
|
||||
...whole,
|
||||
sets: whole.sets.map((set) =>
|
||||
isStroke(set) ? { ...set, ops: strokeOps } : set,
|
||||
),
|
||||
sets: whole.sets.flatMap((set) => {
|
||||
if (isStroke(set)) {
|
||||
if (replaced) {
|
||||
return [];
|
||||
}
|
||||
|
||||
replaced = true;
|
||||
|
||||
return strokeSets;
|
||||
}
|
||||
|
||||
if (set.type === "fillPath") {
|
||||
return [
|
||||
{
|
||||
type: "fillPath",
|
||||
ops: generateSplitSolidFillOps(generator, groups, options),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
if (set.type === "fillSketch") {
|
||||
return [generateSplitPatternFillSet(generator, groups, options) ?? set];
|
||||
}
|
||||
|
||||
return [set];
|
||||
}),
|
||||
};
|
||||
};
|
||||
@@ -1,6 +1,13 @@
|
||||
import { vi } from "vitest";
|
||||
|
||||
import { pointFrom } from "@excalidraw/math";
|
||||
import {
|
||||
distanceToLineSegment,
|
||||
lineSegment,
|
||||
pointFrom,
|
||||
polygonFromPoints,
|
||||
polygonIncludesPoint,
|
||||
} from "@excalidraw/math";
|
||||
import { pointsOnBezierCurves } from "points-on-curve";
|
||||
import { ROUNDNESS, arrayToMap, reseed } from "@excalidraw/common";
|
||||
import {
|
||||
Excalidraw,
|
||||
@@ -30,9 +37,11 @@ import {
|
||||
shiftSplitPointsOnInsert,
|
||||
toggleSplitPoint,
|
||||
} from "../src/splitPoints";
|
||||
import { generateLinearCollisionShape } from "../src/shape";
|
||||
import { generateLinearCollisionShape, getElementShape } from "../src/shape";
|
||||
import { LinearElementEditor } from "../src/linearElementEditor";
|
||||
|
||||
import type { Op } from "roughjs/bin/core";
|
||||
|
||||
import type {
|
||||
ExcalidrawArrowElement,
|
||||
ExcalidrawLineElement,
|
||||
@@ -43,6 +52,16 @@ import type {
|
||||
const { h } = window;
|
||||
const mouse = new Pointer("mouse");
|
||||
|
||||
const distanceToOutline = (point: LocalPoint, outline: readonly LocalPoint[]) =>
|
||||
Math.min(
|
||||
...outline.map((vertex, idx) =>
|
||||
distanceToLineSegment(
|
||||
point,
|
||||
lineSegment(vertex, outline[(idx + 1) % outline.length]),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const basePoints = () => [
|
||||
pointFrom<LocalPoint>(0, 0),
|
||||
pointFrom<LocalPoint>(100, 100),
|
||||
@@ -285,16 +304,107 @@ describe("arrow and line split points", () => {
|
||||
[1],
|
||||
options,
|
||||
);
|
||||
const countMoves = (type: string) =>
|
||||
drawable.sets
|
||||
.filter((set) => set.type === type)
|
||||
.flatMap((set) => set.ops)
|
||||
.filter((op) => op.op === "move").length;
|
||||
const strokeSets = drawable.sets.filter((set) => set.type === "path");
|
||||
const fillOps = drawable.sets
|
||||
.filter((set) => set.type === "fillPath")
|
||||
.flatMap((set) => set.ops);
|
||||
|
||||
// one filled region, but two separate stroked curves (rough.js draws
|
||||
// two separate stroked curves, each kept in its own set (rough.js draws
|
||||
// each of them twice)
|
||||
expect(countMoves("fillPath")).toBe(1);
|
||||
expect(countMoves("path")).toBe(4);
|
||||
expect(strokeSets).toHaveLength(2);
|
||||
expect(
|
||||
strokeSets.map(
|
||||
(set) => set.ops.filter((op) => op.op === "move").length,
|
||||
),
|
||||
).toEqual([2, 2]);
|
||||
|
||||
// the fill is a single region — filling each curve on its own would
|
||||
// seam them closed across the split
|
||||
expect(fillOps.filter((op) => op.op === "move")).toHaveLength(1);
|
||||
|
||||
// ...and it follows the split contour, not the smooth one the element
|
||||
// no longer draws. At roughness 0 the fill shape is the outline.
|
||||
const firstPass = (ops: readonly Op[]) => {
|
||||
const passEnd = ops.findIndex((op, idx) => idx > 0 && op.op === "move");
|
||||
|
||||
return passEnd === -1 ? ops : ops.slice(0, passEnd);
|
||||
};
|
||||
|
||||
expect(fillOps.map((op) => op.data)).toEqual(
|
||||
strokeSets.flatMap((set, idx) =>
|
||||
firstPass(set.ops)
|
||||
// only the first curve keeps its `move`; the rest continue the
|
||||
// region from where the previous one ended
|
||||
.filter((op, opIdx) => idx === 0 || opIdx > 0)
|
||||
.map((op) => op.data),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a patterned fill inside the split outline", () => {
|
||||
const points = [...basePoints(), pointFrom<LocalPoint>(0, 0)];
|
||||
const options = {
|
||||
seed: 1,
|
||||
roughness: 0,
|
||||
fill: "#000",
|
||||
fillStyle: "hachure",
|
||||
};
|
||||
const drawable = generateSplitCurves(
|
||||
new RoughGenerator(),
|
||||
points,
|
||||
[1],
|
||||
options,
|
||||
);
|
||||
|
||||
// the outline the element actually draws, as a polygon
|
||||
const outline = polygonFromPoints<LocalPoint>(
|
||||
drawable.sets
|
||||
.filter((set) => set.type === "path")
|
||||
.flatMap((set) => {
|
||||
const controlPoints: LocalPoint[] = [];
|
||||
|
||||
for (const op of set.ops) {
|
||||
if (op.op === "move") {
|
||||
if (controlPoints.length) {
|
||||
break;
|
||||
}
|
||||
controlPoints.push(pointFrom(op.data[0], op.data[1]));
|
||||
} else {
|
||||
controlPoints.push(
|
||||
pointFrom(op.data[0], op.data[1]),
|
||||
pointFrom(op.data[2], op.data[3]),
|
||||
pointFrom(op.data[4], op.data[5]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return pointsOnBezierCurves(controlPoints, 10, 5) as LocalPoint[];
|
||||
}),
|
||||
);
|
||||
|
||||
const sketch = drawable.sets.find((set) => set.type === "fillSketch");
|
||||
|
||||
expect(sketch).toBeDefined();
|
||||
|
||||
// every hachure line the filler produced is clipped to the contour it
|
||||
// was given, so none of it may fall outside the outline
|
||||
const strayPoints = sketch!.ops
|
||||
.flatMap((op) => [
|
||||
pointFrom<LocalPoint>(op.data[0], op.data[1]),
|
||||
pointFrom<LocalPoint>(
|
||||
op.data[op.data.length - 2],
|
||||
op.data[op.data.length - 1],
|
||||
),
|
||||
])
|
||||
.filter(
|
||||
(point) =>
|
||||
!polygonIncludesPoint(point, outline) &&
|
||||
// the clip lands the endpoints exactly on the outline, where an
|
||||
// even-odd test is a coin flip
|
||||
distanceToOutline(point, outline) > 1,
|
||||
);
|
||||
|
||||
expect(strayPoints).toEqual([]);
|
||||
});
|
||||
|
||||
it("breaks tangent continuity only at the split point", () => {
|
||||
@@ -357,7 +467,9 @@ describe("arrow and line split points", () => {
|
||||
getSplitPoints(arrow),
|
||||
{ seed: arrow.seed, roughness: arrow.roughness },
|
||||
);
|
||||
const ops = drawable.sets[0].ops;
|
||||
const ops = drawable.sets
|
||||
.filter((set) => set.type === "path")
|
||||
.flatMap((set) => set.ops);
|
||||
|
||||
// two curves × two multi-stroke passes
|
||||
const moveIndices = ops
|
||||
@@ -386,6 +498,72 @@ describe("arrow and line split points", () => {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// the merged drawable holds one stroke pass per curve (two when rough.js
|
||||
// double strokes them), and each pass is its own chain of cubic control
|
||||
// points; the closed shape has to convert them separately
|
||||
it.each([
|
||||
["solid", [1]],
|
||||
["solid", [1, 2]],
|
||||
["dashed", [1]],
|
||||
["dashed", [1, 2]],
|
||||
] as const)(
|
||||
"traces the whole outline of a %s filled split polygon (splits %j)",
|
||||
(strokeStyle, splitPoints) => {
|
||||
const closedPoints = [...basePoints(), pointFrom<LocalPoint>(0, 0)];
|
||||
const polygon = (splits?: readonly number[]) =>
|
||||
API.createElement({
|
||||
type: "line",
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 200,
|
||||
height: 100,
|
||||
roughness: 0,
|
||||
roundness: { type: ROUNDNESS.PROPORTIONAL_RADIUS },
|
||||
points: closedPoints,
|
||||
polygon: true,
|
||||
backgroundColor: "#000000",
|
||||
fillStyle: "solid",
|
||||
strokeStyle,
|
||||
splitPoints: splits,
|
||||
}) as NonDeleted<ExcalidrawLineElement>;
|
||||
|
||||
const outlineBounds = (element: NonDeleted<ExcalidrawLineElement>) => {
|
||||
const shape = getElementShape<LocalPoint>(
|
||||
element,
|
||||
arrayToMap([element]),
|
||||
);
|
||||
|
||||
expect(shape.type).toBe("polygon");
|
||||
|
||||
const vertices = shape.data as LocalPoint[];
|
||||
|
||||
return [
|
||||
Math.min(...vertices.map((p) => p[0])),
|
||||
Math.min(...vertices.map((p) => p[1])),
|
||||
Math.max(...vertices.map((p) => p[0])),
|
||||
Math.max(...vertices.map((p) => p[1])),
|
||||
];
|
||||
};
|
||||
|
||||
const [minX, minY, maxX, maxY] = outlineBounds(polygon(splitPoints));
|
||||
|
||||
// the outline goes around every vertex of the polygon — keeping only
|
||||
// some of the curves would cut it short well inside these bounds. It
|
||||
// may overshoot a little, but only as much as the unsplit one does.
|
||||
expect(minX).toBeLessThanOrEqual(0);
|
||||
expect(minY).toBeLessThanOrEqual(0);
|
||||
expect(maxX).toBeGreaterThanOrEqual(200);
|
||||
expect(maxY).toBeGreaterThanOrEqual(100);
|
||||
|
||||
// ...and it stays snug around them, rather than tracing any curve
|
||||
// more than once
|
||||
expect(minX).toBeGreaterThan(-10);
|
||||
expect(minY).toBeGreaterThan(-10);
|
||||
expect(maxX).toBeLessThan(210);
|
||||
expect(maxY).toBeLessThan(110);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("duplicating points", () => {
|
||||
@@ -492,10 +670,15 @@ describe("arrow and line split points", () => {
|
||||
expect(countSubpaths(unsplit)).toBe(2);
|
||||
expect(countSubpaths(split)).toBe(4);
|
||||
|
||||
// the second curve starts exactly at the split point, points[1]
|
||||
expect(split.querySelector("path")!.getAttribute("d")).toContain(
|
||||
"M100 100",
|
||||
// each curve is exported as its own path element, and the second one
|
||||
// starts exactly at the split point, points[1]
|
||||
const paths = Array.from(split.querySelectorAll("path")).map((path) =>
|
||||
path.getAttribute("d"),
|
||||
);
|
||||
|
||||
expect(unsplit.querySelectorAll("path")).toHaveLength(1);
|
||||
expect(paths).toHaveLength(2);
|
||||
expect(paths[1]).toContain("M100 100");
|
||||
});
|
||||
|
||||
it("strokes both curves in canvas (PNG) exports", async () => {
|
||||
|
||||
+36
-31
@@ -196,18 +196,21 @@ export const getEllipseShape = <Point extends GlobalPoint | LocalPoint>(
|
||||
};
|
||||
};
|
||||
|
||||
export const getCurvePathOps = (shape: Drawable): Op[] => {
|
||||
const getCurvePathOpSets = (shape: Drawable): Op[][] => {
|
||||
// NOTE (mtolmacs): Temporary fix for extremely large elements
|
||||
if (!shape) {
|
||||
return [];
|
||||
}
|
||||
|
||||
for (const set of shape.sets) {
|
||||
if (set.type === "path") {
|
||||
return set.ops;
|
||||
}
|
||||
}
|
||||
return shape.sets[0].ops;
|
||||
const paths = shape.sets.filter((set) => set.type === "path");
|
||||
|
||||
return paths.length ? paths.map((set) => set.ops) : [shape.sets[0].ops];
|
||||
};
|
||||
|
||||
export const getCurvePathOps = (shape: Drawable): Op[] => {
|
||||
const opSets = getCurvePathOpSets(shape);
|
||||
|
||||
return opSets.length === 1 ? opSets[0] : opSets.flat();
|
||||
};
|
||||
|
||||
// linear
|
||||
@@ -318,32 +321,34 @@ export const getClosedCurveShape = <Point extends GlobalPoint | LocalPoint>(
|
||||
};
|
||||
}
|
||||
|
||||
const ops = getCurvePathOps(roughShape);
|
||||
// Each op set is one rough.js curve
|
||||
const polygonPoints = getCurvePathOpSets(roughShape)
|
||||
.flatMap((ops) => {
|
||||
const points: Point[] = [];
|
||||
|
||||
const points: Point[] = [];
|
||||
let odd = false;
|
||||
for (const operation of ops) {
|
||||
if (operation.op === "move") {
|
||||
odd = !odd;
|
||||
if (odd) {
|
||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||
}
|
||||
} else if (operation.op === "bcurveTo") {
|
||||
if (odd) {
|
||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||
points.push(pointFrom(operation.data[2], operation.data[3]));
|
||||
points.push(pointFrom(operation.data[4], operation.data[5]));
|
||||
}
|
||||
} else if (operation.op === "lineTo") {
|
||||
if (odd) {
|
||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const operation of ops) {
|
||||
if (operation.op === "move") {
|
||||
if (points.length) {
|
||||
// the next stroke pass of this curve starts here
|
||||
break;
|
||||
}
|
||||
|
||||
const polygonPoints = pointsOnBezierCurves(points, 10, 5).map((p) =>
|
||||
transform(p as Point),
|
||||
) as Point[];
|
||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||
} else if (!points.length) {
|
||||
// ops before the curve's first `move` have no starting point
|
||||
continue;
|
||||
} else if (operation.op === "bcurveTo") {
|
||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||
points.push(pointFrom(operation.data[2], operation.data[3]));
|
||||
points.push(pointFrom(operation.data[4], operation.data[5]));
|
||||
} else if (operation.op === "lineTo") {
|
||||
points.push(pointFrom(operation.data[0], operation.data[1]));
|
||||
}
|
||||
}
|
||||
|
||||
return pointsOnBezierCurves(points, 10, 5);
|
||||
})
|
||||
.map((p) => transform(p as Point)) as Point[];
|
||||
|
||||
return {
|
||||
type: "polygon",
|
||||
|
||||
Reference in new issue
Block a user