我对SVG非常陌生,如果这是一个显而易见的问题,我很抱歉。我已经尝试了几个小时,似乎已经遇到了障碍。
我正试图在HTML文档中创建一个SVG元素,它具有以下特点:
- 在外部视口上有一个固定的边界
- 保留内部元素的长宽比
- 不保留外部SVG元素的纵横比
- 可以任意调整大小,同时保持这些约束条件
这个JSFiddle演示了我的意思和我的尝试:
这里是相同的代码,因为我无法发布它:
<div>
<h2>correct placement and aspect ratio, but cannot be resized without losing relative placement</h2>
<svg viewBox="0 0 100 100" style="width: 100px; height: 100px; border: 1px solid red;">
<circle cx="0" cy="0" r="10"></circle>
<circle cx="50" cy="50" r="10"></circle>
<circle cx="100" cy="100" r="10"></circle>
</svg>
</div>
<div>
<h2>correct aspect ratio of circle, incorrect relative placement</h2>
<svg viewBox="0 0 100 100" preserveAspectRatio="xMinYMin" style="width: 200px; height: 100px; border: 1px solid red;">
<circle cx="0" cy="0" r="10"></circle>
<circle cx="50%" cy="50%" r="10"></circle>
<circle cx="100%" cy="100%" r="10"></circle>
</svg>
</div>
<div>
<h2>correct relative placement, can be resized, but loses internal aspect ratio</h2>
<svg viewBox="0 0 100 100" preserveAspectRatio="none" style="width: 200px; height: 100px; border: 1px solid red;">
<circle cx="0" cy="0" r="10"></circle>
<circle cx="50" cy="50" r="10"></circle>
<circle cx="100" cy="100" r="10"></circle>
</svg>
</div>
这可行吗?我走错路了吗?