Skip to content

Commit cd9bf3d

Browse files
committed
compensate for PCB image offsets in PCB group
1 parent d2704bf commit cd9bf3d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/components/Pcb.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export class Pcb extends Group {
3939
} else {
4040
this.append(this.createRectBackground(pcbX, pcbY, pcbWidth, pcbHeight, fill));
4141
}
42+
43+
this.setPadding(this.pcbPadding(image));
4244
}
4345

4446
/**
@@ -92,4 +94,31 @@ export class Pcb extends Group {
9294
ry: CORNERS,
9395
});
9496
}
97+
98+
/**
99+
* Calculate padding to compensate for image offsets
100+
*
101+
* Offsets for PCB images can increase the size of the pin layout. These offsets are
102+
* different for the front and back images. To be able to fold the diagrams on top of each
103+
* other, we need them both to be the exact same size. This function calculates the
104+
* padding needed to ensure that.
105+
*
106+
* @returns {number|object} The padding needed for the PCB images.
107+
*/
108+
pcbPadding(image) {
109+
if (!image.front.src && !image.back.src) return 0;
110+
111+
const diff = (front, back) => {
112+
if (front > 0 && back > 0) return 0; // only negative values influence the PCB space
113+
if (front < back) return 0; // front is the bigger number and sets the default
114+
return Math.abs(front - back); // return the difference
115+
}
116+
117+
return {
118+
top: diff(image.front.top, image.back.top),
119+
left: diff(image.front.left, image.back.left),
120+
right: diff(image.front.right, image.back.right),
121+
bottom: diff(image.front.bottom, image.back.bottom),
122+
}
123+
}
95124
}

0 commit comments

Comments
 (0)