Skip to content

Commit 255d917

Browse files
authored
Merge pull request #588 from Ayushjhawar8/fixGridColor
Update grid and section colors in render3dPng
2 parents 81a3403 + cb9a08b commit 255d917

11 files changed

+55
-8
lines changed

bun.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"circuit-json-to-simple-3d": "^0.0.9",
1313
"circuit-to-svg": "^0.0.258",
1414
"jscad-fiber": "^0.0.85",
15-
"poppygl": "^0.0.16",
15+
"poppygl": "^0.0.17",
1616
},
1717
"devDependencies": {
1818
"@biomejs/biome": "^1.9.4",
@@ -811,7 +811,7 @@
811811

812812
"polished": ["[email protected]", "", { "dependencies": { "@babel/runtime": "^7.17.8" } }, "sha512-OBatVyC/N7SCW/FaDHrSd+vn0o5cS855TOmYi4OkdWUMSJCET/xip//ch8xGUvtr3i44X9LVyWwQlRMTN3pwSA=="],
813813

814-
"poppygl": ["[email protected].16", "", { "dependencies": { "gl-matrix": "^3.4.4", "pureimage": "^0.4.18", "readable-stream": "^4.7.0" }, "peerDependencies": { "typescript": "^5" } }, "sha512-A29z8dQRyupmLpBU8AurAeAdIYe0nIVuk+o/7PZlhEd4R+SZjt6eY98nnP7g85zcY8FinXtSPysKnMWoo7cz0g=="],
814+
"poppygl": ["[email protected].17", "", { "dependencies": { "gl-matrix": "^3.4.4", "pureimage": "^0.4.18", "readable-stream": "^4.7.0" }, "peerDependencies": { "typescript": "^5" } }, "sha512-nEaOgH9UsR1uCETy2ybb2tCZxsXrmtfYn98MGSYxY6huMrKwYhX1WDiXaBa4RwdOaLIyIvzSKQ0LyMDfzIF0yw=="],
815815

816816
"postcss": ["[email protected]", "", { "dependencies": { "nanoid": "^3.3.6", "picocolors": "^1.0.0", "source-map-js": "^1.0.2" } }, "sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ=="],
817817

@@ -1081,6 +1081,8 @@
10811081

10821082
"tscircuit/circuit-to-svg": ["[email protected]", "", { "dependencies": { "@types/node": "^22.5.5", "bun-types": "^1.1.40", "calculate-elbow": "0.0.12", "svgson": "^5.3.1", "transformation-matrix": "^2.16.1" } }, "sha512-wEvGWOb6H+4BudVjfA/lapWea9KjOFFxCWSpVZQ0xr8cO4N4kCMW1hwBwUW0EovpILi9jtilda651G15N+O/Yw=="],
10831083

1084+
"tscircuit/poppygl": ["[email protected]", "", { "dependencies": { "gl-matrix": "^3.4.4", "pureimage": "^0.4.18", "readable-stream": "^4.7.0" }, "peerDependencies": { "typescript": "^5" } }, "sha512-A29z8dQRyupmLpBU8AurAeAdIYe0nIVuk+o/7PZlhEd4R+SZjt6eY98nnP7g85zcY8FinXtSPysKnMWoo7cz0g=="],
1085+
10841086
"use-mouse-matrix-transform/transformation-matrix": ["[email protected]", "", {}, "sha512-C6NlNnD6T8JqDeY4BpGznuve4d8/JlLDZLcJbnnx7gQKoyk01+uk2XYVFjBGqvNsVxJUpUwb3WZpjpdPr+05FQ=="],
10851087

10861088
"winterspec/zod": ["[email protected]", "", {}, "sha512-hbP9FpSZf7pkS7hRVUrOjhwKJNyampPgtXKc3AN6DsWtoHsg2Sb4SQaS4Tcay380zSwd2VPo9G9180emBACp5g=="],

handlers/three-d-png.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const threeDPngHandler = async (
2222
width: pngWidth,
2323
height: pngHeight,
2424
showInfiniteGrid: ctx.showInfiniteGrid,
25+
backgroundColor: ctx.backgroundColor,
2526
})
2627

