Clipping is the business of hiding what normally would be drawn. The stencil which defines what is and what isn't drawn is called a clipping path. In SVG you can define a clipping path by placing shapes, paths and text inside a 'clipPath' element. You cannot use an image as a mask. It is like a special case of an opacity mask as everything is either on or off. The areas taken up by the graphics are fully transparent and everything else is opaque.
Clipping paths are used by referencing its URI using the 'clip-path' attribute all shapes and container tags use.
The 'clipPath' element can be placed inside the tag of the graphic which uses it separately in the definition section of your SVG document. Making a clipping path a child of a graphic however will not automatically make the graphic use it. You must still reference its URI.
Example 1. The eye is masked by a skewed ellipse. (Download)
Two basic operations are the intersection and the union of two clipping planes. SVG only supports the intersection of two planes by referencing another 'clipPath' element using the 'clip-path' attribute. There is no direct way to intersect 3 or more planes.
There is no direct way to take the union of two planes. The 'use' element doesn't help much either as it can only directly reference shapes, text and paths when used in a clipping path.
Example 2. Taking the intersection and union of two clipping planes. (Download)
Code 2. Code to create an intersection and union clipping plane.
<clipPath>
Attribute
Explanation
clip-path
The referenced clipping path is intersected with the referencing clipping path.
clipPathUnits
The values are either 'userSpaceOnUse' or 'objectBoundingBox'. The second value makes units of childern a fraction of the object bounding box which uses the mask. (default: 'userSpaceOnUse')
The 'clipPath' element
Childern of a clipping path can be any shape, text or path. A child tag is any tag which is inside another tag. All presentation attributes like 'fill' and 'stroke' are ignored as only the shape area is needed to make a clipping path. The 'use' element can be used only if it directly references a shape, text or path. It cannot reference a container element like 'g'.
Article Series
This article is part 12 of a 15 part series. Other articles in this series are shown below: