-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Allow textAlign(CENTER, CENTER) with max width but no max height #6060
Conversation
@davepagurek, please review it. |
@sawaisinghh @davepagurek can you clarify the specification for this, specifically for multi-line text? For the CENTER/CENTER case without height, this PR appears to center on the y-position, and then cut-off all the text above that (left image), rather than the previous behavior (right image) let txt = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.";
let x = 20, y = 50, w = 250;
function setup() {
createCanvas(300, 300);
background(245);
textAlign(CENTER, CENTER);
textFont("georgia", 12);
rect(x, y, w, height - y);
text(txt, x, y, w);
} |
@dhowe okay, I will try to resolve it. |
@dhowe, But, I think no text were visible in previous behavior specifically in multiline. |
Correct -- there was only a warning. The upper right image is from 1.4.2, which is (I think) the behavior you were trying to restore |
@dhowe yeah correct, according to issue, textAlign(CENTER, CENTER) without specifying height were working fine in 1.4.2, but not working in current version. Okay thanks for the review, I will try to fix it. |
@davepagurek @limzykenneth please review my pull request. |
Right, to clarify the expectations here, we want the For multiline behaviour, @dhowe's observation is correct, we'd want it to center the text about the middle y position and not cut off anything vertically and match its behaviour in 1.4.2. Here's a test case (p5 editor):
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on this! I tested it out and it looks like it meets those requirements in from my last comment 🙂 The functionality seems good to go! I just have some final code style comments before merging.
lib/addons/p5.sound.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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.
src/core/p5.Renderer.js
Outdated
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); |
There was a problem hiding this comment.
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
Line 40 in 4e231d9
this._textLeading = 15; |
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?
Line 163 in 4e231d9
this._setProperty('_textLeading', s * constants._DEFAULT_LEADMULT); |
@davepagurek I made the changes. Can you please review it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making those changes! One last very minor thing and then we're good to go!
src/core/p5.Renderer.js
Outdated
if (this._textBaseline === constants.BOTTOM || | ||
this._textBaseline === constants.CENTER) { | ||
// use rectHeight as an approximation for text height | ||
let rectHeight = p.textSize() * (this._textLeading); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last tiny change, we can take out the brackets around this._textLeading
now 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again, great work!
@davepagurek done 🙂 |
@all-contributors please add @sawaisinghh for bug, code |
I've put up a pull request to add @sawaisinghh! 🎉 |
Resolves #5933
Changes:
Now we can use textAlign(CENTER, CENTER) and then call text('something', x, y, w) without specifying a height parameter.
Screenshots of the change:
Before:
After:
PR Checklist
npm run lint
passes