v1.2.9 Bugs fixes and nex generations

This commit is contained in:
Killian
2020-07-05 11:17:04 +02:00
parent 4f58095ba6
commit cd973a0c7f
11 changed files with 161 additions and 7 deletions

View File

@@ -8,11 +8,44 @@ module.exports = {
* @param {number} width the default width
* @param {string} font the font
*/
applyText(canvas, text, defaultFontSize, width, font) {
applyText(canvas, text, defaultFontSize, width, font){
const ctx = canvas.getContext("2d");
do {
ctx.font = `${(defaultFontSize -= 1)-3}px ${font}`;
ctx.font = `${(defaultFontSize -= 1)}px ${font}`;
} while (ctx.measureText(text).width > width);
return ctx.font;
},
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(' ');
const lines = [];
let line = '';
while (words.length > 0) {
let split = false;
while (ctx.measureText(words[0]).width >= maxWidth) {
const temp = words[0];
words[0] = temp.slice(0, -1);
if (split) {
words[1] = `${temp.slice(-1)}${words[1]}`;
}
else {
split = true;
words.splice(1, 0, temp.slice(-1));
}
}
if (ctx.measureText(`${line}${words[0]}`).width < maxWidth) {
line += `${words.shift()} `;
}
else {
lines.push(line.trim());
line = '';
}
if (words.length === 0) lines.push(line.trim());
}
return resolve(lines);
});
}
}