HTML and CSS Reference
In-Depth Information
context.setTransform(1,0,0,1,0,0);
var angleInRadians = 75 * Math.PI / 180;
var x = 100;
var y = 100;
var width = 40;
var height = 40;
context.translate(x+.5*width, y+.5*height);
context.rotate(angleInRadians);
context.fillStyle = "red";
context.fillRect(-.5*width,-.5*height , width, height);
context.setTransform(1,0,0,1,0,0);
var angleInRadians = 90 * Math.PI / 180;
var x = 150;
var y = 100;
var width = 40;
var height = 40;
context.translate(x+.5*width, y+.5*height);
context.rotate(angleInRadians);
context.fillStyle = "red";
context.fillRect(-.5*width,-.5*height , width, height);
context.setTransform(1,0,0,1,0,0);
var angleInRadians = 120 * Math.PI / 180;
var x = 200;
var y = 100;
var width = 40;
var height = 40;
context.translate(x+.5*width, y+.5*height);
context.rotate(angleInRadians);
context.fillStyle = "red";
context.fillRect(-.5*width,-.5*height , width, height);
}
Figure 2-17. Multiple rotated squares
Next, we will examine scale transformations.
Scale Transformations
The context.scale() function takes in two parameters: the first is the scale attribute
for the x-axis, and the second is the scale attribute for the y-axis. The value 1 is the
normal scale for an object. Therefore, if we want to double an object's size, we can set
both values to 2 . Using the code below in drawScreen() produces the red square shown
in Figure 2-18 :
 
Search WWH ::




Custom Search