Translate & Scale [#3]

House 1 Original House 1.1 Original House 1.2 Original House 1.3 Original

Scale & Translate

We saw a brief use of scale on this page.

In those examples, it was used to resize an object, but it has another function.

Recall that the arguments to the scale transformation are the values of the x and y dimensions we wish to resize our object to.

If we make either (or both) x and y scale values negative, it has the effect of flipping the object in the dimension specified.

If a translate transformation is also used we may have to do some calculation to determine the x and y values.

This is because supplying negative values to scale works by flipping the objects across the origin (top left) of the SVG. This may result in the object being outside the SVG document (invisible).

House 1 is our original object, placed where it should be according to our code:

<g id="house1">
<circle cx="20" cy="10" r="1" fill="red" />
<circle cx="20" cy="115" r="1" fill="red"/>
<circle cx="120" cy="10" r="1" fill="red"/>
<circle cx="120" cy="115" r="1" fill="red"/>
<rect x="20" y="40" width="100" height="75" fill="orange"/>
<rect x="30" y="85" width="15" height="30" fill="blue"/>
<rect x="65" y="80" width="40" height="20" fill="ivory"/>
<polygon points="20 40,70 10,120 40" style="fill:#cc460e"/>
<text x=20 y=140>House 1</text>
<text x=20 y=160>Original</text>
</g>

To flip the house left-to-right (horizontally) we assign an x value of -1:

<g id="house1.1" transform="translate(350,0) scale(-1,1)">

Since we only assigned -1 to the x value, the house was only flipped in that dimension.

Note however that we also translated it 350 pixels to the right.

To flip it vertically, we set the y value to -1:

<g id="house1.2" transform="scale(1,-1) translate(0,-350)">

Finally, with House 1.3 we flip it horizontally and vertically:

<g id="house1.3" transform="scale(-1,-1) translate(-300,-350)">

In this case doing translate, then scale also works (but with a slight modification):

<g id="house1.3" transform=" translate(300,350) scale(-1,-1)">

Note that our x and y 'scale' values are both negative, but the 'translate' values flip.

In these examples, we have drawn a new object with each translation and scale. Next we will look at using def and manipulating that.

 

All done.