Jump to content
Search Community

How to move an object to a point in screen in gsap

Abozanona test
Moderator Tag

Go to solution Solved by akapowl,

Recommended Posts

I have a dom element with a specific class name, and I'm trying to move this element to a specific point in the screen after the user finished dragging it.

In order to do that, my plan is to create a temp dom element in the position I want to move the point to, move the original element, and then remove the temp element.

Here's my code

```
gsap.registerPlugin(Draggable, MotionPathPlugin)
const selector = ".my-obj";
Draggable.create(selector, {
    type: "x,y",
    onDragEnd: function (e: any) {
        const pointTag = document.createElement("temp-point-tag");
        pointTag.style.position = 'absolute';
        pointTag.style.top = 200 + window.scrollY + "px";
        pointTag.style.left = 400 + window.scrollX + "px";
        pointTag.style.width = "3px"
        pointTag.style.height = "3px"
        pointTag.style.backgroundColor = "red"
        document.body.appendChild(pointTag);

        const point = MotionPathPlugin.convertCoordinates(pointTag, document.querySelector(selector), { x: 0, y: 0 })
        gsap.to(selector, { x: point.x, y: point.y });
    },
});
```

My code doesn't work as expected. It doesn't have a static behaviour. If I dragged the object. it goes to a random place in the screen, and might also go outside the screen.

The expected behaviour for `MotionPathPlugin.convertCoordinates`, as I understand from the documentation, is to move to {x:0, y:x} relative to the object's coordinates which I specified.

Why is `MotionPathPlugin.convertCoordinates` not working? What is the correct way to move an object to a specific point(x,y) in the screen?

 

Sample codepen: I'm trying to move the red square to the red dot after dragging event has ended 

See the Pen wvZMdLd by abozanona (@abozanona) on CodePen

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

  • Solution

 

11 hours ago, Abozanona said:

If I dragged the object. it goes to a random place in the screen, and might also go outside the screen.

The expected behaviour for `MotionPathPlugin.convertCoordinates`, as I understand from the documentation, is to move to {x:0, y:x} relative to the object's coordinates which I specified.


The place it tweens to is actually not random at all in your case, but exactly what you tell it to.

In your current case with feeding in an object of x:0 and y:0 to the convertCoordinates method what you will get in the end is the distance of the dragged element to your point element on both axis anytime you release - but then to your tween you're feeding those as absolute values, so it will always tween to that absolute point from the draggables origin - try getting very close to your point and you'll see that you'll end up very close to where the dragging originated. Or alternatively just log the values and compare them with the values of the transformMatrix on the element after the tween is done - logging values is a always a huge help with identifying your problems, btw.

So I see two ways you could go about this:
 

  1. Keep the object at {x: 0, y: 0} but instead of using absolute values for your tween, use relative values like x: "+=" + point.x

    See the Pen XWQXaLa by akapowl (@akapowl) on CodePen




     
  2. Keep your tween at the absolute values, but instead feed an object of the current coordinates of the draggable object to the convertCoordinates method - something like {x: this.x, y: this.y}

    See the Pen yLrezgX by akapowl (@akapowl) on CodePen

 

 

 

I will say, that since I never had the need for that method before myself, I can't give you much of a guarantee for any of the two ways to be the right way to go - and if something is inherently wrong with what I suggested, I'm sure someone with some more experience on that will pop in to help out.

While version 2 to me feels like it's more in line with what the documentation says about the object you feed into the convertCoordinates method;
 

Quote

An object with x and y properties like {x:100, y:200} that define the local coordinates in the fromElement that should be converted into the toElement's local coordinates.


... after a bit of tinkering, it also feels like version 1 seems to work more reliable with quick drags and releases. In general though, both versions should work in your current case; but one might be better or worse suited for you when extending your usecase. I hope this will at least help in some way.
 

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