Skip to content

Commit

Permalink
add line[stroke] support (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshgrover23 authored Jan 11, 2025
1 parent 65ac4b7 commit 6c6b60f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/getSvgFromGraphicsObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ export function getSvgFromGraphicsObject(graphics: GraphicsObject): string {
.map((p) => `${p.x},${p.y}`)
.join(" "),
fill: "none",
stroke: "black",
"stroke-width": (line.points[0].stroke || 1).toString(),
stroke: line.strokeColor || "black",
"stroke-width": (line.strokeWidth || 1).toString(),
},
})),
// Rectangles
Expand Down
4 changes: 3 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export interface Point {
}

export interface Line {
points: { x: number; y: number; stroke?: number }[]
points: { x: number; y: number }[]
strokeWidth?: number // new optional line thickness
strokeColor?: string // new optional line color
}

export interface Rect {
Expand Down
3 changes: 2 additions & 1 deletion site/components/GraphicsDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ export function GraphicsDisplay({ graphics }: GraphicsDisplayProps) {
id: `line-${idx}`,
properties: {
points: `[${line.points.map((p) => `(${p.x},${p.y})`).join(", ")}]`,
...(line.points[0].stroke ? { stroke: line.points[0].stroke } : {}),
strokeColor: line.strokeColor || "black",
strokeWidth: line.strokeWidth || 1,
},
})
})
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/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.
17 changes: 15 additions & 2 deletions tests/getSvgFromGraphicsObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,35 @@ describe("getSvgFromGraphicsObject", () => {
expect(svg).toMatchSvgSnapshot(import.meta.path, "points")
})

test("should generate SVG with lines", () => {
test("should generate SVG with lines and custom stroke properties", () => {
const input: GraphicsObject = {
lines: [
{
points: [
{ x: 0, y: 0, stroke: 2 },
{ x: 0, y: 0 },
{ x: 1, y: 1 },
],
strokeWidth: 2,
strokeColor: "blue",
},
{
points: [
{ x: 1, y: 0 },
{ x: 0, y: 1 },
],
// Test default values when properties are not specified
},
],
}

const svg = getSvgFromGraphicsObject(input)
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"')
expect(svg).toContain('stroke="black"')
expect(svg).toMatchSvgSnapshot(import.meta.path, "lines")
})
Expand Down

0 comments on commit 6c6b60f

Please sign in to comment.