Skip to content

Commit c81b7c0

Browse files
committed
Refactor badge drawing logic to use dynamic margins and font sizes for improved layout and readability
1 parent 658140d commit c81b7c0

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

public/script.js

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -195,62 +195,94 @@ function drawBadge() {
195195
ctx.imageSmoothingEnabled = false;
196196
ctx.antialias = 'none';
197197

198-
const bottomMargin = 10;
199-
const leftMargin = 10;
200-
const topMargin = 10;
198+
const margins = {
199+
left: 10,
200+
top: 10,
201+
bottom: 15,
202+
right: 10
203+
};
201204

202205
ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
203206
ctx.textBaseline = 'top';
207+
ctx.fillStyle = '#000000';
204208

205209
// Draw first name in bold
206-
ctx.font = 'bold 32px "Mona Sans"';
207-
ctx.fillStyle = '#000000';
208210
const firstname = document.getElementById('firstname').value;
209-
ctx.fillText(firstname, leftMargin, topMargin);
211+
let firstnameFontSize = 32;
212+
ctx.font = `bold ${firstnameFontSize}px "Mona Sans"`;
213+
while (ctx.measureText(firstname).width > canvas.width - margins.left - margins.right && firstnameFontSize > 24) {
214+
firstnameFontSize--;
215+
ctx.font = `bold ${firstnameFontSize}px "Mona Sans"`;
216+
}
217+
ctx.fillText(firstname, margins.left, margins.top);
210218

211219
// Draw last name in bold
212-
ctx.font = 'bold 24px "Mona Sans"';
213220
const lastname = document.getElementById('lastname').value;
214-
ctx.fillText(lastname, leftMargin, 45);
215-
221+
let lastnameFontSize = 24;
222+
ctx.font = `bold ${lastnameFontSize}px "Mona Sans"`;
223+
while (ctx.measureText(lastname).width > canvas.width - margins.left - margins.right && lastnameFontSize > 18) {
224+
lastnameFontSize--;
225+
ctx.font = `bold ${lastnameFontSize}px "Mona Sans"`;
226+
}
227+
const lastnameY = margins.top + firstnameFontSize + 5;
228+
ctx.fillText(lastname, margins.left, lastnameY);
229+
216230
// Get GitHub handle
217231
let githubhandle = document.getElementById('githubhandle').value;
218232
if (githubhandle && !githubhandle.startsWith('@')) {
219233
githubhandle = '@' + githubhandle;
220234
}
221235

222-
// Calculate dynamic font size for job title
223-
let jobtitleFontSize = 16; // Default font size
224-
const jobtitle = document.getElementById('jobtitle').value;
236+
// Calculate available space for remaining elements
237+
const availableHeight = canvas.height - margins.bottom - (lastnameY + lastnameFontSize + 10);
238+
const maxElementHeight = Math.floor(availableHeight / 3); // Divide space between askmeabout, jobtitle, and handle
225239

226-
if (jobtitle) { // Only calculate new size if jobtitle is not empty
227-
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
228-
while (ctx.measureText(jobtitle).width > canvas.width - 40 && jobtitleFontSize > 8) {
229-
jobtitleFontSize--;
230-
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
231-
}
240+
// Start from bottom for consistent spacing
241+
let currentY = canvas.height - margins.bottom;
242+
243+
// GitHub handle (bottom)
244+
let handleFontSize = 16;
245+
ctx.font = `${handleFontSize}px "Mona Sans"`;
246+
while (ctx.measureText(githubhandle).width > canvas.width - margins.left - margins.right && handleFontSize > 12) {
247+
handleFontSize--;
248+
ctx.font = `${handleFontSize}px "Mona Sans"`;
232249
}
250+
const handleMetrics = ctx.measureText(githubhandle);
251+
const handleHeight = handleMetrics.actualBoundingBoxAscent + handleMetrics.actualBoundingBoxDescent;
252+
currentY -= handleHeight;
253+
const githubHandleY = currentY;
233254

234-
// Calculate text metrics to fit job title within canvas
255+
// Job title (middle)
256+
currentY -= maxElementHeight * 0.8; // Add some spacing
257+
let jobtitleFontSize = 16;
258+
const jobtitle = document.getElementById('jobtitle').value;
235259
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
236-
const fontMetrics = ctx.measureText("Jobby");
237-
const textHeight = fontMetrics.actualBoundingBoxAscent + fontMetrics.actualBoundingBoxDescent;
238-
const lineHeightGap = textHeight * 0.3;
239-
240-
const githubHandleY = (canvas.height) - bottomMargin - textHeight;
241-
const jobTitleY = githubHandleY - textHeight - lineHeightGap;
260+
while (ctx.measureText(jobtitle).width > canvas.width - margins.left - margins.right && jobtitleFontSize > 12) {
261+
jobtitleFontSize--;
262+
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
263+
}
264+
const jobTitleY = currentY;
242265

243-
// Draw Ask Me About
266+
// Ask me about (above job title)
244267
const askmeabout = document.getElementById('askmeabout').value;
245268
if (askmeabout) {
246-
ctx.font = `14px "Mona Sans"`;
269+
currentY -= maxElementHeight * 0.8;
270+
let askMeAboutFontSize = 14;
271+
ctx.font = `${askMeAboutFontSize}px "Mona Sans"`;
247272
const askMeText = `Ask me about: ${askmeabout}`;
248-
ctx.fillText(askMeText, leftMargin, jobTitleY - textHeight - lineHeightGap);
273+
while (ctx.measureText(askMeText).width > canvas.width - margins.left - margins.right && askMeAboutFontSize > 11) {
274+
askMeAboutFontSize--;
275+
ctx.font = `${askMeAboutFontSize}px "Mona Sans"`;
276+
}
277+
ctx.fillText(askMeText, margins.left, currentY);
249278
}
250-
251-
// Draw the text
252-
ctx.fillText(jobtitle, leftMargin, jobTitleY);
253-
ctx.fillText(githubhandle, leftMargin, githubHandleY);
279+
280+
// Draw job title and handle with their calculated positions
281+
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
282+
ctx.fillText(jobtitle, margins.left, jobTitleY);
283+
284+
ctx.font = `${handleFontSize}px "Mona Sans"`;
285+
ctx.fillText(githubhandle, margins.left, githubHandleY);
254286

255287
// Convert to 2-bit black and white after drawing so you get an accurate preview
256288
// of e-ink display

0 commit comments

Comments
 (0)