textBounds() -- can it be used on the default font?

If I want to make a background just behind some text that I draw on the canvas, I could draw a box behind the text, and I would get the box’s position and size with the textBounds() method.

Can this be done with the default font? The example at http://p5js.org/reference/#/p5.Font/textBounds shows this done only with a loaded font. I feel that this should be easy and obvious to do without having to load a font. What if I don’t want to load a font? I suppose the bigger question is if it is possible to call methods on default objects, or do similar things to the default environment?

3 Likes

After some digging through the source code, I would just suggest loading a font because I didn’t see an easy way to extract the default font. The textBounds() function only works on a p5.Font object so you would need to find the default font. It appears however that the way the default text works is it’s never a p5.Font object. To setup the text some hidden variables are set in p5.Renderer.

It appears that the default font is just the browser’s default ‘sans-serif’ font which I think is Helvetica or Arial depending on your operating system so you could just use one of those if the look is important.

I could be wrong I only spent about 15 minutes looking at the source code so I’d be interested to hear if someone else knows how to do this.

4 Likes