Checking total progress of the animation with TweenMax.js

An example of checking total progress of the 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>

<canvas id="myCanvas" width="700" height="300"></canvas>
<div id="textDiv"></div>
<script>
    
var txtdiv = document.getElementById('textDiv');
    txtdiv.innerHTML = "total duration";    
    
var context, canvas, myRectangle, tween;
    
    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 = '#aabb00';
        context.fill();
        context.lineWidth = myRectangle.borderWidth;
        context.strokeStyle = 'black';
        context.stroke();
    
  }
  
  function renderCanvas() {
      
        context.clearRect(0, 0, canvas.width, canvas.height);
        drawRectangle(myRectangle, context);
      
      
        var tp = tween.totalProgress();
        txtdiv.innerHTML = "total progress = " + tp;
    
  }
    
  myRectangle = {
      
        x: 10,
        y: 100,
        width: 50,
        height: 50,
        borderWidth: 5
      
  };
    
    
// creating a tween as a variable
tween = new TweenMax(myRectangle, 5, {x:"+=300", y:"+=100", width:50, height:50, borderWidth: 5, onUpdate:renderCanvas });    
tween.play(); // playing the tween
    
</script>
</body>
</html>

Responses

0 Replies