Transfer to @napi-rs/canvas again (this time w/ line breaks)

This commit is contained in:
Dragon Fire
2024-05-03 10:21:28 -04:00
parent 0d04b94797
commit bed3847f60
174 changed files with 234 additions and 227 deletions
+12 -1
View File
@@ -1,4 +1,4 @@
const { createCanvas } = require('canvas');
const { createCanvas } = require('@napi-rs/canvas');
module.exports = class CanvasUtil {
static greyscale(ctx, x, y, width, height) {
@@ -197,6 +197,17 @@ module.exports = class CanvasUtil {
return ctx;
}
static fillTextWithBreaks(ctx, text, x, y, maxLen) {
const lines = text.split('\n');
let currentY = y;
for (const line of lines) {
ctx.fillText(line, x, currentY, maxLen);
const metrics = ctx.measureText(line);
currentY += metrics.emHeightAscent + metrics.emHeightDescent;
}
return ctx;
}
static shortenText(ctx, text, maxWidth) {
let shorten = false;
while (ctx.measureText(`${text}...`).width > maxWidth) {