Rotate rectangle around its own center in SVG

Svg

Svg Problem Overview


I have following piece of code :

<svg>

<defs>
<rect id = "myRect"
      x = "10"
      y = "10"
      height = "120"
      width = "120"
      stroke-width = "2px"
      stroke = "red"
      fill = "blue" />

</defs>


<g transform = "translate(100,30)">
  <use xlink:href = "#myRect" />
</g>

<g transform = "translate(100, 100) rotate(45 ? ?)">

  <rect id = "myRect"
      x = "10"
      y = "10"
      height = "120"
      width = "120"
      stroke-width = "2px"
      stroke = "green"
      fill = "yellow" />
</g>

</svg>

When I translate rectangle without rotation, it is working fine. But when I rotate it, I wanted to rotate it around its center axis point. What should I need to pass to rotate attribute?

Svg Solutions


Solution 1 - Svg

You would have to set the center as the center of the filled element. Like this:

svg .rotate {
  transform-box: fill-box;
  transform-origin: center;
  transform: rotate(45deg);
}

Solution 2 - Svg

You just need to add half the width/height of the rectangle to get its centre.

<g transform = "translate(100, 100) rotate(45 60 60)">

See transform documentation of the rotate function for more information.

Solution 3 - Svg

The accepted answer works if you are drawing the rectangle starting at point (0,0) which was the OP case. However for me it was not!

Here is what worked for me:

  • To get the rectangle coordinates i used $('#rectID').getBBox() method, should return [rect-height , rect-width , rect-y , rect x ]
  • The center point is ( rect-x + (rect-width/2) , rect-y + (rect-height/2) )

Here is a snippet i used on the browser console:

var coord = $('#elemID')[0].getBBox();
coord.x + (coord.width/2) +' '+ coord.y + (coord.height/2)

Solution 4 - Svg

origin
x = x + width / 2
y = y + height / 2
here
x is 10
y is 10
width is 120
height is 120

<g transform = "translate(100, 100) rotate(45 70 70)">

Solution 5 - Svg

I know this is an old post but if there are people out there who are looking make the values modifiable outside the group element

    const centerX=100;
    const centerY=100;
    const firstAngle=45;
    const secondAngle=60;
    const thirdAngle =60;
  
    <g transform ={`translate(${centerX}, ${centerY}) rotate(${firstAngle} ${secondAngle}, 
    ${thirdAngle})`}>

Solution 6 - Svg

Nothing you just need to write the following code with the element in javascript:

element.rotate(angle Degree);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinay BagaleView Question on Stackoverflow
Solution 1 - SvgbjosterView Answer on Stackoverflow
Solution 2 - SvgRobert LongsonView Answer on Stackoverflow
Solution 3 - SvgNimirView Answer on Stackoverflow
Solution 4 - SvgSUDHIR KUMARView Answer on Stackoverflow
Solution 5 - SvgalittleloopyView Answer on Stackoverflow
Solution 6 - SvgKshitiz singhalView Answer on Stackoverflow