Is there a code to rotate text in Gamelab, or do we have to make the text a Sprite to rotate it? Here is the code if anyone wants to take a look.
Thanks!
Is there a code to rotate text in Gamelab, or do we have to make the text a Sprite to rotate it? Here is the code if anyone wants to take a look.
Thanks!
Great question. I think you would have to make it a Sprite to rotate it? I will ask the rest of the team.
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:
translate(200, 200)
rotate(45)
to rotate subsequent commandstextAlign(CENTER, CENTER)
so that the text can rotate around centertext()
to put your text on the screenrotate(-45)
to make sure we don’t rotate the next things we put on screentranslate(-200, -200)
so any additional things we put on screen are located properlyNote 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
That was the only thing I could come up with, but I knew there were many others out there with a lot more experience, and making the text a Sprite was not easy through the GameLab animation tab very easily. Thanks for helping!!!
Thank you! I shared with the student to give him the remix, but also the explanation. I appreciate your help! I figured the easiest way was with a Sprite, but I also wanted to give him another explanation so that he can be exposed to more options and codes. I’m really loving seeing how creative and persistent my students are!
This is the sort of thing that will really make you appreciate functions with parameters when you get them.