Jump to content
Search Community

Drag and Drop

info@tesseractlearning.com 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 Guys,

 

I am trying to create drag and drop using drag-gable and hittest method.i stuck trying to achieve below task 

* I want to place my drag-gable(represented in blue in the image) to position, scale(achieved by setting width and height of drop-area) and fit inside the drop-area 

but i am not able to get position of droparea correctly to position my drag-gable .i tried draggable.updated() method which just sticks drag-gable to user onDragEnd event .

Please let me know,how can i achieve desired result.

Thanks in advance.

dragnddrop.PNG

 

 

 

See the Pen LYYPxEZ?editors=0011 by sudhakarselva (@sudhakarselva) on CodePen

Link to comment
Share on other sites

Hello and welcome to the GreenSock forums. Thanks so much for being a Business Green member! We couldn't do what we do without people like you.

 

The basic approach is to calculate the position of the drop-area in the Draggable's onRelease. You can use something like the following to do so:

 

Draggable.create('.slide', {
  onRelease: function(e) {
    // Get the position and size of the drop-area
    var dropArea = document.getElementById('drop-area');
    var dropAreaRect = dropArea.getBoundingClientRect();
    
    // Set the target's position and size to the position and size of the drop-area
    //target's x = dropAreaRect.x;
    //target's y = dropAreaRect.y;
    //target's width = dropAreaRect.width;
    //target's height = dropAreaRect.height;
    
    // Maybe remove the target area? or you could move it instead if you needed to.
    dropArea.parentNode.removeChild(dropArea);
  }
});

If you would like help with your specific project, a minimal demo of your setup in a CodePen would be helpful.

Link to comment
Share on other sites

You can't set x, y, width, or height directly like that. Maybe like this.

 

Draggable.create('.slide', {
  onRelease: function(e) {
    // Get the position and size of the drop-area
    var dropArea = document.getElementById('drop-area');
    var r = dropArea.getBoundingClientRect();
    
    TweenLite.set(this.target, {
      x: r.left,
      y: r.top,
      width: r.width,
      height: r.height
    });
        
    // Maybe remove the target area? or you could move it instead if you needed to.
    dropArea.parentNode.removeChild(dropArea);
  }
});

 

I would use left and top from getBoundingClientRect() because x and y are not available in every browser.

https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect#Browser_compatibility

 

  • Like 2
Link to comment
Share on other sites

13 hours ago, OSUblake said:

Actually, just use relative values. I was thinking of something different in my previous post.

 


TweenLite.to(this.target, 0.2, {
  x: "+=" + (dropBound.x - dragBound.x),
  y: "+=" + (dropBound.y - dragBound.y)
})

 

It worked. but could not understand exactly what   += does .but  before i had same without += that didnt work. Thanks a lot for your time @OSUblake :)

Edited by info@tesseractlearning.com
Solution update
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...