The easiest way to do it would be to make the text a sprite, but there is a kinda funky way around it. The underlying library that we use (p5.js) has a lot of functions that we don’t directly expose, but which you can use if you know about them. The two that we’d need for this are rotate()
, which will rotate every command that follows it around the origin of the canvis and translate()
which will change the origin of the canvas. To accomplish text that is rotated 45 degrees around the center of the canvas, we’d need to:
- Move the origin of the canvas to the center with
translate(200, 200)
- Call
rotate(45)
to rotate subsequent commands - Set
textAlign(CENTER, CENTER)
so that the text can rotate around center - Use
text()
to put your text on the screen - Reset the canvas rotation with
rotate(-45)
to make sure we don’t rotate the next things we put on screen - Reset the canvas origin with
translate(-200, -200)
so any additional things we put on screen are located properly
Note that you could skip steps 5 and 6 if you saved the rotated stuff for the very end of the draw loop, but it’s still good practice to put things back where you found them
All that said, probably easiest to just make the sprite. If you want to check out an example of how this all works I created a remix of your code here https://studio.code.org/projects/gamelab/wWoId4JbPqEFCbp_UBzlOg/view