SVG viewBox缩放和裁剪

时间:2014-11-24 09:23:21

标签: html svg crop scaling

如何从SVG输出某个地方并进行缩放?

电子。 g。,我有以下带SVG的HTML(代码副本及其渲染位于http://jsfiddle.net/5e0pas9m/)。

<div style="border: 1px solid black;">1
<svg width="100" height="100" viewBox="0 0 200 200" style="border: 1px solid red; transform: scale(1);" preserveAspectRatio="xMidYMid none">
  <rect width="100%" height="100%" style="fill:rgb(200,200,255);stroke-width:4;stroke:rgb(100,100,255)" />
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  <circle cx="150" cy="150" r="40" stroke="red" stroke-width="4" fill="yellow" />
</svg>
</div>

<div style="border: 1px solid black;">2
<svg width="100" height="100" viewBox="0 0 100 100" style="border: 1px solid red; transform: scale(1);" preserveAspectRatio="xMidYMid none">
  <rect width="100%" height="100%" style="fill:rgb(200,200,255);stroke-width:4;stroke:rgb(100,100,255)" />
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  <circle cx="150" cy="150" r="40" stroke="red" stroke-width="4" fill="yellow" />
</svg>
</div>

<div style="border: 1px solid black;">3
<svg width="100" height="100" viewBox="100 100 200 200" style="border: 1px solid red; transform: scale(1);" preserveAspectRatio="xMidYMid none">
  <rect width="100%" height="100%" style="fill:rgb(200,200,255);stroke-width:4;stroke:rgb(100,100,255)" />
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  <circle cx="150" cy="150" r="40" stroke="red" stroke-width="4" fill="yellow" />
</svg>
</div>

<div style="border: 1px solid black;">4
<svg width="100" height="100" style="border: 1px solid red; transform: scale(1);" preserveAspectRatio="xMidYMid none">
  <g transform="scale(1), translate(-100 -100)">
  <rect width="100%" height="100%" style="fill:rgb(200,200,255);stroke-width:4;stroke:rgb(100,100,255)" />
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  <circle cx="150" cy="150" r="40" stroke="red" stroke-width="4" fill="yellow" />
  </g>
</svg>
</div>

div-1显示我的完整图像。

如果我只想在同一窗口中显示左上角,我会生成viewBox =&#34; 0 0 100 100&#34; (div-2)它给出了我想要的东西:图像的一部分适合窗口。

但是当我对右下角做同样的事情时(div-3代表viewBox =&#34; 100 100 200 200&#34;),图像只会被翻译但不适合/缩放/缩放。< / p>

div-4 nealry展示了我通过另一种技术实现目标的一个例子。

1 个答案:

答案 0 :(得分:1)

viewBox的第三个和第四个参数是width和height,而不是maxX和maxY。因此,右下角示例的viewBox应为&#34; 100 100 100 100&#34;。

相关问题