gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:40:07 27 4
gpt4 key购买 nike

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

How I want text to behave while animating it using SVG's textpath:

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

How i do not want text to behave while animating it using the SVG's textpath:

如何在保持文本水平的同时使用 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"处不动。

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

<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> ),你可以做到这一点。

然而,这有点痛苦,因为您必须单独为每个 Angular 色设置动画。

<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>

关于javascript - 如何为文本设置动画,使其在使用 SVG 的文本路径时不旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39530955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com