我需要一些关于这种特定动画的帮助.这是一个方形螺旋图案,不断向内延伸,直到完全完成.我确实设法让它继续运行,但我不知道如何正确地停止动画,而且我不确定它背后的数学是否基本有效/正确.
以下是我目前的情况:
function createSquareSpiralPath(
strokeWidth,
width,
height,
) {
const maxIterations = Math.trunc(Math.min(width, height) / 2 / strokeWidth); // ???
let path = '';
for (let i = 0; i <= maxIterations; i++) {
const step = strokeWidth * i;
const computed = [
`${step},${height - step}`,
`${step},${step}`,
`${width - step - strokeWidth},${step}`,
`${width - step - strokeWidth},${height - step - strokeWidth} `,
];
path += computed.join(' ');
}
return path.trim();
}
.spiral {
stroke-dasharray: 6130;
stroke-dashoffset: 6130;
animation: moveToTheEnd 5s linear forwards;
}
@keyframes moveToTheEnd {
to {
stroke-dashoffset: 0;
}
}
<svg viewBox="-10 -10 350 350" height="350" width="350">
<polyline class="spiral" points="
0,350 0,0 330,0 330,330 20,330 20,20 310,20 310,310 40,310 40,40 290,40 290,290 60,290 60,60 270,60 270,270 80,270 80,80 250,80 250,250 100,250 100,100 230,100 230,230 120,230 120,120 210,120 210,210 140,210 140,140 190,140 190,190 160,190 160,160 170,160 170,170"
style="fill:transparent;stroke:black;stroke-width:20" />
Sorry, your browser does not support inline SVG.
</svg>
我添加js函数只是为了演示如何生成点.正如你所看到的动画正是我想要的那样,我只是找不到一种方法来正确地包装它.此外,我不确定此函数是否会为不同的宽度/高度/冲程宽度生成正确的点.
非常感谢您的帮助!提前感谢.:)
附言:我找不到这个模式的数学术语(方形螺旋),所以我非常乐意学习如何正确地调用它.
Edit
基于@enxaneta答案(谢谢!)似乎我没有正确计算最大迭代次数.这可以在width !== height
时看到.我将研究如何生成这个值,也许这个公式不足以在没有任何空格的情况下正确地"停止"动画.