P5 text() and console.log treat multiline strings differently

https://github.com/processing/p5.js/blob/main/src/core/p5.Renderer.js#L233 has the code for the text() function which, potentially, does a lot of manipulation to the text to support word wrapping. I’m not sure if it somehow detects raw text to avoid that or not.

I would suggest starting with the simple example you posted that you know works and then gradually add the addition features you want in your full version, testing as you go, to see what causes the problem. At this point, I have no other ideas.

Edit: my final guess would be that performing string manipulation on a raw text string causes it to lose its “rawness”, reverting to a normal text string which p5 then tries to word-wrap. I leave it to you to search the javascript docs on the web about raw text and how to preserve it.