如何为文本设置动画,使其在使用 SVG 的文本路径时不旋转?

How do I animate a text so that it does not rotate while using the SVG's textpath?

我试图使用 SVG 的文本路径实现的是文本在遵循其动画路径时不旋转;所以它在动画时保持水平,像这样:

默认情况下,文本在动画时会旋转到其当前路径角度,使文本看起来像这样:

如何在保持文本水平的同时使用 SVG 的文本路径实现文本动画?

编辑:这里是我用于水平文本的代码;我实际上无法实现动画:

<svg>
<path id="MyPath" d="M 100 200
          C 200 100 300   0 400 100
          C 500 200 600 300 700 200
          C 800 100 900 100 900 100"/>

<use id="curve" xlink:href="#MyPath" fill="none" stroke="red"  />

<text id="origText" fill="white">
    <textpath xlink:href="#MyPath" >
  OH NOES!, DANCING TEXT ARRIVED!

        <animate attributeName="startOffset" from="100%" to ="-100%" 
             begin="0s" dur="10s" repeatCount="indefinite" keyTimes="0;1" 
             calcMode="spline" keySplines="0 0 1 1"/>
    </textpath>
</text>
</svg>

var t = document.getElementById('origText');
var t_text = t.textContent; // "We go up...."
var curve = document.getElementById("MyPath");
var len = curve.getTotalLength(); // curve length

var steps = len/t_text.length; // get width of step
var start_pt = 0; // start at beginning
var prev_pt = curve.getPointAtLength(0); // measure first step


t.textContent = ""; // clear up original text;

for (var i = 0; i < t_text.length; ++i) { // char loop
    var new_pt = curve.getPointAtLength(start_pt); // measure pt
    var ts = genTspan(t_text[i], prev_pt, new_pt); // tspan
    t.appendChild(ts); // add to <text>

    start_pt += steps; // go to next step (point)
    prev_pt = new_pt; // remember previous point
    }

function genTspan(myChar,prev_pt,new_pt) {
    var tspan = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
    tspan.setAttributeNS(null, 'dy', new_pt.y - prev_pt.y); 
    tspan.textContent = myChar;
 return tspan;
 }

我得到这段代码的来源是这个线程; horizontal text on path ,这实际上确实帮助我实现了将文本保持在路径上的水平,而不是在尝试使用 SVG 的文本路径对其进行动画处理时;尝试时,显示的文本保持在 x="0" y="0" 处不动。

与在其路径中移动时旋转角度的方法不同;那个实际上按预期动画,这里也是它的代码:

<svg>
    <path id="myPath2" fill="none" stroke="#FFFFFF" stroke-miterlimit="10"
    d="M91.4,344.2c3.2-3.4,18.4-0.6,23.4-0.6c5.7,0.1,10.8,0.9,16.3,2.3
c13.5,3.5,26.1,9.6,38.5,16.2c12.3,6.5,21.3,16.8,31.9,25.4c10.8,8.7,21,18.3,31.7,26.9c9.3,7.4,20.9,11.5,31.4,16.7
c13.7,6.8,26.8,9.7,41.8,9c21.4-1,40.8-3.7,61.3-10.4c10.9-3.5,18.9-11.3,28.5-17.8c5.4-3.7,10.4-6.7,14.8-11.5
c1.9-2.1,3.7-5.5,6.5-6.5"/>
<text fill="red">
    <textpath xlink:href="#myPath2" >
    PUMCHY PUMCHY PUMCHY PUMCHY!
    <animate attributeName="startOffset" from="100%" to ="-100%" begin="0s" 
    dur="10s" repeatCount="indefinite" keyTimes="0;1" calcMode="spline" keySplines="0 0 1 1"/>
    </textpath>
</text>
</svg>

<textPath> 无法使字符保持垂直。但是如果你使用运动路径(即<animateMotion>)就可以做到。

然而,这有点痛苦,因为您必须单独为每个角色制作动画。

<svg width="500px" height="500px">

    <path id="myPath2" fill="none" stroke="lightgrey" stroke-miterlimit="10"
    d="M91.4,344.2c3.2-3.4,18.4-0.6,23.4-0.6c5.7,0.1,10.8,0.9,16.3,2.3
c13.5,3.5,26.1,9.6,38.5,16.2c12.3,6.5,21.3,16.8,31.9,25.4c10.8,8.7,21,18.3,31.7,26.9c9.3,7.4,20.9,11.5,31.4,16.7
c13.7,6.8,26.8,9.7,41.8,9c21.4-1,40.8-3.7,61.3-10.4c10.9-3.5,18.9-11.3,28.5-17.8c5.4-3.7,10.4-6.7,14.8-11.5
c1.9-2.1,3.7-5.5,6.5-6.5"/>

  <text fill="red" text-anchor="middle">!
    <animateMotion dur="6s" repeatCount="indefinite">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>

  <text fill="red" text-anchor="middle">Y
    <animateMotion dur="6s" repeatCount="indefinite" begin="0.2s">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>

  <text fill="red" text-anchor="middle">H
    <animateMotion dur="6s" repeatCount="indefinite" begin="0.4s">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>

  <text fill="red" text-anchor="middle">C
    <animateMotion dur="6s" repeatCount="indefinite" begin="0.6s">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>

  <text fill="red" text-anchor="middle">N
    <animateMotion dur="6s" repeatCount="indefinite" begin="0.8s">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>

  <text fill="red" text-anchor="middle">U
    <animateMotion dur="6s" repeatCount="indefinite" begin="1.0s">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>

  <text fill="red" text-anchor="middle">P
    <animateMotion dur="6s" repeatCount="indefinite" begin="1.2s">
      <mpath xlink:href="#myPath2"/>
    </animateMotion>
  </text>
</svg>