Skip to content

Commit 4ba48ee

Browse files
committed
Scale line stroke width in SVG snapshots
1 parent 1c6e376 commit 4ba48ee

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

lib/getSvgFromGraphicsObject.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ export function getSvgFromGraphicsObject(
139139
svgWidth,
140140
svgHeight,
141141
)
142+
const strokeScale = Math.hypot(matrix.a, matrix.b)
142143

143-
const shouldRenderLabel = (
144-
type: "points" | "lines" | "rects",
145-
): boolean => {
144+
const shouldRenderLabel = (type: "points" | "lines" | "rects"): boolean => {
146145
if (typeof includeTextLabels === "boolean") {
147146
return includeTextLabels
148147
}
@@ -236,7 +235,9 @@ export function getSvgFromGraphicsObject(
236235
points: projectedPoints.map((p) => `${p.x},${p.y}`).join(" "),
237236
fill: "none",
238237
stroke: line.strokeColor || "black",
239-
"stroke-width": (line.strokeWidth ?? 1).toString(),
238+
"stroke-width": (
239+
strokeScale * (line.strokeWidth ?? 1)
240+
).toString(),
240241
...(line.strokeDash && {
241242
"stroke-dasharray": Array.isArray(line.strokeDash)
242243
? line.strokeDash.join(" ")
Lines changed: 44 additions & 0 deletions
Loading

tests/getSvgFromGraphicsObject.test.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,37 @@ describe("getSvgFromGraphicsObject", () => {
4242
{
4343
points: [
4444
{ x: 0, y: 0 },
45-
{ x: 1, y: 1 },
45+
{ x: 10, y: 10 },
4646
],
4747
strokeWidth: 2,
4848
strokeColor: "blue",
4949
},
5050
{
5151
points: [
52-
{ x: 1, y: 0 },
53-
{ x: 0, y: 1 },
52+
{ x: 10, y: 0 },
53+
{ x: 0, y: 10 },
5454
],
5555
// Test default values when properties are not specified
5656
},
5757
],
5858
}
5959

60-
const svg = getSvgFromGraphicsObject(input)
60+
const svg = getSvgFromGraphicsObject(input, {
61+
svgWidth: 200,
62+
svgHeight: 200,
63+
})
6164
expect(svg).toBeString()
6265
expect(svg).toContain("<polyline")
6366
// Test custom stroke properties
64-
expect(svg).toContain('stroke-width="2"')
6567
expect(svg).toContain('stroke="blue"')
66-
// Test default values
67-
expect(svg).toContain('stroke-width="1"')
68+
// Test stroke width scaling
69+
const strokeWidths = Array.from(
70+
svg.matchAll(/<polyline[^>]*stroke-width="([0-9.]+)"/g),
71+
).map(([, value]) => parseFloat(value))
72+
expect(strokeWidths).toHaveLength(2)
73+
expect(strokeWidths[0]).toBeCloseTo(24)
74+
expect(strokeWidths[1]).toBeCloseTo(12)
75+
// Test default color
6876
expect(svg).toContain('stroke="black"')
6977
expect(svg).toMatchSvgSnapshot(import.meta.path, "lines")
7078
})

0 commit comments

Comments
 (0)