如何根据svg标签的视框或高度宽度缩放绘图

时间:2013-11-11 14:56:27

标签: javascript jquery css html5 svg

嗨我正面临一个问题我有一个svg。当我尝试更改高度和宽度以及视图框的高度和宽度时,svg内的绘图将无法适应框

这是我的mysvg我给的是简短形式

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="500pt" height="500pt" viewBox="0 0 500 500" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <g transform="translate(528,270)" > 
        <g id="states" fill="green" width="500pt" height="500pt" >
            <a id="s01">
                <path d="M158.95067408594068,46.88327098850149L185.03303599629845,44.0148159910488L189.74121811302572,59.50889743727097L196.59587401503094,82.27898337817625L199.0518321008348,87.17077847298319L201.13017099189912,89.85649424407167L200.707578706067,91.7588142001174L202.6261541288344,92.6205139503571L200.33524838576966,95.29216920133321L200.7363444144292,97.59217497211156L199.80999341478454,100.8918397716738L202.09021078470892,106.20782432993735L201.64399529140977,111.17790897235179L204.03767592952832,115.96122130827978L196.3574723373462,117.09985438789514L163.47293491613115,121.08122011183377L163.22294648718562,123.55296427740802L167.13791879442175,126.6835133291724L166.871741037497,129.76735843938286L168.2485001228969,131.1400084527912L166.21795234496457,134.1137855808483L164.12121282499038,134.9547500732084L159.81791064191435,132.36796011584426L158.90469329765804,127.88713803963412L157.64560372254968,127.51168600895127L156.5390262005875,131.08669596034315L156.36678872306632,134.46030822963786L152.20800610122825,133.97284127048096L148.16895577705603,105.98121856614907L148.12136132417422,70.56398790998259L148.15893441899317,50.102043132249676L146.40831263672231,48.33943105796875Z" stroke="#FFFFFF" stroke-dasharray="1, 0" stroke-width="1.5"></path>
                <text x="162.66165594858754" y="86.92631614090374" style="">AL</text>
                <title></title>
            </a>
        </g>
    </g>
</svg>

当我更改宽度和高度或视图框或两者时,会发生这种情况 enter image description here \

现在宽度高度减小但绘图保持不变

2 个答案:

答案 0 :(得分:4)

假设初始高度和宽度为1000px。

<svg height="1000" width="1000" viewBox="0 0 1000 1000">

如果要调整svg及其中的所有元素的大小,请不要更改viewBox值。对于500px SVG:

<svg height="500" width="500" viewBox="0 0 1000 1000">

Example

How to Zoom and Pan with SVG

答案 1 :(得分:1)

解决方案与我昨天给你的答案相同。

Width height of the svg is not changing

请尝试按照以下步骤操作:

(1)将svg宽度和高度设置为500,并将preserveAspectRatio="none"添加到svg。

<svg width="500" height="500" preserveAspectRatio="none" ...>

这可能会让您的地图奇怪地延伸,但不要担心,这只是一个临时的改变,使下一步变得更容易。

(2)现在调整viewBox直到所有地图都可见。找到正确的值后,单独保留viewBox 。你不应该进一步改变它。

(3)删除preserveAspectRatio="none"。拉伸效果将消失。

(4)现在你可以将svg的宽度和高度从500x500调整到你喜欢的任何尺寸。无论你如何改变宽度和高度,整个地图都应该保持可见。