Skip to content

Commit 1348bf9

Browse files
author
Lauren McCarthy
committed
Merge pull request #1369 from kaganjd/master
fix for vertically centering multiline text (issue #1316)
2 parents eaa0540 + 3880ba6 commit 1348bf9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/core/p5.Renderer2D.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,9 +1126,12 @@ p5.Renderer2D.prototype.text = function (str, x, y, maxWidth, maxHeight) {
11261126
}
11271127
}
11281128
else {
1129+
//offset to account for centering multiple lines of text
1130+
var offset = ((cars.length)*0.5-0.5)*p.textLeading();
1131+
11291132
for (jj = 0; jj < cars.length; jj++) {
11301133

1131-
this._renderText(p, cars[jj], x, y, finalMaxHeight);
1134+
this._renderText(p, cars[jj], x, y-offset, finalMaxHeight);
11321135
y += p.textLeading();
11331136
}
11341137
}

src/typography/attributes.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@
1111
var p5 = require('../core/core');
1212

1313
/**
14-
* Sets the current alignment for drawing text. The parameters LEFT, CENTER,
15-
* and RIGHT set the alignment of text in relation to the values for
16-
* the x and y parameters of the text() function.
14+
* Sets the current alignment for drawing text. Accepts two
15+
* arguments: horizAlign (LEFT, CENTER, or RIGHT) and
16+
* vertAlign (TOP, BOTTOM, CENTER, or BASELINE).
17+
*
18+
* The horizAlign parameter is in reference to the x value
19+
* of the text() function, while the vertAlign parameter is
20+
* in reference to the y value.
21+
*
22+
* So if you write textAlign(LEFT), you are aligning the left
23+
* edge of your text to the x value you give in text(). If you
24+
* write textAlign(RIGHT, TOP), you are aligning the right edge
25+
* of your text to the x value and the top of edge of the text
26+
* to the y value.
1727
*
1828
* @method textAlign
1929
* @param {Number/Constant} horizAlign horizontal alignment, either LEFT,

0 commit comments

Comments
 (0)