Why does this SVG filter unexpectedly clip off ~15% from shape

浪尽此生 提交于 2020-01-06 06:09:02

问题


Why does the grid in this SVG not fill the entire 256x256 space? No matter what size I change it to, about 15% of the grid is cut off, which appears to me to be arbitrary in the context of my code.

<svg width="256" height="256" xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
        <pattern id="grid" width="18.75" height="18.75" patternUnits="userSpaceOnUse">
            <path d="M 18.75 0 L 0 0 0 18.75" fill="none" stroke="black" stroke-width="1"/>
        </pattern>
        <rect id="gridRect"  width="100%" height="100%" fill="url(#grid)" />  
        <filter id="gridify"  width="100%" height="100%" filterUnits = "userSpaceOnUse">
            <feImage result="sourceTwo" xlink:href="#gridRect" />
            <feComposite in="SourceGraphic" in2="sourceTwo" operator="in"/>
        </filter>
    </defs>
    <g filter="url(#gridify)" >
        <rect width="100%" height="100%" fill="url(#linGradient)" />
    </g>
    <rect width="100%" height="100%" fill="none" stroke="black" stroke-width="1"/>
</svg>

回答1:


The SVG specification defaults filters to being 10% larger than the filtered object by default.

If ‘x’ or ‘y’ is not specified, the effect is as if a value of -10% were specified.

If ‘width’ or ‘height’ is not specified, the effect is as if a value of 120% were specified.

I imagine that's to stop lots of questions along the lines of "why is my Gaussian blur based drop-shadow filter cut off?"




回答2:


So, looks like all I needed to do to fix it was add an x="0" and a y="0" to the filter. I don't understand why it is necessary though, as it does not make sense that it would default to "-15%".

<svg width="256" height="256" xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
        <pattern id="grid" width="18.75" height="18.75" patternUnits="userSpaceOnUse">
            <path d="M 18.75 0 L 0 0 0 18.75" fill="none" stroke="black" stroke-width="1"/>
        </pattern>
        <rect id="gridRect"  width="100%" height="100%" fill="url(#grid)" />  
        <filter id="gridify" x="0" y="0"  width="100%" height="100%" filterUnits = "userSpaceOnUse">
            <feImage result="sourceTwo" xlink:href="#gridRect" />
            <feComposite in="SourceGraphic" in2="sourceTwo" operator="in"/>
        </filter>
    </defs>
    <g filter="url(#gridify)" >
        <rect width="100%" height="100%" fill="url(#linGradient)" />
    </g>
    <rect width="100%" height="100%" fill="none" stroke="black" stroke-width="1"/>
</svg>


来源:https://stackoverflow.com/questions/45991017/why-does-this-svg-filter-unexpectedly-clip-off-15-from-shape

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