Jump to content
Search Community

convergence / divergence of svg elements

ericshew test
Moderator Tag

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 there,

 

I've been figuring out the svgorigin setting and am really happy with how it works. I am running into one problem that I'm not able to resolve. Basically, I'm wanting a whole bunch of SVG rects to converge to / deconverge from a center position. Essentially, it's centering an object according to the SVGorigin.  Thank you in advance for your help. 

See the Pen OzgdJj by ericshew (@ericshew) on CodePen

Link to comment
Share on other sites

Hi @ericshew :)

 

I think the easiest approach would be to use the x/y attributes. Here's a fork of your pen with that possibility.

 

See the Pen goRqBz by PointC (@PointC) on CodePen

 

That works well for circles(cx/cy) and rectangles, but you can also get the BBox of paths and loop through for those as well. We had a similar question here:

 

Here's the demo I made as an answer to that question.

 

See the Pen RLgLPy by PointC (@PointC) on CodePen

 

Hopefully that all helps. Happy tweening.

:)

 

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

 

8 hours ago, PointC said:

I think the easiest approach would be to use the x/y attributes. Here's a fork of your pen with that possibility.

 

I used to animate position based attributes, like x/y on rects and cx/cy on circles, but then I started noticing problems if you also animate other transforms on the same element. I haven't brought the issue up with @GreenSock before, but I'm guessing it's causing problems with the way GSAP calculates origins. It all depends on the order and when they are applied, but changing a position/size based attribute causes the bounding box to change, which GSAP might not be aware of.

 

So it might be better to use getBBox, and animating transform based x/y just to be safe. Another reason is that there can be a performance advantage in certain browsers when animating a transform over an attribute.

 

 

9 hours ago, ericshew said:

I am running into one problem that I'm not able to resolve. Basically, I'm wanting a whole bunch of SVG rects to converge to / deconverge from a center position. Essentially, it's centering an object according to the SVGorigin.  

 

For what you're doing, you don't need to set an origin. svgOrigin is just like transformOrigin. It's defines the point/anchor that an element will scale from and rotate around. It has nothing to do with translation, i.e. animating x and y. The difference between the two is where the origin is relative to. For svgOrigin, it's relative to the svg element, and for transformOrigin, it's relative to the element. 

 

This is a very simple problem. All you have to do is get the center of 2 bounding boxes, and then subtract the difference. 

 

See the Pen baRZjN by osublake (@osublake) on CodePen

 

 

 

 

  • Like 4
Link to comment
Share on other sites

18 hours ago, PointC said:

Wow. This is really helpful. What an elegant solution. Thanks very much. 

18 hours ago, PointC said:

Hi @ericshew :)

 

I think the easiest approach would be to use the x/y attributes. Here's a fork of your pen with that possibility.

 

See the Pen goRqBz by PointC (@PointC) on CodePen

 

That works well for circles(cx/cy) and rectangles, but you can also get the BBox of paths and loop through for those as well. We had a similar question here:

 

Here's the demo I made as an answer to that question.

 

See the Pen RLgLPy by PointC (@PointC) on CodePen

 

Hopefully that all helps. Happy tweening.

:)

 

 

18 hours ago, PointC said:

Hi @ericshew :)

 

I think the easiest approach would be to use the x/y attributes. Here's a fork of your pen with that possibility.

 

See the Pen goRqBz by PointC (@PointC) on CodePen

 

That works well for circles(cx/cy) and rectangles, but you can also get the BBox of paths and loop through for those as well. We had a similar question here:

9 hours ago, OSUblake said:

@PointC link about animating an explosion from the center made me remember showing how to do something similar. You can create some pretty interesting effects with very little code.

 

 

 

See the Pen wzkrBr by osublake (@osublake) on CodePen

 

 

Here's the demo I made as an answer to that question.

 

See the Pen RLgLPy by PointC (@PointC) on CodePen

 

Hopefully that all helps. Happy tweening.

:)

 

 

Link to comment
Share on other sites

10 hours ago, OSUblake said:

 

 

I used to animate position based attributes, like x/y on rects and cx/cy on circles, but then I started noticing problems if you also animate other transforms on the same element. I haven't brought the issue up with @GreenSock before, but I'm guessing it's causing problems with the way GSAP calculates origins. It all depends on the order and when they are applied, but changing a position/size based attribute causes the bounding box to change, which GSAP might not be aware of.

 

So it might be better to use getBBox, and animating transform based x/y just to be safe. Another reason is that there can be a performance advantage in certain browsers when animating a transform over an attribute.

 

 

 

For what you're doing, you don't need to set an origin. svgOrigin is just like transformOrigin. It's defines the point/anchor that an element will scale from and rotate around. It has nothing to do with translation, i.e. animating x and y. The difference between the two is where the origin is relative to. For svgOrigin, it's relative to the svg element, and for transformOrigin, it's relative to the element. 

 

This is a very simple problem. All you have to do is get the center of 2 bounding boxes, and then subtract the difference. 

 

See the Pen baRZjN by osublake (@osublake) on CodePen

 

 

 

 

 

 

Wow. Thanks very much. I've gone over your solution multiple times now and I'm finally beginning to understand it. Your code is always so concise and clean. I really appreciate the clarification. Thanks! 

  • Like 2
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...