Translate [#1] - Rotate, Scale

(250,250) (500,250) (0,250) (250,0) (250,500) (500,0) (0,0) (0,500) (500,500) Ex. 1 Ex. 2 Ex. 3 Ex. 4

Translate

'Translate' is a useful but tricky attribute that can be used to simulate a 'move' for any shape.

We say 'simulate' because it is NOT the object that is moved, but the coordinate system. Thus a 'translate(50,50)' command moves the origin of the original coordinate system to (50,50). Setting any x or y coordinates after this 'translates' to increasing each value by 50. For example 'x=30 y=30' is now interpreted as 'x=80 y=80'. This results in the shape appearing to move to (80,80) in the original coordinate system.

The 1st example here uses 'translate' to 'move' a rectangle.

The original vertical rectangle (red) is first 'translated' (duplicated) and moved 30px right, then filled using a different color. Next the same rectangle is translated and moved 60px right, and filled using another color.

The code for the 3 vertical rectangles:

<rect x="100" y="20" width="10" height="100" style="fill:red"/>
<rect x="100" y="20" width="10" height="100" style="fill:green" transform="translate(30,0)"/>
<rect x="100" y="20" width="10" height="100" style="fill:blue" transform="translate(60,0)"/>

Note the 'x' coordinate increases for all 3 'translate' units in this case.

 

In the 2nd example we start with a horizontal red rectangle. Using the 'transform / translate' commands, we duplicate it twice and move it down, while changing the fill color each time.

Code to produce the 3 horizontal rectangles:
<rect x="320" y="20" width="100" height="10" style="fill:red"/>
<rect x="320" y="20" width="100" height="10" style="fill:green" transform="translate(0,20)"/>
<rect x="320" y="20" width="100" height="10" style="fill:blue" transform="translate(0,40)"/>

This time the 'y' coordinate is increased with each 'translate' method.

 

Rotate

Ex. 3 uses the 'rotate' method. Beginning with a vertical red rectangle, we rotate it counter-clock-wise about a common point, while filling it with a different color each time.

The 3 arguments to the 'rotate' method are: angle, x, y. In this case only the angle changes.

<rect x="100" y="250" width="10" height="100" style="fill:red"/>
<rect x="100" y="250" width="10" height="100" style="fill:green" transform="rotate(345,100,250)"/>
<rect x="100" y="250" width="10" height="100" style="fill:blue" transform="rotate(330,100,250)"/>
<rect x="100" y="250" width="10" height="100" style="fill:gold" transform="rotate(315,100,250)"/>
<rect x="100" y="250" width="10" height="100" style="fill:fuchsia" transform="rotate(300,100,250)"/>
<rect x="100" y="250" width="10" height="100" style="fill:aqua" transform="rotate(285,100,250)"/>
<rect x="100" y="250" width="10" height="100" style="fill:black" transform="rotate(270,100,250)"/>

 

Scale

Our 4th example scales (alters the size of) an existing rectangle, but the object could be a circle, ellipse, or another shape.

We first create a rectangle (red) 20x40px. Using the 'scale' method of the 'transform' function, we then scale the x or horizontal dimension by factors of 2, 3, and 5, while leaving y (vertical dimension) the same.

The very last rectangle is vertically scaled to 1/2 the height. Both x and y coordinates have to be used, but only the y coordinate had to be altered.

Note that the arguments to the 'scale' method are the (x,y) scaling factors.


<rect x="50" y="420" width="20" height="40" style="fill:red"/>
<rect x="50" y="420" width="20" height="40" rx="3" ry="3" style="fill:green" transform="scale(2,1)"/>
<rect x="50" y="420" width="20" height="40" style="fill:blue" transform="scale(3,1)"/>
<rect x="50" y="420" width="20" height="40" style="fill:black" transform="scale(5,1)"/>
<rect x="400" y="880" width="20" height="40" style="fill:indianred" transform="scale(1,.5)"/>

 

All done.