How To Manipulate SVG Text - Vanseo Design |
Posted: 27 Oct 2015 05:30 AM PDT Last week I showed you how to create SVG text using the
You aren't limited to the values of x and y attributes when working with SVG text. There are a few more attributes you can add to the The dx and dy AttributesLet's start with the dx and dy attributes which are similar to x and y, except each represents a length relative to the previous character instead of an absolute position relative to the viewport. Just like x and y, dx and dy take a list of lengths instead of a single length, though you can set a list of one length. Let's pick up the example from last week and work in some values for dx and dy.
Here I set both x and y to 0 and then used a list of lengths on dx and dy. Notice as the value increases, the distance to the next character also increases. If you want the same space between characters you want to give dx and dy the same value as the previous value in the list. One thing you may or may not have noticed is the lengths and the spacings between characters don't quite add up in the list above. If you map each length to a character, you would expect the "i" and "s" in "is" to be separated by 60px, which is the last value set. The reason they aren't, is because those 60px separate the "i" from the space between the the words "This" and "is." Spaces count as characters. Rotating SVG CharactersYou can also rotate characters using the rotate attribute, which takes a list of numbers. Each number again represents a specific character.
Here I left x and y at 0 and added a list of numbers to the rotate attribute. The "T" will be rotated 0 degrees, the "H" will be rotated 45 degrees, and so on until we reach the end of the list and all remaining characters will be rotated 0 degrees. Similar to the previous example, spaces count. Here the space between "This" and "is" is being rotated 180 degrees, which is why I added a 0 to the end of the list. Otherwise the rest of the characters would also be rotated 180 degrees and be upside down. Again it's the individual characters that are being rotated and not the entire string of text. To rotate the entire string you would use a transform. The textLength AttributeThe next attribute you can add is textLength which takes a single length as a value. The textLength attribute lets you set the length of the text to a specific amount regardless of the size of the container.
Here I set textLength to the same width as the viewport so the text stretches from end to end. Note that the last character doesn't touch the right edge. That's because it's still inside an EM box. The right edge of the EM box is what touches the right edge of the viewport. The characters are automatically spaced out so the text string fills the space. It's like justifying the content, except you can set the width on the fly. You can also squish the characters together if you'd like, by setting a length smaller than what's necessary to display them all.
Naturally this isn't advisable with text you want people to read, but it could be used to create some interesting effects when the text doesn't need to be read. One thing you may have noticed is that it was the space between characters that was adjusted with textLength, but the characters themselves remained the same size. You can change that with the last attribute specific to the textElement. The lengthAdjust AttributeThe lengthAdjust attribute takes two values (spacing or spacingAndGlyphs), which determine what gets stretched or squished. Of the two values, spacing is the default, which is why the previous examples used the space between characters to adjust to the the length specified. Here's a previous example with a change to lengthAdjust.
The characters are now being stretched in addition to the spaces between them. I mentioned earlier that because SVG text is rendered as a graphic element that you can apply things like strokes and fills , patterns and gradients. Any attribute you can add to SVG elements in general can also be applied to SVG text. To prove that, here's an example where I've changed the fill to blue, added a red stroke, and doubled the stroke-width to 2.
Between this and the last post, I've shown quite a few ways to manipulate text and we still have more text related elements to get to. Let's stop here today, though. Closing ThoughtsI hope you agree that SVG text is relatively easy to work with. Instead of a Even limiting ourselves to the attributes of the Next week I'll pick things up with the Download a free sample from my book Design Fundamentals. The post How To Manipulate SVG Text 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 |