Skip to content

Commit fe76255

Browse files
authored
add stroke width scaling (#47)
* add stroke width scaling * also add option to include text labels
1 parent 299bd2d commit fe76255

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

examples/interactive-graphics-canvas.fixture.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const steppedGraphics: GraphicsObject = {
1818
{ x: 50, y: 50 },
1919
],
2020
strokeColor: "gray",
21-
strokeWidth: 2,
21+
strokeWidth: 0.5,
2222
step: 1,
2323
},
2424
{
@@ -27,7 +27,7 @@ const steppedGraphics: GraphicsObject = {
2727
{ x: -50, y: 20 },
2828
],
2929
strokeColor: "green",
30-
strokeWidth: 2,
30+
strokeWidth: 0.5,
3131
step: 2,
3232
},
3333
{
@@ -36,7 +36,7 @@ const steppedGraphics: GraphicsObject = {
3636
{ x: -20, y: -30 },
3737
],
3838
strokeColor: "purple",
39-
strokeWidth: 2,
39+
strokeWidth: 0.5,
4040
step: 3,
4141
},
4242
],

lib/drawGraphicsToCanvas.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ export function drawGraphicsToCanvas(
229229
}
230230

231231
ctx.strokeStyle = line.strokeColor || "black"
232-
ctx.lineWidth = line.strokeWidth || 1
232+
if (line.strokeWidth) {
233+
ctx.lineWidth = line.strokeWidth * matrix.a
234+
} else {
235+
ctx.lineWidth = 2
236+
}
237+
ctx.lineCap = "round"
233238

234239
if (line.strokeDash) {
235240
if (typeof line.strokeDash === "string") {

lib/getSvgFromGraphicsObject.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ function projectPoint(point: Point, matrix: Matrix) {
8484
return { ...point, ...projected }
8585
}
8686

87-
export function getSvgFromGraphicsObject(graphics: GraphicsObject): string {
87+
export function getSvgFromGraphicsObject(
88+
graphics: GraphicsObject,
89+
{
90+
includeTextLabels = false,
91+
}: {
92+
includeTextLabels?: boolean
93+
} = {},
94+
): string {
8895
const bounds = getBounds(graphics)
8996
const matrix = getProjectionMatrix(bounds, graphics.coordinateSystem)
9097

@@ -120,7 +127,7 @@ export function getSvgFromGraphicsObject(graphics: GraphicsObject): string {
120127
fill: point.color || "black",
121128
},
122129
},
123-
...(point.label
130+
...(includeTextLabels && point.label
124131
? [
125132
{
126133
name: "text",
@@ -151,7 +158,10 @@ export function getSvgFromGraphicsObject(graphics: GraphicsObject): string {
151158
.join(" "),
152159
fill: "none",
153160
stroke: line.strokeColor || "black",
154-
"stroke-width": (line.strokeWidth || 1).toString(),
161+
"stroke-width": (line.strokeWidth
162+
? line.strokeWidth * matrix.a
163+
: 1
164+
).toString(),
155165
},
156166
})),
157167
// Rectangles

0 commit comments

Comments
 (0)