Jump to content
Search Community

Can I animate preserveAspectRatio (svg) ?

Lollibomber test
Moderator Tag

Go to solution Solved by Lollibomber,

Recommended Posts

I've got a svg-file that I placed (due to responsive reasons) this way: 

 

<svg width="100%" height="100%" viewBox="0 0 1000 500" preserveAspectRatio="xMaxYMax slice" class="MAIN">My super duper svg</svg>

I want to animate this svg with some kind of event to 

<svg width="100%" height="100%" viewBox="0 0 1000 500" preserveAspectRatio="xMidYMax slice" class="MAIN">My super duper svg</svg>

In other words, the position of the svg changes from "xMaxYMax slice" to "xMidYMax slice" (depending on the aspect ratio of the browser). Is there a way to achieve this? Maybe with FLIP? 

Link to comment
Share on other sites

Hi @Lollibomber :)

 

Welcome to the forum and thanks for being a Club member. 🎉

 

I don't think that will work. @GreenSock will correct me if I'm wrong but I believe the Flip plugin is only for CSS properties. You can certainly set that attribute with GSAP and the Attr plugin. MDN docs does list preserveAspectRatio as animatable, but in my experiments, using the SVG built-in <animate> element produces a result that is no different than setting it with GSAP.

 

You can animate the viewBox with GSAP if you like.

 

Happy tweening and welcome aboard.

:)

 

  • Like 1
Link to comment
Share on other sites

  • Solution

Thank you for pointing me to the right direction. I noticed that this does not animate, but does change the aspectRatio:

 

gsap.set(svg,{attr:{preserveAspectRatio:"xMaxYMax slice"}});
gsap.to(svg, 1, {attr:{preserveAspectRatio:"xMinYMax slice"}});

So by first animating the viewbox, and the end of the animation changing the preserveAspectRatio that way, I could establish what I wanted. Thanks!

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