用kineticjs暂停整个流程

时间:2014-01-30 21:19:13

标签: javascript jquery kineticjs

好的,所以我有一个动画图表,同时动画多个线条和形状。一旦用户播放动画,我希望他/她能够暂停并且整个事情停止,然后当用户再次点击播放按钮时再次开始。我不应该需要一个循环来执行此操作,因为它应该在每次单击鼠标时执行此操作。这是代码示例。

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);

有人有任何建议吗?

1 个答案:

答案 0 :(得分:1)

您必须存储所有补间并关闭/打开它们:

演示:http://jsfiddle.net/m1erickson/rF3Mm/

代码:

<!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>
相关问题