Skip to content

Commit 704c69d

Browse files
committed
Add Per-Polygon Stroke Width Support
1 parent cea5385 commit 704c69d

File tree

6 files changed

+15
-3
lines changed

6 files changed

+15
-3
lines changed

lib/drawGraphicsToCanvas.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ export function drawGraphicsToCanvas(
271271

272272
if (polygon.stroke) {
273273
ctx.strokeStyle = polygon.stroke
274+
if (polygon.strokeWidth !== undefined) {
275+
ctx.lineWidth = polygon.strokeWidth * Math.abs(matrix.a)
276+
}
274277
ctx.stroke()
275278
}
276279
})

lib/getSvgFromGraphicsObject.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ export function getSvgFromGraphicsObject(
344344
const ys = projectedPoints.map((p) => p.y)
345345
const minX = xs.length > 0 ? Math.min(...xs) : 0
346346
const minY = ys.length > 0 ? Math.min(...ys) : 0
347+
const polygonStrokeWidth =
348+
polygon.strokeWidth === undefined
349+
? Math.abs(1 / matrix.a)
350+
: strokeScale * polygon.strokeWidth
347351

348352
return {
349353
name: "g",
@@ -362,7 +366,7 @@ export function getSvgFromGraphicsObject(
362366
points: projectedPoints.map((p) => `${p.x},${p.y}`).join(" "),
363367
fill: polygon.fill || "none",
364368
stroke: polygon.stroke || "black",
365-
"stroke-width": Math.abs(1 / matrix.a).toString(),
369+
"stroke-width": polygonStrokeWidth.toString(),
366370
},
367371
},
368372
...(shouldRenderLabel("polygons") && polygon.label

lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface Polygon {
4343
points: { x: number; y: number }[]
4444
fill?: string
4545
stroke?: string
46+
strokeWidth?: number
4647
color?: string
4748
layer?: string
4849
step?: number

site/components/InteractiveGraphics/Polygon.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const Polygon = ({
1515
interactiveState: InteractiveState
1616
index: number
1717
}) => {
18-
const { points, fill, stroke } = polygon
18+
const { points, fill, stroke, strokeWidth } = polygon
1919
const { realToScreen, onObjectClicked } = interactiveState
2020
const [isHovered, setIsHovered] = useState(false)
2121

@@ -51,6 +51,8 @@ export const Polygon = ({
5151
? safeLighten(0.2, baseStroke)
5252
: baseStroke
5353
: undefined
54+
const screenStrokeWidth =
55+
strokeWidth === undefined ? undefined : strokeWidth * realToScreen.a
5456

5557
return (
5658
<div
@@ -68,6 +70,7 @@ export const Polygon = ({
6870
points={localPoints.map((p) => `${p.x},${p.y}`).join(" ")}
6971
fill={displayFill}
7072
stroke={displayStroke ?? "none"}
73+
strokeWidth={screenStrokeWidth}
7174
pointerEvents="all"
7275
onMouseEnter={() => setIsHovered(true)}
7376
onMouseLeave={() => setIsHovered(false)}

tests/__snapshots__/getSvgFromGraphicsObject-polygons.snap.svg

Lines changed: 1 addition & 1 deletion
Loading

tests/getSvgFromGraphicsObject.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ describe("getSvgFromGraphicsObject", () => {
114114
],
115115
fill: "gold",
116116
stroke: "black",
117+
strokeWidth: 0.2,
117118
label: "Tri",
118119
},
119120
],

0 commit comments

Comments
 (0)