How To Reuse SVG Text With The tref Element - Vanseo Design |
How To Reuse SVG Text With The tref Element Posted: 10 Nov 2015 05:30 AM PST Writing SVG code once and being able to use it in multiple places helps you write more modular code and it helps make maintenance easier. SVG makes it easy to reuse text through the tref element.
Last week I walked you through the tspan element and showed how you could use it to style and position lines of text independent from one another. I presented some examples using the positioning attributes x, y, dx, and dy and mentioned there were a couple more attributes to look at. Let's pick things up with the two remaining attributes and then I'll talk about reusing SVG text with the tref element. Please note that while the tref element is part of the SVG 1.1 spec, it’s being removed from the SVG 2.0 spec. I included in this series, in case you run across it somewhere. The textLength and lengthAdjust AttributesThe two remaining attributes are textLength and lengthAdjust, and either or both can be added directly to any tspan. As I've done with most of the examples in this series. I created an SVG element, defined the size of the viewport, and styled it so we can see an outline of the viewport and any text that renders outside of it. I also bumped up the font-size.
I positioned the two tspans so one sits below the other and styled the second with a red fill. I also added both textLength and lengthAdjust attributes to the However, adding the attributes to the first tspan does what you would expect. It stretches both the space and glyphs as the next example shows.
This suggests that unlike the rotate attribute, which we saw last week, textLength and lengthAdjust set on the text element don't propagate to the tspans. One exception I found while playing around is if you add the textLength and/or lengthAdjust to the text element and haven't set x and y values for the first tspan, the textLength and lengthAdjust do seem to propagate. I'm not sure if this is an error or the intended behavior, but I wanted to mention it in case it happens and you're wondering why.
In this example I added the text "hello" directly to the text element and before the first tspan and I added our two attributes directly on the text element. I also positioned the text element and since I didn't give any x or y coordinate to the first tspan it gets positioned just after the "hello" text. The word "hello" is stretched as you would expect, but so is the SVG 1 text from the first tspan. It seems setting textLength and lengthAdjust stretch the line they're applied to regardless of which element they're set on. If you take away the coordinates on the second tspan, it will also become part of the stretched line of SVG text. Again I'm not sure if this is the expected behavior, a bug, or if I'm doing something wrong, but I thought I'd point it out in case you run across it. The tref ElementNote: The tref element is part of the SVG 1.1 spec, but it’s being removed from the SVG 2.0 spec. What follows may or not work by the time you read this. I’ll be using images to show the results of the examples. Earlier in the year during the previous round of this SVG series I showed how you could define SVG code in one location and then use and reuse it in other parts of your SVG document. The tref element works the same way. It lets you define reference text inside a Or you can just follow along with the example in which I created a To reference the text you use the xlink:href attribute inside a
Here I referenced the SVG 1 text inside a tref element while SVG 2 is inside a tspan that I've positioned beneath SVG 1. I could be more efficient and set the defs to contain the text SVG and reference it in both tspans adding the 1 or 2 at the end.
Here I added the 1 and 2 on each tspan, but have the SVG text as a reference inside trefs in each. I also changed the id to referenced–2 so that it doesn't conflict with the previous example. In these examples I placed the tref inside tspans, but the tref could also have been placed directly inside the text element. The referenced text will appear wherever the tref is added. Closing ThoughtsIf you understand how to work with You define the text you want to reuse inside There's still plenty more to cover in regards to SVG text. Over the next two week I'll talk about text layout features that are supported by SVG and show you some properties to help align SVG text in both the horizontal and vertical directions. Download a free sample from my book Design Fundamentals. The post How To Reuse SVG Text With The tref Element appeared first on Vanseo Design. This posting includes an audio/video/photo media file: Download Now |
You are subscribed to email updates from Vanseo Design. To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |