4

I'm trying to animate one of SVG-line endpoints to move along certain path, while the other endpoint stands still, so the line is stretching and shrinking, while staying straight.

What I achieved so far is to make my entire line move along the path with one of endpoints bound to it:

<svg viewBox="0 0 500 500">
  <path stroke="grey" fill="none" id="route" d="M50,25 l25,30 l-40,20 z" />
  <g>
  <line x1="0" y1="0" x2="150" y2="50" stroke="blue" />
  <circle r=5 fill="blue" />
  <text x="-5" y="-10">A</text>
  <circle cx="150" cy="50" r="5" fill="blue" />
  <text x="145" y="40">B</text>
    <animateMotion dur="5s" repeatCount="indefinite" >
      <mpath xlink:href="#route" />
    </animateMotion>
  </g>
</svg>

What I want to get is point A moving along the path and point B standing still.

I'd gladly consider CSS/JavaScript solution, but libraries are not an option.

Would you, please, point me to the right direction?

1 Answers1

6

For your specific example, we can do it by using the values="..." attribute of the <animate/> tag. Your path is quite a simple example so creating this values="..." list is quite trivial.

If you want to to do this more generally, for any path, then you likely need to build a JavaScript parser of the d path and turn that into a list of x and y values (and for curved paths, this would be very difficult, but not impossible: https://stackoverflow.com/a/17096947/9792594)

Here's the demo: https://codepen.io/Alexander9111/pen/Jjogbbe

enter image description here

HTML:

<svg viewBox="0 0 500 500">
    <path stroke="grey" fill="none" id="route" d="M50,25 l25,30 l-40,20 z" />
    <circle cx="50" cy="25" r=5 fill="blue">
        <animate attributeName="cx" values="50;75;35;50" dur="5s" repeatCount="indefinite" />
        <animate attributeName="cy" values="25;55;75;25" dur="5s" repeatCount="indefinite" />
    </circle>
    <text x="50" y="25" text-anchor="middle" transform="translate(0,-7)">A
        <animate attributeName="x" values="50;75;35;50" dur="5s" repeatCount="indefinite" />
        <animate attributeName="y" values="25;55;75;25" dur="5s" repeatCount="indefinite" />
    </text>
    <circle cx="150" cy="50" r="5" fill="blue"> </circle>
    <text x="145" y="40">B</text>
    <line x1="50" y1="25" x2="150" y2="50" stroke="blue">
        <animate attributeName="x1" values="50;75;35;50" dur="5s" repeatCount="indefinite" />
        <animate attributeName="y1" values="25;55;75;25" dur="5s" repeatCount="indefinite" />
    </line>
</svg>

Alternatively, we could think about JavaScript to animate it.

UPDATE - using JavaScript to track the circle's location as it is animated with the <animateMotion/> tag:

Demo: https://codepen.io/Alexander9111/pen/NWPQbma

HTML:

<svg viewBox="0 0 500 500">
    <path stroke="grey" fill="none" id="route" d="M50,25 l25,30 l-40,20 z" />
    <circle id="circle_motion" r=5 fill="blue">
        <animateMotion dur="5s" fill="freeze">
            <mpath xlink:href="#route" />
        </animateMotion>
    </circle>
    <rect id="BBox" x="" y="" width="" height=""></rect>
    <text id="text_motion" x="50" y="25" text-anchor="middle" transform="translate(0,-7)">A
    </text>
    <circle cx="150" cy="50" r="5" fill="blue"> </circle>
    <text x="150" y="50" text-anchor="middle" transform="translate(0,-7)">B</text>
    <line id="line_motion" x1="50" y1="25" x2="150" y2="50" stroke="blue">
    </line>
</svg>

JS:

const svg = document.querySelector('svg');
const animateElem = document.querySelector('animateMotion');
const circle_motion = document.querySelector('#circle_motion');
const text_motion = document.querySelector('#text_motion');
const line_motion = document.querySelector('#line_motion');
const BBox = document.querySelector('#BBox');
var myInterval;

function startFunction() {
  const box = circle_motion.getBoundingClientRect();
  var pt = svg.createSVGPoint();
  pt.x = (box.left + box.right) / 2;
  pt.y = (box.top + box.bottom) / 2;
  var svgP = pt.matrixTransform(svg.getScreenCTM().inverse());
  //console.log(svgP.x,svgP.y)
  text_motion.setAttribute('x', (svgP.x) );
  text_motion.setAttribute('y', (svgP.y) );
  line_motion.setAttribute('x1', (svgP.x) );
  line_motion.setAttribute('y1', (svgP.y) );
}

function endFunction() {
  clearInterval(myInterval)
}

animateElem.addEventListener('beginEvent', () => {
  console.log('beginEvent fired');
  myInterval = setInterval(startFunction, 10);
})

animateElem.addEventListener('endEvent', () => {
  console.log('endEvent fired');
  endFunction();
})

This is much more flexible, and we can change our amimate path to: <path stroke="grey" fill="none" id="route" d="M50,25 75,55 Q75,75 35,75 z" /> and we can also follow this non-linear path:

enter image description here

Alex L
  • 4,168
  • 1
  • 9
  • 24