Rotate rectangle around its own center in SVG

前端 未结 5 1533
孤独总比滥情好
孤独总比滥情好 2020-12-05 08:59

I have following piece of code :






        
相关标签:
5条回答
  • 2020-12-05 09:33

    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)
    
    0 讨论(0)
  • 2020-12-05 09:42

    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.

    0 讨论(0)
  • 2020-12-05 09:47

    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);
    }
    
    0 讨论(0)
  • 2020-12-05 09:48

    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)">
    
    0 讨论(0)
  • 2020-12-05 09:50

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

    element.rotate(angle Degree);
    
    0 讨论(0)
提交回复
热议问题