Tuesday 14 June 2016

SVG Masking Examples — Paths, Gradients, And Patterns - Vanseo Design

SVG Masking Examples — Paths, Gradients, And Patterns - Vanseo Design


SVG Masking Examples — Paths, Gradients, And Patterns

Posted: 14 Jun 2016 05:30 AM PDT

You can use any shape, path, or text to create an SVG mask. You can combine any or all of them as the masking content. You can also create some interesting masks using gradient or pattern fills.

As I did for clipping paths, I've tried to keep the examples so far in the masking part of this series simple by using the same rectangular mask on top of the same circular graphic as much as possible. As was the case with clipping paths , you can do more  with masks than I've shown to this point.

I thought I'd walk you through some different examples today and next week showing some other options for the mask content and the elements being masked. Hopefully it will give you some ideas for how you might use masks in your work.

Paths as Masks

First here's what is probably a familiar looking green circle for anyone who's been following this series. I promise we'll mask it with more than a single and simple rectangle today.

To prove that you can create masks with things other than rectangles, I'll create one using a path I borrowed from the clipping path examples post.

The path is inside a mask element to which I've given an id as well as values for x, y, width, and height. Notice that these values are all between 0 and 1, because the default maskUnits is objectBoundingBox. The linear path also gets a fill of #555.

1  2  3  4  5  6  7  8  9  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <mask id="mask-path" x="0" y="0" width="1" height="1">         <path d="M0,0  l 75,100  150,75  -25,-125  Z" fill="#555" />       </mask>     </defs>     <circle cx="110" cy="110" r="100" fill="#9c6" mask="url(#mask-path)" />    </svg>

The result is a circle that's clipped outside of the path and inside the path is masked with a medium gray fill of #555, resulting in a lighter value than the original. The blue outline (not shown in the code) shows the mask boundary.

Take note that you can use a mask as a clipping path if you use a solid fill color of white (#fff) which allows 100% of the pixels being masked to show while clipping everything outside the boundary of the mask.

Multiple Shapes as Masks

Like clipping paths, you can use multiple SVG elements as the content inside the mask. In this next example I used four rectangles and placed them so each covers a different quadrant of the circle.

I'm using the same SVG shape four times, but you could certainly mix rectangles and circles and polygons and paths as the elements used as the mask content.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>      <mask id="mask-multiple" x="0" y="0" width="1" height="1">      <rect  x="10" y="10" width="100" height="100" fill="#999" />      <rect  x="110" y="10" width="100" height="100" fill="#777" />      <rect  x="110" y="110" width="120" height="120" fill="#555" />      <rect  x="10" y="110" width="100" height="120" fill="#333" />      </mask>    </defs>     <circle cx="110" cy="110" r="100" fill="#9c6" mask="url(#mask-multiple)" />     <rect  x="10" y="10" width="100" height="100" fill="none" stroke="#999" />     <rect  x="110" y="10" width="100" height="100" fill="none" stroke="#777" />     <rect  x="110" y="110" width="100" height="100" fill="none" stroke="#555" />     <rect  x="10" y="110" width="100" height="100" fill="none" stroke="#333" />    </svg>

Here's the result of applying the multi-shape mask. Notice I gave each rectangle a different fill so each quadrant of the circle is a slightly different shade

I've also shown each rectangle without a fill, but with a stroke so you can see the outline of each rectangle in the mask.

Gradient Masks

By now you can probably tell the fill is the most interesting part of the mask. The boundaries of the mask act like a clipping path, but the fill determines how much of the graphic below shows through.

Solid colors as mask fills aren't all that exciting as all they do is change the color or value of the original graphic. Fortunately you aren't limited to solid color fills.

In this example I use a gradient as the fill. Notice that I defined the linear gradient first and used the id of the gradient as the fill for the mask content. The mask itself then gets an id which is referenced by the circle.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <linearGradient id="gradient">         <stop offset="0" stop-color="white" stop-opacity="0.25" />         <stop offset="1" stop-color="white" stop-opacity="1" />       </linearGradient>       <mask id="gradient-mask">         <rect x="0" y="0" width="210" height="210" fill="url(#gradient)"  />       </mask>      </defs>      <circle cx="110" cy="110" r="100" fill="#9c6" mask="url(#gradient-mask)" />    </svg>

Here's the result. The gradient moves from 25% opaque on the left to fully opaque on the right. The change makes the circle appear as though it's a 3-dimensional sphere as opposed to a 2-dimensional circle.

Once again note the pattern. The SVG graphic (the circle) references the mask (#gradient-mask) and the mask content references the gradient (#gradient) for its fill. Also notice that the mask content is another unexciting rectangle, but because the fill is more interesting, the mask, even a rectangular one, is more interesting.

Pattern Masks

You can also create a pattern and use the pattern as the fill for the mask content. The difference between this example and the previous one is that I changed the gradient to a pattern of repeating circles. Otherwise it's the same.

1  2  3  4  5  6  7  8  9  10  11  12  13  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <pattern id="pattern" x="0" y="0" width="10" height="10" patternUnits="userSpaceOnUse">         <circle cx="5" cy="5" r="5" fill="#999" />       </pattern>       <mask id="pattern-mask" x="0" y="0" width="1" height="1" >         <rect x="0" y="0" width="220" height="220" fill="url(#pattern)"/>       </mask>     </defs>     <circle cx="110" cy="110" r="100" fill="#9c6" mask="url(#pattern-mask)" />    </svg>

Here's the result, which you can see is the same circle with a pattern for a mask. The pattern fill color changes the value of the original green circle.

And of course you can combine patterns and gradients, which I did here by borrowing the linear gradient from the example in the previous section and using it as the fill on the pattern.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <linearGradient id="gradient-2">         <stop offset="0" stop-color="white" stop-opacity="0.25" />         <stop offset="1" stop-color="white" stop-opacity="1" />       </linearGradient>       <pattern id="pattern" x="0" y="0" width="10" height="10" patternUnits="userSpaceOnUse">         <circle cx="5" cy="5" r="5" fill="url(#gradient-2)" />       </pattern>       <mask id="pattern-mask" x="0" y="0" width="1" height="1" >         <rect x="0" y="0" width="220" height="220" fill="url(#pattern)"/>       </mask>     </defs>     <circle cx="110" cy="110" r="100" fill="#9c6" mask="url(#pattern-mask)" />     </svg>

Here's the result of giving the pattern a gradient fill.

Closing Thoughts

Hopefully you agree that masks become more interesting when the mask is more than a simple shape with single solid color fill. You can get create with the shape of your mask by building them with paths or combining multiple elements as the mask content.

More interesting, I think, is when you get creative with the fill and use gradients, patterns or both as the mask fill.

I offered two clipping path example posts. Guess what I have planned for the masking? Next week I want to show you a few more examples that make use of text and images as the mask content and allow you to create even more effects.

Download a free sample from my book Design Fundamentals.

Join me as I share my creative process and journey as a writer.

This posting includes an audio/video/photo media file: Download Now