SVG4: Groups, Transforms, Re-Use, and Hierarchies

by Mike Gleicher on September 6, 2014

In the last tutorial we saw that we could make groups out of primitives, and that we can also apply transformations to elements. The examples just applied transforms to primitives, but, of course, we can apply transforms to groups.

<svg xmlns="http://www.w3.org/2000/svg" height="60" width="60">
<!-- two squares (the same, except that one gets translated) -->
<rect x="10" y="10" height="20" width="20" />
<rect x="10" y="10" height="20" width="20" transform="translate(30,0)" />

<!-- make a group - notice that any properties trickle down -->
<g transform="translate(0,30)" fill="#f00">
	<!-- these are the same two squares as above, just not in the group -->
	<rect x="10" y="10" height="20" width="20" />
	<!-- note that transforms compose/add not override -->
	<rect x="10" y="10" height="20" width="20" transform="translate(30,0)" />
</g>
</svg>

You might also notice in this example that I put some comments into my SVG code.

Make sure you understand what’s happening here: the lower right square got there by having two translations.

Re-Use

Since I am typing all of this SVG by hand, it would be nice to avoid repeating myself so much with each little square (since they can be the same and just transformed into place). SVG lets us define our own objects and re-use them.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="60" width="60">
<!-- two squares (the same, except that one gets translated) -->
<rect id="mysquare" x="10" y="10" height="20" width="20" />
<use xlink:href="#mysquare" transform="translate(30,0)" />
</svg>

The trick is that we give the object that we draw a name (here, I am calling it “my square”, and then we can re-use it later). Notice that in order for the web browser to understand how to use the link in the use statement, I needed to add a magic string to the svg tag. And also notice that when I refer to the tag, I have to use a # (a hash) to say that this is something in the current document.

Although the SVG is a little on the cryptic side (the

xmlns:xlink="http://www.w3.org/1999/xlink"

on the first line, the hashtag for the name in the use statement), this can save a lot of typing. Especially if I name a group.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="60" width="60">
<g id="mygroup">
	<rect id="mysquare" x="10" y="10" height="20" width="20" />
	<use xlink:href="#mysquare" transform="translate(30,0)" />
</g>
<use xlink:href="#mygroup" transform="translate(0,30)" fill="red" />
</svg>

Sometimes, you might want to make an object just to be used. Here, I’ll define a little square in a convenient place (at the origin), and then I can just move it all over the place. The trick is that I will put the square into a <defs> tag, which basically says “don’t draw the stuff inside this tag.” Things still can be named, and used later.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="70" width="70">
<defs>
	<rect id="mysquare" x="-10" y="-10" height="20" width="20" transform="rotate(45)" />
</defs>
<use xlink:href="#mysquare" transform="translate(20,20)" />
<use xlink:href="#mysquare" transform="translate(50,20)" />
<use xlink:href="#mysquare" transform="translate(20,50)" />
<use xlink:href="#mysquare" transform="translate(50,50)" />
</svg>

Notice how I placed my square with its center at the origin, so when I used it, I could place it by placing the center (much as I would place a circle).

Hierarchies

Of course, you can put groups within groups (within groups …)

The key is to remember that the coordinate systems combine by composition. To everything you do to the outermost (or “parent”) group, gets applied to all of the children. Of course, you can (and maybe should) think about it as the children start out with the parent’s coordinate system (which, the might change before drawing or passing it on to their children).

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="70" width="300">
<defs>
	<rect id="mysquare" x="-10" y="-10" height="20" width="20" transform="rotate(45)" />
	<g id="foursquare">
		<use xlink:href="#mysquare" transform="translate( 0, 0)" />
		<use xlink:href="#mysquare" transform="translate(30, 0)" />
		<use xlink:href="#mysquare" transform="translate( 0,30)" />
		<use xlink:href="#mysquare" transform="translate(30,30)" />
	</g>
</defs>
<g transform="translate(20,20)">
	<use xlink:href="#foursquare" />
	<g transform="translate(60,0)">
		<use xlink:href="#foursquare" />
		<g transform="translate(60,0)">
			<use xlink:href="#foursquare" />
			<g transform="translate(60,0)" stroke="red" stroke-width="3">
				<use xlink:href="#foursquare" />
			</g>
		</g>
	</g>
</g>
</svg>

It’s really worth making sure that you understand this example. Not for the SVG and the re-use, but more importantly for the nested groups and the hierarchical transforms. Notice how each group is offset 60 units in X relative to its parent (except for the outermost group that is translated 20 units from the edge).

Print Friendly, PDF & Email

Previous post:

Next post: