【发布时间】:2018-02-26 20:30:47
【问题描述】:
我正在尝试使用 stroke-dashoffset 和 stroke-dasharray 属性为线条设置动画。这是带有圆圈和线路连接器的复杂图形。我使用的是 Snapsvg,这是我的图,它被简化了,例如:
<svg version="1.1" id="fImg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 345 320">
<g id="balls">
<circle cx="125" cy="9.3" r="6.7"/>
<circle cx="230.2" cy="63.5" r="6.7"/>
<circle cx="211.6" cy="18.3" r="6.7"/>
<circle cx="292.6" cy="63.5" r="6.7"/>
</g>
<g id="lines">
<line class="st0" x1="103.8" y1="11.6" x2="125" y2="9.3"/>
<line class="st0" x1="103.8" y1="11.6" x2="115.8" y2="44.9"/>
<line class="st0" x1="103.8" y1="11.6" x2="85.9" y2="32.4"/>
<line class="st0" x1="85.9" y1="32.4" x2="115.8" y2="44.9"/>
</g>
</svg>
我使用 Snap 函数来设置圆的属性并为其设置动画。 lineDrow - 行的回调函数,但它只在.animate({.....}, 1000)之前有效:
var balls = Snap("#balls");
balls.attr({
fill:'rgba(0,0,0,0)'
}).animate({
fill: '#ccc'
}, 2000, mina.easeout, lineDraw);
function lineDraw() {
var lines = document.querySelectorAll("#lines line");
for (var i = 0; i < lines.length; i++) {
var line = Snap(lines[i]),
x = Math.ceil(line.getTotalLength());
line.attr({
'strokeDasharray': x,
'strokeDashoffset': x,
'stroke-width': 1,
'stroke-linecap':'round',
'stroke': '#ccc'
}).animate({
'strokeDasharray': 0,
'strokeDashoffset': 0
}, 1000);
}
}
它让我折痕!怎么了?
【问题讨论】:
标签: javascript animation svg snap.svg