processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.73k stars 3.34k forks source link

Text Line Number function #7401

Open emilepainchaud opened 2 days ago

emilepainchaud commented 2 days ago

Increasing access

I think that would make p5 more accessible for designers like me who are used to work with paragraphs containing multiple lines!

Most appropriate sub-area of p5.js?

Feature request details

Hi! I was trying to create a textbox width a rectangle that surrounds it. When my string is short, I can simply use the textWidth function to get the width, but my string is longer and has returns, it gets bit complicated to get both the width and the height of the text displayed on screen.

So I wrote this function to get the number of lines (so that I can use that number to multiply the height for one line of text by the number of lines so I have my nice rectangle surrounding my textbox) :

function getLineNb(txt, maxWidth) { let totalWidth = 0; let lineNb = 1; for (let i = 0; i < txt.length; i++) { totalWidth = totalWidth + textWidth(txt[i]); if (totalWidth >= maxWidth || txt[i] === "\n") { lineNb = lineNb + 1; totalWidth = textWidth(txt[i]); } } return lineNb; }

It doesn't output the right number when the maxWidth is very small though, so it should be reworked.

Maybe this could be a new feature inside P5!

welcome[bot] commented 2 days ago

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

Deathnote45 commented 1 day ago

Hi, Can I contribute on this issue??

dhowe commented 18 hours ago

Can you provide an example sketch of what you are hoping to do, so we can see the functionality ? thanks