如何缩放 SVG 路径

2023-12-31

我尝试缩放 svg 路径之类的元素。但缩放对于 div 元素工作正常,不适用于 svg 路径元素。我在下面附上了我的代码。有什么建议吗?

<style>
    .two {
        transition: all 2s ease-in-out 0.5s;
        -webkit-transition: all 2s ease-in-out 0.5s;
    }
    
    #scale {
        height: 150px;
        width: 100px;
        text-align: center;
        margin: 0 auto;
    }
    
    #scale {
        border: 1px blue solid;
    }
    
    .grow:hover {
        transform: scale(2.0);
        -ms-transform: scale(2.0);
        -webkit-transform: scale(2.0);
    }
</style>
<body>
    <svg width="1350" height="900">
        <path d="M 673 248.625 A 67.875 67.875 0 0 1 740.1841400272543 326.159552463664 L 673 316.5 A 0 0 1 0 0 673 316.5 z" id="scale" class="two grow"></path>
    </svg>
</body>

你的代码相当糟糕。你不需要添加<body> or <style>标签作为开始。特别是,它看起来像额外的<style>标签已做出声明.two类无法解析。

另一个问题是 CSS 属性如border不适用于 SVG 元素。尝试使用stroke and/or stroke-width反而。

也许主要问题是您的 SVG 内容与原点偏移了很远。当您将其放大 2 倍时,基本上只是将所有坐标加倍。结果,绘图从 SVG 视图框的右下角消失。解决这个问题最简单的方法是使用<g>元素重新定位 SVG 原点。

这是一个简单的示例,其中一个三角形位于 SVG 的中间:

.two {
  transition: all 2s ease-in-out 0.5s;
  -webkit-transition: all 2s ease-in-out 0.5s;
}
#scale {
  height: 150px;
  width: 100px;
  text-align: center;
  margin: 0 auto;
}
#scale {
  fill: yellow;
  stroke: blue;
  stroke-width: 2px;
}
.grow:hover {
  transform: scale(2.0);
  -ms-transform: scale(2.0);
  -webkit-transform: scale(2.0);
}
<svg width="220" height="220">
  <g transform="translate(110,110)">
    <path d="M0 -43.3 50 43.3 -50 43.3Z"
          id="scale" class="two grow" />
  </g>
</svg>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何缩放 SVG 路径 的相关文章

随机推荐