【发布时间】:2017-06-25 21:09:24
【问题描述】:
我正在尝试为 SVG 文档中的图像元素实现几种图形拉伸模式。 需要注意的是,该解决方案无需 JavaScript 和 CSS 即可工作,而且我对图像的尺寸(仅关于必须放置图像的空间)一无所知。
对于 Uniform、UniformToFill 和 Fill 这真的没问题。下面的示例将图像放置在 400x100 像素的区域上:
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
<!-- GraphicStretchMode: UniformToFill -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink" x="10" y="10" width="400" height="100">
<image xlink:href="https://thereforeigeek.files.wordpress.com/2013/07/nessy.jpg" height="100%" width="100%" preserveAspectRatio="xMidYMid slice"/>
<rect x="0" y="0" width="400" height="100" fill="transparent" stroke="#630" stroke-width="5px"/>
</svg>
<!-- GraphicStretchMode: Fill -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink" x="10" y="130" width="400" height="100">
<image xlink:href="https://thereforeigeek.files.wordpress.com/2013/07/nessy.jpg" height="100%" width="100%" preserveAspectRatio="none"/>
<rect x="0" y="0" width="400" height="100" fill="transparent" stroke="#630" stroke-width="5px"/>
</svg>
<!-- GraphicStretchMode: Uniform -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink" x="10" y="250" width="400" height="100">
<image xlink:href="https://thereforeigeek.files.wordpress.com/2013/07/nessy.jpg" height="100%" width="100%" preserveAspectRatio="xMidYMid meet"/>
<rect x="0" y="0" width="400" height="100" fill="transparent" stroke="#630" stroke-width="5px"/>
</svg>
</svg>
尽管如此,我没有完成拉伸模式 None(未缩放、未拉伸、原始大小但居中!)。 (这是我最初认为最简单的情况)
有什么提示或想法吗?
【问题讨论】:
标签: image svg alignment aspect-ratio