Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
updating SVG animateMotion path using JavaScript

I try changing an SVG motion path according to a html select value using JS. The path updates as expected, but the element, which uses the path as a motion path continues to move along the original path. What am I missing?

function changepath(selectObject) {
  let value = selectObject.value;
     let path = document.getElementById("planePath");
     let plane = document.getElementById("animPath");
    
     let rotation = "rotate(" + value + ")";
     path.setAttribute("transform", rotation);
     plane.setAttribute("transform", rotation);
}
body {
 background: #eee;
}

.planePath {
    opacity: 0.8;
 stroke: darkslategrey;
 stroke-width: 2px;
 fill: none;
}

.plane {
    transform: scale(0.15);
}

select {
    margin-left: 2em;;
}
<svg viewBox="0 0 2000 200">
<!--    <path class="planePath" id="planePath" d="M 0 0 C 200 250 250 50 550 150 C 850 250 700 180 1000 200 " /> -->
        <path class="planePath" id="planePath" d="M 50 100 c 14 -3 736 -115 1900 0" />
    <g id="plane" class="plane">
    <rect x="0" y="0" width="100" height="100"/>
 </g>
 <animateMotion xlink:href="#plane" dur="6s" repeatCount="indefinite" rotate="auto">
  <mpath id="animPath" xlink:href="#planePath" />
    </animateMotion>
</svg>

<select name="route" id="route" onchange="changepath(this)">
     <option value="0">0°</option>
  <option value="1">1°</option>
  <option value="2">2°</option>
  <option value="3">3°</option>
  <option value="4">4°</option>
</select>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Looks like animateMotion mpath can't handle the transform

<svg viewBox="0 0 2000 200">
  <path id="NO_rotate" fill="none" stroke="green" stroke-width="5" 
        d="M 50 100 c 14 -3 736 -115 1900 0"/>
  <path id="path" fill="none" stroke="red" stroke-width="5" 
        d="M 50 100 c 14 -3 736 -115 1900 0" 
        transform="rotate(5)"/>
  <rect id="block" x="0" y="0" width="20" height="20"/>
  <animateMotion href="#block" dur="2s" repeatCount="indefinite" 
                 rotate="auto" restart="always">
    <mpath href="#path" />
  </animateMotion>
</svg>

about 4 years ago · Juan Pablo Isaza Relatório

0

As Danny mentioned in his answer the mpath is using the untransformed path. If you need it to be transformed you can wrap both the path and the animated rect in a group and transform the group.

<svg viewBox="0 0 2000 200">
 <g transform="rotate(5)">
  <path id="path" fill="none" stroke="red" stroke-width="5" 
        d="M 50 100 c 14 -3 736 -115 1900 0" />
  <rect id="block" x="0" y="0" width="20" height="20"/>
  <animateMotion href="#block" dur="2s" repeatCount="indefinite" 
                 rotate="auto" restart="always">
    <mpath href="#path" />
  </animateMotion>
 </g>
</svg>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda