Jump to content
Search Community

Draggable - onSnap?

SteveS test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Wondering if anyone knows of an implementation to get a sort of "onSnap" behavior using Draggable. The use case is in implementing a sortable grid. Instead of running collision calculations in onDrag every frame, I'd prefer to only call the function to sort elements if snapping occurs.

Link to comment
Share on other sites

I tried something like that by placing custom logic in the liveSnap method but liveSnap runs as frequently as onDrag does. The problem is that it isn't really an "issue" I can illustrate. When snapping occurs, I would like to run some logic.

Here is the start of what I am trying to achieve. You'll note that it is pretty broken right now for several reasons but I don't think they're super relevant:

See the Pen gOzWrYy by StevenStavrakis (@StevenStavrakis) on CodePen


 

My idea is that once an element snaps, it runs logic to update an array which I can call another function on to handle all the animations. I'd like to avoid doing it with hit detection and it would be extremely convenient if I could easily call a function when snapping occurs.

Link to comment
Share on other sites

  • 3 months later...

I'm using a point grid to handle liveSnap. When using a function to determine when something snaps, it is easy to check when a snap occurs like above, but if I am passing a points array,  the snapping is handled inside of Draggable and I can't tell when something snaps.

Link to comment
Share on other sites

Sure, once we see a minimal demo I'm sure it'll become more obvious what the issue is in context and then we can suggest a solution. 

 

You could probably do your own point snapping in a custom function if you need access to the logic to conditionally do other things, or maybe just tap into the onDrag to sense the new location, etc. Again, a minimal demo will go a long way to getting you a quick answer. It always really helps to see the issue in context. 👍

Link to comment
Share on other sites

  • 1 month later...

Finally coming back to this.

Essentially, when using the liveSnap feature configured with a points object, how would one detect a change such that, every time the dragged element snaps, its background color changes? I assume this can be done with a reimplementation of the liveSnap function, but I don't know how to do that.

Here is a light demo showing the setup:

See the Pen f49fd2ba20f512d65a237c86c74b2a4a by StevenStavrakis (@StevenStavrakis) on CodePen

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