在不影响路径的情况下缩放 SVG

Scaling an SVG without affecting paths

我正在寻找一些关于在不影响内部路径的情况下缩放 SVG 容器的指示。我正在使用 Greensock.js 并希望 SVG 容器随字母本身的宽度缩放(不改变内部路径的纵横比)。最终目标是周围的字母与缩放字母一起洗牌。有关预期结果,请参见附加的 gif。

Scaling Letters Codepen

查看 Codepen,但这是我设置 HTML 的方式:

<div class="letters">
    <svg class="lcontainer" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 324 406">
        <rect class="bottom" x="46" y="353" width="278" height="53" />
        <rect width="53" height="406" />
    </svg>
    <svg class="econtainer" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 324 406">
        <rect class="outer" x="46" width="278" height="53" />
        <rect class="inner" x="46" y="247" width="240" height="53" />
        <rect class="outer" x="46" y="353" width="278" height="53" />
        <rect width="54" height="406" />
    </svg>
</div>

假设您控制 SVG,您需要重新制作每个 SVG,以便您可以将 SVG height/width 和 viewbox 转换为百分比。

一个例子是用 canvas 100px x 100px 重新制作 svg。然后,如果您将单位更改为 %em 等相对度量,路径上的点仍然是相对的,因为它们基于 100。

这有点痛苦,但它是我找到的在不弄乱路径的情况下缩放容器的最佳解决方案。

这里有一些 additional details with examples

感谢您的回复,正如 enxaneta 所建议的那样,我最终将所有路径放在一个 SVG 中并在其中使用它们。

我的最终答案是将它们分组,这样我就可以在字母的各个部分及其容器上使用 EventListeners,同时将它们保持在同一个 viewBox 中。

这是我的解决方案代码笔: https://codepen.io/leecranny/full/MLdBra

<div class="leah">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 982 408" height="400">
    <g class="hbox">
        <rect  x="797.125" width="53.55" height="408" />
        <rect id="h_middle" x="806.05" y="104.55" width="158.1" height="53.55" />
        <rect id="h_side" x="928.45" width="53.55" height="408" />
    </g>
    <g class="abox">
        <path id="a_short" d="M750.75169,405.5H698.34861V106.05385c0-29.082-18.01843-53.65077-39.34861-53.65077s-39.34861,24.56881-39.34861,53.65077V405.5H567.24831V106.05385C567.24831,47.57558,608.40754,0,659,0s91.75169,47.57558,91.75169,106.05385Z" transform="translate(0 1)"/>
        <path id="a_wide" d="M821.75169,405.5H769.34861V146.05385C769.34861,87,716,52.40308,659,52.40308S549.65139,91,549.65139,146.05385V405.5H497.24831V136.05385C497.24831,61,568,0,659,0S821.75169,55,821.75169,136.05385Z" transform="translate(0 1)"/>
        <rect id="a_middle" x="590.37692" y="299.5908" width="137.24615" height="52.40308"/>
    </g>
    <g class="ebox">
        <rect id="e_inner" x="243.03125" y="248" width="240" height="53" />
        <rect id="e_outer" x="243.03125" y="354" width="278" height="53" />
        <rect id="e_outer" x="243.03125" y="1" width="278" height="53" />
        <rect id="e_left" x="197.03125" y="1" width="54" height="406" />
    </g>
    <g class="lbox">
        <rect id="l_short" x="46" y="354" width="105" height="53" />
        <rect id="l_left" y="1" width="54" height="406" />
    </g>
</svg>