mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-18 21:40:49 +02:00
Transfer to @napi-rs/canvas again (this time w/ line breaks)
This commit is contained in:
+12
-1
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user