Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/getSvgFromGraphicsObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ export function getSvgFromGraphicsObject(
svgWidth,
svgHeight,
)
const strokeScale = Math.hypot(matrix.a, matrix.b)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const strokeScale = Math.hypot(matrix.a, matrix.b)
const strokeScale = Math.abs(matrix.a)


const shouldRenderLabel = (
type: "points" | "lines" | "rects",
): boolean => {
const shouldRenderLabel = (type: "points" | "lines" | "rects"): boolean => {
if (typeof includeTextLabels === "boolean") {
return includeTextLabels
}
Expand Down Expand Up @@ -236,7 +235,9 @@ export function getSvgFromGraphicsObject(
points: projectedPoints.map((p) => `${p.x},${p.y}`).join(" "),
fill: "none",
stroke: line.strokeColor || "black",
"stroke-width": (line.strokeWidth ?? 1).toString(),
"stroke-width": (
strokeScale * (line.strokeWidth ?? 1)
).toString(),
...(line.strokeDash && {
"stroke-dasharray": Array.isArray(line.strokeDash)
? line.strokeDash.join(" ")
Expand Down
44 changes: 44 additions & 0 deletions tests/__snapshots__/getSvgFromGraphicsObject-lines.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 15 additions & 7 deletions tests/getSvgFromGraphicsObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,37 @@ describe("getSvgFromGraphicsObject", () => {
{
points: [
{ x: 0, y: 0 },
{ x: 1, y: 1 },
{ x: 10, y: 10 },
],
strokeWidth: 2,
strokeColor: "blue",
},
{
points: [
{ x: 1, y: 0 },
{ x: 0, y: 1 },
{ x: 10, y: 0 },
{ x: 0, y: 10 },
],
// Test default values when properties are not specified
},
],
}

const svg = getSvgFromGraphicsObject(input)
const svg = getSvgFromGraphicsObject(input, {
svgWidth: 200,
svgHeight: 200,
})
expect(svg).toBeString()
expect(svg).toContain("<polyline")
// Test custom stroke properties
expect(svg).toContain('stroke-width="2"')
expect(svg).toContain('stroke="blue"')
// Test default values
expect(svg).toContain('stroke-width="1"')
// Test stroke width scaling
const strokeWidths = Array.from(
svg.matchAll(/<polyline[^>]*stroke-width="([0-9.]+)"/g),
).map(([, value]) => parseFloat(value))
expect(strokeWidths).toHaveLength(2)
expect(strokeWidths[0]).toBeCloseTo(24)
expect(strokeWidths[1]).toBeCloseTo(12)
// Test default color
expect(svg).toContain('stroke="black"')
expect(svg).toMatchSvgSnapshot(import.meta.path, "lines")
})
Expand Down