SVG supports the following clipping/masking features:
One key distinction between a clipping path and a mask is that clipping paths are hard masks (i.e., the silhouette consists of either fully opaque pixels or fully transparent pixels, with the possible exception of anti-aliasing along the edge of the silhouette) whereas masks consist of an image where each pixel value indicates the degree of transparency vs. opacity. In a mask, each pixel value can range from fully transparent to fully opaque.
SVG supports only simple alpha blending compositing (see Simple Alpha Compositing).
SVG 2 Requirement: | Support new compositing and blending modes from the Compositing and Blending specification. |
---|---|
Resolution: | SVG WG agrees with publishing the Compositing spec. |
Purpose: | To allow common graphical effects supported by Illustrator, etc. |
Owner: | Nikos (ACTION-3332) |
Graphics elements are blended into the elements already rendered on the canvas using simple alpha compositing, in which the resulting color and opacity at any given pixel on the canvas is the result of the following formulas (all color values use premultiplied alpha):
Er, Eg, Eb - Element color value Ea - Element alpha value Cr, Cg, Cb - Canvas color value (before blending) Ca - Canvas alpha value (before blending) Cr', Cg', Cb' - Canvas color value (after blending) Ca' - Canvas alpha value (after blending) Ca' = 1 - (1 - Ea) * (1 - Ca) Cr' = (1 - Ea) * Cr + Er Cg' = (1 - Ea) * Cg + Eg Cb' = (1 - Ea) * Cb + Eb
The following rendering properties, which provide information about the color space in which to perform the compositing operations, apply to compositing operations:
Note that this section may be moved to a separate CSS Masking specification in a future draft.
The clipping path restricts the region to which paint can be applied. Conceptually, any parts of the drawing that lie outside of the region bounded by the currently active clipping path are not drawn. A clipping path can be thought of as a mask wherein those pixels outside the clipping path are black with an alpha value of zero and those pixels inside the clipping path are white with an alpha value of one (with the possible exception of anti-aliasing along the edge of the silhouette).
When an ‘svg’ element is either the root element in the
document or is embedded within a document whose layout is determined
according to the layout rules of CSS or XSL, then the user agent must
establish an initial clipping path for the SVG document fragment. The
‘overflow
’ and ‘clip
’ properties along with additional SVG
user agent processing rules determine the initial clipping path which
the user agent establishes for the SVG document fragment:
This property definition table need to be replaced with a link to css3-box.
Name: | overflow |
---|---|
Value: | visible | hidden | scroll | auto |
Initial: | visible |
Applies to: | elements which establish a new viewport, ‘pattern’ elements and ‘marker’ elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | as specified |
Animatable: | yes |
The ‘overflow
’ property has the same parameter values and has the
same meaning as defined in CSS 2.1
([CSS21], section 11.1.1);
however, the following additional points apply:
overflow
’ property applies to
elements that establish new viewports
(e.g., ‘svg’ elements), ‘pattern’ elements and ‘marker’
elements. For all other elements, the property has no effect (i.e., a clipping
rectangle is not created).overflow
’ property can apply, if
the ‘overflow
’ property has the value hidden or scroll,
the effect is that a new clipping path in the shape of a rectangle is created.
The result is equivalent to defining a ‘clipPath’ element whose
content is a ‘rect’ element which defines the equivalent rectangle,
and then specifying the <uri> of this ‘clipPath’ element on the
‘clip-path
’ property for the given element.overflow
’ property has a value other than
hidden or scroll,
the property has no effect (i.e., a clipping rectangle is not created).overflow
’ property has the value hidden
or scroll, then the user agent will
establish an initial clipping path equal to the bounds of the initial
viewport; otherwise, the initial
clipping path is set according to the clipping rules as defined in CSS 2.1
([CSS21], section 11.1.1).overflow
’ property on the outermost svg element is ignored
for the purposes of visual rendering and the initial clipping path is set to
the bounds of the initial viewport.overflow
’ as defined
in [CSS21-overflow]
is 'visible', and this applies also to the rootmost ‘svg’ element; however,
for child elements of an SVG document, SVG's user agent style sheet
overrides this initial value and sets the ‘overflow
’ property on
elements that establish new viewports
(e.g., ‘svg’ elements), ‘pattern’ elements and
‘marker’ elements to the value 'hidden'.As a result of the above, the default behavior of SVG user agents is to establish a clipping path to the bounds of the initial viewport and to establish a new clipping path for each element which establishes a new viewport and each ‘pattern’ and ‘marker’ element.
For related information, see Clip to viewport vs. clip to ‘viewBox’.
This property definition table need to be replaced with a link to CSS 2.1
Name: | clip |
---|---|
Value: | <shape> | auto |
Initial: | auto |
Applies to: | elements which establish a new viewport, ‘pattern’ elements and ‘marker’ elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | as specified |
Animatable: | yes |
The ‘clip
’ property has the same parameter values
as defined in CSS 2.1
([CSS21], section 11.1.2).
Unitless values, which indicate current user coordinates, are
permitted on the coordinate values on the <shape>. The
value of auto defines a clipping path along
the bounds of the viewport created by the given element.
It is important to note that initial values for the ‘overflow
’ and
‘clip
’ properties and the user agent
style sheet will result in an initial clipping path that is set to the
bounds of the initial viewport. When attributes ‘viewBox’ and
‘preserveAspectRatio’ attributes are specified, it is sometime
desirable that the clipping path be set to the bounds of the ‘viewBox’
instead of the viewport (or reference rectangle, in the case of
‘marker’ and ‘pattern’ elements), particularly when
‘preserveAspectRatio’ specifies uniform scaling and the aspect ratio of
the ‘viewBox’ does not match the aspect ratio of the viewport.
To set the initial clipping path to the bounds of the ‘viewBox’, set
the bounds of ‘clip
’ property to the same rectangle as specified on the
‘viewBox’ attribute. (Note that the parameters do not match.
‘clip
’ takes values <top>, <right>,<bottom> and
<left>, whereas ‘viewBox’ takes values <min-x>,
<min-y>, <width> and <height>.)
A clipping path is defined with a ‘clipPath’
element. A clipping path is used/referenced using the ‘clip-path
’
property.
A ‘clipPath’ element can contain ‘path’ elements, ‘text’ elements, basic shapes (such as ‘circle’) or a ‘use’ element. If a ‘use’ element is a child of a ‘clipPath’ element, it must directly reference ‘path’, ‘text’ or basic shape elements. Indirect references are an error (see Error processing).
The raw geometry of each child element exclusive of rendering properties
such as ‘fill
’, ‘stroke
’, ‘stroke-width
’ within a
‘clipPath’ conceptually defines a 1-bit mask (with the possible
exception of anti-aliasing along the edge of the geometry) which represents
the silhouette of the graphics associated with that element. Anything outside
the outline of the object is masked out. If a child element is
made invisible by ‘display
’ or ‘visibility
’ it does not contribute
to the clipping path. When the ‘clipPath’ element
contains multiple child elements, the silhouettes of the child elements are
logically OR'd together to create a single silhouette which is then used to
restrict the region onto which paint can be applied. Thus, a point is inside
the clipping path if it is inside any of the children of the
‘clipPath’.
For a given graphics element, the actual clipping path used
will be the intersection of the clipping path specified by its
‘clip-path
’ property (if any) with any clipping paths on its
ancestors, as specified by the ‘clip-path
’ property on the
ancestor elements, or by the ‘overflow
’ property on ancestor
elements
which establish a new viewport. Also, see the discussion of
the initial clipping path.)
A couple of notes:
clip-path
’.clip-path
’ reference
is placed on a ‘clipPath’ element, the resulting clipping path is the
intersection of the contents of the ‘clipPath’ element with the
referenced clipping path.clip-path
’ reference is placed on one of the children of
a ‘clipPath’ element, then the given child element is clipped by the
referenced clipping path before OR'ing the silhouette of the child element
with the silhouettes of the other child elements.clip-path
’ property applied.Attribute definitions:
clip-path
’ property).Properties inherit into the ‘clipPath’ element from its ancestors; properties do not inherit from the element referencing the ‘clipPath’ element.
‘clipPath’ elements are never rendered directly; their only usage is
as something that can be referenced using the ‘clip-path
’ property. The
‘display
’ property does not apply to the ‘clipPath’ element;
thus, ‘clipPath’ elements are not directly rendered even if the
‘display
’ property is set to a value other than
none, and ‘clipPath’ elements are
available for referencing even when the ‘display
’ property on the
‘clipPath’ element or any of its ancestors is set to
none.
Name: | clip-path |
---|---|
Value: | <funciri> | none |
Initial: | none |
Applies to: | container elements, graphics elements and ‘clipPath’ |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | as specified, but with <funciri> values made absolute |
Animatable: | yes |
clip-path
’
property must be treated as if it hadn't been specified.Name: | clip-rule |
---|---|
Value: | nonzero | evenodd |
Initial: | nonzero |
Applies to: | graphics elements within a ‘clipPath’ element |
Inherited: | yes |
Percentages: | N/A |
Media: | visual |
Computed value: | as specified |
Animatable: | yes |
fill-rule
’ property.fill-rule
’ property.The ‘clip-rule
’ property only applies to graphics elements that are
contained within a ‘clipPath’ element. The following fragment of code
will cause an evenodd clipping rule to be applied to the clipping path because
‘clip-rule
’ is specified on the ‘path’ element that defines the
clipping shape:
<g clip-rule="nonzero"> <clipPath id="MyClip"> <path d="..." clip-rule="evenodd" /> </clipPath> <rect clip-path="url(#MyClip)" ... /> </g>
whereas the following fragment of code will not cause an
evenodd clipping rule to be applied because the ‘clip-rule
’ is
specified on the referencing element, not on the object defining the
clipping shape:
<g clip-rule="nonzero"> <clipPath id="MyClip"> <path d="..." /> </clipPath> <rect clip-path="url(#MyClip)" clip-rule="evenodd" ... /> </g>
A clipping path is conceptually equivalent to a custom viewport for
the referencing element. Thus, it affects the rendering of an element,
but not the element's inherent geometry. The bounding box of a clipped
element (that is, an element which references a ‘clipPath’
element via a ‘clip-path
’ property, or a child of the
referencing element) must remain the same as if it were not clipped.
By default, pointer events must not be dispatched on the clipped (non-visible) regions of a shape. For example, a circle with a radius of 10 which is clipped to a circle with a radius of 5 will not receive 'click' events outside the smaller radius. Later versions of SVG may define new properties to enable fine-grained control over the interactions between hit testing and clipping.
Note that this section may be moved to a separate CSS Masking specification in a future draft.
In SVG, it is possible to specify another graphics element or file to be used as an alpha mask for compositing the current object into the background.
A mask is applied using the ‘mask
’ property.
The mask source may be defined using a ‘mask’ element.
Alternatively, the ‘mask
’ property may refer to a CSS image or
SVG paint server.
The effect of applying a mask is as if the mask source elements are rendered into an offscreen image which has been initialized to transparent black. The graphical object to which the mask is applied will be painted onto the background through the mask, thus completely or partially masking out parts of the graphical object.
Example mask01 uses an image to mask a rectangle.
<?xml version="1.0" standalone="no"?> <svg width="8cm" height="3cm" viewBox="0 0 800 300" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <desc>Example mask01 - blue text masked with gradient against red background </desc> <defs> <linearGradient id="Gradient" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="800" y2="0"> <stop offset="0" stop-color="white" stop-opacity="0" /> <stop offset="1" stop-color="white" stop-opacity="1" /> </linearGradient> <mask id="Mask" maskUnits="userSpaceOnUse" x="0" y="0" width="800" height="300"> <rect x="0" y="0" width="800" height="300" fill="url(#Gradient)" /> </mask> <text id="Text" x="400" y="200" font-family="Verdana" font-size="100" text-anchor="middle" > Masked text </text> </defs> <!-- Draw a pale red rectangle in the background --> <rect x="0" y="0" width="800" height="300" fill="#FF8080" /> <!-- Draw the text string twice. First, filled blue, with the mask applied. Second, outlined in black without the mask. --> <use xlink:href="#Text" fill="blue" mask="url(#Mask)" /> <use xlink:href="#Text" fill="none" stroke="black" stroke-width="2" /> </svg>
Example mask01
Name: | mask |
---|---|
Value: | [ <mask-source> [ luminance | alpha | auto ]? ] | none |
Initial: | none |
Applies to: | container elements and graphics elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | A <mask-source> followed by a mask type (one of luminance, alpha, or auto), or otherwise the keyword none. |
Animatable: | yes |
Where:
The ability for the ‘mask
’ property to reference a child element
without an ID reference, or a CSS <image> is new in SVG 2.
The meaning of the different <mask-source> values is as follows:
<image> overlaps with <funciri> and hence the parsing for url(...) needs to be defined (e.g. references to elements may only refer to ‘mask’ elements or paint server elements, whilst url(...) values without a fragment identifier are processed in usual fashion for CSS Images).
mask
’ property applied.
The first matching element in tree order (as defined in [DOM4]) as a result of evaluating the list of
selectors is taken as the mask source.
If there are no matching elements the mask source is invalid.If a <mask-source> is provided, a mask type may also be specified. The mask type determines which of the methods described in Calculating mask values should be used for calculating mask values from the mask source.
If a <mask-source> is provided without a mask type, the value auto is used.
If a mask type other than auto is specified
and the <mask-source> refers to a ‘mask’ element
with a ‘maskType’ attribute, the mask type specified on the ‘mask
’ property takes precedence.
The possible values for the mask type are as follows:
Should the default be luminance instead?
The Lacuna value for the ‘mask’ property is 'none'.
The content and behavior of a mask can be defined using a ‘mask’ element, a paint server element, or a CSS <image> value.
The ability to use the alpha of a mask source as the mask values is new in SVG 2. In SVG 1.1 masks always use the luminance of the mask source as the mask values.
A mask source may be interpreted using one of two different methods with regards to calculating the mask values that will be multiplied with the target alpha values.
The first and simplest method of calculating the mask values is to use the alpha channel of the mask source. In this case the mask value at a given point is simply the value of the alpha channel at that point. The color channels do not contribute to the mask value.
The second method of calculating the mask values is to use the luminance of the mask source. In this case the mask value at a given point is computed from the color channel values and alpha channel value using the following procedure.
color-interpolation
’ on the ‘mask’ element is linearRGB, convert the
original image color values (potentially in the sRGB color space) to the linear
RGB color space (see Rendering properties).
Regardless of the method used, the procedure for calculating mask values assumes the content of the mask is a four-channel RGBA graphics object. For other types of graphics objects, special handling is required as follows.
For a three-channel RGB graphics object that is used in a mask (e.g., when referencing a three-channel image file), the effect is as if the object were converted into a four-channel RGBA image with the alpha channel uniformly set to 1.
For a single-channel image that is used in a mask (e.g., when referencing a single-channel grayscale image file), the effect is as if the object were converted into a four-channel RGBA image, where the single channel from the referenced object is used to compute the three color channels and the alpha channel is uniformly set to 1. Note that when referencing a grayscale image file, the transfer curve relating the encoded grayscale values to linear light values must be taken into account when computing the color channels.
Note that SVG ‘path’s, shapes (e.g., ‘circle’) and ‘text’ are all treated as four-channel RGBA images for the purposes of masking operations.
The effect of a mask is identical to what would have happened if there were no mask but instead the alpha channel of the given object were multiplied with the mask's resulting mask values.
Attribute definitions:
mask
’ property).mask
’
property).Properties inherit into the ‘mask’ element from its ancestors; properties do not inherit from the element referencing the ‘mask’ element.
‘mask’ elements are never rendered directly; their only
usage is as something that can be referenced using the ‘mask
’
property. The ‘opacity
’, ‘filter
’ and ‘display
’ properties do not apply to the
‘mask’ element; thus, ‘mask’ elements are not
directly rendered even if the ‘display
’ property is set to a value
other than none, and ‘mask’
elements are available for referencing even when the ‘display
’ property
on the ‘mask’ element or any of its ancestors is set to
none.
The ability to reference paint servers with the ‘mask
’ property
is new in SVG 2.
Besides an SVG ‘mask’ element, the 'mask' property may also refer to an SVG paint server element such as a ‘linearGradient’ element or ‘pattern’ element. When the mask source is an SVG paint server, the effect is as if the following steps were performed:
fill
’ property of the ‘rect’ element
refer to the paint server element.See the CSS Color Module Level 3 for the definition
of ‘opacity
’. [CSS3COLOR]
The ‘opacity
’ property specifies how opaque a given
graphical element or container element will be when it is
painted to the canvas. When applied to a container element,
this is known as group opacity, and when applied to
an individual rendering element, it is known as object
opacity. The principle for these two operations however
is the same.
There are several other opacity-related properties in SVG:
fill-opacity
’, which specifies the opacity of a fill
operation;stroke-opacity
’, which specifies the opacity of a stroking
operation;solid-opacity
’, which specifies the opacity of a solid color
paint server; andstop-opacity
’, which specifies the opacity of a gradient stop.These four opacity properties are involved in intermediate rendering operations.
Object and group opacity however can be thought of as a post-processing
operation. Conceptually, the object or group to which ‘opacity
’ applies
is rendered into an RGBA offscreen image. The offscreen image as whole is then blended
into the canvas with the specified ‘opacity
’ value used uniformly
across the offscreen image.
An ‘opacity
’ value of 0 means fully transparent and 1 means fully
opaque. Opacity values are clamped to the range [0, 1];
see Clamping values which are restricted
to a particular range for details.
The ‘opacity
’ property applies to the following SVG elements:
‘svg’, ‘g’, ‘symbol’, ‘marker’,
‘a’, ‘switch’, graphics elements and
text content child elements.
The following example illustrates various usage of the ‘opacity
’
property on objects and groups.
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="175" viewBox="0 0 1200 350"> <!-- Background blue rectangle --> <rect x="100" y="100" width="1000" height="150" fill="blue"/> <!-- Red circles going from opaque to nearly transparent --> <circle cx="200" cy="100" r="50" fill="red" opacity="1"/> <circle cx="400" cy="100" r="50" fill="red" opacity=".8"/> <circle cx="600" cy="100" r="50" fill="red" opacity=".6"/> <circle cx="800" cy="100" r="50" fill="red" opacity=".4"/> <circle cx="1000" cy="100" r="50" fill="red" opacity=".2"/> <!-- Opaque group, opaque circles --> <g opacity="1"> <circle cx="182.5" cy="250" r="50" fill="red" opacity="1"/> <circle cx="217.5" cy="250" r="50" fill="green" opacity="1"/> </g> <!-- Group opacity: .5, opacity circles --> <g opacity=".5"> <circle cx="382.5" cy="250" r="50" fill="red" opacity="1"/> <circle cx="417.5" cy="250" r="50" fill="green" opacity="1"/> </g> <!-- Opaque group, semi-transparent green over red --> <g opacity="1"> <circle cx="582.5" cy="250" r="50" fill="red" opacity=".5"/> <circle cx="617.5" cy="250" r="50" fill="green" opacity=".5"/> </g> <!-- Opaque group, semi-transparent red over green --> <g opacity="1"> <circle cx="817.5" cy="250" r="50" fill="green" opacity=".5"/> <circle cx="782.5" cy="250" r="50" fill="red" opacity=".5"/> </g> <!-- Group opacity .5, semi-transparent green over red --> <g opacity=".5"> <circle cx="982.5" cy="250" r="50" fill="red" opacity=".5"/> <circle cx="1017.5" cy="250" r="50" fill="green" opacity=".5"/> </g> </svg>
Each group of red and green circles is first rendered
to an offscreen image before being blended with the background
blue rectangle as a whole, with the given ‘opacity
’ values.
In the example, the top row of circles have differing opacities, ranging from 1.0 to 0.2. The bottom row illustrates five ‘g’ elements, each of which contains overlapping red and green circles, as follows:
The SVGClipPathElement interface corresponds to the ‘clipPath’ element.
interface SVGClipPathElement : SVGDefinitionElement { readonly attribute SVGAnimatedEnumeration clipPathUnits; }; SVGClipPathElement implements SVGUnitTypes;
The SVGMaskElement interface corresponds to the ‘mask’ element.
interface SVGMaskElement : SVGDefinitionElement { // Mask Types const unsigned short SVG_MASKTYPE_LUMINANCE = 0; const unsigned short SVG_MASKTYPE_ALPHA = 1; readonly attribute SVGAnimatedEnumeration maskUnits; readonly attribute SVGAnimatedEnumeration maskContentUnits; readonly attribute SVGAnimatedEnumeration maskType; readonly attribute SVGAnimatedLength x; readonly attribute SVGAnimatedLength y; readonly attribute SVGAnimatedLength width; readonly attribute SVGAnimatedLength height; }; SVGMaskElement implements SVGUnitTypes;