Version 1.4.0

This commit is contained in:
Killian
2020-10-20 19:42:51 +02:00
parent afa32e0211
commit 10fcffd6b6
43 changed files with 238 additions and 203 deletions

View File

@@ -9,7 +9,7 @@ module.exports = {
* @param {string} font the font
*/
applyText(canvas, text, defaultFontSize, width, font){
const ctx = canvas.getContext("2d");
const ctx = canvas.getContext(`2d`);
do {
ctx.font = `${(defaultFontSize -= 1)}px ${font}`;
} while (ctx.measureText(text).width > width);
@@ -19,10 +19,10 @@ module.exports = {
wrapText(ctx, text, maxWidth) {
return new Promise(resolve => {
if (ctx.measureText(text).width < maxWidth) return resolve([text]);
if (ctx.measureText('W').width > maxWidth) return resolve(null);
const words = text.split(' ');
if (ctx.measureText(`W`).width > maxWidth) return resolve(null);
const words = text.split(` `);
const lines = [];
let line = '';
let line = ``;
while (words.length > 0) {
let split = false;
while (ctx.measureText(words[0]).width >= maxWidth) {
@@ -41,11 +41,11 @@ module.exports = {
}
else {
lines.push(line.trim());
line = '';
line = ``;
}
if (words.length === 0) lines.push(line.trim());
}
return resolve(lines);
});
}
}
};