Skip to content

Commit abe7bb7

Browse files
committed
fix(desktop): bound the path text a draw stroke re-parses per move
Rewriting one <path>'s full d still made the renderer re-parse the whole stroke on every pointermove. Strokes now render as chunked paths under one group: cuts land on committed midpoints, so the chunks stay tangent-continuous and paint the same pixels, while a move only rewrites the growing chunk.
1 parent a64af81 commit abe7bb7

3 files changed

Lines changed: 120 additions & 17 deletions

File tree

‎apps/desktop/src/preview/AnnotationStroke.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import type { PreviewAnnotationPoint, PreviewAnnotationRect } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

4-
import { beginStroke, extendStroke, strokeBounds, strokePath } from "./AnnotationStroke.ts";
4+
import {
5+
STROKE_CHUNK_SEGMENTS,
6+
beginStroke,
7+
extendStroke,
8+
strokeBounds,
9+
strokeChunkPath,
10+
strokePath,
11+
takeClosedChunk,
12+
} from "./AnnotationStroke.ts";
513

614
/**
715
* The rescan-everything implementations these helpers replaced. Kept here as
@@ -75,6 +83,40 @@ describe("stroke geometry", () => {
7583
]);
7684
});
7785

86+
it("splits rendering into chunks that reassemble to the uncut path", () => {
87+
const points = wobble(505);
88+
const geometry = beginStroke(points[0]!);
89+
const closed: Array<string> = [];
90+
let longestRewrite = 0;
91+
for (let index = 1; index < points.length; index += 1) {
92+
extendStroke(geometry, points[index]!);
93+
const cut = takeClosedChunk(geometry);
94+
if (cut !== null) closed.push(cut);
95+
longestRewrite = Math.max(longestRewrite, strokeChunkPath(geometry).length);
96+
}
97+
expect(takeClosedChunk(geometry)).toBeNull();
98+
expect(closed).toHaveLength(Math.floor((points.length - 2) / STROKE_CHUNK_SEGMENTS));
99+
100+
// Every cut hands over at the exact coordinates the next chunk resumes from.
101+
const chunks = [...closed, geometry.chunk];
102+
for (let index = 1; index < chunks.length; index += 1) {
103+
const previous = chunks[index - 1]!;
104+
const handover = previous.slice(previous.lastIndexOf(" Q"));
105+
const [, , , midX, midY] = handover.trim().split(" ");
106+
expect(chunks[index]!.startsWith(`M ${midX} ${midY}`)).toBe(true);
107+
}
108+
109+
// Stripping each resume `M` and concatenating restores the uncut prefix.
110+
const reassembled = chunks
111+
.map((chunk, index) => (index === 0 ? chunk : chunk.replace(/^M [^ ]+ [^ ]+/, "")))
112+
.join("");
113+
expect(reassembled).toBe(geometry.prefix);
114+
115+
// The renderer only ever re-parses one chunk of path text per move.
116+
expect(longestRewrite).toBeLessThan(5_000);
117+
expect(strokePath(geometry).length).toBeGreaterThan(longestRewrite * 2);
118+
});
119+
78120
it("survives a stroke longer than the engine's argument limit", () => {
79121
// `Math.min(...xs)` throws RangeError somewhere past ~100k arguments, and a
80122
// per-point rescan would not finish this inside the test timeout.

‎apps/desktop/src/preview/AnnotationStroke.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515

1616
import type { PreviewAnnotationPoint, PreviewAnnotationRect } from "@t3tools/contracts";
1717

18+
/**
19+
* Curve segments per rendered `<path>`. Rewriting a `d` attribute makes the
20+
* renderer re-parse the whole string, so the stroke is split across chunk
21+
* paths and only the growing chunk is ever rewritten: per-move parse cost is
22+
* bounded by this constant instead of growing with the stroke.
23+
*/
24+
export const STROKE_CHUNK_SEGMENTS = 100;
25+
1826
export interface StrokeGeometry {
1927
/** Shared with the stroke target: appended in place, never reallocated. */
2028
readonly points: PreviewAnnotationPoint[];
@@ -25,6 +33,12 @@ export interface StrokeGeometry {
2533
maxY: number;
2634
/** The `d` attribute minus its trailing segment, which the last point owns. */
2735
prefix: string;
36+
/** Same, for the chunk currently being rendered. */
37+
chunk: string;
38+
/** Curve segments committed to the current chunk so far. */
39+
chunkSegments: number;
40+
/** Final `d` of the chunk the last append closed, until the renderer takes it. */
41+
closedChunk: string | null;
2842
}
2943

3044
export function beginStroke(point: PreviewAnnotationPoint): StrokeGeometry {
@@ -35,6 +49,9 @@ export function beginStroke(point: PreviewAnnotationPoint): StrokeGeometry {
3549
maxX: point.x,
3650
maxY: point.y,
3751
prefix: `M ${point.x} ${point.y}`,
52+
chunk: `M ${point.x} ${point.y}`,
53+
chunkSegments: 0,
54+
closedChunk: null,
3855
};
3956
}
4057

@@ -44,7 +61,19 @@ export function extendStroke(geometry: StrokeGeometry, point: PreviewAnnotationP
4461
// The quadratic through `previous` needs the point after it, so it can only
4562
// join the prefix now that `point` has arrived.
4663
if (geometry.points.length > 1) {
47-
geometry.prefix += ` Q ${previous.x} ${previous.y} ${(previous.x + point.x) / 2} ${(previous.y + point.y) / 2}`;
64+
const midX = (previous.x + point.x) / 2;
65+
const midY = (previous.y + point.y) / 2;
66+
const segment = ` Q ${previous.x} ${previous.y} ${midX} ${midY}`;
67+
geometry.prefix += segment;
68+
geometry.chunk += segment;
69+
geometry.chunkSegments += 1;
70+
// Cutting at a committed midpoint keeps the chunks tangent-continuous, so
71+
// adjacent chunk paths paint the same pixels as one uncut path.
72+
if (geometry.chunkSegments >= STROKE_CHUNK_SEGMENTS) {
73+
geometry.closedChunk = geometry.chunk;
74+
geometry.chunk = `M ${midX} ${midY}`;
75+
geometry.chunkSegments = 0;
76+
}
4877
}
4978
geometry.points.push(point);
5079
geometry.minX = Math.min(geometry.minX, point.x);
@@ -53,13 +82,27 @@ export function extendStroke(geometry: StrokeGeometry, point: PreviewAnnotationP
5382
geometry.maxY = Math.max(geometry.maxY, point.y);
5483
}
5584

56-
/** The `d` attribute for the stroke as it stands. */
85+
/** The `d` attribute for the stroke as it stands, as one uncut path. */
5786
export function strokePath(geometry: StrokeGeometry): string {
5887
if (geometry.points.length === 1) return `${geometry.prefix} l 0.01 0.01`;
5988
const last = geometry.points[geometry.points.length - 1]!;
6089
return `${geometry.prefix} L ${last.x} ${last.y}`;
6190
}
6291

92+
/** The `d` attribute for the chunk currently being rendered. */
93+
export function strokeChunkPath(geometry: StrokeGeometry): string {
94+
if (geometry.points.length === 1) return `${geometry.chunk} l 0.01 0.01`;
95+
const last = geometry.points[geometry.points.length - 1]!;
96+
return `${geometry.chunk} L ${last.x} ${last.y}`;
97+
}
98+
99+
/** The finalized `d` of a chunk the last append closed, once per cut. */
100+
export function takeClosedChunk(geometry: StrokeGeometry): string | null {
101+
const closed = geometry.closedChunk;
102+
geometry.closedChunk = null;
103+
return closed;
104+
}
105+
63106
/** The stroke's painted extent, padded for the round cap of `width`. */
64107
export function strokeBounds(geometry: StrokeGeometry, width: number): PreviewAnnotationRect {
65108
const padding = width + 3;

‎apps/desktop/src/preview/PickPreload.ts‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import type {
1616

1717
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
1818
import type { StrokeGeometry } from "./AnnotationStroke.ts";
19-
import { beginStroke, extendStroke, strokeBounds, strokePath } from "./AnnotationStroke.ts";
19+
import {
20+
beginStroke,
21+
extendStroke,
22+
strokeBounds,
23+
strokeChunkPath,
24+
takeClosedChunk,
25+
} from "./AnnotationStroke.ts";
2026
import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts";
2127
import {
2228
ANNOTATION_CAPTURED_CHANNEL,
@@ -457,7 +463,8 @@ function startAnnotation(): void {
457463
let dragStart: PreviewAnnotationPoint | null = null;
458464
let activeStroke: {
459465
target: PreviewAnnotationStrokeTarget;
460-
path: SVGPathElement;
466+
group: SVGGElement;
467+
chunkPath: SVGPathElement;
461468
geometry: StrokeGeometry;
462469
} | null = null;
463470
let pendingCapture = false;
@@ -1052,7 +1059,16 @@ function startAnnotation(): void {
10521059
if (tool === "draw" && activeStroke) {
10531060
extendStroke(activeStroke.geometry, { x: event.clientX, y: event.clientY });
10541061
activeStroke.target.bounds = strokeBounds(activeStroke.geometry, activeStroke.target.width);
1055-
activeStroke.path.setAttribute("d", strokePath(activeStroke.geometry));
1062+
// Only the growing chunk's `d` is ever rewritten, so the renderer never
1063+
// re-parses more than one chunk of path text per move.
1064+
const closedChunk = takeClosedChunk(activeStroke.geometry);
1065+
if (closedChunk !== null) {
1066+
activeStroke.chunkPath.setAttribute("d", closedChunk);
1067+
const nextChunk = document.createElementNS("http://www.w3.org/2000/svg", "path");
1068+
activeStroke.group.appendChild(nextChunk);
1069+
activeStroke.chunkPath = nextChunk;
1070+
}
1071+
activeStroke.chunkPath.setAttribute("d", strokeChunkPath(activeStroke.geometry));
10561072
}
10571073
};
10581074

@@ -1079,16 +1095,18 @@ function startAnnotation(): void {
10791095
points: geometry.points,
10801096
bounds: { x: dragStart.x, y: dragStart.y, width: 1, height: 1 },
10811097
};
1082-
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
1083-
path.setAttribute(OVERLAY_ATTRIBUTE, "");
1084-
path.setAttribute("data-stroke-id", stroke.id);
1085-
path.setAttribute("fill", "none");
1086-
path.setAttribute("stroke", stroke.color);
1087-
path.setAttribute("stroke-width", String(stroke.width));
1088-
path.setAttribute("stroke-linecap", "round");
1089-
path.setAttribute("stroke-linejoin", "round");
1090-
svg.appendChild(path);
1091-
activeStroke = { target: stroke, path, geometry };
1098+
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
1099+
group.setAttribute(OVERLAY_ATTRIBUTE, "");
1100+
group.setAttribute("data-stroke-id", stroke.id);
1101+
group.setAttribute("fill", "none");
1102+
group.setAttribute("stroke", stroke.color);
1103+
group.setAttribute("stroke-width", String(stroke.width));
1104+
group.setAttribute("stroke-linecap", "round");
1105+
group.setAttribute("stroke-linejoin", "round");
1106+
const chunkPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
1107+
group.appendChild(chunkPath);
1108+
svg.appendChild(group);
1109+
activeStroke = { target: stroke, group, chunkPath, geometry };
10921110
}
10931111
};
10941112

@@ -1115,7 +1133,7 @@ function startAnnotation(): void {
11151133
}
11161134
} else if (tool === "draw" && activeStroke) {
11171135
if (activeStroke.target.points.length > 1) strokes.push(activeStroke.target);
1118-
else activeStroke.path.remove();
1136+
else activeStroke.group.remove();
11191137
activeStroke = null;
11201138
}
11211139
dragStart = null;

0 commit comments

Comments
 (0)