fitText()
is a function to make text strings fit in a given box (rectangle) in p5.js
libraray.
How To Add This To Your p5js Sketch:
- You can just copy paste the
fitText.js
and paste it in your p5.js sketch. - You can add this in your html file: https://cdn.jsdelivr.net/gh/sa-azeem/fitText@latest/fitText.js to reference
fitText.js
without copy-pasting the whole function.
How to call it:
fitText()
takes 5 arguments in this sequence:
- A string which you want to show on canvas (string)
x
position of the box (number)y
position of the box (number)width
of the box (number)height
of the box (number)
Depending on your rectMode()
, it then draws the text on your screen.
It also return [fontSize, lines]
which is the textSize()
used and lines
is just your string, but broken into lines so that it can fit inside the box.
Demo: https://editor.p5js.org/Abdul_Azeem/sketches/FNiQiPrvu