Jump to content
Search Community

GSAP 3.x Error with Draggable Event Binding

KIDS interactive test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hi!

You dont need create eventlisteners

You can use just like:

const $el = document.getElementById('drag-me');

let dragInstance = null;

dragInstance = Draggable.create($el, {
  type: 'x,y',
  zIndexBoost: false,
  onDragStart: ()=> document.body.style.backgroundColor = 'pink',
  onDragEnd: ()=>document.body.style.backgroundColor = ''
})[0];

 

  • Like 3
Link to comment
Share on other sites

2 hours ago, KIDS interactive said:

Thanks for your help guys, but i explicit need to create a new listener each time. The main-problem is more complex and the codepen shows just the main-cause.

Anyway for me it is a bug that it wouldn't work on the first time, but always afterwards. Any other ideas how to solve it?

Can you explain why do you need create listener inside listener callback (you create two listeners for one event)
By the way, Draggable already have listener onDragonMove ...  you dont need create it again.

 

I can't imagine why do you need to create listener every time on Drag start. 

  • Like 1
Link to comment
Share on other sites

the dragInstance is created from another Class. I couln't modify the creation of the instance. The only thing i can get is the firing of an extra event of dragstart / dragend where the instance of the object is passed.

So i have to bind my drag-listener afterwards.

 

And it works after the second dragStart. Just on the first dragStart it doesn't work.

Link to comment
Share on other sites

  • Solution

As a performance optimization, Draggable checks when each onPress occurs to see if there's a drag listener so that (if not) it can skip some expensive logic on every move of the pointer. It looks like you're trying to add a drag even listener WHILE it's dragging (after the onPress), so that check has already happened for that drag. You have several options:

  • Listen for the "pressInit" event and add your drag listener there instead. 
  • Or just make sure there's an onDrag applied to the Draggable instance when it's initially created (even if the function does nothing). 
  • Like 2
Link to comment
Share on other sites

On 10/2/2020 at 10:45 AM, KIDS interactive said:

dragend where the instance of the object is passed.

let isPassed = false // global variable for detct is instanse is passed
const $el = document.getElementById('drag-me');

let dragInstance = null;

dragInstance = Draggable.create($el, {
  type: 'x,y',
  zIndexBoost: false,
  onDragStart: ()=> isPasses ? document.body.style.backgroundColor = 'pink': false,
  onDragEnd: ()=> isPassed ? document.body.style.backgroundColor = '' : false
})[0];

What about check isPassed and if it passed - do something 
Or you can create public method and check it yourClass.isPassed() // return true/false

Link to comment
Share on other sites

22 hours ago, Nekiy2 said:

What about check isPassed and if it passed - do something 
Or you can create public method and check it yourClass.isPassed() // return true/false

The problem is, that the `onDragStart`, `onDragEnd` and `onDrag` are binded by the class. I can't modify them in any way. I can't modify the Draggable creation.

The only thing i get is the instance of the object, with the dragInstance, on drag-start and on drag end.


---

On 10/2/2020 at 8:18 PM, GreenSock said:

As a performance optimization, Draggable checks when each onPress occurs to see if there's a drag listener so that (if not) it can skip some expensive logic on every move of the pointer. It looks like you're trying to add a drag even listener WHILE it's dragging (after the onPress), so that check has already happened for that drag

Okay i see this point. But why it is working on the second try and afterwards ?

Link to comment
Share on other sites

On 10/5/2020 at 2:19 AM, KIDS interactive said:

The problem is, that the `onDragStart`, `onDragEnd` and `onDrag` are binded by the class. I can't modify them in any way. I can't modify the Draggable creation.

The only thing i get is the instance of the object, with the dragInstance, on drag-start and on drag end.


---

Okay i see this point. But why it is working on the second try and afterwards ?

That's what I tried to explain here: 

On 10/2/2020 at 1:18 PM, GreenSock said:

Draggable checks when each onPress occurs to see if there's a drag listener so that (if not) it can skip some expensive logic on every move of the pointer. It looks like you're trying to add a drag even listener WHILE it's dragging (after the onPress), so that check has already happened for that drag.

On the first drag, it's adding the listener thus on every subsequent onPress, it passes the internal test of "having an onDrag listener", thus it sets that flag to do the expensive work of dispatching onDrag events for that episode.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
On 10/8/2020 at 12:53 PM, KIDS interactive said:

Forget it.. i was to dumb to read correctly. `OnPressInit` helps a lot.
---
Could be closed

Thanks for posting your finding. Faced the same issue and was able to resolve it with `onPressInit`

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