Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow textAlign(CENTER, CENTER) with max width but no max height #6060

Merged
merged 6 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,12 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
}
} else {
// no text-height specified, show warning for BOTTOM / CENTER
if (this._textBaseline === constants.BOTTOM) {
return console.warn(
'textAlign(*, BOTTOM) requires x, y, width and height'
);
}
if (this._textBaseline === constants.CENTER) {
return console.warn(
'textAlign(*, CENTER) requires x, y, width and height'
);
if (this._textBaseline === constants.BOTTOM ||
this._textBaseline === constants.CENTER) {
// use rectHeight as an approximation for text height
let rectHeight = p.textSize() * this._textLeading;
finalMinHeight = y - rectHeight / 2;
finalMaxHeight = y + rectHeight / 2;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/typography/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ import p5 from '../core/main';
* textAlign(CENTER, BASELINE);
* text('BASELINE', 0, 62, width);
*
* line(0, 87, width, 87);
* line(0, 97, width, 97);
* textAlign(CENTER, BOTTOM);
* text('BOTTOM', 0, 87, width);
* text('BOTTOM', 0, 97, width);
*
* describe(`The names of the four vertical alignments (TOP, CENTER, BASELINE,
* and BOTTOM) rendered each showing that alignment's placement relative to a
Expand Down