We help IT Professionals succeed at work.
Get Started

HTML5 - flickering animation

Tom Knowlton
Tom Knowlton asked
on
1,295 Views
Last Modified: 2015-03-09
I seem to be able to draw different sized shapes on the HTML5 "canvas" from an array.

But there is a lot of flickering.  How can this be smoothed-out?


<!DOCTYPE html>
<html>
  <head>
  <title></title>
    <style type="text/css">
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="400"></canvas>
    <script type="text/javascript">

        var i = 0;
        var canvas = document.getElementById('myCanvas');
        var context = canvas.getContext('2d'); 
        var one = {
            x: 300,
            y: 300,
            radius: 50,
            startAngle: 0,
            endAngle: Math.PI * 2,
            counterClockwise: false,
            color: '#aff',
            useletter: 'G'
        };

        var two = {
            x: 120,
            y: 120,
            radius: 20,
            startAngle: 0,
            endAngle: Math.PI * 2,
            counterClockwise: false,
            color: '#eee',
            useletter: 'T'
        };   

        _myCircleArray = [one,two];

        window.requestAnimFrame = (function (callback) {
            return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function (callback) {
            window.setTimeout(callback, 1000 / 60);
        };
        })();

        function drawCircle(myCircle, context) {
            //context.save();
            context.beginPath();
            context.arc(myCircle.x, myCircle.y, myCircle.radius, myCircle.startAngle, myCircle.endAngle, myCircle.counterClockwise);
            context.fillStyle = myCircle.color;            
            context.fill();
            context.strokeStyle = '#000';
       //     context.closePath();
            context.stroke();
            //context.retore();
        }

        function drawText(myCircle, context, text) {
          //  context.save();
            context.beginPath();          
            context.fillStyle = '#000';          
            context.fill();
            context.font = "40px Georgia";
            context.fillText(myCircle.useletter, myCircle.x - 10, myCircle.y + 10);
            context.strokeStyle = '#000';
    //        context.closePath();
            context.stroke();           
          //  context.restore();
        }

        function animate(myCircle, canvas, context, startTime) {           
            var time = (new Date()).getTime() - startTime;           
            var amplitude = 150;   
            var period = 2000;           
            var centerX = (canvas.width / 2) - ((myCircle.radius * 2) / 2);        
            var nextX = amplitude * Math.sin(time * 2 * Math.PI / period) + centerX;            
            myCircle.x = nextX;
            context.clearRect(0, 0, canvas.width, canvas.height);
            drawCircle(myCircle, context);
            drawText(myCircle, context, "T");
            requestAnimFrame(function () {
                
                if (i < 1) {
                    i++;
                }
                else {
                    i = 0;
                }

                var a = _myCircleArray[i];
                animate(a, canvas, context, startTime);

            });
        }

      

       

       
        // wait one second before starting animation
        setTimeout(function () {
            var startTime = (new Date()).getTime();
            var a = _myCircleArray[i];
            animate(a, canvas, context, startTime);
           
        }, 1000);

    </script>
  </body>
</html> 

Open in new window

Comment
Watch Question
Software Engineer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE