meetar
meetar

Reputation: 7611

tween.js - Setting default easing values

I'm using https://github.com/sole/tween.js/ and creating a lot of tweens - is there a way to set a default value for the .easing property so I don't have to declare it every time?

Upvotes: 0

Views: 483

Answers (1)

Giovanni Filardo
Giovanni Filardo

Reputation: 1022

I do not know the library, but having looked at the code, it unfortunately seems that it has not been created for that. In fact, if you look at the source code, the easing function in the TWEEN.Tween constructor is private. You could change the code of the library yourself, or you could decorate the constructor. A simple way to achieve that is to overwrite the constructor by substituting it with a function that creates a TWEEN.Tween object, runs the easing function and returns the created tween. The following code has to be included just after the TWEEN library, and before your code.

TWEEN.origTween = TWEEN.Tween;
TWEEN.Tween = function (options){
    var res = new TWEEN.origTween(options);
    res.easing(TWEEN.Easing.Elastic.InOut);
    return res;
};

You could take the example further and implement some other "default" values yourself by calling other methods inside the newly defined "constructor". Please note that the defaults can be changed at any time using the .easing method like you have being doing until now, even after the decoration of the constructor function.

Here is a working fiddle with the default easing function set to TWEEN.Easing.Elastic.InOut

http://jsfiddle.net/5fgn2/

As you can see, by uncommenting the easing invocation, you can still change it, if you need to.

Upvotes: 1

Related Questions