feat(projects): render preview in top screen, also prepare for markdown rendering

This commit is contained in:
2025-11-20 23:57:10 +01:00
parent a9bc2a7358
commit 17eeb4cb13
15 changed files with 1109 additions and 577 deletions

57
app/utils/canvas.ts Normal file
View File

@@ -0,0 +1,57 @@
export const fillTextCentered = (
ctx: CanvasRenderingContext2D,
text: string,
x: number,
y: number,
width: number,
): number => {
const measure = ctx.measureText(text);
const textX = Math.floor(x + width / 2 - measure.actualBoundingBoxRight / 2);
const textY =
measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent;
ctx.fillText(text, textX, y + textY);
return measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent + 1;
};
export const fillTextWordWrapped = (
ctx: CanvasRenderingContext2D,
text: string,
x: number,
y: number,
width: number,
lineHeight?: number,
): number => {
const words = text.split(" ");
let line = "";
let currentY = y;
let lineCount = 0;
const height =
lineHeight ||
ctx.measureText("M").actualBoundingBoxAscent +
ctx.measureText("M").actualBoundingBoxDescent;
currentY += height;
for (let i = 0; i < words.length; i++) {
const testLine = line + (line ? " " : "") + words[i];
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if (testWidth > width && line) {
ctx.fillText(line, x, currentY);
line = words[i]!;
currentY += height;
lineCount++;
} else {
line = testLine;
}
}
if (line) {
ctx.fillText(line, x, currentY);
lineCount++;
}
return lineCount * height;
};