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 3 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
2 changes: 1 addition & 1 deletion lib/addons/p5.sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -10618,7 +10618,7 @@ var soundRecorder_ac = main.audiocontext;
* // send result to soundFile
* recorder.stop();
*
* text('Done! Tap to play and download', width/2, height/2, width - 20);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't edit p5.sound.js here, as it comes from builds in https://github.com/processing/p5.js-sound that we periodically pull from. If we want to make a change to it, we'd have to make it in that repo for it to not get overwritten in the next build.

* text('Done! Tap to play and download', width/2, height/2, width - 100);
* state++;
* }
*
Expand Down
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 || 1.25);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this._textLeading is always initialized here

this._textLeading = 15;
and also set when the user calls textSize(), so I think it's safe to just use this._textLeading here.

If there's a case I'm not considering though, and we need the || 1.25, can we replace 1.25 with constants._DEFAULT_LEADMULT like we use here?

this._setProperty('_textLeading', s * constants._DEFAULT_LEADMULT);

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