Created
December 10, 2014 00:36
-
-
Save anonymous/673a4bb2b06e024ae199 to your computer and use it in GitHub Desktop.
svg clipping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="svgess-CHP-9-EX-1"> | |
<p><b>Example 9-1. Clipping to a rectangular path</b> | |
</p> | |
<pre><defs> | |
<clipPath id="rectClip"> | |
<rect id="rect1" x="15" y="15" | |
width="40" height="45" | |
style="stroke: gray; fill: none;"/> | |
</clipPath> | |
</defs> | |
<!-- clip to rectangle --> | |
<use xlink:href="minicat.svg#cat" style="clip-path: url(#rectClip);"/> | |
<!-- | |
for reference, show entire picture with clipping area outlined --> | |
<g transform="translate(100,0)"> | |
<use xlink:href="#rect1"/> <!-- show clip rectangle --> | |
<use xlink:href="minicat.svg#cat"/> | |
</g> | |
</pre> | |
</div> | |
<div id="svgess-CHP-9-FIG-1"> | |
<p><b>Figure 9-1. Simple rectangular clipping</b> | |
</p><p><a href="/wiki/index.php/Image:SVG_Essentials_I_9_tt180.png" class="image" title="Simple rectangular clipping"><img alt="Simple rectangular clipping" src="/wiki/images/4/41/SVG_Essentials_I_9_tt180.png" width="167" height="83" border="0" /></a> | |
</p> | |
</div> | |
<p>As the name <tt><clipPath></tt> implies, you can clip to any arbitrary path. Indeed, the <tt><clipPath></tt> element can contain any number of basic shapes, <tt><path></tt> elements, or <tt><text></tt> elements. <a href="/wiki/index.php/SVG_Essentials/Clipping_and_Masking#svgess-CHP-9-EX-2" title="SVG Essentials/Clipping and Masking">Example 9-2</a> shows a group of shapes clipped to a curved path and the same group of shapes clipped by text. | |
</p> | |
<div id="svgess-CHP-9-EX-2"> | |
<p><b>Example 9-2. Complex clip paths</b> | |
</p> | |
<pre><defs> | |
<clipPath id="curveClip"> | |
<path id="curve1" | |
d="M5 55 C 25 5, 45 -25, 75 55, 85 85, 20 105, 40 55 Z" | |
style="stroke: black; fill: none;"/> | |
</clipPath> | |
<clipPath id="textClip"> | |
<text id="text1" x="20" y="20" transform="rotate(60)" | |
style="font-size: 48pt; stroke: black; fill: none;"> | |
CLIP | |
</text> | |
</clipPath> | |
<g id="shapes"> | |
<rect x="0" y="50" width="90" height="60" style="fill: #999;"/> | |
<circle cx="25" cy="25" r="25" style="fill: #666;"/> | |
<polygon points="30 0 80 0 80 100" style="fill: #ccc;"/> | |
</g> | |
</defs> | |
<!-- draw with curved clip-path --> | |
<use xlink:href="#shapes" style="clip-path: url(#curveClip);" /> | |
<g transform="translate(100,0)"> | |
<use xlink:href="#shapes"/> | |
<use xlink:href="#curve1"/> <!-- show clip path --> | |
</g> | |
<!-- draw with text as clip-path --> | |
<g transform="translate(0,150)"> | |
<use xlink:href="#shapes" style="clip-path: url(#textClip);"/> | |
</g> | |
<g transform="translate(100,150)"> | |
<use xlink:href="#shapes"/> | |
<use xlink:href="#text1"/> | |
</g> | |
</pre> | |
</div> | |
<p>To help you see the areas better, the preceding SVG draws the clipping path above the entire figure; you see this in the right half of <a href="/wiki/index.php/SVG_Essentials/Clipping_and_Masking#svgess-CHP-9-FIG-2" title="SVG Essentials/Clipping and Masking">Figure 9-2</a>. | |
</p><p>The coordinates for the preceding clip paths have been specified in user coordinates. If you wish to express coordinates in terms of the object bounding box, then set <tt>clipPathUnits</tt> to <tt>objectBoundingBox</tt> (the default is <tt>userSpaceOnUse</tt>). <a href="/wiki/index.php/SVG_Essentials/Clipping_and_Masking#svgess-CHP-9-EX-3" title="SVG Essentials/Clipping and Masking">Example 9-3</a> uses a clip path that will produce a circular (or oval) window on any object that it's applied to. | |
</p> | |
<div id="svgess-CHP-9-EX-3"> | |
<p><b>Example 9-3. clipPathUnits using objectBoundingBox</b> | |
</p> | |
<pre><defs> | |
<clipPath id="circularPath" clipPathUnits="objectBoundingBox"> | |
<circle cx="0.5" cy="0.5" r="0.5"/> | |
</clipPath> | |
<g id="shapes"> | |
<rect x="0" y="50" width="100" height="50" style="fill: #999;"/> | |
<circle cx="25" cy="25" r="25" style="fill: #666;"/> | |
<polygon points="30 0 80 0 80 100" style="fill: #ccc;"/> | |
</g> | |
<g id="words"> | |
<text x="0" y="19" style="font-size: 12;"> | |
<tspan x="0" y="19">If you have form'd a circle</tspan> | |
<tspan x="12" y="33">to go into,</tspan> | |
<tspan x="0" y="47">Go into it yourself</tspan> | |
<tspan x="12" y="61">and see how you would do.</tspan> | |
<tspan x="50" y="80">&#xad;William Blake</tspan> | |
</text> | |
</g> | |
</defs> | |
<use xlink:href="#shapes" style="clip-path: url(#circularPath);" /> | |
<use xlink:href="#words" transform="translate(110,0)" | |
style="clip-path: url(#circularPath);"/> | |
</pre> | |
</div> | |
<div id="svgess-CHP-9-FIG-2"> | |
<p><b>Figure 9-2. Complex path clipping</b> | |
</p><p><a href="/wiki/index.php/Image:SVG_Essentials_I_9_tt181.png" class="image" title="Complex path clipping"><img alt="Complex path clipping" src="/wiki/images/d/d7/SVG_Essentials_I_9_tt181.png" width="199" height="285" border="0" /></a> | |
</p> | |
</div> | |
<p>In <a href="/wiki/index.php/SVG_Essentials/Clipping_and_Masking#svgess-CHP-9-FIG-3" title="SVG Essentials/Clipping and Masking">Figure 9-3</a>, the geometric figures happen to have a square bounding box, so the clipping appears circular. The text is bounded by a rectangular area, so the clipping area appears to be an oval. | |
</p> | |
<div id="svgess-CHP-9-FIG-3"> | |
<p><b>Figure 9-3. Use of a circular/oval clipping path</b> | |
</p><p><a href="/wiki/index.php/Image:SVG_Essentials_I_9_tt182.png" class="image" title="Use of a circular/oval clipping path"><img alt="Use of a circular/oval clipping path" src="/wiki/images/9/92/SVG_Essentials_I_9_tt182.png" width="276" height="100" border="0" /></a> | |
</p> | |
</div> | |
<div class="note"> | |
<p><b>Note</b> | |
</p><p>Specify a clipping rectangle for <tt><marker></tt> and <tt><symbol></tt> tags with the <tt>clip</tt> style property. Its value is four whitespace-separated numbers specifying the rectangle's top, right, bottom, and left bounds. This sets the clip rectangle to match the marker or symbol's <tt>viewBox</tt>, rather than its viewport. | |
</p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment