Mask rotated SVG element

放肆的年华 提交于 2019-12-07 08:23:51

问题


I am using svg.js to draw my SVG. I was wondering, whether it's possible to mask rotated element. I could achieve rotating masked element, which isn't the same.

See a minimal working example - http://jsfiddle.net/mreq/LP7sa/1/ - I'd like the rotated image to be masked with the red rectangle. The example produces following code:

<svg xmlns="http://www.w3.org/2000/svg" id="SvgjsSvg1000" version="1.1" width="100%" height="100%" xlink="http://www.w3.org/1999/xlink" style="position:relative;overflow:hidden;left:0px;top:0px;">
<rect id="SvgjsRect1003" width="50%" height="50%" y="75" x="125" fill="red"/>
<image id="SvgjsImage1004" href="http://t1.gstatic.com/images?q=tbn:ANd9GcQo_CFlPKdX-_-EEs34S-Y8T9l2kVNY59fwfhCKHBK90XSwS21grA" width="400" height="250" transform="rotate(75 200 125)" mask="url(&quot;#SvgjsMask1005&quot;)"/>
<defs id="SvgjsDefs1001">
    <mask id="SvgjsMask1005">
        <rect id="SvgjsRect1002" width="50%" height="50%" fill="#ffffff" x="125" y="75"/>
    </mask>
</defs>
</svg>

The solution can be a generic SVG one - it doesn't have to use svg.js and can use any other library. I tried rotating the mask-rectangle, but that wouldn't do as I need to resize, move and flip the image too.


回答1:


Just put the mask (or clip-path) on a parent <g> element, like this:

<svg xmlns="http://www.w3.org/2000/svg" id="SvgjsSvg1000" version="1.1" width="100%" height="100%" xlink="http://www.w3.org/1999/xlink" style="position:relative;overflow:hidden;left:0px;top:0px;">
    <defs>
        <mask id="mask" maskUnits="userSpaceOnUse">
            <rect id="SvgjsRect1003" width="50%" height="20%" y="75" x="125" fill="white"/>
        </mask>
    </defs>
    <g mask="url(#mask)">
        <image id="SvgjsImage1004" xlink:href="http://t1.gstatic.com/images?q=tbn:ANd9GcQo_CFlPKdX-_-EEs34S-Y8T9l2kVNY59fwfhCKHBK90XSwS21grA" width="400" height="250" transform="rotate(75 200 125)" />
    </g>
</svg>


来源:https://stackoverflow.com/questions/17695534/mask-rotated-svg-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!