Hi @shaunhurley
The callback system for Draggable works just like the one TweenLite/Max and TimelineLite/Max. If figuring out how to set the scope isn't obvious, then it's probably due to how your code is written.
My guess is that you're doing something like this, which is what all the examples in the docs do. There is a way to set the callback scope to the target when you create draggables like that, but I'm not going to get into that because it requires more work.
Draggable.create(".elements", {
...
});
To set the callback scope to the target, you should be working at the element level, creating one draggable at a time. This may require you to use some sort of loop if you want to create more than one.
var draggable = new Draggable(theElement, {
throwProps: true,
onThrowComplete: update
onThrowCompleteScope: theElement
});
And knowing how to bind a function can simplify how you define callbacks in GSAP, or any library for the matter. Consider the following. You want to use a scoped callback for different actions, and want to pass in a couple of parameters to that callback. Pretty messy, right?
var animation = TweenLite.to(theElement, 1, {
onUpdate: update,
onUpdateParams: [100, 500],
onUpdateScope: theElement,
...
});
var draggable = new Draggable(theElement, {
throwProps: true,
onDrag: update,
onDragParams: [100, 500],
onDragScope: theElement,
onThrowUpdate: update,
onThrowUpdateParams: [100, 500],
onThrowUpdateScope: theElement
});
function update(min, max) {
console.log("this", this); // => theElement
console.log("min", min); // => 100
console.log("max", max); // => 500
}
Instead of specifying each scope separately, you can use "callbackScope", but it's still kind of messy. We were only able to get rid of 1 line of code.
var animation = TweenLite.to(theElement, 1, {
onUpdate: update,
onUpdateParams: [100, 500],
callbackScope: theElement,
...
});
var draggable = new Draggable(theElement, {
throwProps: true,
onDrag: update,
onDragParams: [100, 500],
onThrowUpdate: update,
onThrowUpdateParams: [100, 500],
callbackScope: theElement
});
This is where creating a bound function might be useful. You can combine everything into a single, reusable function. The first parameter is the scope. Any additional parameters will be passed into the callback.
var boundUpdate = update.bind(theElement, 100, 500);
var animation = TweenLite.to(theElement, 1, {
onUpdate: boundUpdate,
...
});
var draggable = new Draggable(theElement, {
throwProps: true,
onDrag: boundUpdate,
onThrowUpdate: boundUpdate
});
Much, MUCH cleaner!