circuit-to-canvas 0.0.29 → 0.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AnyCircuitElement, NinePointAnchor, PcbPlatedHole, PCBVia, PCBHole, PcbSmtPad, PCBTrace, PcbBoard, PcbSilkscreenText, PcbSilkscreenRect, PcbSilkscreenCircle, PcbSilkscreenLine, PcbSilkscreenPath, PcbSilkscreenPill, PcbCutout, PcbCopperPour, PcbCopperText, PcbFabricationNoteText, PcbFabricationNoteRect, PcbNoteRect, PcbFabricationNotePath, PcbNotePath, PcbNoteText, PcbNoteDimension } from 'circuit-json';
1
+ import { AnyCircuitElement, PcbRenderLayer, NinePointAnchor, PcbPlatedHole, PCBVia, PCBHole, PcbSmtPad, PCBTrace, PcbBoard, PcbSilkscreenText, PcbSilkscreenRect, PcbSilkscreenCircle, PcbSilkscreenLine, PcbSilkscreenPath, PcbSilkscreenPill, PcbCutout, PcbCopperPour, PcbCopperText, PcbFabricationNoteText, PcbFabricationNoteRect, PcbNoteRect, PcbFabricationNotePath, PcbNotePath, PcbNoteText, PcbNoteDimension } from 'circuit-json';
2
2
  import { Matrix } from 'transformation-matrix';
3
3
 
