Skip to content

Commit adab46f

Browse files
Fix up sizing using container queries
1 parent 0fa66a5 commit adab46f

3 files changed

Lines changed: 52 additions & 41 deletions

File tree

pinout.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ <h2 class="connector-name"></h2>
4545
</div>
4646
</div>
4747
<div class="connector-container">
48-
<div class="connector-div">
49-
<img class="connector-img"></img>
48+
<div class="connector-div-container">
49+
<div class="connector-div">
50+
<img class="connector-img"></img>
51+
</div>
5052
</div>
5153
</div>
5254
<table class="info-table general-table">

script.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,6 @@ function checkImagesLoaded() {
205205
}
206206
}
207207

208-
let beforePrintHandlers = [];
209-
210-
window.addEventListener("beforeprint", () => {
211-
beforePrintHandlers.forEach((h) => h());
212-
});
213-
214-
let afterPrintHandlers = [];
215-
216-
window.addEventListener("afterprint", () => {
217-
afterPrintHandlers.forEach((h) => h());
218-
});
219-
220208
// This is a butchery and I hate it and never want to touch it again.
221209
function calcPinSize(pin, cdiv, connector, pinfo) {
222210
// Find the closest pin, to maximize the pin size for best readability,
@@ -229,31 +217,28 @@ function calcPinSize(pin, cdiv, connector, pinfo) {
229217
closest = distance;
230218
}
231219
});
232-
// Set the pin's size
233220
closest = Math.sqrt(closest);
221+
// Set the pin's size
234222
const divheight = cdiv.clientHeight;
235223
const divwidth = cdiv.clientWidth;
236-
const mult = cdiv.querySelector("img").naturalHeight / divheight;
237-
const newheight = (closest / mult)
238-
let pxheight = divheight * 0.08;
239-
if (newheight < pxheight) {
240-
pxheight = newheight;
224+
const scale = divheight / cdiv.querySelector("img").naturalHeight;
225+
const newheight = closest * scale;
226+
// Default height is 8% of div
227+
let pinsize = divheight * 0.08;
228+
if (newheight < pinsize) {
229+
pinsize = newheight;
241230
}
242-
const height = (pxheight / divheight) * 100;
243-
const width = (pxheight / divwidth) * 100;
244-
pin.pdiv.style.height = "calc(" + height + "%)";
245-
pin.pdiv.style.width = "calc(" + width + "%)";
246-
pin.pdiv.style.lineHeight = "calc(" + pxheight + "px - 0.21vw)";
231+
// Use percent for scaling when printing
232+
const height = (pinsize / divheight) * 100;
233+
const width = (pinsize / divwidth) * 100;
234+
pin.pdiv.style.height = height + "%";
235+
pin.pdiv.style.width = width + "%";
236+
// 0.5cqh achieves vertical centering, roughly
237+
pin.pdiv.style.lineHeight = (height - 0.5) + "cqh";
238+
// When using a percent for margins, the width value is used even for top and bottom
247239
pin.pdiv.style.marginTop = "-" + (width / 2) + "%";
248240
pin.pdiv.style.marginLeft = "-" + (width / 2) + "%";
249-
pin.pdiv.style.fontSize = (pxheight * 0.5) + "px";
250-
// Recalculate the size for printing.
251-
beforePrintHandlers.push(() => {
252-
pin.pdiv.style.fontSize = "calc(calc(" + width + "px * min(640, " + divwidth + ")) * 0.0055)";
253-
});
254-
afterPrintHandlers.push(() => {
255-
pin.pdiv.style.fontSize = (pxheight * 0.5) + "px";
256-
});
241+
pin.pdiv.style.fontSize = (height * 0.5) + "cqh";
257242
}
258243

259244
function findBounds(pins, i) {
@@ -327,6 +312,8 @@ function setupColorToggle(sdiv, columns) {
327312

328313
function handleImageLoad(connector, c, sdiv, img, columns) {
329314
const cdiv = sdiv.querySelector(".connector-div");
315+
const cdc = sdiv.querySelector(".connector-div-container");
316+
cdc.style.aspectRatio = img.naturalWidth / img.naturalHeight;
330317
const ptemplate = document.getElementById("pin-template");
331318
const pitemplate = document.getElementById("pin-info-template");
332319
const imgHeight = img.naturalHeight;

style.css

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,25 @@ body {
7171

7272
.connector-container {
7373
width: 100%;
74-
overflow-x: scroll;
74+
overflow-x: auto;
7575
line-height: 0px;
7676
position: relative;
7777
height: max(3in, 50vh);
7878
}
7979

80+
.connector-div-container {
81+
container-type: size;
82+
position: relative;
83+
height: 100%;
84+
margin-left: auto;
85+
margin-right: auto;
86+
}
87+
8088
.connector-div {
8189
height: 100%;
8290
position: relative;
8391
z-index: 0;
8492
width: max-content;
85-
margin-left: auto;
86-
margin-right: auto;
8793
}
8894

8995
.connector-img {
@@ -311,17 +317,33 @@ body {
311317
min-height: 2in;
312318
}
313319

320+
.connector-div-container {
321+
max-height: 3in;
322+
max-width: 100%;
323+
height: unset;
324+
}
325+
314326
.connector-div {
315327
max-width: 100%;
316-
height: unset;
328+
height: 100%;
317329
}
318330

319331
.connector-img {
320332
max-width: 100% !important;
321-
max-height: 3in;
333+
height: 100%;
322334
}
323335

324-
h2 {
336+
.pin-marker.highlight {
337+
background-color: #eee;
338+
}
339+
340+
.pin-marker.selected {
341+
background-color: #eee;
342+
color: #111;
343+
text-shadow: none;
344+
}
345+
346+
h2 {
325347
font-size: 14px;
326348
}
327349

@@ -334,14 +356,14 @@ body {
334356
}
335357

336358
td:not(.print-column), .info-table, thead, #board-link {
337-
display: none;
359+
display: none !important;
338360
}
339361

340362
td[data-field="pin"] {
341363
border-width: 3px;
342364
border-radius: 10px;
343365
width: 10px;
344-
margin: 0;
366+
text-align: center;
345367
padding: 0;
346368
}
347369

0 commit comments

Comments
 (0)