The canvas rotate method tips tricks and related topics
The canvas rotate method can be useful for doing quick on the fly rotations, but doing so will cost some overhead compared to having sprite sheets where the rotations have been worked out before hand.
The canvas rotate method is often used in conjunction with other canvas methods such as the translate method, as well as save and restore. There is also of course the context methods that are used to render to the canvas once a rotation has been preformed.
Still if I just want to quickly rotate something in canvas there is the rotate method in the 2d drawing context, so lets look at some examples of this as well as related topics such as the canvas translate method, save and restore, and many others.
1 - A Basic canvas rotate example
So lets start off with a basic example of the canvas rotate method just to get the basics of this worked out. Here I have the external basic.js file, in which I start out by getting a reference to the canvas element I have in my html file, and the 2d drawing context of that element just as with any other canvas project.
I then worked out a simple draw method that just strokes a box to the canvas, so that I have something to draw when I rotate the canvas. When I call draw box method I pass a reference to the context, and an additional object that passes the dimensions of the box I want to draw. By default It will place the box centered around the top left corner of the canvas, and this is intentional when it comes to using the canvas translate method along with the canvas rotate method as that is where I want to draw actually.
|
|
I then paint a black background for the whole canvas followed by using the drawBox method by itself so I have something to compare to. After that I use the save method to store the current state of the drawing context, so that I can make changes to the translation and rotating of the canvas and then restore back to normal. After I save the state of the context I then translate the canvas to the point that I want the center of the box to be, and then use the canvas rotate method to rotate the canvas by passing a radian value that I want to rotate by. Once That is done I can then restore the context, and that is it I have preformed a rotation with the canvas rotate method.
So now that I jave the javaScript for this basic canvas rotate example together lets take a quick look at the html. Here I am using a hard coded canvas tag in my HTML file, and am using a single script tag to link to an external javaScript file where I will have javaScript code of this basic canvas rotate example outlined above.
|
|
So that is the basic typical idea at least when it comes to using the rotate method. I have something I want to draw, I save the context, and then translate to the point I want to draw at, where I then rotate the canvas. I then have to account for the size of what it is that I want to draw, subtracting half the width and height from the position that I then draw at. Once done I then restore the context back to normal so that I can then draw other things with the previous canvas context state.
2 - Rotation point and render point example
In this section I will be going over a canvas rotate example that involves changing the point at which an object rotates.
Just about all canvas rotate examples involve saving the context, translating to a point in the canvas matrix, and rotating the canvas around that point that the canvas was translated to. At that point the object is then rendered at a point that is a negative value that is half the width and height of the object. However it does not always have to be that way, there are in effect two points of interest here, the point that I am translating to, and another point that is relative to that point.
|
|
This example results in a box that is rotating around the center point that I translated to, but the point that is at the center of the box ventures away from that point of translation. It can be fun to play around with these things a little, but for the most part I just need to keep things centered at the translate point.
3 - Making a before hand rendered sprite sheet with the canvas rotate method
using the canvas rotate method can eat up a lot of background resources in order to preform the rotation. If it is just one or two sprites in a project it is not a big deal, but if there are a lot of them the loss in frame rate can add up. One way to address this word be to use a pre rendered sprite sheet. This can be loaded as an external image file that was created separately, or it can be another canvas element, where all the possible rotations have been preformed once.
3.1 - The draw methods
Here I have the draw methods worked out. I have a draw box method that will just draw a given box object that will be used in my make sprite sheet method later. Also once I have my sprite sheet I have a draw method that can be passed that sprite sheet object and render a given cell index from that sheet.
|
|
3.2 - The make box sheet method
The make box sheet method in this section is used by just calling it and giving a cell size, and cell count. The returned result then is an object that contains a canvas element that can then be used with the draw image 2d context method.
|
|
3.3 - Lets see it in action
Now to test this all out to see if it works and sure enough it does. However in order to get a real idea as to how much this helps I will need to make a project that is far more complex with many instances of sprites that make use of this sprite sheet.
|
|
4 - Conclusion
The canvas rotate method works okay for on the fly rotations, but it might not always be a good idea to rely on it all the time for all projects. It can cost a fair amount of system resources to preform a rotation, and if you have a lot of display objects all at once on the canvas it can really slow things down on clients that do not have a great deal of CPU overhead to work with.
So a better solution sometimes might be to create a sprite sheet with all the rotations preformed before hand and then use that as a way to change the rotation of display objects. Other solutions that might involve collections of points might be just as expensive if not more expensive depending on the number of points.