HTML and CSS Reference
In-Depth Information
Figure 2-12. Simple rotation transformation
We get a result this time, but it will probably differ from what you expect. The red box
is rotated, but it looks like the canvas was rotated with it. The entire canvas did not
rotate, only the portion drawn after the context.rotate() function was called. So, why
did our square both rotate and move off to the left of the screen? The origin of the
rotation was set at the “nontranslated” 0,0 position, resulting in the square rotating
from the top left of the entire canvas.
Example 2-8 offers a slightly different scenario: draw a black box first, then set the
rotation transform, and finally draw the red box again. See the results in Figure 2-13 .
Example 2-8. Rotation and the Canvas state
function drawScreen() {
//draw black square
context.fillStyle = "black";
//now draw a red square
var angleInRadians = 45 * Math.PI / 180;
context.fillStyle = "red";
Figure 2-13. Rotation and the Canvas state
The small black square was unaffected by the rotation, so you can see that only the
shapes drawn after the context.rotate() function was called were affected.
Search WWH ::

Custom Search