Skip to content

Commit 1efa8e5

Browse files
Fix: Improve measurement text rendering and use global EPSILON
This commit addresses your feedback on measurement text rendering: 1. Text Offset: - I modified `screenCanvas.drawController.ts` to set `textBaseline = 'middle'` when drawing text. This ensures text is vertically centered on its anchor point. - I adjusted `MeasurementEntity.ts` to calculate the text label's offset by adding half the text height (`MEASUREMENT_FONT_SIZE / 2`) to the `MEASUREMENT_LABEL_OFFSET`. This provides a consistent gap between the measurement line and the bounding box of the text, preventing overlap when text orientation is flipped. 2. Global EPSILON: - I updated `MeasurementEntity.ts` to use the global `EPSILON` constant from `src/App.consts.ts` for floating-point comparisons in the text orientation logic, replacing a locally defined epsilon. These changes ensure correct text appearance (no overlap) and maintainability (use of global constant). The original fix for text orientation (ensuring readability from bottom/right) remains in place. This addresses part of the feedback for issue #17.
1 parent 0987239 commit 1efa8e5

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/drawControllers/screenCanvas.drawController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ export class ScreenCanvasDrawController implements DrawController {
370370
this.context.font = `${opts.fontSize}px ${opts.fontFamily}`;
371371
this.context.textAlign = opts.textAlign;
372372
this.context.fillStyle = opts.textColor;
373+
this.context.textBaseline = 'middle';
373374
this.context.fillText(label, 0, 0);
374375
this.context.restore();
375376
}

src/entities/MeasurementEntity.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MEASUREMENT_LABEL_OFFSET,
1111
MEASUREMENT_ORIGIN_MARGIN,
1212
TO_RADIANS,
13+
EPSILON,
1314
} from '../App.consts';
1415
import type {Shape, SnapPoint} from '../App.types';
1516
import type {DrawController} from '../drawControllers/DrawController';
@@ -96,10 +97,12 @@ export class MeasurementEntity implements Entity {
9697
(offsetStartPoint.x + offsetEndPoint.x) / 2,
9798
(offsetStartPoint.y + offsetEndPoint.y) / 2
9899
);
100+
const textHeight = MEASUREMENT_FONT_SIZE;
101+
const totalOffset = MEASUREMENT_LABEL_OFFSET + textHeight / 2;
99102
const midpointMeasurementLineOffset = midpointMeasurementLine
100103
.clone()
101104
.translate(
102-
vectorPerpendicularFromLineTowardsOffsetPointUnit.multiply(MEASUREMENT_LABEL_OFFSET)
105+
vectorPerpendicularFromLineTowardsOffsetPointUnit.multiply(totalOffset)
103106
);
104107

105108
return {
@@ -201,11 +204,10 @@ export class MeasurementEntity implements Entity {
201204
round(pointDistance(this.startPoint, this.endPoint), MEASUREMENT_DECIMAL_PLACES)
202205
);
203206
const originalTextDirection = normalUnit.rotate90CW();
204-
const epsilon = 1e-6;
205207
let finalTextDirection = originalTextDirection;
206208
if (
207-
originalTextDirection.x < -epsilon ||
208-
(Math.abs(originalTextDirection.x) < epsilon && originalTextDirection.y > epsilon)
209+
originalTextDirection.x < -EPSILON ||
210+
(Math.abs(originalTextDirection.x) < EPSILON && originalTextDirection.y > EPSILON)
209211
) {
210212
finalTextDirection = new Vector(-originalTextDirection.x, -originalTextDirection.y);
211213
}

0 commit comments

Comments
 (0)