简体   繁体   English

围绕矩形路径循环 SVG 文本 animation

[英]Loop SVG text animation around rectangle path

I am using SVG to animate text around the path of a rounded rectangle.我正在使用 SVG 为圆角矩形路径周围的文本设置动画。 The idea is to have a stream of text that is constantly moving around the rectangle.这个想法是让 stream 的文本不断地围绕矩形移动。

Here's an example of the rectangle animation in this tutorial video for After Effects这是此 After Effects 教程视频中矩形 animation 的示例

But when closing the SVG path for the text, the animation does not run at all and if I leave the path open the text disappears as it reaches the end as in this code snippet:但是,当关闭文本的 SVG 路径时,animation 根本不会运行,如果我让路径保持打开状态,文本会在到达末尾时消失,如以下代码片段所示:

 html, body { background: black; margin: 0 auto; }.container { widht: 100%; background: red; }.svgwave { margin-left: calc(50% - 150px); margin-top: 100px; }
 <div class="wrapper"> <svg class="svgwave" xmlns="http://www.w3.org/2000/svg" width="301" height="301" viewBox="0 0 301 301" style="width:auto; height: auto; overflow: visible;"> <path id="wavepath" d="M145.5 301.5H13C6.09645 301.5 0.5 295.904 0.5 289V13C0.5 6.09645 6.09644 0.5 13 0.5H289C295.904 0.5 301.5 6.09644 301.5 13V289C301.5 295.904 295.904 301.5 289 301.5H156.5" style="fill: transparent; stroke: transparent; stroke-width: 1px;"></path> <foreignObject x='6' y='6' width='300px' height='300px'> <div style="width: 282px; height: 282px; border-radius: 8px; background-size: contain; border: 4px solid white; display:inline-block; " ></div> </foreignObject> <text text-anchor="middle" style="text-transform: uppercase; font-family: Arial; font-size: 20px; fill: white;"> <textPath style=" fill-opacity: 1" href="#wavepath" side="left" startOffset="0%"> <animate attributeName="startOffset" from="30%" to="42%" begin="0s" dur="4s" repeatCount="indefinite"></animate> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> </textPath> </text> </svg> </div>

  • What would be the best way to achieve this animation using SVG?使用 SVG 实现此 animation 的最佳方法是什么?
  • Would there be any alternative way to achieve this with Javascript?有没有其他方法可以用 Javascript 实现这一目标?

If you extend the path and add textLength settings to make sure that the text wraps perfectly - and tweak a few other things you can get this to look better.如果您扩展路径并添加 textLength 设置以确保文本完美环绕 - 并调整一些其他内容,您可以使它看起来更好。 Still has a tiny seam jitter, but it's not that noticeable.仍然有微小的接缝抖动,但不是那么明显。

 html, body { background: black; margin: 0 auto; }.container { widht: 100%; background: red; }.svgwave { margin-left: calc(50% - 150px); margin-top: 100px; }
 <svg class="svgwave" xmlns="http://www.w3.org/2000/svg" width="301" height="301" viewBox="0 0 301 301" style="width:auto; height: auto; overflow: visible;"> <path id="wavepath" d="M145.5 301.5H13C6.09645 301.5 0.5 295.904 0.5 289V13C0.5 6.09645 6.09644 0.5 13 0.5H289C295.904 0.5 301.5 6.09644 301.5 13V289C301.5 295.904 295.904 301.5 289 301.5H156.5 H13C6.09645 301.5 0.5 295.904 0.5 289V13C0.5 6.09645 6.09644 0.5 13 0.5H289C295.904 0.5 301.5 6.09644 301.5 13V289C301.5 295.904 295.904 301.5 289 301.5H156.5 H13C6.09645 301.5 0.5 295.904 0.5 289V13C0.5 6.09645 6.09644 0.5 13 0.5H289C295.904 0.5 301.5 6.09644 301.5 13V289C301.5 295.904 295.904 301.5 289 301.5H156.5 H13C6.09645 301.5 0.5 295.904 0.5 289V13C0.5 6.09645 6.09644 0.5 13 0.5H289C295.904 0.5 301.5 6.09644 301.5 13V289C301.5 295.904 295.904 301.5 289 301.5H156.5" style="fill: transparent; stroke: transparent; stroke-width: 1px;" ></path> <foreignObject x='6' y='6' width='300px' height='300px'> <div style="width: 282px; height: 282px; border-radius: 8px; background-size: contain; border: 4px solid white; display:inline-block; " ></div> </foreignObject> <text text-anchor="left" style="text-transform: uppercase; font-family: Arial; font-size: 20px; fill: white;"> <textPath style=" fill-opacity: 1" href="#wavepath" side="left" startOffset="0%" textLength="1175"> <animate attributeName="startOffset" from="20%" to="42%" begin="0s" dur="12s" repeatCount="indefinite"></animate> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> First <tspan style="fill: #DED279;">Second</tspan> </textPath> </text> </svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM