Search code examples
svgsvg-filters

Why does SVG feColorMatrix + <use xlink:href="#"/> result in a clipped path?


Why does the shape referenced from the section appear different (clipped!) when I use a filter on the element?

  • The white stars are displayed correctly ("spiky" corners)
  • But the brown (filtered) stars are strangely clipped

Code-Snippet:

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
        <filter id="colorScheme">
            <feColorMatrix in="SourceGraphic" type="matrix" color-interpolation-filters="sRGB"  values="0.5 0.0   0.0   0.0 0.0 0.0   0.3 0.0   0.0 0.0 0.0   0.0   0.0   0.0 0.0 0.0   0.0   0.0   1.0 0.0"/>
        </filter>
        <defs>
            <g id="shape_star">
                <path d="M 50.000001,16.850071 60.828477,38.790929 85.041657,42.309326 67.520845,59.387917 71.656934,83.503263 50,72.117556 28.343064,83.503262 32.479155,59.387917 14.958344,42.309324 39.171522,38.790929 Z"/>
            </g>
            <g id="img_star_active">
                <use xlink:href="#shape_star" style="stroke: #FFFFFF; stroke-width: 10; fill: #FFFFFF;"/>
            </g>
            <g id="img_star_inactive">
                <use xlink:href="#shape_star" style="stroke: #FFFFFF; stroke-width: 10; fill: none; "/>
            </g>
        </defs>
        <use xlink:href="#img_star_active" filter="url(#colorScheme)"/>
        <use xlink:href="#img_star_inactive" transform="translate(100,0)" filter="url(#colorScheme)"/>
        <use xlink:href="#img_star_active" transform="translate(0,100)" />
        <use xlink:href="#img_star_inactive" transform="translate(100,100)"/>
    </svg>

See example at https://jsfiddle.net/kx2zjnec/3/


Solution

  • Filter results are clipped at a bounding box that is described relative to the bounding box of the source grafic, but in this case apparently not including the stroke width. I have to admit I do not understand why this happens, but the solution is easy enough: raise the size of the filter region (1 is the width/height of the bounding box):

        <filter id="colorScheme" x="-0.2" y="-0.2" width="1.4" height="1.4">
            <feColorMatrix in="SourceGraphic" type="matrix" color-interpolation-filters="sRGB"  values="0.5 0.0   0.0   0.0 0.0 0.0   0.3 0.0   0.0 0.0 0.0   0.0   0.0   0.0 0.0 0.0   0.0   0.0   1.0 0.0"/>
        </filter>