Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

Can i create a circle that follow an existing line in svg?

As the title suggests, i'm trying to create an animation in svg with a point/circle that follows an irregular path. I am using the animateTransform, for example:

<animateTransform attributeName="transform"
                  attributeType="XML"
                  type="translate"
                  from="0 60 70"
                  to="360 60 70"
                  dur="10s"
                  repeatCount="definite"/>

Can i implement this method (in svg) to let the circle to follow the path, or i have to use necessarily javascript? If i can do it in svg could you give me a basic example with an explanation?

question from:https://stackoverflow.com/questions/65842420/can-i-create-a-circle-that-follow-an-existing-line-in-svg

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the <animateMotion> element.

Example adapted from MDN:

<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
  <path id="path" fill="none" stroke="lightgrey"
    d="M20,50 C20,-50 180,150 180,50 C180-50 20,150 20,50 z" />

  <circle r="5" fill="red">
    <animateMotion dur="10s" repeatCount="indefinite">
      <mpath xlink:href="#path"/>
    </animateMotion>
  </circle>
</svg>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...