HTML5 - Clearing canvas with multiple animations - where is the main draw loop? -


i have mixture of static, interactive, , animated objects in canvas. can't seem find way clear draw loop in global way such there no 'smearing' in of objects. code below. appreciated.

 <script>     var canvas = document.getelementbyid("mycanvas");     var ctx = canvas.getcontext("2d");     var mousex = 300;     var mousey = 300;     var inc = 0;     var requestanimationframe = window.requestanimationframe || window.mozrequestanimationframe || window.webkitrequestanimationframe || window.msrequestanimationframe;     ctx.clearrect(0, 0, canvas.width, canvas.height);     movething();     drawbezier();     drawcircle();     drawinteractive();     var putpoint = function (e) {         // update mouse         mousex = e.clientx;         mousey = e.clienty;         // clear canvas         ctx.clearrect(0, 0, canvas.width, canvas.height);         drawbezier();         drawcircle();         drawinteractive();     }     canvas.addeventlistener("mousemove", putpoint);      function drawbezier() {         (var = 0; < 100; i++) {             ctx.beginpath();             ctx.moveto(0, 0);             ctx.beziercurveto(200, mousex, 900, -300, * 20, 600 + math.random() * 30);             ctx.linewidth = 1;             ctx.strokestyle = "black";             ctx.stroke();             ctx.closepath();         }     }      function drawcircle() {         ctx.beginpath();         ctx.fillstyle = "red";         ctx.arc(95, 50, 40, 0, 2 * math.pi);         ctx.fill();         ctx.closepath();     }      function drawinteractive() {         ctx.beginpath();         ctx.fillstyle = "red";         ctx.arc(mousex, mousey - 40, 100, 0, math.pi * 2);         ctx.fill();         ctx.closepath();     }      function movething() {         inc++; //        ctx.clearrect(0, 0, canvas.width, canvas.height);         ctx.beginpath();         ctx.fillstyle = "blue";         ctx.arc(95, 50 + inc, 40, 0, 2 * math.pi);         ctx.fill();         ctx.closepath();         requestanimationframe(movething);     } </script> 

i have refactored code little creating function update main loop of canvas.

 var canvas = document.getelementbyid("mycanvas");      var ctx = canvas.getcontext("2d");      var mousex = 300;      var mousey = 300;      var inc = 0;      var requestanimationframe = window.requestanimationframe || window.mozrequestanimationframe || window.webkitrequestanimationframe || window.msrequestanimationframe;      update();      var putpoint = function (e) {          // update mouse          mousex = e.clientx;          mousey = e.clienty;          // clear canvas          ctx.clearrect(0, 0, canvas.width, canvas.height);          drawbezier();          drawcircle();          drawinteractive();      }      canvas.addeventlistener("mousemove", putpoint);        function drawbezier() {          (var = 0; < 100; i++) {              ctx.beginpath();              ctx.moveto(0, 0);              ctx.beziercurveto(200, mousex, 900, -300, * 20, 600 + math.random() * 30);              ctx.linewidth = 1;              ctx.strokestyle = "black";              ctx.stroke();              ctx.closepath();          }      }        function drawcircle() {          ctx.beginpath();          ctx.fillstyle = "red";          ctx.arc(95, 50, 40, 0, 2 * math.pi);          ctx.fill();          ctx.closepath();      }        function drawinteractive() {          ctx.beginpath();          ctx.fillstyle = "red";          ctx.arc(mousex, mousey - 40, 100, 0, math.pi * 2);          ctx.fill();          ctx.closepath();      }        function update() {                    ctx.clearrect(0, 0, canvas.width, canvas.height);                    movething();          drawbezier();          drawcircle();          drawinteractive();                   requestanimationframe(update);      }            function movething() {          inc++;          ctx.beginpath();          ctx.fillstyle = "blue";          ctx.arc(95, 50 + inc, 40, 0, 2 * math.pi);          ctx.fill();          ctx.closepath();      }      
<canvas id="mycanvas" />


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -