Imagine, for a minute, that you want to rotate a red square around its centre… on a web page. Following on from my previous post about games using HTML5 / JS this post details how to do such a thing.
Before drawing a rectangle, rotating it, or anything else, you need a context:
var canvas = document.getElementById("mainCanvas"); var ctx = canvas.getContext("2d");
Now you have a context, you can do things like clear the canvas; for example:
ctx.clearRect(0, 0, windowWidth, windowHeight);
In HTML5, you have three methods that will be of use, and the first, and probably most important, is fillRect. It is impossible to rotate a square around its centre without a square. The syntax for fillRect is probably as you would expect:
ctx.fillRect(x, y, width, height);
The syntax for rotation is this:
ctx.rotate(rotationDegree \* Math.PI / 180);
Whilst I may, during my school years, have been able to explain the sum above - I just copied it from the internet. Given the number of places where is looks exactly alike, I would guess that I’m not the first person to do that.
Just using the three lines above will give you a rotating rectangle; however, the rotation axis will be 0, 0. It took me a while to understand exactly how this works, but the key is `translate`.
To me, this function is completely counter-intuitive. What it does it to offset the centre of the context by the parameters given. If the initial centre is 0, 0 (which it is by default), the following line will make it 10, 10:
The centre of the context is 10, 10; if I call it a second time:
The centre of the context is now 20, 20! There are two ways to reset the offset - you can simply negate the offset (by calling it with negative values), or you can call ctx.save() before the change, and ctx.restore() afterwards.
Putting it all together
So, what does all this look like in a single coherent piece of code:
var canvas = document.getElementById("mainCanvas"); var ctx = canvas.getContext("2d"); ctx.clearRect(0, 0, windowWidth, windowHeight); var halfWidth = (iconWidth / 2); var halfHeight = (iconHeight / 2); var centreX = x - halfWidth; var centreY = y - halfHeight; ctx.fillStyle = "#FF0000"; ctx.translate(centreX, centreY); ctx.rotate(rotationDegree \* Math.PI / 180); ctx.fillRect(-halfWidth, -halfHeight, iconWidth, iconHeight); ctx.translate(-centreX, -centreY);
The key part to note here is the call to fillRect. Because the translate has now set the centre to be the centre of the drawn image, the image needs to be positioned at -(image width / 2).
… and you, too can have a spinning red rectangle on your screen.