In this example, the svg-plus button transforms into a close button when clicked and vice versa with svg.setAttribute("transform","rotate(45)");
Is it possible to time this transformation so that it’s not just a change from one to another but a smooth transition?
Here is the example:
function rotate() {
var svg = document.getElementById("svgID");
if (svg.getAttribute("transform") === "rotate(45)") {
svg.setAttribute("transform", "rotate(0)");
} else {
svg.setAttribute("transform", "rotate(45)");
}
}
#button {
margin: 0;
padding: 0;
background: none;
outline: none;
box-shadow: none;
border: none;
}
<button id="button" onclick="rotate()">
<svg id="svgID"
width="100%"
height="100%"
viewBox="0 0 142.40739 142.40991"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs6" />
<path
id="rect234"
style="display:inline;opacity:1;fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:0;stroke-linecap:round;stroke-miterlimit:0;stroke-dasharray:none;paint-order:stroke fill markers"
d="m 85.223081,28.929132 a 4.2333333,4.2333333 0 0 0 -4.233333,4.233333 v 62.801852 l -65.486442,-0.24339 a 4.107912,4.107912 0 0 0 -4.247803,3.96255 4.2333333,4.2333333 0 0 0 4.217313,4.249353 l 65.516932,0.24339 v 62.9295 a 4.107912,4.107912 0 0 0 3.978568,4.23333 4.2333333,4.2333333 0 0 0 4.233334,-4.23333 v -62.89901 l 60.21338,0.22376 a 4.107912,4.107912 0 0 0 4.2478,-3.96255 4.2333333,4.2333333 0 0 0 -4.21731,-4.248833 L 89.20165,95.994807 V 33.162465 a 4.107912,4.107912 0 0 0 -3.978569,-4.233333 z"
transform="translate(-11.255471,-28.929132)" />
</svg>
</button>
svg transformation with
setAttribute("transform",rotate(45)")`:
2
Answers
You
need tocan use the CSS propertyanimate
.The following shows a one-way/one-time animation. (once clicked it wont return or animate further)
Method 1: requestAnimationFrame
Method 2: animate