【发布时间】:2023-03-16 10:44:01
【问题描述】:
我正在尝试将<path> 动画水平和垂直居中(在使用transform="scale(0.5)) 将其缩小后,同时将<svg> 保持在其容器的100% 宽度和高度。我正在使用Snap.svg .
正如您在下面看到的,<svg> 很好,但<path> 全部塞在左上角。
var s = Snap("#me");
var myPath = s.select("#mypath");
function reset( el ) {
el.stop();
el.attr({ "stroke-dashoffset": 125 });
};
function startAnim( el ) {
el.animate( { "stroke-dashoffset": 600 }, 1000 );
};
reset( myPath );
s.mouseover( function() {
startAnim( myPath );
} );
s.mouseout( function() {
reset( myPath );
} );
.test {
border: 1px solid black;
height: 500px;
width: 500px;
}
#me {
border: 2px solid green;
}
#mypath {
border: 2px solid blue;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
}
<script src="http://tedbeer.net/lib/snap.svg-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
<div class="pie">
<svg id="me" viewBox="0 0 350 350">
<!--<svg id="me" viewBox="0 0 350 350" width="100" height="100">-->
<path id="mypath" d="M 175, 175 m 0, -75 a 75, 75 0 1, 0 0, 150 a 75, 75 0 1, 0 0, -150" fill="none" stroke="#ccc" stroke-width="150" stroke-dasharray="0 600 600 0" stroke-dashoffset="1000" transform="scale(0.5)">
</path>
</svg>
</div>
</div>
【问题讨论】:
-
如果您围绕其中心对比例进行变换会发生什么?
-
这和你前面提到的
element.getBBox()有关系吗? -
你能解释一下吗?
-
我想知道你是否做了类似 myPath.transform('s0.5,0.5,175,175'); jsfiddle.net/poyt9qew 这取决于你是想使用标记还是快照或其他任何东西
-
这似乎很好地解决了这个问题......谢谢伙计!