我有一些图案,每个图案中都有一个单独的图片。我需要这些图片缩放到其路径所在容器的完整宽度或高度,同时保持其比例不变。基本上,他们需要像设置 min-width:100%; min-height:100%;
的HTML图像一样。
我以前没有使用过 SVG,并不知道要更改哪些属性才能实现这种行为。我尝试了各种组合,包括 viewBox
, preserveAspectRatio
, patternUnits
等等,但似乎都不能得到我想要的结果。
我有一些图案,每个图案中都有一个单独的图片。我需要这些图片缩放到其路径所在容器的完整宽度或高度,同时保持其比例不变。基本上,他们需要像设置 min-width:100%; min-height:100%;
的HTML图像一样。
我以前没有使用过 SVG,并不知道要更改哪些属性才能实现这种行为。我尝试了各种组合,包括 viewBox
, preserveAspectRatio
, patternUnits
等等,但似乎都不能得到我想要的结果。
objectBoundingBox
units work in SVG, and also how preserveAspectRatio
works.
渐变、图案和许多其他SVG功能的大小和内容可以通过指定objectBoundingBox
作为单位来根据正在绘制的对象(path
, rect
, circle
)的大小来指定。相反,总是使用userSpaceOnUse
,它使用绘制形状的坐标系统。
通常,对象边界框单位是声明图形填充元素的大小和位置的默认值;您可以通过在<pattern>
元素上设置patternUnits
属性来更改这个值。然而,用户空间单位通常是用于内容图形中使用的任何单位的默认值;要更改此值,您需要设置patternContentUnits
属性。
patternContentUnits =“objectBoundingBox”
。您不能在图案内容本身(即图像尺寸)中使用100%作为1个对象边界框单位的同义词;百分比是相对于SVG大小而不是objectBoundingBox解释的。*
我应该提到,由于您说您的形状是<path>
元素,因此对象边界框是与路径绘制的坐标系垂直且包含所有路径点的最小矩形。它不包括描边。例如,一条水平直线具有零高度的边界框;倾斜线具有边界框矩形,使得线是框的对角线。如果您的路径形状笨拙且/或与坐标系不太对齐,则边界框可能比路径大得多。
preserveAspectRatio
属性适用于图像和任何可以有viewBox
属性的元素:父级<svg>
、嵌套<svg>
、<symbol>
、<marker>
和<pattern>
。对于图像,它的纵横比是从图像固有的宽度:高度比率计算出来的,对于其他所有元素,则是从viewBox属性中的宽度:高度数字计算出来的。
preserveAspectRatio
属性将确定内容是被拉伸适应(none
),在一个维度上调整大小并在另一个维度上裁剪(slice
),还是缩小以适应两个维度并留有额外空间(meet
);对于meet
和slice
选项,您还需要指定如何在空间中对齐内容。none
。对于<image>
和<pattern>
元素,其默认值为xMidYMid meet
——即缩小以适应并居中。当然,此默认值仅在图案元素具有viewBox
属性时才会影响图案元素(否则,它被认为没有要保留的纵横比)。preserveAspectRatio
值将取决于图像和设计:
preserveAspectRatio="none"
?<pattern>
元素进行任何操作(没有视口框意味着没有纵横比控制),但您确实需要明确关闭图像上的纵横比控制。<pattern>
元素上设置viewBox
和preserveAspectRatio
属性;如果您想要与默认值不同的内容,请在<image>
上设置preserveAspectRatio
属性。这个示例展示了三种使图案填充形状的方法。
The top row has aspect control turned off.
<!-- pattern1 - no aspect ratio control -->
<pattern id="pattern1" height="100%" width="100%"
patternContentUnits="objectBoundingBox">
<image height="1" width="1" preserveAspectRatio="none"
xlink:href="/*url*/" />
</pattern>
The middle row has aspect ratio control on the <image>
element so the picture is cropped to fit the pattern, but the picture is still distorted when the pattern is drawn in the rectangle because the objectBoundingBox
units that define the coordinate system are different for height versus width. (The image in the circle isn't distorted because the circle's bounding box is a square.)
<!-- pattern2 - aspect ratio control on the image only -->
<pattern id="pattern2" height="100%" width="100%"
patternContentUnits="objectBoundingBox">
<image height="1" width="1" preserveAspectRatio="xMidYMid slice"
xlink:href="/*url*/" />
</pattern>
The bottom row has preserveAspectRatio
set on both the image and the pattern (and also a viewBox
set on the pattern). The image gets cropped but not stretched.
<!-- pattern3 - aspect ratio control on both image and pattern -->
<pattern id="pattern3" height="100%" width="100%"
patternContentUnits="objectBoundingBox"
viewBox="0 0 1 1" preserveAspectRatio="xMidYMid slice">
<image height="1" width="1" preserveAspectRatio="xMidYMid slice"
xlink:href="/*url*/" />
</pattern>
preserveAspectRatio
,但是通过这个回复,我学到了很多东西。 - raphaeltmhttps://jsfiddle.net/
替换 http://fiddle.jshell.net
。 - AmeliaBR
<feImage>
元素的过滤器大致相同。 - AmeliaBR