Skip to content Skip to sidebar Skip to footer

Set Attribute Of Basic Svg Element With Javascript

First, below is the desired result without any JavaScript added Notice the animateMotion element and values of the path attribute. Everything works perfectly in the snippet above.

Solution 1:

Try passing null as the namespace in setAttributeNS

let animateMotion = document.querySelector( `animateMotion` );

animateMotion.setAttributeNS(
  null, `path`, 
  `M20,50 C20,-50 180,150 180,50 C180-50 20,150 20,50 z`
);
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { display: flex; justify-content: center; align-items: center; }
svg { width: 12.5rem; height: 6rem; }
path { fill: none; stroke: #444; stroke-width: 5px; }
circle { opacity: 0.75; fill: #0dd; }
<svg><pathd="M20,50 C20,-50 180,150 180,50 C180-50 20,150 20,50 z" 
  /><circler='10'><animateMotiondur="5s"repeatCount="indefinite"
     /></circle></svg>

Post a Comment for "Set Attribute Of Basic Svg Element With Javascript"