Jump to content
Search Community

Draggable Snap to Grid Based on Another Object

Guest GRDC
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

I have two objects on the screen.  I've had success in snapping to a grid based on the draggable element.  Is snapping to a grid based on another element (or given coordinates) possible with GSAP api?  Would rounding and setting the dom element's position programatically in the drag() function be an acceptable workaround?

Link to comment
Share on other sites

Thanks for the replies.  I'm using top, left by percentages to store the object's position.  Blake, would you demonstrate a fork in your grid layout with top/left as percentages of their parent?  I'm having trouble getting it to work.

Link to comment
Share on other sites

I'm having trouble figuring out what you're trying to do.

 

Are you talking about making the grid percentage based, and you're having trouble snapping to a percentage?

 

And by object's position, do mean you are storing the draggable's position as a percentage, the grid's position, the grid cells, or everything? 

 

If you could clarify or show me your problem, then I'll be able to help you out.

  • Like 1
Link to comment
Share on other sites

The dragged object's top/left are stored as percentages (due to resizing of container that happens regularly).  There is one object on the screen that is, in essence, the origin for snapping.  It can reside at any arbitrary location within the container.  I wish to snap to percentage intervals (say every 2%) based on the object that is considered the "origin" within that same container.

Link to comment
Share on other sites

Thanks for the replies guys.  Diaco, I forked your pen, and monkeyed for a while.  I'm struggling with programatically setting the top/left percentages during drag.  Is this type of thing possible?

 

In the pen, what I'd like to accomplish is that the green knob snaps to the red knob (origin), regardless of where that knob is on the screen.  I'd ideally like to set the object's percentages during drag so as to be able to reference them via means other than the transform.

 

See the Pen xbvxbM?editors=001 by anon (@anon) on CodePen

 

Another idea I toyed with is using the snap function to get the coordinates of the origin object, and using a modulus to snap to a grid based on the those values.  This may be enough, as I don't need to do anything fancy in the onDrag method.

 

Any thoughts?

Link to comment
Share on other sites

Sorry for the delay.

 

I've been dealing with responsive draggables for quite some time now, and my current approach is to just covert it's position to a percentage when you're done dragging. If you want to snap to a percentage, just convert it to a px value, and snap to that value. When you're done dragging, convert it back to a percentage, and you're good to go.

 

In my demo, I calculate the snap during onDrag, both in percent and px values. I then store the percent value, and set the draggable to a px value. It will only snap while it's over the grid, so if I don't have a stored snap value, I just manually calculate it's position when setting it relative.

 

Since my demo is based on a grid, I automatically adjust the snap values when you change them so that they will be evenly spaced. So if you enter 32 for snapX, it will change it to 33.333... If you were going after this behavior, you can check out how to calculate it in the marker's snapX/Y getter.

 

See the Pen WbVRrj by osublake (@osublake) on CodePen

  • Like 2
Link to comment
Share on other sites

My draggable object always starts with this.x and this.y at zero.  So, the second that I drag to the left or top, the x,y values become negative.  I noticed that yours does not behave in this way.  Is there something in your code that makes the draggable instance begin at this.x,this.y coordinates relative to their container, and not themselves?

Link to comment
Share on other sites

I'd like the green box to snap to the red box  (at percent intervals based on the red box) during drag.

 

In my efforts to set the green box's position during drag (either to pixels or percent), once I drag, it goes nuts and drags far beyond the cursor.  I'm using object.position() to get the coordinates for snapping in these cases, which have proven unsuccessful.

 

I'm guessing that my this.x and this.y values are negative due to absolute positioning, and clearProps after each dragEnd.

 

What I may resort to: (pseudo)

 

before drag:  see how far the dragged object is from the origin object.

snap:  snap to the modulus of the draggable object vs origin object

 

Thus far, I have not been successful at calculating the dragged object's current position and (re)setting its coordinates.

Link to comment
Share on other sites

Diaco, thanks for the help on this.  I think I'm really close (at least to understanding).

 

When I change the css of the origin to 49%, the snapping is off.  Is it possible to calculate the snapping during (or on start of) drag, and respond to those values dynamically?

 

One scenario that might arise is if the user elects to change the position of the origin (which is possible in my application through a drag on the red box).

Link to comment
Share on other sites

I guess what I was toying with was calculating and resetting the top/left values of the two objects "during" drag.  Doing so resulted in the top left values becoming increasingly large, and not staying with the cursor.

 

What I've opted to do is calculate the modulus (based on the fake origin) to which the object must snap prior to the draggable's creation, and change that value when the window resizes.  Due to time constraints, this will have to suffice for now.

Link to comment
Share on other sites

  • 2 years later...

You can instead create your own question and refer this thread in it. I noticed 4 posts from you with similar question being posted on all old posts.

 

Blake will reply you whenever he gets online and notices your question. So please just create your own thread.

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