简体   繁体   English

如何将SVG剪切路径调整为与图像相同的大小

[英]How to resize SVG clip-path to be same size as image

I've got little problem resizing my svg clip-path to fit the image size. 我几乎没有问题来调整我的svg剪切路径以适合图像大小。

I've got code like this: 我有这样的代码:

<svg id="image-svg" class="clip">
     <img class="main-img" src="http://25.media.tumblr.com/tumblr_m5nre6cxkQ1qbs7p5o1_r1_500.jpg" alt="" />
</svg>
<svg class="clip">
     <clipPath id="clipPolygon">
          <polygon points="52 0,100 45,50 100,0 50">
          </polygon>
     </clipPath>
</svg>

And then i'm using css like this: 然后我正在使用这样的CSS:

#image-svg {
  left:0;
  top:0;
}
.main-img {
  clip-path: url('#clipPolygon');
  width:90%;
}

Everything works fine, except the clip-path is much smaller then image itself. 一切工作正常,只是剪切路径比图像本身小得多。 How to fix this? 如何解决这个问题? Here is my working fiddle: 这是我的工作小提琴:

https://jsfiddle.net/7egbccpw/ https://jsfiddle.net/7egbccpw/

A solution is to specify the path directly with CSS and use % for the values 一种解决方案是直接使用CSS指定路径,并将%用作值

 .main-img { clip-path: polygon(50% 0%, 100% 45%, 50% 100%, 0 50%); } 
 <img class="main-img" src="https://lorempixel.com/200/200/" alt="" /> <img class="main-img" src="https://lorempixel.com/100/100/" alt="" /> <img class="main-img" src="https://lorempixel.com/50/50/" alt="" /> 

Firstly, your example is broken because there is no <img> element in SVGs. 首先,您的示例已损坏,因为SVG中没有<img>元素。 You want the <image> element. 您需要<image>元素。

 #image-svg { left:0; top:0; } .main-img { clip-path: url('#clipPolygon'); width:90%; } 
 <svg id="image-svg" class="clip"> <image class="main-img" xlink:href="http://25.media.tumblr.com/tumblr_m5nre6cxkQ1qbs7p5o1_r1_500.jpg" alt="" /> </svg> <svg class="clip"> <clipPath id="clipPolygon"> <polygon points="52 0,100 45,50 100,0 50"></polygon> </clipPath> </svg> 

Now I presume you mean you want the clip path to automatically fit the image size. 现在,我假设您的意思是您希望剪辑路径自动适合图像尺寸。 Otherwise I am guessing you would have just made the clip path coordinates bigger. 否则,我想您会把剪切路径的坐标做得更大。

The way you do that in SVG is to use objectBoundingBox coordinates. 在SVG中执行此操作的方法是使用objectBoundingBox坐标。 When objectBoundingBox coordinate mode in in effect, coordinates are scaled so that (0,0) represents the top left of the target element, and (1,1) represents the bottom right of the target element. 启用objectBoundingBox坐标模式时,将缩放坐标,以使(0,0)代表目标元素的左上角,而(1,1)代表目标元素的右下角。 In this case, the target element is the image. 在这种情况下,目标元素是图像。

For use this mode for clip paths, you need to set clipPathUnits="objectBoundingBox" . 要将此模式用于剪辑路径,您需要设置clipPathUnits="objectBoundingBox" Then you just need to scale all your polygon coordinate values so that the are between 0 and 1. 然后,您只需要缩放所有多边形坐标值,以使它们在0到1之间。

 #image-svg { width: 500px; height: 500px; } .main-img { clip-path: url('#clipPolygon'); width:90%; height: 90%; } 
 <svg id="image-svg" class="clip"> <image class="main-img" xlink:href="http://25.media.tumblr.com/tumblr_m5nre6cxkQ1qbs7p5o1_r1_500.jpg" alt="" /> </svg> <svg class="clip"> <clipPath id="clipPolygon" clipPathUnits="objectBoundingBox"> <polygon points="0.52 0 1 0.45 0.5 1 0 0.5"></polygon> </clipPath> </svg> 

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

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