简体   繁体   English

使用背景图像填充 SVG 路径元素,无需平铺或缩放

[英]Fill SVG path element with a background image without tiling or scaling

I'd like to do something very much like Fill SVG path element with a background-image except that the solution offered there will tile the image to fill the the entire background area.我想做一些非常类似于使用背景图像填充 SVG 路径元素的事情,除了那里提供的解决方案将平铺图像以填充整个背景区域。 I don't want that.我不想要那个。 If the image doesn't fill either the entire height or the entire width, then I'd just like it to center within the path shape and let that be that.如果图像没有填满整个高度或整个宽度,那么我只是希望它在路径形状中居中,就这样吧。 If I try to restrict the behavior by changing the height/width attributes, then the rendering just scales the image until it fills at least one of the dimensions.如果我尝试通过更改高度/宽度属性来限制行为,那么渲染只会缩放图像,直到它填充至少一个尺寸。

I guess this behavior somewhat makes sense since it uses patterns.我想这种行为有点道理,因为它使用了模式。 I can see that what I want is not really a "pattern" per se.我可以看到我想要的并不是真正的“模式”本身。 I just want to slap an image in the middle of my shape just as it is, not make a pattern out of it.我只想按原样在我的形状中间拍一个图像,而不是用它做一个图案。 However, I do want the shape boundaries defined by my SVG path to cut off the rendering of the image, whenever the image's size extends past those boundaries.但是,我确实希望由我的 SVG 路径定义的形状边界来切断图像的渲染,只要图像的大小超出这些边界。 I don't know else to get that behavior besides the use of a pattern for the fill attribute, as is done in the answer to that question.除了对填充属性使用模式之外,我不知道还有什么方法可以得到这种行为,正如在该问题的答案中所做的那样。

In a similar question: Add a background image (.png) to a SVG circle shape , the user at the very bottom seems to indicate that he used a filter rather than a pattern to achieve what I'm seeking to achieve.在类似的问题中: Add a background image (.png) to a SVG circle shape ,最底部的用户似乎表明他使用过滤器而不是模式来实现我想要实现的目标。 However, when I try that, no regard is paid to the actual shape during rendering.但是,当我尝试这样做时,渲染过程中不会考虑实际形状。 The image is rendered without any regard to the shape boundaries, which seems pretty much useless.渲染图像时不考虑形状边界,这似乎毫无用处。

Is there any way in SVG to get that pattern-like background image behavior but without actually tiling the image into a pattern? SVG 中是否有任何方法可以获得类似图案的背景图像行为,但实际上没有将图像平铺成图案?

Just make the x , y , width and height of the pattern match the bounding box of your path. 只需使模式的xywidthheight与路径的边界框匹配即可。 You can just use "0","0","1" & "1" respectively here because the patternUnits defaults to objectBoundingBox , so the units are expressed relative to the bounding box. 这里你可以分别使用“0”,“0”,“1”和“1”,因为patternUnits默认为objectBoundingBox ,因此单位是相对于边界框表示的。 Then make the image in your pattern have the width and height of the path bounding box also. 然后使图案中的图像也具有路径边界框的widthheight This time, you'll need to use "real" sizes though. 这次,您需要使用“真实”尺寸。

The image will be centred in the pattern automatically because the default value of preserveAspectRatio for <image> does exactly what you want. 图像将自动居中在图案中,因为<image>preserveAspectRatio的默认值完全符合您的要求。

 <svg width="600" height="600"> <defs> <pattern id="imgpattern" x="0" y="0" width="1" height="1"> <image width="120" height="250" xlink:href="http://lorempixel.com/animals/120/250/"/> </pattern> </defs> <path fill="url(#imgpattern)" stroke="black" stroke-width="4" d="M 100,50 L 120,110 150,90 170,220 70,300 50,250 50,200 70,100 50,70 Z" /> </svg> 

I have tried to implement my first scale vector graphic extension file to github repository maintained as pages.我试图将我的第一个比例矢量图形扩展文件实现为作为页面维护的 github 存储库。 I know the rollercoast to aim extra lines to make a fit within an environment.我知道过山车会瞄准额外的线条以适应环境。 But i have to tell that it is not necessary to implement the pattern tagline to avoid automatic pattern.但我不得不说,没有必要实施模式标语来避免自动模式。 In my css folder i have a file named gradientellipse.svg with the content在我的 css 文件夹中,我有一个名为 gradientellipse.svg 的文件,其内容

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--?xml-stylesheet href="css/styles.css" type="text/css"?-->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs> 
  <ellipse id="myEllipse"
           cx="200"
           cy="70"
           rx="85"
           ry="55">
</ellipse>
    <linearGradient id="grad1"
                    x1="0%"
                    y1="0%"
                    x2="0%"
                    y2="100%">
      
    <stop offset="0%"
          style="stop-color:rgb(255,0,0);stop-opacity:1" />
    <stop offset="100%"
          style="stop-color:rgb(255,255,0);stop-opacity:1" />
</linearGradient>
</defs>
  <use x="5" y="5" href="#myEllipse" fill="url('#grad1')" />
</svg>

The tagline <use> also on the site <defs> has been noted with the sentence网站<defs>上的标语<use>已用句子注明

element takes nodes from within the SVG document元素从 SVG 文档中获取节点

and i like that a lot, because it gives me the opportunity to aim for something instead of thinking about XML server scripting.我非常喜欢这一点,因为它让我有机会瞄准目标,而不是思考 XML 服务器脚本。

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

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