Skip to content

Commit 0b23607

Browse files
Add 2-bit preview
1 parent dd6ef57 commit 0b23607

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

public/script.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -210,34 +210,43 @@ function drawBadge() {
210210
ctx.font = 'bold 24px "Mona Sans"';
211211
const lastname = document.getElementById('lastname').value;
212212
ctx.fillText(lastname, leftMargin, 45);
213-
214-
// Calculate dynamic font size for job title
215-
const jobtitle = document.getElementById('jobtitle').value;
216-
let jobtitleFontSize = 14;
217-
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
218-
while (ctx.measureText(jobtitle).width > canvas.width - 40 && jobtitleFontSize > 8) {
219-
jobtitleFontSize--;
220-
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
221-
}
222-
213+
223214
// Get GitHub handle
224215
let githubhandle = document.getElementById('githubhandle').value;
225216
if (githubhandle && !githubhandle.startsWith('@')) {
226217
githubhandle = '@' + githubhandle;
227218
}
228219

220+
// Calculate dynamic font size for job title
221+
let jobtitleFontSize = 16; // Default font size
222+
const jobtitle = document.getElementById('jobtitle').value;
223+
224+
if (jobtitle) { // Only calculate new size if jobtitle is not empty
225+
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
226+
while (ctx.measureText(jobtitle).width > canvas.width - 40 && jobtitleFontSize > 8) {
227+
jobtitleFontSize--;
228+
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
229+
}
230+
}
231+
229232
// Calculate text metrics to fit job title within canvas
230233
ctx.font = `${jobtitleFontSize}px "Mona Sans"`;
231-
const fontMetrics = ctx.measureText(jobtitle);
234+
const fontMetrics = ctx.measureText("Jobby");
232235
const textHeight = fontMetrics.actualBoundingBoxAscent + fontMetrics.actualBoundingBoxDescent;
233-
const lineHeightGap = textHeight * 0.5;
236+
const lineHeightGap = textHeight * 0.3;
234237

235238
const githubHandleY = (canvas.height) - bottomMargin - textHeight;
236239
const jobTitleY = githubHandleY - textHeight - lineHeightGap;
237240

238241
// Draw the text
239242
ctx.fillText(jobtitle, leftMargin, jobTitleY);
240243
ctx.fillText(githubhandle, leftMargin, githubHandleY);
244+
245+
// Convert to 2-bit black and white after drawing
246+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
247+
const bwCanvas = convertTo2BitBW(imageData);
248+
ctx.clearRect(0, 0, canvas.width, canvas.height);
249+
ctx.drawImage(bwCanvas, 0, 0);
241250
}
242251

243252
// Replace the font loading with combined font and image loading
@@ -284,15 +293,9 @@ function convertTo2BitBW(imageData) {
284293
// Modify the downloadBadge function to use the conversion
285294
function downloadBadge() {
286295
const canvas = document.getElementById('badgeCanvas');
287-
const ctx = canvas.getContext('2d');
288-
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
289-
290-
// Convert to 2-bit black and white
291-
const bwCanvas = convertTo2BitBW(imageData);
292-
293296
const link = document.createElement('a');
294297
link.download = 'badge.png';
295-
link.href = bwCanvas.toDataURL('image/png');
298+
link.href = canvas.toDataURL('image/png');
296299
link.click();
297300
}
298301

0 commit comments

Comments
 (0)