Skip to content

Commit 460f9bc

Browse files
committed
fully debugged text centering
1 parent b44ccc4 commit 460f9bc

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,13 @@ window.onload = function () {
432432
}
433433

434434
if (options.ui_message_message) {
435-
let bb = getTextBoundingBox(options.ui_message_text);
435+
let cc = getCharsCount(options.ui_message_text);
436436
if (options.ui_message_scale > 0) {
437-
let center = [Math.floor((columns - bb[0] * 4 * options.ui_message_scale) / 2), Math.floor((rows - bb[1] * 6 * options.ui_message_scale) / 2)];
437+
let bb = getTextBoundingBox(options.ui_message_text, options.ui_message_scale);
438+
let center = [Math.floor((columns - bb[0]) / 2), Math.floor((rows - bb[1]) / 2)];
438439
drawTextOnMask(options.ui_message_text, center[0] + options.ui_message_positionX, center[1] + options.ui_message_positionY, options.ui_message_scale);
439440
} else {
440-
let center = [Math.floor((columns - bb[0]) / 2), Math.floor((rows - bb[1]) / 2)];
441+
let center = [Math.floor((columns - cc[0]) / 2), Math.floor((rows - cc[1]) / 2)];
441442
drawTextOnMatrix(options.ui_message_text, center[0] + options.ui_message_positionX, center[1] + options.ui_message_positionY);
442443
}
443444
}
@@ -447,13 +448,10 @@ window.onload = function () {
447448
mask.fillStyle = "#FFF";
448449
lines = text.split("\\n");
449450

450-
var maxChars = 0;
451-
for (let i = 0; i < lines.length; i++)
452-
if (lines[i].length > maxChars)
453-
maxChars = lines[i].length;
451+
let cc = getCharsCount(text);
454452

455-
x = clamp(0, columns - maxChars, x);
456-
y = clamp(0, rows - lines.length, y);
453+
x = clamp(0, columns - cc[0], x);
454+
y = clamp(0, rows - cc[1], y);
457455

458456
for (let i = 0; i < lines.length; i++) {
459457

@@ -473,11 +471,17 @@ window.onload = function () {
473471
mask.font = options.ui_font_size * 5 * scale + "px neo-matrix";
474472
mask.fillStyle = "#FFF";
475473
lines = text.split("\\n");
474+
476475
for (let i = 0; i < lines.length; i++)
477-
mask.fillText(lines[i], options.ui_font_size * x - font_offset_x, options.ui_font_size * (y + (6 * (i + 1) * scale)) + font_offset_y);
476+
mask.fillText(lines[i], options.ui_font_size * x - font_offset_x, options.ui_font_size * (y + ((6 * (i + 1)) - 1) * scale) + font_offset_y);
477+
}
478+
479+
function getTextBoundingBox(text, scale) {
480+
let cc = getCharsCount(text);
481+
return [(cc[0] * 4 - 1) * scale, (cc[1] * 6 - 1) * scale];
478482
}
479483

480-
function getTextBoundingBox(text) {
484+
function getCharsCount(text) {
481485
lines = text.split("\\n");
482486
var maxChars = 0;
483487
for (let i = 0; i < lines.length; i++)

0 commit comments

Comments
 (0)