Jump to content
Search Community

Zoom & Center SVG-Element

sdstdt test
Moderator Tag

Go to solution Solved by Jonathan,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

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?

 

Cheers

Gregor

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

  • Like 1
Link to comment
Share on other sites

  • Solution

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:

 

http://greensock.com/svg-tips

 

Resource:

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

 

:)

  • Like 4
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.
×
×
  • Create New...