我有两个点 (A和B) 和连接它们的贝塞尔曲线。线条绘制动画在文档加载后立即开始。
svg path {
fill: none;
stroke: cornflowerblue;
stroke-width: 1;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 100;
stroke-dashoffset: 100;
animation: path 5s linear forwards;
}
@keyframes path {
to {
stroke-dashoffset: 0;
}
}
<svg width="500" height="300" viewBox="15 15 150 50">
<circle id="a" cx="26.5" cy="13.2" r="4" fill="cornflowerblue" cursor="pointer"></circle>
<circle id="b" cx="87.3" cy="23.8" r="4" fill="cornflowerblue" cursor="pointer"></circle>
<text y="15" x="18" fill="cornflowerblue" stroke="none" stroke-width="0.1" font-size="6px">A</text>
<text y="26" x="92" fill="cornflowerblue" stroke="none" stroke-width="0.1" font-size="6px">B</text>
<path d="m 26.458333,13.229167 c 2.776366,16.871891 9.790064,18.199908 18.520833,13.229166 L 63.499999,13.229167 C 77.382834,4.1082137 88.663388,0.52362484 87.312499,23.8125 v 0"stroke="cornflowerblue" stroke-width="1" fill="none" id="mPath"></path>
</svg>
问题:如何在单击某个点时开始绘制一条线的动画A?有兴趣通过问题标签中指示的任何方式和技术详细描述此类实现和实现本身,这可以是 and CSS, and SMIL SVG, 以及使用 and JavaScript。
我们挂在带有
id=a点击处理程序的元素上,然后我们寻找父元素svg并给它一个 classactive,它开始动画(在 css 中path,我们svg用 class开始动画active)。纯 SVG SMIL
线条绘制动画基于
stroke-dashoffset从最大值减小到零的技术。因此,实现了线从零增长到全长的动画。getTotalLength()console.log(p1.getTotalLength());我们明白了~=91px, 在哪里
begin="a.click"是通过单击圆圈来启动动画的条件id="a"来回动画
增加了
values="91;0;0;91;91",也就是从最大值-91到0和返回到0重复值\u200b\u200b在极端位置提供停顿