circuit-to-canvas 0.0.57 → 0.0.59
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 +4 -1
- package/dist/index.js +27 -4
- package/lib/drawer/elements/pcb-silkscreen-circle.ts +2 -1
- package/lib/drawer/shapes/circle.ts +34 -4
- package/package.json +1 -1
- package/tests/elements/__snapshots__/pcb-silkscreen-circle.snap.png +0 -0
- package/tests/elements/__snapshots__/pcb-silkscreen-on-component.snap.png +0 -0
- package/tests/elements/__snapshots__/pcb-trace-hints.snap.png +0 -0
- package/tests/elements/pcb-trace-hints.test.ts +259 -0
package/dist/index.d.ts
CHANGED
|
@@ -115,8 +115,11 @@ interface DrawCircleParams {
|
|
|
115
115
|
y: number;
|
|
116
116
|
};
|
|
117
117
|
radius: number;
|
|
118
|
-
fill
|
|
118
|
+
fill?: string;
|
|
119
119
|
realToCanvasMat: Matrix;
|
|
120
|
+
stroke?: string;
|
|
121
|
+
strokeWidth?: number;
|
|
122
|
+
isStrokeDashed?: boolean;
|
|
120
123
|
}
|
|
121
124
|
declare function drawCircle(params: DrawCircleParams): void;
|
|
122
125
|
|
package/dist/index.js
CHANGED
|
@@ -642,13 +642,35 @@ function drawPcbCopperText(params) {
|
|
|
642
642
|
// lib/drawer/shapes/circle.ts
|
|
643
643
|
import { applyToPoint as applyToPoint7 } from "transformation-matrix";
|
|
644
644
|
function drawCircle(params) {
|
|
645
|
-
const {
|
|
645
|
+
const {
|
|
646
|
+
ctx,
|
|
647
|
+
center,
|
|
648
|
+
radius,
|
|
649
|
+
fill,
|
|
650
|
+
realToCanvasMat,
|
|
651
|
+
stroke,
|
|
652
|
+
strokeWidth,
|
|
653
|
+
isStrokeDashed = false
|
|
654
|
+
} = params;
|
|
646
655
|
const [cx, cy] = applyToPoint7(realToCanvasMat, [center.x, center.y]);
|
|
647
656
|
const scaledRadius = radius * Math.abs(realToCanvasMat.a);
|
|
657
|
+
const scaledStrokeWidth = strokeWidth ? strokeWidth * Math.abs(realToCanvasMat.a) : void 0;
|
|
648
658
|
ctx.beginPath();
|
|
649
659
|
ctx.arc(cx, cy, scaledRadius, 0, Math.PI * 2);
|
|
650
|
-
|
|
651
|
-
|
|
660
|
+
if (fill) {
|
|
661
|
+
ctx.fillStyle = fill;
|
|
662
|
+
ctx.fill();
|
|
663
|
+
}
|
|
664
|
+
if (stroke && scaledStrokeWidth) {
|
|
665
|
+
if (isStrokeDashed) {
|
|
666
|
+
ctx.setLineDash([scaledStrokeWidth * 3, scaledStrokeWidth * 2]);
|
|
667
|
+
} else {
|
|
668
|
+
ctx.setLineDash([]);
|
|
669
|
+
}
|
|
670
|
+
ctx.strokeStyle = stroke;
|
|
671
|
+
ctx.lineWidth = scaledStrokeWidth;
|
|
672
|
+
ctx.stroke();
|
|
673
|
+
}
|
|
652
674
|
}
|
|
653
675
|
|
|
654
676
|
// lib/drawer/elements/pcb-cutout.ts
|
|
@@ -1850,7 +1872,8 @@ function drawPcbSilkscreenCircle(params) {
|
|
|
1850
1872
|
ctx,
|
|
1851
1873
|
center: circle.center,
|
|
1852
1874
|
radius: circle.radius,
|
|
1853
|
-
|
|
1875
|
+
stroke: color,
|
|
1876
|
+
strokeWidth: circle.stroke_width,
|
|
1854
1877
|
realToCanvasMat
|
|
1855
1878
|
});
|
|
1856
1879
|
}
|
|
@@ -6,18 +6,48 @@ export interface DrawCircleParams {
|
|
|
6
6
|
ctx: CanvasContext
|
|
7
7
|
center: { x: number; y: number }
|
|
8
8
|
radius: number
|
|
9
|
-
fill
|
|
9
|
+
fill?: string
|
|
10
10
|
realToCanvasMat: Matrix
|
|
11
|
+
stroke?: string
|
|
12
|
+
strokeWidth?: number
|
|
13
|
+
isStrokeDashed?: boolean
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
export function drawCircle(params: DrawCircleParams): void {
|
|
14
|
-
const {
|
|
17
|
+
const {
|
|
18
|
+
ctx,
|
|
19
|
+
center,
|
|
20
|
+
radius,
|
|
21
|
+
fill,
|
|
22
|
+
realToCanvasMat,
|
|
23
|
+
stroke,
|
|
24
|
+
strokeWidth,
|
|
25
|
+
isStrokeDashed = false,
|
|
26
|
+
} = params
|
|
15
27
|
|
|
16
28
|
const [cx, cy] = applyToPoint(realToCanvasMat, [center.x, center.y])
|
|
17
29
|
const scaledRadius = radius * Math.abs(realToCanvasMat.a)
|
|
30
|
+
const scaledStrokeWidth = strokeWidth
|
|
31
|
+
? strokeWidth * Math.abs(realToCanvasMat.a)
|
|
32
|
+
: undefined
|
|
18
33
|
|
|
19
34
|
ctx.beginPath()
|
|
20
35
|
ctx.arc(cx, cy, scaledRadius, 0, Math.PI * 2)
|
|
21
|
-
|
|
22
|
-
|
|
36
|
+
|
|
37
|
+
if (fill) {
|
|
38
|
+
ctx.fillStyle = fill
|
|
39
|
+
ctx.fill()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (stroke && scaledStrokeWidth) {
|
|
43
|
+
// Set up dashed line if needed (after path is drawn, before stroke)
|
|
44
|
+
if (isStrokeDashed) {
|
|
45
|
+
ctx.setLineDash([scaledStrokeWidth * 3, scaledStrokeWidth * 2])
|
|
46
|
+
} else {
|
|
47
|
+
ctx.setLineDash([])
|
|
48
|
+
}
|
|
49
|
+
ctx.strokeStyle = stroke
|
|
50
|
+
ctx.lineWidth = scaledStrokeWidth
|
|
51
|
+
ctx.stroke()
|
|
52
|
+
}
|
|
23
53
|
}
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { createCanvas } from "@napi-rs/canvas"
|
|
3
|
+
import { CircuitToCanvasDrawer } from "../../lib/drawer"
|
|
4
|
+
|
|
5
|
+
test("draw trace with port hints", async () => {
|
|
6
|
+
const canvas = createCanvas(800, 600)
|
|
7
|
+
const ctx = canvas.getContext("2d")
|
|
8
|
+
const drawer = new CircuitToCanvasDrawer(ctx)
|
|
9
|
+
|
|
10
|
+
drawer.setCameraBounds({ minX: -8, minY: -2, maxX: 0, maxY: 6 })
|
|
11
|
+
|
|
12
|
+
ctx.fillStyle = "#1a1a1a"
|
|
13
|
+
ctx.fillRect(0, 0, 800, 600)
|
|
14
|
+
|
|
15
|
+
const circuit: any = [
|
|
16
|
+
{
|
|
17
|
+
type: "source_component",
|
|
18
|
+
source_component_id: "simple_resistor_0",
|
|
19
|
+
name: "R1",
|
|
20
|
+
supplier_part_numbers: {},
|
|
21
|
+
ftype: "simple_resistor",
|
|
22
|
+
resistance: "10k",
|
|
23
|
+
pcbX: -5,
|
|
24
|
+
pcbY: 0,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
type: "schematic_component",
|
|
28
|
+
source_component_id: "simple_resistor_0",
|
|
29
|
+
schematic_component_id: "schematic_component_simple_resistor_0",
|
|
30
|
+
rotation: 0,
|
|
31
|
+
size: {
|
|
32
|
+
width: 1,
|
|
33
|
+
height: 0.3,
|
|
34
|
+
},
|
|
35
|
+
center: {
|
|
36
|
+
x: 0,
|
|
37
|
+
y: 0,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
type: "source_port",
|
|
42
|
+
name: "left",
|
|
43
|
+
source_port_id: "source_port_0",
|
|
44
|
+
source_component_id: "simple_resistor_0",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: "schematic_port",
|
|
48
|
+
schematic_port_id: "schematic_port_0",
|
|
49
|
+
source_port_id: "source_port_0",
|
|
50
|
+
center: {
|
|
51
|
+
x: -0.5,
|
|
52
|
+
y: 0,
|
|
53
|
+
},
|
|
54
|
+
facing_direction: "left",
|
|
55
|
+
schematic_component_id: "schematic_component_simple_resistor_0",
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
type: "pcb_port",
|
|
59
|
+
pcb_port_id: "pcb_port_0",
|
|
60
|
+
source_port_id: "source_port_0",
|
|
61
|
+
pcb_component_id: "pcb_component_simple_resistor_0",
|
|
62
|
+
x: -5.95,
|
|
63
|
+
y: 0,
|
|
64
|
+
layers: ["top"],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
type: "source_port",
|
|
68
|
+
name: "right",
|
|
69
|
+
source_port_id: "source_port_1",
|
|
70
|
+
source_component_id: "simple_resistor_0",
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
type: "schematic_port",
|
|
74
|
+
schematic_port_id: "schematic_port_1",
|
|
75
|
+
source_port_id: "source_port_1",
|
|
76
|
+
center: {
|
|
77
|
+
x: 0.5,
|
|
78
|
+
y: 0,
|
|
79
|
+
},
|
|
80
|
+
facing_direction: "right",
|
|
81
|
+
schematic_component_id: "schematic_component_simple_resistor_0",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: "pcb_port",
|
|
85
|
+
pcb_port_id: "pcb_port_1",
|
|
86
|
+
source_port_id: "source_port_1",
|
|
87
|
+
pcb_component_id: "pcb_component_simple_resistor_0",
|
|
88
|
+
x: -4.05,
|
|
89
|
+
y: 0,
|
|
90
|
+
layers: ["top"],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
type: "schematic_text",
|
|
94
|
+
text: "R1",
|
|
95
|
+
schematic_text_id: "schematic_text_0",
|
|
96
|
+
schematic_component_id: "schematic_component_simple_resistor_0",
|
|
97
|
+
anchor: "left",
|
|
98
|
+
position: {
|
|
99
|
+
x: -0.2,
|
|
100
|
+
y: -0.5,
|
|
101
|
+
},
|
|
102
|
+
rotation: 0,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
type: "schematic_text",
|
|
106
|
+
text: "10k",
|
|
107
|
+
schematic_text_id: "schematic_text_1",
|
|
108
|
+
schematic_component_id: "schematic_component_simple_resistor_0",
|
|
109
|
+
anchor: "left",
|
|
110
|
+
position: {
|
|
111
|
+
x: -0.2,
|
|
112
|
+
y: -0.3,
|
|
113
|
+
},
|
|
114
|
+
rotation: 0,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
type: "pcb_component",
|
|
118
|
+
source_component_id: "simple_resistor_0",
|
|
119
|
+
pcb_component_id: "pcb_component_simple_resistor_0",
|
|
120
|
+
layer: "top",
|
|
121
|
+
center: {
|
|
122
|
+
x: -5,
|
|
123
|
+
y: 0,
|
|
124
|
+
},
|
|
125
|
+
rotation: 0,
|
|
126
|
+
width: 3.1,
|
|
127
|
+
height: 1.2,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
type: "pcb_smtpad",
|
|
131
|
+
pcb_smtpad_id: "pcb_smtpad_0",
|
|
132
|
+
shape: "rect",
|
|
133
|
+
x: -5.95,
|
|
134
|
+
y: 0,
|
|
135
|
+
width: 1.2,
|
|
136
|
+
height: 1.2,
|
|
137
|
+
layer: "top",
|
|
138
|
+
pcb_component_id: "pcb_component_simple_resistor_0",
|
|
139
|
+
port_hints: ["1", "left"],
|
|
140
|
+
pcb_port_id: "pcb_port_0",
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
type: "pcb_smtpad",
|
|
144
|
+
pcb_smtpad_id: "pcb_smtpad_1",
|
|
145
|
+
shape: "rect",
|
|
146
|
+
x: -4.05,
|
|
147
|
+
y: 0,
|
|
148
|
+
width: 1.2,
|
|
149
|
+
height: 1.2,
|
|
150
|
+
layer: "top",
|
|
151
|
+
pcb_component_id: "pcb_component_simple_resistor_0",
|
|
152
|
+
port_hints: ["2", "right"],
|
|
153
|
+
pcb_port_id: "pcb_port_1",
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
type: "source_trace",
|
|
157
|
+
source_trace_id: "source_trace_0",
|
|
158
|
+
connected_source_port_ids: ["source_port_0", "source_port_1"],
|
|
159
|
+
connected_source_net_ids: [],
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
type: "schematic_trace",
|
|
163
|
+
source_trace_id: "source_trace_0",
|
|
164
|
+
schematic_trace_id: "schematic_trace_0",
|
|
165
|
+
edges: [
|
|
166
|
+
{
|
|
167
|
+
from: {
|
|
168
|
+
x: -0.6000000000000001,
|
|
169
|
+
y: 0,
|
|
170
|
+
},
|
|
171
|
+
to: {
|
|
172
|
+
x: -0.6000000000000001,
|
|
173
|
+
y: -0.19999999999999996,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
from: {
|
|
178
|
+
x: -0.6000000000000001,
|
|
179
|
+
y: -0.19999999999999996,
|
|
180
|
+
},
|
|
181
|
+
to: {
|
|
182
|
+
x: 0.7000000000000002,
|
|
183
|
+
y: -0.19999999999999996,
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
from: {
|
|
188
|
+
x: 0.7000000000000002,
|
|
189
|
+
y: -0.19999999999999996,
|
|
190
|
+
},
|
|
191
|
+
to: {
|
|
192
|
+
x: 0.7000000000000002,
|
|
193
|
+
y: 0,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
from: {
|
|
198
|
+
x: -0.5,
|
|
199
|
+
y: 0,
|
|
200
|
+
ti: 0,
|
|
201
|
+
},
|
|
202
|
+
to: {
|
|
203
|
+
x: -0.6000000000000001,
|
|
204
|
+
y: 0,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
from: {
|
|
209
|
+
x: 0.5,
|
|
210
|
+
y: 0,
|
|
211
|
+
ti: 1,
|
|
212
|
+
},
|
|
213
|
+
to: {
|
|
214
|
+
x: 0.7000000000000002,
|
|
215
|
+
y: 0,
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
type: "pcb_trace",
|
|
222
|
+
pcb_trace_id: "pcb_trace_0",
|
|
223
|
+
source_trace_id: "source_trace_0",
|
|
224
|
+
route_thickness_mode: "interpolated",
|
|
225
|
+
route: [
|
|
226
|
+
{
|
|
227
|
+
route_type: "wire",
|
|
228
|
+
layer: "top",
|
|
229
|
+
width: 0.2,
|
|
230
|
+
x: -4,
|
|
231
|
+
y: 0,
|
|
232
|
+
start_pcb_port_id: "pcb_port_0",
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
route_type: "wire",
|
|
236
|
+
layer: "top",
|
|
237
|
+
width: 0.5,
|
|
238
|
+
x: -5.9,
|
|
239
|
+
y: 4,
|
|
240
|
+
start_pcb_port_id: "pcb_port_1",
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
route_type: "wire",
|
|
244
|
+
layer: "top",
|
|
245
|
+
width: 0.5,
|
|
246
|
+
x: -5.9,
|
|
247
|
+
y: 0,
|
|
248
|
+
start_pcb_port_id: "pcb_port_1",
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
},
|
|
252
|
+
]
|
|
253
|
+
|
|
254
|
+
drawer.drawElements(circuit)
|
|
255
|
+
|
|
256
|
+
await expect(canvas.toBuffer("image/png")).toMatchPngSnapshot(
|
|
257
|
+
import.meta.path,
|
|
258
|
+
)
|
|
259
|
+
})
|