4
4
  /**
@@ -86,7 +86,7 @@ interface DrawContext {
86
86
  }
87
87
 
88
88
  interface DrawElementsOptions {
89
- layers?: string[];
89
+ layers?: PcbRenderLayer[];
90
90
  }
91
91
  interface CanvasLike {
92
92
  getContext(contextId: "2d"): CanvasContext | null;
package/dist/index.js CHANGED
@@ -1,3 +1,16 @@
1
+ // lib/drawer/pcb-render-layer-filter.ts
2
+ import { getElementRenderLayers } from "@tscircuit/circuit-json-util";
3
+ function shouldDrawElement(element, options) {
4
+ if (!options.layers || options.layers.length === 0) {
5
+ return true;
6
+ }
7
+ const elementLayers = getElementRenderLayers(element);
8
+ if (elementLayers.length === 0) {
9
+ return true;
10
+ }
11
+ return elementLayers.some((layer) => options.layers.includes(layer));
12
+ }
13
+
1
14
  // lib/drawer/CircuitToCanvasDrawer.ts
2
15
  import { identity, compose, translate, scale } from "transformation-matrix";
3
16
 
@@ -1516,6 +1529,9 @@ var CircuitToCanvasDrawer = class {
1516
1529
  }
1517
1530
  }
1518
1531
  drawElement(element, options) {
1532
+ if (!shouldDrawElement(element, options)) {
1533
+ return;
1534
+ }
1519
1535
  if (element.type === "pcb_plated_hole") {
1520
1536
  drawPcbPlatedHole({
1521
1537
  ctx: this.ctx,
@@ -1,10 +1,10 @@
1
1
  import type {
2
2
  AnyCircuitElement,
3
3
  PcbPlatedHole,
4
- PCBVia,
5
- PCBHole,
4
+ PcbVia,
5
+ PcbHole,
6
6
  PcbSmtPad,
7
- PCBTrace,
7
+ PcbTrace,
8
8
  PcbBoard,
9
9
  PcbSilkscreenText,
10
10
  PcbSilkscreenRect,
@@ -23,7 +23,9 @@ import type {
23
23
  PcbNoteText,
24
24
  PcbNoteDimension,
25
25
  PcbNoteLine,
26
+ PcbRenderLayer,
26
27
  } from "circuit-json"
28
+ import { shouldDrawElement } from "./pcb-render-layer-filter"
27
29
  import { identity, compose, translate, scale } from "transformation-matrix"
28
30
  import type { Matrix } from "transformation-matrix"
29
31
  import {
@@ -58,7 +60,7 @@ import { drawPcbNoteDimension } from "./elements/pcb-note-dimension"
58
60
  import { drawPcbNoteLine } from "./elements/pcb-note-line"
59
61
 
60
62
  export interface DrawElementsOptions {
61
- layers?: string[]
63
+ layers?: PcbRenderLayer[]
62
64
  }
63
65
 
64
66
  interface CanvasLike {
@@ -155,6 +157,11 @@ export class CircuitToCanvasDrawer {
155
157
  element: AnyCircuitElement,
156
158
  options: DrawElementsOptions,
157
159
  ): void {
160
+ // Check if element should be drawn based on layer options
161
+ if (!shouldDrawElement(element, options)) {
162
+ return
163
+ }
164
+
158
165
  if (element.type === "pcb_plated_hole") {
159
166
  drawPcbPlatedHole({
160
167
  ctx: this.ctx,
@@ -167,7 +174,7 @@ export class CircuitToCanvasDrawer {
167
174
  if (element.type === "pcb_via") {
168
175
  drawPcbVia({
169
176
  ctx: this.ctx,
170
- via: element as PCBVia,
177
+ via: element as PcbVia,
171
178
  realToCanvasMat: this.realToCanvasMat,
172
179
  colorMap: this.colorMap,
173
180
  })
@@ -176,7 +183,7 @@ export class CircuitToCanvasDrawer {
176
183
  if (element.type === "pcb_hole") {
177
184
  drawPcbHole({
178
185
  ctx: this.ctx,
179
- hole: element as PCBHole,
186
+ hole: element as PcbHole,
180
187
  realToCanvasMat: this.realToCanvasMat,
181
188
  colorMap: this.colorMap,
182
189
  })
@@ -194,7 +201,7 @@ export class CircuitToCanvasDrawer {
194
201
  if (element.type === "pcb_trace") {
195
202
  drawPcbTrace({
196
203
  ctx: this.ctx,
197
- trace: element as PCBTrace,
204
+ trace: element as PcbTrace,
198
205
  realToCanvasMat: this.realToCanvasMat,
199
206
  colorMap: this.colorMap,
200
207
  })
@@ -0,0 +1,30 @@
1
+ import type { AnyCircuitElement } from "circuit-json"
2
+ import type { DrawElementsOptions } from "./CircuitToCanvasDrawer"
3
+ import { getElementRenderLayers } from "@tscircuit/circuit-json-util"
4
+
5
+ /**
6
+ * Gets the render layer(s) for an element based on its type and layer property
7
+ */
8
+
9
+ /**
10
+ * Checks if an element should be drawn based on layer options
11
+ */
12
+ export function shouldDrawElement(
13
+ element: AnyCircuitElement,
14
+ options: DrawElementsOptions,
15
+ ): boolean {
16
+ // If no layers specified, draw all elements
17
+ if (!options.layers || options.layers.length === 0) {
18
+ return true
19
+ }
20
+
21
+ const elementLayers = getElementRenderLayers(element)
22
+
23
+ // If element has no layer info (board, holes, etc.), always draw
24
+ if (elementLayers.length === 0) {
25
+ return true
26
+ }
27
+
28
+ // Check if any of the element's layers match the requested layers
29
+ return elementLayers.some((layer) => options.layers!.includes(layer))
30
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "circuit-to-canvas",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.29",
4
+ "version": "0.0.30",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsup-node ./lib/index.ts --format esm --dts",
@@ -13,11 +13,11 @@
13
13
  "@napi-rs/canvas": "^0.1.84",
14
14
  "@resvg/resvg-js": "^2.6.2",
15
15
  "@tscircuit/alphabet": "^0.0.17",
16
- "@tscircuit/circuit-json-util": "^0.0.73",
16
+ "@tscircuit/circuit-json-util": "^0.0.75",
17
17
  "@tscircuit/math-utils": "^0.0.29",
18
18
  "@types/bun": "latest",
19
19
  "bun-match-svg": "^0.0.14",
20
- "circuit-json": "^0.0.342",
20
+ "circuit-json": "^0.0.346",
21
21
  "circuit-json-to-connectivity-map": "^0.0.23",
22
22
  "circuit-to-svg": "^0.0.297",
23
23
  "looks-same": "^10.0.1",
@@ -0,0 +1,42 @@
1
+ import { expect, test } from "bun:test"
2
+ import { createCanvas } from "@napi-rs/canvas"
3
+ import type { PcbSmtPad } from "circuit-json"
4
+ import { CircuitToCanvasDrawer } from "../../lib/drawer"
5
+
6
+ test("layer filter only draws elements on specified layers", async () => {
7
+ const canvas = createCanvas(100, 100)
8
+ const ctx = canvas.getContext("2d")
9
+ const drawer = new CircuitToCanvasDrawer(ctx)
10
+
11
+ ctx.fillStyle = "#1a1a1a"
12
+ ctx.fillRect(0, 0, 100, 100)
13
+
14
+ const topPad: PcbSmtPad = {
15
+ type: "pcb_smtpad",
16
+ pcb_smtpad_id: "pad1",
17
+ shape: "rect",
18
+ x: 30,
19
+ y: 50,
20
+ width: 20,
21
+ height: 20,
22
+ layer: "top",
23
+ }
24
+
25
+ const bottomPad: PcbSmtPad = {
26
+ type: "pcb_smtpad",
27
+ pcb_smtpad_id: "pad2",
28
+ shape: "rect",
29
+ x: 70,
30
+ y: 50,
31
+ width: 20,
32
+ height: 20,
33
+ layer: "bottom",
34
+ }
35
+
36
+ // Draw only top layer - should only show top pad (left side)
37
+ drawer.drawElements([topPad, bottomPad], { layers: ["top_copper"] })
38
+
39
+ await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
40
+ import.meta.path,
41
+ )
42
+ })