SVG Patterns — Build More Complex Patterns With Images, Attributes, And Nesting - Vanseo Design |
SVG Patterns — Build More Complex Patterns With Images, Attributes, And Nesting Posted: 18 May 2015 05:30 AM PDT Patterns for web design elements are usually created with images. While some patterns can be created using CSS alone, most involve images of some kind. Fortunately SVG makes it easy to create simple and more complex patterns that can scale with your design and likely require fewer http requests.
Last week I began a look at the SVG Today I want to continue and talk about a few more pattern attributes, one of which will help us build up more complex patterns by referencing one pattern inside another. First a quick review to show you something I skipped last week. Using an Image as the PatternIn all of the examples last week I used SVG shapes to create the pattern. I did, however, mention that you could also use images to create the pattern. To show you how to use an image as an SVG pattern I created this one in Pixelmator. It's 50px wide, 50px high, and you can see it's a couple of green squares that will form a checkerboard pattern. Here's how we can use it as a pattern.
Here I used an Here's the resulting SVG, which you can see is the image repeated both horizontally and vertically to fill the rectangle. Now let's look at the different attributes you can add to the Attributes of the |
1 2 3 4 5 6 7 8 9 | <svg width="660" height="220"> <defs> <pattern id="pattern-2" x="0" y="0" width="0.25" height="0.25" patternContentUnits="objectBoundingBox"> <circle cx=".125" cy=".125" r="0.125" stroke="none" fill="#393" /> </pattern> </defs> <rect x="10" y="10" width="600" height="200" stroke="#630" stroke-width="5px" fill="url(#pattern-2)" /> </svg> |
The code looks similar, but because the content scales, you need to reduce the coordinates and the radius of the circle below 1.0. Here I used 0.125, which means the width and height of the circle will be 0.25 units.
Setting both width and height of the pattern to the same 0.25 or 25% ensures that the pattern will repeat by placing the next circle adjacent to the previous one.
Here's the result.
Notice that the circles are no longer circles, but elongated ovals instead. Because the circle, defined by a single radius, is allowed to scale inside a rectangle with different x and y dimensions, the circle will distort in one direction unless the x and y of the rectangle are the same.
The xlink:href and patternTransform attributes
Another attribute that should be familiar if you've been following along with this series is the xlink:href attribute. You can use it to reference other patterns in the SVG document.
You can also transform patterns with the patternTransform attribute. Let's look at an example using both xlink:href and patternTransform.
Here I created two patterns. The first (id="pattern") is the same pattern of green circles we used last week and in the previous example here.
The second pattern (id="pattern-transformed") refers to the first pattern through it's xlink:href attribute. It inherits everything defined on the first pattern. I added a 10 degree rotation to it through the patternTransform attribute as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <svg width="660" height="480"> <defs> <pattern id="pattern" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse"> <circle cx="10" cy="10" r="10" stroke="none" fill="#393" /> </pattern> </defs> <defs> <pattern id="pattern-transformed" xlink:href="#pattern" patternTransform="rotate(10)"> </pattern> </defs> <rect x="10" y="10" width="600" height="200" stroke="#630" stroke-width="5px" fill="url(#pattern)" /> <rect x="10" y="250" width="600" height="200" stroke="#630" stroke-width="5px" fill="url(#pattern-transformed)" /> </svg> |
Each pattern is referenced by a different rectangle and here's the resulting SVG graphic showing both of them.
You can see that even though the graphic element was defined only for the first pattern, the second pattern inherited everything from it and added a transform. It's a nice way to set up variations of the same pattern.
Nested Patterns
You can also nest patterns by referencing one inside another. Here I created two patterns, an inner pattern and and outer pattern. The inner pattern is a series of repeating green squares and the outer pattern is a series of repeating circles with a dark red stroke and the inner pattern as a fill.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <svg width="660" height="220"> <defs> <pattern id="pattern-inner" x="2" y="2" width="6" height="6" patternUnits="userSpaceOnUse"> <rect x="0" y="0" width="4" height="4" stroke="none" fill="#6a6" /> </pattern> <pattern id="pattern-outer" x="13" y="13" width="50" height="50" patternUnits="userSpaceOnUse"> <circle cx="22" cy="22" r="20" stroke="#900" stroke-width="4px" fill="url(#inner-pattern)" /> </pattern> </defs> <rect x="10" y="10" width="600" height="200" stroke="#630" fill="url(#outer-pattern)" /> </svg> |
Here's the resulting SVG, which as you can see is a repeating pattern of circles (outer pattern) filled with a repeating pattern of squares (inner pattern).
Here's the example again, this time without the stroke on the outer pattern. Hopefully this second example of nested patterns is giving you some ideas for creative ways to nest patterns.
Closing Thoughts
Patterns are another element you can add to your SVG toolbox for defining a graphic in one location and referencing and using it in another. If you've followed along with the other reusable elements in the series, patterns should be fairly easy to understand and use.
While they're similar to other reusable SVG elements, patterns might be more fun to work with as you add them to elements and see the results. Perhaps that's just me, though the ability to nest patterns can be used for some very creative results.
Next week I want to continue with another element that's similar to a pattern and that you can add to the fill or stroke of graphic elements. I'll talk about how to create and work with gradients in SVG.
Download a free sample from my book Design Fundamentals.
The post SVG Patterns — Build More Complex Patterns With Images, Attributes, And Nesting 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 |