在使用剪辑路径的svg rect元素上重复背景图像

时间:2014-06-23 09:14:08

标签: html css svg viewbox

我想将重复的背景图像添加到使用剪辑路径的svg rect元素。 svg文件必须具有viewbox属性。

查看小提琴(http://jsfiddle.net/tbbtester/ES9cB/2/) - 我希望三角形的背景与矩形相似 - 它们使用相同的背景图像,为什么它们看起来如此不同?

HTML:

<p></p>
<div class="section-top">
    <div>
    <svg viewBox='0 0 100 50' preserveAspectRatio="none">
        <rect x="0" y="0" height="50" width="100" />

        <defs><clipPath id="section2a"><polygon points='0,0 100,5 100,50 '/></clipPath>
        <pattern patternUnits="userSpaceOnUse" id="pat1" x="0" y="0" width="1px" height="1px">
            <image width="1px" height="1px" xlink:href="http://svgtest.tbb.dev.novicell.dk/bg.png" />
        </pattern>
        </defs>
        </svg>
    </div>
</div>

CSS:

.section-top{position:absolute;width:100%;top:250px;}
.section-top    div{height:0;position: relative;padding-top:50%;}
svg{height: 100%;display:block;width: 100%;position: absolute;top:0;left:0;}
rect{stroke:none;fill:url(#pat1);clip-path: url(#section2a);}
p{height:200px;background: url(http://svgtest.tbb.dev.novicell.dk/bg.png);}

1 个答案:

答案 0 :(得分:0)

没有任何方法可以使模式不随SVG一起缩放。它受viewBox和preserveAspectRatio设置的影响与其他所有设置相同。

如果事先知道缩放的内容,可以在patternTransform属性中应用变换的逆。否则,你运气不好,

相关问题