将纹理应用于SVG中的图像

时间:2019-02-09 11:58:02

标签: svg textures svg-filters

我正在尝试将纹理应用于图像

原始

enter image description here

纹理

enter image description here

结果(由PHP GD制成)

enter image description here

但是使用SVG,我得到的最接近的结果是

<svg preserveAspectRatio="none" width="500" height="500" viewBox="0 0 500 500">
    <defs>
    
      <filter id="texture">
        <feImage href="https://i.imgur.com/pjWcnJs.jpg" result="texture-img"/>
        <feBlend in="SourceGraphic" in2="texture-img" mode="multiply"/>
      </filter>
    
    </defs>
    
    <g>
        <g filter="url(#texture)">
            <image x="0" y="0" href="https://i.imgur.com/oVEdsQt.png" opacity="1" width="500" height="500" />
        </g>
    </g>
    
</svg>

fiddle

还有另一种方法可以不对透明像素进行纹理化吗?

1 个答案:

答案 0 :(得分:4)

我找到了一种解决方案,即将纹理通过复合滤镜,将其裁剪为源图像

<svg preserveAspectRatio="none" width="500" height="500" viewBox="0 0 500 500">
    <defs>
    
      <filter id="texture">
        <feImage href="https://i.imgur.com/pjWcnJs.jpg" result="texture-img"/>
        <feComposite in2="SourceGraphic" operator="in" in="texture-img" result="composite"/>
        <feBlend in="SourceGraphic" in2="composite" mode="multiply"/>
      </filter>
    
    </defs>
    
    <g>
        <g filter="url(#texture)">
            <image x="0" y="0" href="https://i.imgur.com/oVEdsQt.png" opacity="1" width="500" height="500" />
        </g>
    </g>
    
</svg>

要平铺纹理,我像这样使用feTile

<svg preserveAspectRatio="none" width="500" height="500" viewBox="0 0 500 500">
    <defs>
    
      <filter id="texture" x="0" y="0" width="100%" height="100%">
        <feImage href="https://i.imgur.com/gWH7NLm.jpg" result="texture-img" width="256" height="256"/>
        <feTile in="texture-img" x="0" y="0" width="100%" height="100%" result="tile" ></feTile>
        <feComposite in2="SourceGraphic" operator="in" in="tile" result="composite"/>
        <feBlend in="SourceGraphic" in2="composite" mode="multiply"/>
      </filter>
    
    </defs>
    
    <g>
        <g filter="url(#texture)">
            <image x="0" y="0" href="https://i.imgur.com/oVEdsQt.png" opacity="1" width="500" height="500" />
        </g>
    </g>
    
</svg>

我通过检查inkscape如何应用材质纹理来了解这个主意