Jump to content


Zoom & Center SVG-Element

Recommended Posts

Hi Greensocks,


after reading tons of topics on the subject of animating svg I did not find an answer that works properly for my goal. Now I desperately need your help:


Elements inside my svg are scaled to 2.8 by click. After that I need them to be centered inside the viewbox. Scaling works fine, centering does not.


Do you have any suggestions?




See the Pen QKgaZd by sdstdt (@sdstdt) on CodePen

Link to comment
Share on other sites

Hello sdstdt, and Welcome to the GreenSock Forum!


xPercent and yPercent really wont work on SVG since percentages are not accounted for. xPercent and yPercent are better used on regular non SVG DOM elements that accept percentages, not SVG child elements.


So you have to calculate the centering with getBBox() for SVG and its child elements. Basically the below calculates the <svg> elements viewbox to postion the SVG child element in relation to their parent, the <svg> element.


See the Pen YGQvjy by jonathan (@jonathan) on CodePen


if you nested SVG elements within each other the centering probably could have been done without trying to calculate the viewbox with getBBox(). Since nested <svg> elements are easier to center than child SVG elements.


Also check out the SVG Tips page:





getBBox() : http://www.w3.org/TR/SVG/types.html#__svg__SVGLocatable__getBBox



  • Like 3
Link to comment
Share on other sites

This is amazing! Thank you so much! You made my day)!!!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.