Alexander Hein
Alexander Hein

Reputation: 990

Simply function with similar properties

How do I simplify the following function?

I have a bunch of tweens that have basicailly the same ease properties. I realize this question is pretty broad, but I've haven't come up with anything satisfactory enough.

    $('input').change(function() {
    if($("#radio").is(":checked")){

        var tween = new TWEEN.Tween(tween1.scale).to({ y: 4 }, 1000).start();
        tween.easing(TWEEN.Easing.Elastic.InOut);
        tween.yoyo(true);

        var tween = new TWEEN.Tween(tween2.position).to({ y: 0.05 }, 1000).start();
        tween.easing(TWEEN.Easing.Elastic.InOut);
        tween.yoyo(true);

        var tween = new TWEEN.Tween(tween3.scale).to({ y: 6 }, 1000).start();
        tween.easing(TWEEN.Easing.Elastic.InOut);
        tween.yoyo(true);

        ...

 }
 });

Upvotes: 0

Views: 57

Answers (1)

Kenney
Kenney

Reputation: 9093

The most classic way is to declare a function:

function doTween(attr, y) {
    var tween = new TWEEN.Tween(attr).to({ y: y }, 1000).start();
    tween.easing(TWEEN.Easing.Elastic.InOut);
    tween.yoyo(true);
}

and call using

doTween( tween1.scale, 4 );
doTween( tween2.position, 0.05 );
doTween( tween3.scale, 6 );

Upvotes: 1

Related Questions