2011-01-27 65 views
16

通常,<clipPath>元素隐藏了超出剪辑路径的所有内容。为了达到相反的效果 - 即从图像中“剪出”某些东西 - 我想在clipPath和clip-rule="evenodd"属性中使用两条路径。基本上,我想“异或”剪辑路径。SVG clipPath将* outer *内容剪出

但它不起作用。它显示了区域 “或运算”:

<clipPath clip-rule="evenodd" id="imageclippath" clipPathUnits = "objectBoundingBox"> 
     <rect clip-rule="evenodd" x="0.3" y="0.3" height="0.6" width="6" /> 
     <rect clip-rule="evenodd" x="0" y="0" height="0.5" width="0.5" /> 
    </clipPath>  

<rect clip-path="url(#imageclippath)" x="0" y="0" height="500" width="500" fill="red"/> 

编辑:

我的问题是,AFAIK <mask>不会在iOS版WebKit的工作。

回答

27

使用面罩做后续操作要容易得多,请参阅this example。以下是面罩的定义:

<mask id="maskedtext"> 
    <circle cx="50%" cy="50%" r="50" fill="white"/> 
    <text x="50%" y="55%" text-anchor="middle" font-size="48">ABC</text> 
</mask> 

面罩内白色的区域将被保留,其他所有区域都将被裁剪掉。

这里使用clipPath代替another example,有点棘手,因为您需要使用路径元素来获取应用剪辑规则。在那里使用剪辑规则的clipPath看起来像这样:

<clipPath id="clipPath1"> 
    <path id="p1" d="M10 10l100 0 0 100 -100 0ZM50 50l40 0 0 40 -40 0Z" clip-rule="evenodd"/> 
</clipPath> 
+1

谢谢。我忘了补充说我无法在iOS WebKit上使用'`。例如,使用``可以工作,但很难用点来指定椭圆。 – tillda 2011-01-27 15:45:13