Tuesday, 21 June 2016

SVG Masking Examples — Text And Image Masks - Vanseo Design

SVG Masking Examples — Text And Image Masks - Vanseo Design


SVG Masking Examples — Text And Image Masks

Posted: 21 Jun 2016 05:30 AM PDT

Last week I showed you some masking examples where the mask was more than a rectangle, including a couple where the mask fill was a gradient or pattern. Perhaps even more interesting, you can add a mask on top of an image or use an image or text as the mask.

Today I want to share a few more examples working with text and images as the mask content. Hopefully you'll agree they're more interesting than masking a circle with a rectangle.

Text Masks

Before we get started, here's the image I'll be using throughout this post. If you've followed along with this series, it should be familiar. It's an image of the Strawberry Fields Memorial in Central Park.

Text is an interesting option for the content of a mask. A common use for a text mask is to place it over an image similar to using text as a clipping path. The mask clips the image outside of the text.

In this first example, I created a text mask with the ever so original words Text Mask. I've given the text some basic properties like a font family, weight and size. I've filled it with a medium gray (#999).

1  2  3  4  5  6  7  8  9  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <mask id="text-mask-1" x="0" y="0" width="100" height="100">         <text x="30" y="140" font-family="Helvetica" font-weight="bold" font-size="7em" fill="#999">Text Mask</text>       </mask>     </defs>     <image width="660" height="495" xlink:href="http://www.vanseodesign.com/blog/wp-content/uploads/2013/09/strawberry-fields.jpg" mask="url(#text-mask-1)"/>    </svg>

The result is the image showing through the text. The values of the image that show through are a little lighter than in the original image as they're masked by the text fill, which is #999.

Text Mask

More interesting would be to replace the solid fill color of the text mask with a pattern. Here I used the same pattern defined in last week's pattern example.

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-1" x="0" y="0" width="10" height="10" patternUnits="userSpaceOnUse">         <circle cx="5" cy="5" r="5" fill="#999" />       </pattern>       <mask id="text-mask-2" x="0" y="0" width="100" height="100">         <text x="30" y="140" font-family="Helvetica" font-weight="bold" font-size="7em" fill="url(#pattern-1)">Text Mask</text>       </mask>     </defs>     <image width="660" height="495" xlink:href="http://www.vanseodesign.com/blog/wp-content/uploads/2013/09/strawberry-fields.jpg" mask="url(#text-mask-2)"/>    </svg>

Hopefully you agree the result is a bit more interesting than the previous example with the solid fill.

Text Mask

Firefox has an issue with the previous example. It doesn't seem to like adding the pattern to the text mask. Here's an image of the result in case you happen to be viewing this in Firefox

Text mask example with a pattern fill

Instead of an SVG pattern, you can also fill the text mask with a gradient as I've done here, by borrowing the linear gradient I used last week.

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-1">         <stop offset="0" stop-color="white" stop-opacity="0.1" />         <stop offset="1" stop-color="white" stop-opacity="0.9" />       </linearGradient>       <mask id="text-mask-3" x="0" y="0" width="100" height="100">         <text x="30" y="140" font-family="Helvetica" font-weight="bold" font-size="7em" fill="url(#gradient-1)">Text Mask</text>       </mask>     </defs>     <image width="660" height="495" xlink:href="http://www.vanseodesign.com/blog/wp-content/uploads/2013/09/strawberry-fields.jpg" mask="url(#text-mask-3)"/>    </svg>

Again the shape of the text clips the image and in this case applies a gradient mask so the text to the left is lighter than the text to the right.

Text Mask

Similar to the previous example, Firefox doesn't like this one either. Here's an image if Firefox is the browser you're currently using.

Text mask example with gradient fill

Image Masks

Let's reverse things and instead of adding a mask on top of an image, let's use the same image from the previous examples as the mask content.

In this example I'm using the image of Strawberry Fields as the mask content. I placed three circles inside a group <g> and have their fill colors set to be red (#c69), green (#9c6), and blue (#69c). The image is used to mask all three as I added the reference to the mask on the group as opposed to adding it to any of the individual circles.

1  2  3  4  5  6  7  8  9  10  11  12  13  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <mask id="image-mask" x="0" y="0" width="1" height="1" >         <image width="660" height="495" xlink:href="http://www.vanseodesign.com/blog/wp-content/uploads/2013/09/strawberry-fields.jpg" />       </mask>     </defs>     <g mask="url(#image-mask)">       <circle cx="110" cy="110" r="100" fill="#c69" />       <circle cx="310" cy="110" r="100" fill="#9c6" />       <circle cx="510" cy="110" r="100" fill="#69c" />     </g>    </svg>

Here's the result and hopefully you can tell that a different part of the image masks each of the three circles.

You don't have to add the mask to the group. You can also add masks to individual elements in the group.

In this next example I added a second mask that uses the pattern from the earlier example. I added the same image mask to the group of all three circles, but added the pattern mask directly on the first circle in the group.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  
<svg width="660" height="220" style="outline: 1px solid red">     <defs>       <pattern id="pattern-2" x="0" y="0" width="10" height="10" patternUnits="userSpaceOnUse">         <circle cx="5" cy="5" r="5" fill="#999" />       </pattern>       <mask id="pattern-mask-2" x="0" y="0" width="1" height="1" >         <rect x="0" y="0" width="225" height="220" fill="url(#pattern-2)"/>       </mask>       <mask id="image-mask-2" x="0" y="0" width="1" height="1" >         <image width="660" height="495" xlink:href="http://www.vanseodesign.com/blog/wp-content/uploads/2013/09/strawberry-fields.jpg" />       </mask>     </defs>     <g mask="url(#image-mask-2)">       <circle cx="125" cy="110" r="100" fill="#c69" mask="url(#pattern-mask-2)" />       <circle cx="325" cy="110" r="100" fill="#9c6" />       <circle cx="525" cy="110" r="100" fill="#69c" />     </g>    </svg>

Here's the result. As you might expect, all three circles are masked by the image, but only the first circle is masked by the pattern.

Take note that this example uses two different masks, one applied to the entire group of circles and one applied directly to the first circle in the group. It shows that you can use multiple masks in the same SVG.

Closing Thoughts

That brings me to the end of this series on SVG clipping and SVG masking. I hope you agree that both are relatively easy to work with. With either you define the clipping path or the mask in one location and then reference it in another.

I think you'll agree that masks are more interesting than clipping paths. In fact a clipping path is really a mask with a solid fill of white (#fff).

Masks become the most interesting when you use gradients and patterns as the fill or better use an image to mask another image or graphic element.

I have another SVG series on filter effects planned for later in the summer, but I think it's time to switch topics. Next up is a series on Sass data types. I'll talk about numbers, strings, color, lists, and maps, and then close the series with a look at control directives in Sass

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

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

Wednesday, 8 June 2016

More About SVG Masking — Properties And Attributes - Vanseo Design

More About SVG Masking — Properties And Attributes - Vanseo Design


More About SVG Masking — Properties And Attributes

Posted: 07 Jun 2016 05:30 AM PDT

For the last several weeks I've been talking about clipping and masking in SVG. Last week I covered the very basics of SVG masking and mentioned I would continue today and talk about some of the additional properties and attributes you can use for greater control of your masks.

These attributes will hopefully make clear the distinction between masks and mask content and I'll talk about the difference between the two.

One thing I didn't get to last time was CSS masking. In the clipping part of this series I showed how CSS handles clipping and I'd like to do the same for masking.

Masking with CSS

The situation with masking in CSS is similar to the situation with clipping in CSS. In time, I expect we'll use CSS more often to mask SVG elements, but at the moment support isn't quite there. Support for masking is about the same, though a little less than the support for clipping and no browser has full support yet.

Last week's examples featured a green circle, which I then masked. As a reminder here's what the circle looks like unmasked.

Just as I did last week, I'm defining a mask as a rectangle with a fill of medium gray (#999). Instead of referencing the mask using the SVG mask property, here I've called it using the shorthand CSS mask property.

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

The result is the same as we saw last week.

You can also create the mask directly in CSS, however, I don't think you can then mask SVG elements with it at the current time. You can add a mask to an image, which is what I've done in the following example.

1  2  
<img src="http://www.vanseodesign.com/blog/wp-content/uploads/2013/09/strawberry-fields.jpg"    style="mask-image: linear-gradient(black, transparent);"/>

Since this code may or may not work in the browser you're using, here's an image of what it looks like in Safari (using -webkit-mask-image instead of mask-image).

CSS Masking Example

Again, I don't suggest you use CSS for masking at the moment given the lack of support, but I wanted to show you how it's done for when support is there.

Let's get back to SVG masking and some of the additional attributes you can use to control the mask.

Mask Attributes

Like clipping paths, masks can take several attributes that should be familiar if you've been working with SVG at all.

  • x — x-coordinate for the top/left corner of the mask
  • y — y-coordinate for the top/left corner of the mask
  • width — The width of the mask
  • height — The height of the mask
  • maskUnits — Defines the coordinate system for the mask attributes x, y, width, and height
  • maskContentUnits — Defines the coordinate system for the contents of the mask

Both maskUnits and maskContentUnits take the same values. They can be either userSpaceOnUse or objectBoundingBox. These work the same way they did for the clipUnits of a clipping path.

  • userSpaceOnUse — represents values in the current user coordinate system in place at the time when the mask is referenced
  • objectBoundingBox — represents fractions or percentages of the bounding box of the element to which the mask is applied.

You may be wondering what the difference is between them, especially as they take the same two values. The difference is in the name. maskUnits refer to the mask and maskContentUnits refers to the contents of the mask, which has been a <rect> in all the examples to this point.

Mask and Mask Content

If you like back up at the first CSS masking example, it probably seemed like I set the four attributes, x, y, width, and height, but the truth is I didn't set any of them. The x, y, width, and height set in the example were all attributes of the rectangle inside the mask. In other words they were set on the mask content, but not the mask itself.

Here's a variation of the example (using the SVG mask property to reference the mask). Here I added x, y, width, and height attributes to the <mask> element. You'll notice both x and y are 0 and the width and height are 0.5.

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

The reason for these values is because the default value of maskUnits is objectBoundingBox, which means the values will be a percent of the bounding box or a number between 0.0 and 1.0.

The default for maskContentUnits is userSpaceOnUse and so the x, y, width, and height of the mask content, the <rect>, are given in pixels.

Here's the result of the example, which should look familiar.

In the example we just looked at, the location and dimensions of the mask are the same as the mask content. They don't have to be.

Here I've changed the width of the mask (not the mask content) to 0.25. Everything else is the same as the previous example.

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

Even though the mask content is a rectangle that's 100px wide, the mask is 25% of the bounding box of the element that references the mask or 25% of 200px (the diameter of the circle), or 50px. The mask is now half the width it was before.

Here's the result. The blue outline shows the boundary of the mask content (the rectangle) and the purple line shows the extent of the mask itself.

Of course you don't have to use the default values for maskUnits of maskContentUnits. Here I've set the maskUnits to userSpaceOnUse and maskContentUnits to objectBoundingBox, switching both from their defaults.

Notice that both attributes are set on the mask element. Because I've switched the units from their default values, the mask now runs from 10px to 200px in each direction and the mask content (the rectangle) needs to be between 0.0 and 1.0.

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

Here's the result. Notice that this time the mask (purple outline) is the full 100px by 100px in width and height, and it's the mask content (blue outline) that's half the height (0.25 of 200px or 50px).

Let's leave things here for today.

Closing Thoughts

That covers the basics and a little more for working with SVG masks. As I did with clipping paths, I tried to keep the mask and the elements being masked as simple as possible so the concepts could sink in.

Next week and the week after, I'll offer some examples that go a little further. I'll show you how to work with paths, text, gradients, and patterns as the mask. I'll look at multiple shapes for the mask as well as masking a group of elements. Finally I'll show you how to work with image masks and how to mask images.

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

Wednesday, 1 June 2016

SVG Masking - Vanseo Design

SVG Masking - Vanseo Design


SVG Masking

Posted: 31 May 2016 05:30 AM PDT

Like clipping paths, masks are used to hide and reveal parts of graphics and other elements. The difference is where clipping paths either show elements as fully opaque or fully transparent, masks allow you to control the degree of transparency or opacity. Where a clipping path leave every pixel as 0% or 100% opaque, masks can be 42% or 68% or any other percent opaque for any pixel in the mask.

The last few weeks, I've been looking at clipping paths, their attributes and some examples for how to work with them in SVG. Today I want to shift focus and talk about masks. You'll find them to be similar in regards to the code you use to create them, but as you'll see masks do different things than clipping paths and you'll use each to create different effects.

What is Masking?

Any SVG object, graphic element, or group can be used as an alpha mask. That includes shapes, gradients, and patterns. The mask will determine what parts of and to what degree the SVG element(s) beneath the mask are visible.

Creating SVG masks is similar to creating clipping paths You define a mask inside a <mask> element, you give the mask an id, and then you reference the id on whatever element or group of elements you want the mask to apply.

Like clipping paths masks are never rendered directly. They're only used as something that can be referenced. As with clipping paths I think masks will be easiest to understand and explain with examples so let's dive right in.

Simple Examples

Here's the same green circle I used through the clipping path posts. I used an SVG circle element, set it's location via cx and cy and gave it a radius of 100px. The red outline shows the boundaries of the viewport.

1  2  3  
<svg width="660" height="220" style="outline: 1px solid red">     <circle cx="110" cy="110" r="100" fill="#9c6" />    </svg>

Here's what the SVG looks like without any mask applied. It's nothing particularly exciting, but it shows us where we are prior to adding an SVG mask.

Now let's add a mask and see what happens.

I defined a mask inside <mask> tags. In this example the mask is a rectangle with equal height and width of 100px. Notice that unlike clipping paths, the mask has a fill, in this case a medium gray (#999). I also wrapped <defs> tags around the mask since the mask is being defined in one location and referenced in another.

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

The mask is referenced using the mask property, which is added to the element to be masked.

1  
mask: url("mask-1")

If you followed along with the clipping path posts, the general set up for how this works should look very familiar. Here's the result of adding the mask.

Note that I added a blue outline (not shown in the code) so we can see the boundaries of the mask.

There are two things to notice in this example. First is that anything outside the mask is clipped. Here the mask is 100px by 100px, so everything outside the square is clipped.

The second is that the value of the initial square changed. That's because the mask is filled with a medium gray (#999). Nothing of the initial square would show through a black mask and 100% would show through a white mask. For everything in between the color and alpha channels of the mask are used to determine how much of the graphic shows through.

Here's the result of using black (#000) as the fill for the mask. Everything else from the previous example is the same. Nothing now shows through the mask.

And here's the result of using white (#fff) as the mask fill. Now everything shows through the mask, though the circle is still clipped outside the mask's boundaries.

Using rgba() as Mask Fill

So far I've used hex values for the fill color. Another option is to use rgba(), which gives you direct control of the alpha channel or transparency.

In this example I set the fill to rgba(255,255,255,0.5625), which is white, but at 0.5625. As it happens 9/16 is 0.5625 so this is the same as setting the mask fill as #999 in hex.

1  2  3  4  5  6  7  8  9  
<svg width="660" height="220" style="outline: 1px solid red">      <defs>        <mask id="mask-rgb-1">          <rect x="10" y="10" width="100" height="100" fill="rgba(255,255,255,0.5625)" />        </mask>      </defs>       <circle cx="110" cy="110" r="100" fill="#9c6" mask="url(#mask-rgb-1)"/>     </svg>

If you compare the result of this mask to the one with #999 as a fill, they should look exactly the same.

Alternatively I could set the rgba() value as rgba(144, 144, 144, 1.0), leaving the alpha value as 1.0. Note: Technically #999 would be rgb(143.4375, 143.4375, 143.4375), but I figured rounding up was close enough for the example.

One last option would be to set the mask fill to white using hex or rgb() (#fff, 255,255,255) and then add an opacity (or fill-opacity) of 0.5625 (9÷16).

In all three examples the fill will be the same medium gray and it's that fill that determines how much of the graphic shows through. I'll skip the algorithm itself, since I don't think it will help you work with masks or understand them any better, but you can read how the mask value at any point is computed here.

Closing Thoughts

If you understand how to work with SVG clipping paths, I think you'll agree masks are easy to work with as well. You define the mask in one place and then you reference the mask on any element to which you want to apply it. The main difference is the mask fill which determines how much of the underlying element shows through the mask.

As you can no doubt guess, there's more to masks than what I've shown here. Next week I want to talk about additional properties and attributes you can use with masks and talk about the difference between masks and mask content.

Like clipping paths, masks are coming to CSS, though the support isn't quite there. However as I did with clipping paths, I'll show you an example or two that shows how to work with masks in CSS. Then in the weeks that follow, I'll show a variety of different examples to show that masks can be more than rectangles and they can be used to mask more than a simple circle.

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