如何使SVG TextPath与动画路径一起使用?

发布于 2025-01-28 14:57:31 字数 1404 浏览 1 评论 0原文

请在此处查看演示: https://codepen.io/bsoutendendijk/pen/pen/pen/govlmaq

行为:单击“更新路径”按钮后,路径将更改,并且文本将在动画结束时沿着路径出现。

实际行为:文本在加载时正确出现在路径上,但是在单击“更新路径”时,文本不会出现在正确的位置。

代码:

const path = document.getElementById('myPath');
const button = document.getElementById('myButton');

button.addEventListener('click', () => {
  console.log('clicked!');
  path.setAttribute('d', getRandomCurve()); 
})

function getRandomCurve() {
  const curveSize = Math.round(Math.random() * 300);
  
  return `M 100 350 q ${curveSize} -300 300 0`
}
#myPath {
  transition: all ease-out 2s;
}
<div>
  <button id="myButton">Update Path</button>
  <svg height="400" width="450">
    <path id="myPath" d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" />
    <text style="font-size: 2em;">
      <textPath startOffset="30%" text-anchor="middle" href="#myPath">
        Hello World
      </textPath>
    </text>
  </svg>
</div>

如何以一种可以与动画路径一起使用的方式使用TextPath?

Please view the demo here: https://codepen.io/bsoutendijk/pen/gOvLMaq

Expected behavior: After clicking "update path" button, the path will change, and the text will appear along the path by the end of the animation.

Actual behavior: The text appears correctly on the path upon load, but when clicking update path, the text does not appear in the correct place.

code:

const path = document.getElementById('myPath');
const button = document.getElementById('myButton');

button.addEventListener('click', () => {
  console.log('clicked!');
  path.setAttribute('d', getRandomCurve()); 
})

function getRandomCurve() {
  const curveSize = Math.round(Math.random() * 300);
  
  return `M 100 350 q ${curveSize} -300 300 0`
}
#myPath {
  transition: all ease-out 2s;
}
<div>
  <button id="myButton">Update Path</button>
  <svg height="400" width="450">
    <path id="myPath" d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" />
    <text style="font-size: 2em;">
      <textPath startOffset="30%" text-anchor="middle" href="#myPath">
        Hello World
      </textPath>
    </text>
  </svg>
</div>

How do I use textPath in a way that it can work with animated paths?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2025-02-04 14:57:32
const path = document.getElementById("myPath2");

function getRandomCurve() {
    const curveSize = Math.round(Math.random() * 300);

    return `M 100 350 q ${curveSize} -300 300 0`;
}

setInterval(() => {
    const pathDef = getRandomCurve();
    path.setAttribute("d", pathDef);
}, 1000)
.anim {
            transition: 1s;
        }
<div>
    <svg height="400" width="450">
    <path id="myPath2" class="anim" d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" />
    <text x="0" y="0" style="font-size: 2em;">
        <textPath startOffset="30%" href="#myPath2" class="anim">
            Hello World
            <animateTransform attributeName="transform"  dur="2s" repeatCount="indefinite"/>
        </textPath>
    </text>
</svg>
</div>

哦,我可以做到:)

“ AnimAtetransform”可以解决您的问题。
“重复” - 重要属性,尝试将其删除,您的动画将在“ dur”之后停止

const path = document.getElementById("myPath2");

function getRandomCurve() {
    const curveSize = Math.round(Math.random() * 300);

    return `M 100 350 q ${curveSize} -300 300 0`;
}

setInterval(() => {
    const pathDef = getRandomCurve();
    path.setAttribute("d", pathDef);
}, 1000)
.anim {
            transition: 1s;
        }
<div>
    <svg height="400" width="450">
    <path id="myPath2" class="anim" d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" />
    <text x="0" y="0" style="font-size: 2em;">
        <textPath startOffset="30%" href="#myPath2" class="anim">
            Hello World
            <animateTransform attributeName="transform"  dur="2s" repeatCount="indefinite"/>
        </textPath>
    </text>
</svg>
</div>

Oh, i can do it :)

"animateTransform" can fix your problem.
"repeatCount" - important attribute, try to remove it and your animation will stop after "dur"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文