cap fixed

This commit is contained in:
lolcat 2024-12-31 01:01:53 -05:00
parent 469320c31a
commit 1431cd3d1b
1 changed files with 101 additions and 27 deletions

128
client.js
View File

@ -1721,45 +1721,119 @@ deek.on("message", async function(msg, user){
var fontsize = Math.floor(image.width * 0.08); var fontsize = Math.floor(image.width * 0.08);
ctx.font = fontsize + "px Impact"; ctx.font = fontsize + "px Impact";
ctx.fillStyle = "white"; ctx.fillStyle = "white";
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.strokeStyle = "black"; ctx.strokeStyle = "black";
ctx.lineWidth = image.width * 0.01; ctx.lineWidth = image.width * 0.01;
ctx.textAlign = "center"; ctx.textAlign = "center";
ctx.textBaseline = "top"; ctx.textBaseline = "bottom";
var parts = value.replace(/ +/g, " ").split(""); value = value.split("|");
var tmp_text = ""; wrap(value[0], false);
var tmp_text_old = "";
var text_to_add = ""; if(typeof value[1] != "undefined"){
var line = 0;
for(var i=0; i<parts.length; i++){
tmp_text_old = tmp_text; wrap(value[1], true);
tmp_text += parts[i]; }
function wrap(text, is_bottom){
var just_pushed_line = false;
var words = text.split(" ");
var lines = [];
var line = "";
var line_tmp = "";
var metrics = ctx.measureText(tmp_text.trim()); for(var i=0; i<words.length; i++){
if( var word_width = ctx.measureText(words[i]).width;
metrics.width >= image.width ||
parts.length - 1 === i if(word_width >= image.width){
){
// draw
if(metrics.width >= image.width){
// put text on next line since it overflows if(line != ""){
tmp_text_old = tmp_text_old.trim();
tmp_text = parts[i]; lines.push(line.trim());
line = "";
}
line_tmp = "";
// increment letters till we go over the limit
var letters = words[i].split("");
for(var k=0; k<letters.length; k++){
line_tmp += letters[k];
if(ctx.measureText(line_tmp).width >= image.width){
lines.push(line.trim());
line = letters[k];
line_tmp = letters[k];
}else{
line += letters[k];
}
}
// output remainder
lines.push(line);
line = "";
line_tmp = "";
text_to_add = tmp_text_old;
}else{ }else{
text_to_add = tmp_text; // wrap word normally
line_tmp += words[i] + " ";
if(ctx.measureText(line_tmp.trim()).width >= image.width){
// push line
lines.push(line.trim());
line = "";
line_tmp = words[i] + " ";
}
line += words[i] + " ";
}
}
// output remainder
if(line != ""){
lines.push(line);
line = "";
line_tmp = "";
}
var tmp_metrics = ctx.measureText(lines[i]);
var space = (tmp_metrics.actualBoundingBoxAscent + tmp_metrics.actualBoundingBoxDescent) / 3;
if(is_bottom){
height = image.height - space;
lines = lines.reverse();
}else{
height = space;
}
for(var i=0; i<lines.length; i++){
var metrics = ctx.measureText(lines[i]);
if(is_bottom){
if(i !== 0){
height -= space + metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
}
}else{
height += space + metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
} }
var textheight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; ctx.strokeText(lines[i], image.width / 2, height);
ctx.fillText(lines[i], image.width / 2, height);
ctx.strokeText(text_to_add, image.width / 2, textheight * line);
ctx.fillText(text_to_add, image.width / 2, textheight * line);
line++;
} }
} }