简体   繁体   English

用图案填充SVG图像

[英]Fill SVG image with pattern

I'm trying to fill SVG image with a pattern in HTML, but I'm not successfull. 我正在尝试用HTML中的模式填充SVG图像,但没有成功。 If I fill with the pattern path, it works. 如果我填满模式路径,它就可以工作。 But I cannot apply it onto svg image. 但是我不能将其应用于svg图像。

Could you help me please? 请问你能帮帮我吗? Here is example . 这是例子

Here is example code: 这是示例代码:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" width="400" height="400">
    <defs>
        <pattern id="image" x="0" y="0" width="400" height="400" patternUnits="userSpaceOnUse">
            <image x="0" y="0" xlink:href="latka.jpg" width="100" height="100" />
        </pattern>
    </defs>
    <image x="0" y="0" width="400" height="400" xlink:href="kosile.svg"  fill="url(#image)"/>
</svg>

It makes no sense to apply a fill attribute to an embedded SVG. fill属性应用于嵌入式SVG没有任何意义。 I assume what you were trying to do is create a tiled background on which to superimpose the linked SVG. 我假设您要尝试做的是创建一个平铺的背景,在该背景上叠加链接的SVG。 The easiest way to do this is by adding a <rect> element filled with the background pattern, then put your embedded SVG image on top of this. 最简单的方法是添加一个填充有背景图案的<rect>元素,然后将嵌入的SVG图像置于此之上。

Here's an example: 这是一个例子:

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200" viewBox="0 0 200 200"> <defs> <!-- define a pattern using a yellow tile image --> <pattern id="bgimg" x="0" y="0" width="60" height="60" patternUnits="userSpaceOnUse"> <image x="0" y="0" width="60" height="60" xlink:href="http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Shades_of_yellow.png/60px-Shades_of_yellow.png" /> </pattern> </defs> <!-- use this pattern to fill the SVG background --> <rect x="0" y="0" width="200" height="200" fill="url(#bgimg)" /> <!-- Embed another SVG (purple circle) on top of this background --> <image x="40" y="40" width="120" height="120" xlink:href="http://upload.wikimedia.org/wikipedia/commons/5/5e/FF0084_circle.svg" /> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM