Search code examples
javascriptjquerykineticjs

Pausing an entire flow with kineticjs


Ok, so I have an animated diagram that animates multiple lines and shapes at the same time. Once the user plays the animation, I want he/she to be able to hit pause and the whole thing stop, then start again when the user hits the play button once more. I shouldn't need a loop to do this as it should do it with each mouse click. Here's a sample of the code.

var tween = new Kinetic.Tween({
    node: lineTween,
    points: [563.965, 258.163, 604.272, 258.163],
    duration: 2
});

var tween3 = new Kinetic.Tween({
    node: path3,
    points: [582.81, 257.901, 582.81, 214.216],
    duration: 2,
    onFinish: function(){
        tween3a.play();
    }
});

document.getElementById('play').addEventListener('click', function(){
    tween.play(
    setTimeout(function(){
        tween3.play();
    }, 1000)
    );
}, false);

document.getElementById('pause').addEventListener('click', function(){
    tween.pause();
}, false);

Anyone have any suggestions?


Solution

  • You will have to store all tweens and turn them off/on:

    Demo: http://jsfiddle.net/m1erickson/rF3Mm/

    Code:

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Prototype</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
        <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script>
    
    <style>
    body{padding:20px;}
    #container{
      border:solid 1px #ccc;
      margin-top: 10px;
      width:350px;
      height:350px;
    }
    </style>        
    <script>
    $(function(){
    
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 350,
            height: 350
        });
        var layer = new Kinetic.Layer();
        stage.add(layer);
    
    
        $(stage.getContent()).on('click', function (event) {
            var pos = stage.getMousePosition();
            var mouseX = parseInt(pos.x);
            var mouseY = parseInt(pos.y);
        });
    
        var tweens = [];
    
        var circle1 = new Kinetic.Circle({
            x: 30,
            y: 100,
            radius: 20,
            fill: 'blue',
            stroke: 'black',
            strokeWidth: 4,
            draggable: true
        });
        layer.add(circle1);
    
        var circle2 = new Kinetic.Circle({
            x: 30,
            y: 30,
            radius: 15,
            fill: 'red',
            stroke: 'black',
            strokeWidth: 4,
            draggable: true
        });
        layer.add(circle2);
    
        layer.draw();
    
        var tween1 = new Kinetic.Tween({
            node: circle1,
            duration: 15,
            x: 250,
            y: 250,
        });
        tweens.push(tween1);
    
        var tween2 = new Kinetic.Tween({
            node: circle2,
            duration: 15,
            x: 250,
            y: 250,
        });
        tweens.push(tween2);
    
        tween1.play();
        tween2.play();
    
        isPaused = false;
    
        $("#toggle").click(function () {
            if (isPaused) {
                for (var i = 0; i < tweens.length; i++) {
                    tweens[i].play();
                }
            } else {
                for (var i = 0; i < tweens.length; i++) {
                    tweens[i].pause();
                }
            }
            isPaused = !isPaused
        });
    
    
    }); // end $(function(){});
    
    </script>       
    </head>
    
    <body>
        <button id="toggle">Toggle:Pause/Resume</button>
        <div id="container"></div>
    </body>
    </html>