P5.js: Inaccurate calculation of vertical textAlign position

Created on 26 Mar 2020  路  7Comments  路  Source: processing/p5.js

Most appropriate sub-area of p5.js?

  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [x] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Other (specify if possible)

Details about the bug:

  • p5.js version: 1.0.0 & in web editor
  • Web browser and version: 80.0.3987.149
  • Operating System: Windows 10, MacOS Catalina

    - Steps to reproduce this: set CENTER or BOTTOM vertical textAlign, and display a word that does not fit into the text box

In that case vertical aligning calculates wrong

Annotation 2020-03-26 122435

here is the example:

const s = 'Theeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelazydog.';

function setup() {
  createCanvas(200, 100);
}

function draw() {
  background(220);
  textAlign(CENTER, CENTER)
  textSize(22);
  text(s, 0, 0, 200, 100);
}
bug

Most helpful comment

That could still cause issues if the longer word comes later in a sentence. Like in this sketch https://editor.p5js.org/akshay_07cf/sketches/bhpN46kAX

What I thought was to keep a track of the number of words added so far in testLine, I saw it resets every time a split happens. So if testLine only has one word i.e the current line only has one word, don't adjust the height.

All 7 comments

Welcome! 馃憢 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.

From what I understand this seems to be an issue in the library code here.

Since the textWidth (the actual width of the text ) turns out to be more than the provided width, the intention here was to increase the totalHeight of the line and to break up the text into multiple lines as seen in this sketch.
https://editor.p5js.org/akshay_07cf/sketches/ae-zLrbff

But since here we just have one big word "Theeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelazydog", it is not split into two lines, but the height is still adjusted. This causes the issue.

Seems like it. Maybe we can at least check if n > 1 before increasing totalHeight? 馃
Or if n == 1 split it into two parts

That could still cause issues if the longer word comes later in a sentence. Like in this sketch https://editor.p5js.org/akshay_07cf/sketches/bhpN46kAX

What I thought was to keep a track of the number of words added so far in testLine, I saw it resets every time a split happens. So if testLine only has one word i.e the current line only has one word, don't adjust the height.

@lmccart @limzykenneth I will go ahead and patch this?

@akshay-99 Sounds good, do go ahead. I'll assign the issue to you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ScottGrogin picture ScottGrogin  路  36Comments

shiffman picture shiffman  路  56Comments

lmccart picture lmccart  路  49Comments

ofZach picture ofZach  路  29Comments

Ongshu777 picture Ongshu777  路  24Comments