简体   繁体   English

在多边形点路径(svg)内调整图像大小并使其不被剪裁

[英]Resize image inside polygon point path(svg) and make it not clipped

I want to achive the following:我想实现以下目标:

Resize the image inside the svg element to perfectly fit inside a polygon, and furthermore that it is fully viewable, and not clipped ( see jsfiddle).调整 svg 元素内的图像大小以完全适合多边形,而且它是完全可见的,而不是剪裁的(参见 jsfiddle)。

I have gone through many stackoverflow questions but cannot figure it out:我经历了许多计算器溢出问题,但无法弄清楚:

Code:代码:

 <svg width="50%" height="50%" viewBox="0 0 25 10" preserveAspectRatio="none"> <defs> <pattern id="im1" width="100%" height="100%" preserveAspectRatio="none"> <image preserveAspectRatio="none" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://dummyimage.com/600x400/abc" width="100%" height="100%"></image> </pattern> </defs> <polygon points="0,10 29, 10 10, 0" x="0" y="0" style="fill:url(#im1);"></polygon> </svg>

See https://jsfiddle.net/f8ktzyLw/https://jsfiddle.net/f8ktzyLw/

Can someone point me in the right direction ?有人可以指出我正确的方向吗? is this achievable with svg only or do i need JavaScript/Canvas ?这只能通过 svg 实现还是我需要 JavaScript/Canvas?

Polygon size 29px horizontal is 4px larger than viewBox = "0 0 25 10"多边形大小 29px 水平比 viewBox = "0 0 25 10" 大 4px

I added a gray frame that shows the boundaries of the SVG canvas我添加了一个灰色框来显示 SVG 画布的边界

 <svg width="50%" height="50%" viewBox="0 0 25 10" preserveAspectRatio="none" style="border:1px solid gray"> <defs> <pattern id="im1" width="100%" height="100%" preserveAspectRatio="none"> <image preserveAspectRatio="none" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://dummyimage.com/600x400/abc" width="100%" height="100%"></image> </pattern> </defs> <polygon points="0,10 29, 10 10, 0" x="0" y="0" style="fill:url(#im1);"></polygon> </svg>

To make the polygon fully fit inside the canvas of the SVG, it is necessary to increase the size of the SVG horizontally by 4px viewBox="0 0 29 10"为了使多边形完全适合 SVG 的画布,需要将 SVG 的大小水平增加 4px viewBox="0 0 29 10"

 <svg width="50%" height="50%" viewBox="0 0 29 10" preserveAspectRatio="none" style="border:1px solid gray"> <defs> <pattern id="im1" width="100%" height="100%" preserveAspectRatio="none"> <image preserveAspectRatio="none" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://dummyimage.com/600x400/abc" width="100%" height="100%"></image> </pattern> </defs> <polygon points="0,10 29, 10 10, 0" x="0" y="0" style="fill:url(#im1);"></polygon> </svg>

Or you can leave the dimensions of the viewBox="0 0 25 10 unchanged, but then you need to reduce the horizontal size of the polygon by the same 4px或者您可以保持viewBox="0 0 25 10的尺寸不变,但是您需要将多边形的水平尺寸减小相同的4px

 <svg width="50%" height="50%" viewBox="0 0 25 10" preserveAspectRatio="none" style="border:1px solid gray"> <defs> <pattern id="im1" width="100%" height="100%" preserveAspectRatio="none"> <image preserveAspectRatio="none" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://dummyimage.com/600x400/abc" width="100%" height="100%"></image> </pattern> </defs> <polygon points="0,10 25, 10 10, 0" x="0" y="0" style="fill:url(#im1);"></polygon> </svg>

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

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