Search code examples
javascriptcanvaskineticjstween

Applied tween inside each(), how do I use reverse()?


Let's suppose I apply a tween to each element on my canvas

elements.each(function (element) {
    new Kinetic.Tween({
        node: element,
        rotationDeg: 180
    }).play();
});

All items have been tweened and have passed from their original state to a final state. My question is: How would I apply a reverse() to revert each item to their original state?


Solution

  • Store the tweens inside an array, and then loop through that array and use .reverse()

        elements = stage.get('Rect');
        var tweenArray = [];
    
        // reverse tween
        document.getElementById('reverse').addEventListener('click', function () {
            for (var i=0; i<tweenArray.length; i++) {
                tweenArray[i].reverse();
            }
        }, false);
    
        // play tween forward
        document.getElementById('play').addEventListener('click', function () {
            elements.each(function (element) {
                var tween = new Kinetic.Tween({
                    node: element,
                    rotationDeg: 180
                }).play();
                tweenArray.push(tween);
            });
        }, false);
    

    jsfiddle