Share Posted March 12 Hey all, is there any dynamic way (instead of defining: `x/y: <coords>` to move an object to the center of another object? Let’s say my `anchor` is not always fixed at the same spot. And my `ball` needs to dynamically read out the current target coords from the anchor. What is the best way to achieve that? Thank you! See the Pen zYJRvXP by Kalaschnik (@Kalaschnik) on CodePen Link to comment Share on other sites More sharing options...
Solution Author Solution Share Posted March 12 I solved it using bbox,cx,cy. See the Pen NWLyGLP by Kalaschnik (@Kalaschnik) on CodePen I am just wondering if that is the way GSAP people would solve this? Or is there any gsap-built-in magic? 1 Link to comment Share on other sites More sharing options...
Share Posted March 12 Hi In SVG certainly that's the way to achieve that. For regular DOM elements you could use getBoundingClientRect: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect Also you can use other methods if your element is not in the viewport. So as you can see there are a few different avenues that can be explored to solve this. Happy Tweening! Link to comment Share on other sites More sharing options...
Author Share Posted March 13 Is it correct when I assume in my mental modal; SVG = getBBox and non-SVG: etBoundingClientRect to get the correct coordinates? I am still a bit confused by the two. Link to comment Share on other sites More sharing options...
Share Posted March 13 Pretty much, but just keep in mind that the getBBox() thing reports coordinates within the parent <svg>'s coordinate system which is usually very different than the overall web page's coordinate system. If I remember correctly, getBoundingClientRect() can be used on most SVG elements too if you want to get coordinates according to the viewport. 2 Link to comment Share on other sites More sharing options...
Share Posted March 13 Another little trick is to create everything at 0,0 and then start moving things. That way you know exactly where everything is located. Not always feasible, but works in some cases. YMMV 4 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now