Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask rotated SVG element

Tags:

html

css

svg

svg.js

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.

like image 313
mreq Avatar asked Jan 13 '23 21:01

mreq


1 Answers

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>
like image 135
Erik Dahlström Avatar answered Jan 16 '23 00:01

Erik Dahlström