p5.js
p5.js copied to clipboard
Improve Text Measuring API
Increasing access
This would make the text API more redundant and accessible to those not familiar with OOP.
Most appropriate sub-area of p5.js?
- [ ] Accessibility
- [ ] Color
- [ ] Core/Environment/Rendering
- [ ] Data
- [ ] DOM
- [ ] Events
- [ ] Image
- [ ] IO
- [ ] Math
- [X] Typography
- [ ] Utilities
- [ ] WebGL
- [ ] Build process
- [ ] Unit testing
- [ ] Internationalization
- [ ] Friendly errors
- [ ] Other (specify if possible)
Feature enhancement details
Problem
Right now, there are 2 ways to measure text in p5.js: textWidth()
and Font.textBounds()
. These 2 calls sit in different parts of the API, and have slightly different behaviors across renderers and font types:
textWidth | textBounds | ||
---|---|---|---|
P2D | system font | ✅ | ❌ |
P2D | opentype | ✅ | ✅ |
WGL | system font | ❌ | ❌ |
WGL | opentype | ✅ | ✅ |
A couple additional issues:
-
textWidth
andtextBounds.w
show slightly different values (bounds often feels like the more precise one) -
textWidth
on WebGL + system font returns 0 with no error
Suggestion
This separation of functionality feels arbitrary and potentially confusing to new users. These small changes could be implemented as part of the 2.0 typography push or just as an improvement to the current API:
-
Make both functions available in the global scope and in p5.Font. This should ensure backwards compatibility.
- Add
textWidth
to p5.Font - Add
textBounds
to p5.Renderer
- Add
-
Allow
textBounds
to work with system fonts in P2D by using the native measureText