I’m new to canvas, so pardon the beginner question. I’m trying to animate a square on a canvas that changes colors, rotates, and scales up and down (this is just for practice). I’m just trying to grasp all of the concepts and create an example that does all of them at once.
What I want it to do is scale up to a certain amount, then once it reaches that amount, it scales back down to the beginning amount (and then repeats). The same goes for the color (continue animating, then go backwards through the colors). How can I accomplish this?
Here’s an example of my code that I wrote:
You’ll notice a couple bugs:
-
Once the color gets to yellow, it stops animating.
-
The scaling obviously doesn’t work.
-
The rotation isn’t either clearing the canvas fast enough or something, because it’s showing a trail of positions.
Where can I find some resources on accomplishing this? Any help is appreciated.
There’s one small problem that’s causing all the other problems (well, apart from the yellow – I’m not experiencing that particular one):
That line does nothing. You need to call
ctx.restoreusing parentheses. Once you do that, the scaling works, andclearRect()will clear a non-transformed rectangle:And here’s the updated demo.