简体   繁体   English

SVG模式平铺图像

[英]SVG pattern tiled image

Hi I am really new to SVG's and creating assets using SVG's. 嗨,我真的是SVG的新手,并使用SVG创建资产。 This is my first attempt and what I have is a rounded rectangle with a gif of water as the background. 这是我的第一次尝试,我所拥有的是一个以gif为水的圆角矩形作为背景。 I was wondering how to modify this code in order to make it not have those gaps. 我想知道如何修改此代码,以使其没有那些差距。

 <svg id="water" width="440" height="440"> <defs> <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100"> <image xlink:href="https://i.imgur.com/u7ufQto.gif" x="0" y="0" width="100" height="100" /> </pattern> </defs> <path d="M100,100 h200 a20,20 0 0 1 20,20 v200 a20,20 0 0 1 -20,20 h-200 a20,20 0 0 1 -20,-20 v-200 a20,20 0 0 1 20,-20 z" fill="url(#img1)" stroke="black" stroke-width="3" /> </svg> 

The problem is that your GIF is 251x132 pixels, but you are asking the SVG to draw the image at 100x100. 问题是您的GIF为251x132像素,但是您正在要求SVG以100x100绘制图像。

By default SVG won't squeeze or stretch an image to the the size you ask. 默认情况下,SVG不会将图像压缩或拉伸到您要求的尺寸。 It will keep the image at the same aspect ratio. 它将使图像保持相同的纵横比。 So in your case, the image is being scaled down to 100x53, which leaves a blank space above and below. 因此,在您的情况下,图像被缩小为100x53,在上方和下方都留有空白。

I presume you don't want to distort the water animation, so the solution is to set your <image> and <pattern> widths and heights to 251x132. 我想您不想扭曲水上动画,因此解决方案是将<image><pattern>宽度和高度设置为251x132。

 <svg id="water" width="440" height="440"> <defs> <pattern id="img1" patternUnits="userSpaceOnUse" width="251" height="132"> <image xlink:href="https://i.imgur.com/u7ufQto.gif" x="0" y="0" width="251" height="132" /> </pattern> </defs> <path d="M100,100 h200 a20,20 0 0 1 20,20 v200 a20,20 0 0 1 -20,20 h-200 a20,20 0 0 1 -20,-20 v-200 a20,20 0 0 1 20,-20 z" fill="url(#img1)" stroke="black" stroke-width="3" /> </svg> 

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

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