2728
return new Response(pngBuffer as any, {

lib/getRequestContext.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export async function getRequestContext(
6666
}
6767
}
6868

69+
// Parse background_color from query parameter
70+
const backgroundColorQuery = url.searchParams.get("background_color")
71+
if (backgroundColorQuery != null) {
72+
ctx.backgroundColor = backgroundColorQuery
73+
}
74+
6975
// Parse fsMap from query parameter
7076
const fsMapQueryParam = url.searchParams.get("fs_map")
7177
if (fsMapQueryParam) {

lib/render3dPng.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Render3dPngOptions {
66
height?: number
77
zoomMultiplier?: number
88
showInfiniteGrid?: boolean
9+
backgroundColor?: string
910
}
1011

1112
export async function render3dPng(
@@ -70,14 +71,14 @@ export async function render3dPng(
7071
return await renderGLTFToPNGBufferFromGLBBuffer(glbBinary, {
7172
width: pngWidth,
7273
height: pngHeight,
73-
backgroundColor: null,
74+
backgroundColor: options.backgroundColor ?? null,
7475
camPos,
7576
lookAt,
7677
grid: options.showInfiniteGrid
7778
? {
7879
infiniteGrid: true,
79-
gridColor: [0.8, 0.8, 0.8],
80-
sectionColor: [0.7, 0.7, 0.7],
80+
gridColor: [0.9, 0.9, 0.9],
81+
sectionColor: [0.7, 0.7, 0.9],
8182
offset: { y: 0 },
8283
}
8384
: false,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
"circuit-json-to-simple-3d": "^0.0.9",
3838
"circuit-to-svg": "^0.0.258",
3939
"jscad-fiber": "^0.0.85",
40-
"poppygl": "^0.0.16"
40+
"poppygl": "^0.0.17"
4141
}
4242
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect, test } from "bun:test"
2+
import { getCompressedBase64SnippetString } from "@tscircuit/create-snippet-url"
3+
import { getTestServer } from "./fixtures/get-test-server"
4+
5+
const pngSignature = [137, 80, 78, 71, 13, 10, 26, 10]
6+
7+
const simpleGridSnippet = `
8+
export default () => (
9+
<board width="10mm" height="10mm" />
10+
)
11+
`
12+
13+
test(
14+
"renders 3d infinite grid on white background",
15+
async () => {
16+
const { serverUrl } = await getTestServer()
17+
const encodedCode = encodeURIComponent(
18+
getCompressedBase64SnippetString(simpleGridSnippet),
19+
)
20+
21+
const colorResponse = await fetch(
22+
`${serverUrl}?svg_type=3d&format=png&background_color=%23ffffff&show_infinite_grid=true&code=${encodedCode}`,
23+
)
24+
25+
expect(colorResponse.status).toBe(200)
26+
expect(colorResponse.headers.get("content-type")).toContain("image/png")
27+
28+
const buffer = new Uint8Array(await colorResponse.arrayBuffer())
29+
expect(Array.from(buffer.slice(0, pngSignature.length))).toEqual(
30+
pngSignature,
31+
)
32+
expect(buffer.byteLength).toBeGreaterThan(1000)
33+
34+
await expect(Buffer.from(buffer)).toMatchPngSnapshot(import.meta.path)
35+
},
36+
{ timeout: 30000 },
37+
)

tests/3d-infinite-grid-with-model.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test(
5757
)
5858

5959
const response = await fetch(
60-
`${serverUrl}?svg_type=3d&format=png&show_infinite_grid=true&code=${encodedSnippet}`,
60+
`${serverUrl}?svg_type=3d&format=png&background_color=%23ffffff&show_infinite_grid=true&code=${encodedSnippet}`,
6161
)
6262

6363
expect(response.status).toBe(200)

tests/3d-infinite-grid.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test(
2424
)
2525

2626
const response = await fetch(
27-
`${serverUrl}?svg_type=3d&format=png&show_infinite_grid=true&code=${encodedSnippet}`,
27+
`${serverUrl}?svg_type=3d&format=png&background_color=%23ffffff&show_infinite_grid=true&code=${encodedSnippet}`,
2828
)
2929

3030
expect(response.status).toBe(200)
308 KB
Loading
-748 KB
Loading

0 commit comments

Comments
 (0)