Skip to content

Commit 79dc73a

Browse files
Fix: Rotate measurement text in SVG export
Previously, measurement text in SVG exports was always horizontal, regardless of the measurement line's orientation. This change modifies the SvgDrawController.drawText method to: - Utilize the `textDirection` vector (already calculated by MeasurementEntity) to determine the correct rotation angle for the text. - Apply an SVG `transform="rotate(...)"` attribute to the <text> element. - Ensure `text-anchor="middle"` is set when `textAlign` is `center`, to maintain proper centering after rotation. This addresses issue #37 on GitHub, ensuring that measurement text is correctly aligned and centered with its corresponding line in SVG outputs.
1 parent b18719f commit 79dc73a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/drawControllers/svg.drawController.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,21 @@ export class SvgDrawController implements DrawController {
219219
// No need to handle black to white, as SVG background is white.
220220
// Other colors will remain as they are.
221221

222+
let transformAttribute = '';
223+
if (textOptions.textDirection) {
224+
const angleRadians = Math.atan2(textOptions.textDirection.y, textOptions.textDirection.x);
225+
const angleDegrees = angleRadians * 180 / Math.PI;
226+
transformAttribute = `transform="rotate(${angleDegrees}, ${canvasBasePoint.x}, ${canvasBasePoint.y})"`;
227+
}
228+
229+
let textAnchorAttribute = '';
230+
if (textOptions.textAlign === 'center') {
231+
textAnchorAttribute = 'text-anchor="middle"';
232+
}
233+
222234
this.svgStrings.push(
223235
// Use finalTextColor here
224-
`<text x="${canvasBasePoint.x}" y="${canvasBasePoint.y}" fill="${finalTextColor}" font-size="${textOptions.fontSize}" font-family="${textOptions.fontFamily}">${label}</text>`
236+
`<text x="${canvasBasePoint.x}" y="${canvasBasePoint.y}" fill="${finalTextColor}" font-size="${textOptions.fontSize}" font-family="${textOptions.fontFamily}" ${transformAttribute} ${textAnchorAttribute}>${label}</text>`
225237
);
226238
}
227239

0 commit comments

Comments
 (0)