Skip to content

Visualize Your Ideas With Code

fix(2d): fix cache bbox for lines (#467)

Jacob9fd144417bb0
diff --git a/packages/2d/src/components/Line.ts b/packages/2d/src/components/Line.ts
index c2291a62..1f5cc4cd 100644
--- a/packages/2d/src/components/Line.ts
+++ b/packages/2d/src/components/Line.ts
@@ -238,7 +238,22 @@ export class Line extends Shape {
const box = this.childrenBBox();
const arrowSize = this.arrowSize();
const lineWidth = this.lineWidth();
- return box.expand(Math.max(0, arrowSize, lineWidth / 2));
+ const radius = this.radius();
+ const join = this.lineJoin();
+ const cap = this.lineCap();
+
+ let coefficient = 0.5;
+ if (radius === 0 && join === 'miter') {
+ const {minSin} = this.profile();
+ if (minSin > 0) {
+ coefficient = Math.max(coefficient, 0.5 / minSin);
+ }
+ }
+ if (cap === 'square') {
+ coefficient = Math.max(coefficient, 0.5 * 1.4143);
+ }
+
+ return box.expand(Math.max(0, arrowSize, lineWidth * coefficient));
}
protected override drawShape(context: CanvasRenderingContext2D) {
diff --git a/packages/2d/src/curves/CurveProfile.ts b/packages/2d/src/curves/CurveProfile.ts
index 21ef8118..d01bf1a7 100644
--- a/packages/2d/src/curves/CurveProfile.ts
+++ b/packages/2d/src/curves/CurveProfile.ts
@@ -3,4 +3,5 @@ import {Segment} from './Segment';
export interface CurveProfile {
arcLength: number;
segments: Segment[];
+ minSin: number;
}
diff --git a/packages/2d/src/curves/getPolylineProfile.ts b/packages/2d/src/curves/getPolylineProfile.ts
index 3d18db7e..ca601b74 100644
--- a/packages/2d/src/curves/getPolylineProfile.ts
+++ b/packages/2d/src/curves/getPolylineProfile.ts
@@ -12,6 +12,7 @@ export function getPolylineProfile(
const profile: CurveProfile = {
arcLength: 0,
segments: [],
+ minSin: 1,
};
if (points.length === 0) {
@@ -71,6 +72,8 @@ export function getPolylineProfile(
profile.arcLength += line.arcLength;
profile.arcLength += circle.arcLength;
+ profile.minSin = Math.min(profile.minSin, Math.abs(angleSin));
+
last = center.add(endVector.scale(pointOffsetDistance));
}