Search code examples
csssvgtransform

svg path transform origin issue


I am trying to move a path along x axis on :hover, and rotate it. What value of transform-origin should I use?

a {
  display: inline-block;
}
svg, path {
  transition: all ease .3s;
  transform-origin: 50% 50%;
  transform-box: fill-box;
}
.circle {
  transform: rotate(-45deg) translateX(20px);
  opacity: 0;
}
a:hover .circle{
  transform: rotate(45deg) translateX(-20px);
  opacity: 1;
}
<a href="" class="play">
<svg width="252" height="188" viewBox="0 0 252 188" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M118.765 118.031a2.945 2.945 0 01-2.942-2.942V72.912a2.944 2.944 0 014.827-2.261l25.306 21.089a2.943 2.943 0 010 4.52L120.65 117.35a2.945 2.945 0 01-1.885.682zm2.944-38.837v29.612L139.476 94l-17.767-14.806z" fill="#000"/>
<path class="circle" d="M125.913 144a49.728 49.728 0 01-24.437-6.386 2.942 2.942 0 112.887-5.128c17.169 9.664 38.901 6.676 52.847-7.276 17.21-17.209 17.21-45.21 0-62.419-17.209-17.212-45.212-17.21-62.42 0-17.21 17.21-17.21 45.21 0 62.419a2.944 2.944 0 01-4.162 4.163c-19.503-19.505-19.503-51.24 0-70.744 19.505-19.503 51.238-19.504 70.745 0 19.504 19.505 19.504 51.239 0 70.744-9.608 9.608-22.476 14.627-35.46 14.627z" fill="#FDB500"/></svg>
                        </a>


Solution

  • Looks like you just need to swap the order of the translateX and rotate parameters. Matrix multiplication is not commutative.

    a {
      display: inline-block;
    }
    svg, path {
      transition: all ease .3s;
      transform-origin: 50% 50%;
      transform-box: fill-box;
    }
    .circle {
      transform: translateX(20px) rotate(-45deg);
      opacity: 0;
    }
    a:hover .circle{
      transform: translateX(-20px) rotate(45deg);
      opacity: 1;
    }
    <a href="" class="play">
    <svg width="252" height="188" viewBox="0 0 252 188" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M118.765 118.031a2.945 2.945 0 01-2.942-2.942V72.912a2.944 2.944 0 014.827-2.261l25.306 21.089a2.943 2.943 0 010 4.52L120.65 117.35a2.945 2.945 0 01-1.885.682zm2.944-38.837v29.612L139.476 94l-17.767-14.806z" fill="#000"/>
    <path class="circle" d="M125.913 144a49.728 49.728 0 01-24.437-6.386 2.942 2.942 0 112.887-5.128c17.169 9.664 38.901 6.676 52.847-7.276 17.21-17.209 17.21-45.21 0-62.419-17.209-17.212-45.212-17.21-62.42 0-17.21 17.21-17.21 45.21 0 62.419a2.944 2.944 0 01-4.162 4.163c-19.503-19.505-19.503-51.24 0-70.744 19.505-19.503 51.238-19.504 70.745 0 19.504 19.505 19.504 51.239 0 70.744-9.608 9.608-22.476 14.627-35.46 14.627z" fill="#FDB500"/></svg>
                            </a>