Jump to content

Search In
  • More options...
Find results that contain...
Find results in...
Tasty Sites

Align one SVG to a child element of another SVG

Recommended Posts

Hello Everyone :) 

I'm struggling with something that I'm not sure if it's trivial, impossible or I'm just doing it wrong. I've checked everything that I could find about .getBoundingClientRect() .getBBox() and I realized that you recently released an suuuuperb update with .convertCoordinates() and a lot of cool stuff.

Though I feel a bit silly trying to use it. Or maybe it's way complicated due to 2 different SVGs and their coordinates systems.

Please let me know if it's possible to put that blue box over the green one but specifically to #align-line (so the top of the blue box is touching the line)
After  6hrs of doing something similar as blue box is doing in the attached CodePen demo and demystifying some awesome @OSUblake demos I would very much appreciate some guidance on this one.

Why I have to do this in such way - I have a mask element (a card that comes out of envelope) and it has to be aligned with the envelope - otherwise the animation is a no no - but later I need to position it (card) anywhere on the screen, so the elements must be separated, but in sync - they will also have dynamic sizes and widths depending on the needs and all the stuff wouldn't be much of a problem if not that one sync issue.

Hopefully I'm just being silly and it's possible, If not, well I need to rethink the whole thing :D 

Thanks for the awesome update!


See the Pen XWbvZOm by tastysites (@tastysites) on CodePen

Link to comment
Share on other sites

Sure, that's possible. The problem is that you're converting to the wrong coordinate space - if you want to apply transforms to the blue-box, those coordinates would actually be in the parent element's space. In other words, you were converting things into the blue-box's coordinate space (like...inside of it), thus if you move -100px for example, it'd actually look like it's moving a lot less because the blue-box is already scaled down. 


I put together a function for you that'd do the conversion:

See the Pen 8871a41b7d65dd82c153d693c8f2141e?editors=0010 by GreenSock (@GreenSock) on CodePen


Technically you should be able to use the getRelativePosition() method for that but it's very late and my brain is foggy right now so I'm not sure why that's not working. I'll have to look into that a different time, but the helper function in the codepen above seems to do the trick just fine. 


Happy tweening!

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

Thanks a ton Jack!

I was switching places of fromElement/toElement and I wasn't in best mind state - it was already getting early ;) 

Yeah, I've tested most of the MotionPathPlugin stuff, getRelativePosition() was my first try and I almost had it working but there were some offsets from my position that I just couldn't figure out. Though those were the beginnings when I was testing it inside my transformed environment - when I realized I can't even do this with two simple boxes I just had to reach out here for help and get some sleep.

This solution is a life saver - thank you again
Happy tweening to you too!

  • Like 3
Link to comment
Share on other sites

  • 1 month later...

Just to circle back on this, I believe I figured out what was going on with getRelativePosition() and I applied a fix to the upcoming release which you can preview at https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MotionPathPlugin.min.js. It'd only affect things when both the to and from elements are SVG and they do NOT share the same ownerSVGElement.


Happy tweening!

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