Pausing animation with TweenMax.js

An example of pausing an animation with TweenMax.js.
<!DOCTYPE html>
<html>
<head>
    <script src="js/plugins/BezierPlugin.min.js"></script>
    <script src="js/easing/EasePack.min.js"></script>
    <script src="js/TweenMax.min.js"></script>
</head>
<body>
<button id="stopBut" onclick="stopAnimation()">Stop Animation</button>
    
<canvas id="myCanvas" width="700" height="300"></canvas>

<script>
    

var context, canvas, myRectangle;
    
    canvas = document.getElementById('myCanvas');
    context = canvas.getContext('2d');

  function drawRectangle(myRectangle, context) {
      
        context.beginPath();
        context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
        context.fillStyle = '#00ff00';
        context.fill();
        context.lineWidth = myRectangle.borderWidth;
        context.strokeStyle = 'black';
        context.stroke();
    
  }
  
  function renderCanvas() {
      
        context.clearRect(0, 0, canvas.width, canvas.height);
        drawRectangle(myRectangle, context);
      
  }
    
  myRectangle = {
      
        x: 10,
        y: 100,
        width: 50,
        height: 50,
        borderWidth: 5
      
  };
    

function stopAnimation() {
    
    tween.pause();
    
}
    
// creating a tween as a variable
var tween = new TweenMax(myRectangle, 4, {x:"+=100", y:"+=100", width:50, height:50, borderWidth: 5, onUpdate:renderCanvas });    

tween.play(); // playing the tween
    
</script>
</body>
</html>

Responses

0 Replies